1
Fork 0
mirror of https://github.com/SapphireServer/Sapphire.git synced 2025-04-26 06:27:45 +00:00

zone and api work a bit better now

This commit is contained in:
NotAdam 2018-10-26 22:14:12 +11:00
parent ccb29c88de
commit ddfbeb724f
5 changed files with 419 additions and 408 deletions

View file

@ -36,11 +36,11 @@ add_subdirectory( "src/common" )
add_subdirectory( "src/servers" ) add_subdirectory( "src/servers" )
#add_subdirectory( "src/dbm" ) #add_subdirectory( "src/dbm" )
add_subdirectory( "src/tools/exd_common_gen" ) #add_subdirectory( "src/tools/exd_common_gen" )
add_subdirectory( "src/tools/exd_struct_gen" ) #add_subdirectory( "src/tools/exd_struct_gen" )
add_subdirectory( "src/tools/exd_struct_test" ) #add_subdirectory( "src/tools/exd_struct_test" )
add_subdirectory( "src/tools/quest_parser" ) #add_subdirectory( "src/tools/quest_parser" )
add_subdirectory( "src/tools/discovery_parser" ) #add_subdirectory( "src/tools/discovery_parser" )
add_subdirectory( "src/tools/mob_parse" ) #add_subdirectory( "src/tools/mob_parse" )
#add_subdirectory("src/tools/pcb_reader") #add_subdirectory("src/tools/pcb_reader")
#add_subdirectory("src/tools/event_object_parser") #add_subdirectory("src/tools/event_object_parser")

View file

@ -487,7 +487,12 @@ void checkSession( shared_ptr< HttpServer::Response > response, shared_ptr< Http
} }
else else
{ {
std::string json_string = "{\"result\":\"" + std::to_string( result ) + "\"}"; std::string json_string = nlohmann::json( {
{ "result", result },
{ "result2", "penis" },
{ "result3", "wtf" }
} ).dump()
;
*response << buildHttpResponse( 200, json_string, JSON ); *response << buildHttpResponse( 200, json_string, JSON );
} }
} }

View file

@ -57,7 +57,7 @@ Core::LobbySessionPtr Core::Network::RestConnector::getSession( char* sId )
try try
{ {
json.parse( content ); json = json.parse( content );
} }
catch( std::exception& e ) catch( std::exception& e )
{ {
@ -99,7 +99,7 @@ bool Core::Network::RestConnector::checkNameTaken( std::string name )
try try
{ {
json.parse( content ); json = json.parse( content );
} }
catch( std::exception& e ) catch( std::exception& e )
{ {
@ -133,7 +133,7 @@ uint32_t Core::Network::RestConnector::getNextCharId()
try try
{ {
json.parse( content ); json = json.parse( content );
} }
catch( std::exception& e ) catch( std::exception& e )
{ {
@ -172,7 +172,7 @@ uint64_t Core::Network::RestConnector::getNextContentId()
try try
{ {
json.parse( content ); json = json.parse( content );
} }
catch( std::exception& e ) catch( std::exception& e )
{ {
@ -213,7 +213,7 @@ CharList Core::Network::RestConnector::getCharList( char* sId )
try try
{ {
json.parse( content ); json = json.parse( content );
} }
catch( std::exception& e ) catch( std::exception& e )
{ {
@ -230,10 +230,11 @@ CharList Core::Network::RestConnector::getCharList( char* sId )
{ {
g_log.debug( child["contentId"] ); g_log.debug( child["contentId"] );
//std::string, uint32_t, uint64_t, std::string //std::string, uint32_t, uint64_t, std::string
list.push_back( std::make_tuple( child["name"].get< std::string >() ), list.push_back( { child["name"].get< std::string >(),
child["charId"].get< uint32_t >(), child["charId"].get< uint32_t >(),
child["contentId"].get< uint64_t >(), child["contentId"].get< uint64_t >(),
child["infoJson"].get< std::string >() ); child["infoJson"].get< std::string >()
} );
} }
return list; return list;
@ -266,7 +267,7 @@ bool Core::Network::RestConnector::deleteCharacter( char* sId, std::string name
auto json = nlohmann::json(); auto json = nlohmann::json();
try try
{ {
json.parse( content ); json = json.parse( content );
} }
catch( std::exception& e ) catch( std::exception& e )
{ {
@ -303,7 +304,7 @@ int Core::Network::RestConnector::createCharacter( char* sId, std::string name,
try try
{ {
json.parse( content ); json = json.parse( content );
} }
catch( std::exception& e ) catch( std::exception& e )
{ {

View file

@ -4,414 +4,419 @@
#include <asio.hpp> #include <asio.hpp>
#include <Util/Util.h> #include <Util/Util.h>
#include <unordered_map> #include <unordered_map>
#include <map> #include <map>
#include <random> #include <random>
#include <mutex> #include <mutex>
#include <type_traits> #include <type_traits>
#include <functional>
#ifndef CASE_INSENSITIVE_EQUALS_AND_HASH
#define CASE_INSENSITIVE_EQUALS_AND_HASH
class case_insensitive_equals { class case_insensitive_equals {
public: public:
bool operator()( const std::string &key1, const std::string &key2 ) const { bool operator()(const std::string &key1, const std::string &key2) const {
return Core::Util::toLowerCopy( key1 ) == Core::Util::toLowerCopy( key2 ); return Core::Util::toLowerCopy( key1 ) == Core::Util::toLowerCopy( key2 );
} }
}; };
class case_insensitive_hash { class case_insensitive_hash {
public: public:
size_t operator()( const std::string &key ) const { size_t operator()( const std::string &key ) const
std::size_t seed = 0; {
for( auto &c : key ) std::size_t seed=0;
Core::Util::hashCombine< char >( seed, std::tolower( c ) ); for( auto &c : key )
return seed; Core::Util::hashCombine< char >( seed, std::tolower( c ) );
} return seed;
}
}; };
#endif
namespace SimpleWeb { namespace SimpleWeb {
template <class socket_type> template <class socket_type>
class Client; class Client;
template <class socket_type> template <class socket_type>
class ClientBase { class ClientBase {
public: public:
virtual ~ClientBase() {} virtual ~ClientBase() {}
class Response { class Response {
friend class ClientBase<socket_type>; friend class ClientBase<socket_type>;
friend class Client<socket_type>; friend class Client<socket_type>;
public: public:
std::string http_version, status_code; std::string http_version, status_code;
std::istream content; std::istream content;
std::unordered_multimap<std::string, std::string, case_insensitive_hash, case_insensitive_equals> header; std::unordered_multimap<std::string, std::string, case_insensitive_hash, case_insensitive_equals> header;
private: private:
asio::streambuf content_buffer; asio::streambuf content_buffer;
Response() : content( &content_buffer ) {} Response(): content(&content_buffer) {}
}; };
class Config { class Config {
friend class ClientBase<socket_type>; friend class ClientBase<socket_type>;
private: private:
Config() {} Config() {}
public: public:
/// Set timeout on requests in seconds. Default value: 0 (no timeout). /// Set timeout on requests in seconds. Default value: 0 (no timeout).
size_t timeout = 0; size_t timeout=0;
/// Set proxy server (server:port) /// Set proxy server (server:port)
std::string proxy_server; std::string proxy_server;
}; };
/// Set before calling request /// Set before calling request
Config config; Config config;
std::shared_ptr<Response> request( const std::string& request_type, const std::string& path = "/", std::string_view content = "", std::shared_ptr<Response> request(const std::string& request_type, const std::string& path="/", std::string_view content="",
const std::map<std::string, std::string>& header = std::map<std::string, std::string>() ) { const std::map<std::string, std::string>& header=std::map<std::string, std::string>()) {
auto corrected_path = path; auto corrected_path=path;
if( corrected_path == "" ) if(corrected_path=="")
corrected_path = "/"; corrected_path="/";
if( !config.proxy_server.empty() && std::is_same<socket_type, asio::ip::tcp::socket>::value ) if(!config.proxy_server.empty() && std::is_same<socket_type, asio::ip::tcp::socket>::value)
corrected_path = "http://" + host + ':' + std::to_string( port ) + corrected_path; corrected_path="http://"+host+':'+std::to_string(port)+corrected_path;
asio::streambuf write_buffer; asio::streambuf write_buffer;
std::ostream write_stream( &write_buffer ); std::ostream write_stream(&write_buffer);
write_stream << request_type << " " << corrected_path << " HTTP/1.1\r\n"; write_stream << request_type << " " << corrected_path << " HTTP/1.1\r\n";
write_stream << "Host: " << host << "\r\n"; write_stream << "Host: " << host << "\r\n";
for( auto& h : header ) { for(auto& h: header) {
write_stream << h.first << ": " << h.second << "\r\n"; write_stream << h.first << ": " << h.second << "\r\n";
} }
if( content.size()>0 ) if(content.size()>0)
write_stream << "Content-Length: " << content.size() << "\r\n"; write_stream << "Content-Length: " << content.size() << "\r\n";
write_stream << "\r\n"; write_stream << "\r\n";
connect(); connect();
auto timer = get_timeout_timer(); auto timer=get_timeout_timer();
asio::async_write( *socket, write_buffer, asio::async_write(*socket, write_buffer,
[this, &content, timer]( const std::error_code &ec, size_t /*bytes_transferred*/ ) { [this, &content, timer](const std::error_code &ec, size_t /*bytes_transferred*/) {
if( timer ) if(timer)
timer->cancel(); timer->cancel();
if( !ec ) { if(!ec) {
if( !content.empty() ) { if(!content.empty()) {
auto timer = get_timeout_timer(); auto timer=get_timeout_timer();
asio::async_write( *socket, asio::buffer( content.data(), content.size() ), asio::async_write(*socket, asio::buffer(content.data(), content.size()),
[this, timer]( const std::error_code &ec, size_t /*bytes_transferred*/ ) { [this, timer](const std::error_code &ec, size_t /*bytes_transferred*/) {
if( timer ) if(timer)
timer->cancel(); timer->cancel();
if( ec ) { if(ec) {
std::lock_guard<std::mutex> lock( socket_mutex ); std::lock_guard<std::mutex> lock(socket_mutex);
this->socket = nullptr; this->socket=nullptr;
throw std::system_error( ec ); throw std::system_error(ec);
} }
} ); });
} }
} }
else { else {
std::lock_guard<std::mutex> lock( socket_mutex ); std::lock_guard<std::mutex> lock(socket_mutex);
socket = nullptr; socket=nullptr;
throw std::system_error( ec ); throw std::system_error(ec);
} }
} ); });
io_service.reset(); io_service.reset();
io_service.run(); io_service.run();
return request_read(); return request_read();
} }
std::shared_ptr<Response> request( const std::string& request_type, const std::string& path, std::iostream& content, std::shared_ptr<Response> request(const std::string& request_type, const std::string& path, std::iostream& content,
const std::map<std::string, std::string>& header = std::map<std::string, std::string>() ) { const std::map<std::string, std::string>& header=std::map<std::string, std::string>()) {
auto corrected_path = path; auto corrected_path=path;
if( corrected_path == "" ) if(corrected_path=="")
corrected_path = "/"; corrected_path="/";
if( !config.proxy_server.empty() && std::is_same<socket_type, asio::ip::tcp::socket>::value ) if(!config.proxy_server.empty() && std::is_same<socket_type, asio::ip::tcp::socket>::value)
corrected_path = "http://" + host + ':' + std::to_string( port ) + corrected_path; corrected_path="http://"+host+':'+std::to_string(port)+corrected_path;
content.seekp( 0, std::ios::end ); content.seekp(0, std::ios::end);
auto content_length = content.tellp(); auto content_length=content.tellp();
content.seekp( 0, std::ios::beg ); content.seekp(0, std::ios::beg);
asio::streambuf write_buffer; asio::streambuf write_buffer;
std::ostream write_stream( &write_buffer ); std::ostream write_stream(&write_buffer);
write_stream << request_type << " " << corrected_path << " HTTP/1.1\r\n"; write_stream << request_type << " " << corrected_path << " HTTP/1.1\r\n";
write_stream << "Host: " << host << "\r\n"; write_stream << "Host: " << host << "\r\n";
for( auto& h : header ) { for(auto& h: header) {
write_stream << h.first << ": " << h.second << "\r\n"; write_stream << h.first << ": " << h.second << "\r\n";
} }
if( content_length>0 ) if(content_length>0)
write_stream << "Content-Length: " << content_length << "\r\n"; write_stream << "Content-Length: " << content_length << "\r\n";
write_stream << "\r\n"; write_stream << "\r\n";
if( content_length>0 ) if(content_length>0)
write_stream << content.rdbuf(); write_stream << content.rdbuf();
connect(); connect();
auto timer = get_timeout_timer(); auto timer=get_timeout_timer();
asio::async_write( *socket, write_buffer, asio::async_write(*socket, write_buffer,
[this, timer]( const std::error_code &ec, size_t /*bytes_transferred*/ ) { [this, timer](const std::error_code &ec, size_t /*bytes_transferred*/) {
if( timer ) if(timer)
timer->cancel(); timer->cancel();
if( ec ) { if(ec) {
std::lock_guard<std::mutex> lock( socket_mutex ); std::lock_guard<std::mutex> lock(socket_mutex);
socket = nullptr; socket=nullptr;
throw std::system_error( ec ); throw std::system_error(ec);
} }
} ); });
io_service.reset(); io_service.reset();
io_service.run(); io_service.run();
return request_read(); return request_read();
} }
void close() { void close() {
std::lock_guard<std::mutex> lock( socket_mutex ); std::lock_guard<std::mutex> lock(socket_mutex);
if( socket ) { if(socket) {
std::error_code ec; std::error_code ec;
socket->lowest_layer().shutdown( asio::ip::tcp::socket::shutdown_both, ec ); socket->lowest_layer().shutdown(asio::ip::tcp::socket::shutdown_both, ec);
socket->lowest_layer().close(); socket->lowest_layer().close();
} }
} }
protected: protected:
asio::io_service io_service; asio::io_service io_service;
asio::ip::tcp::resolver resolver; asio::ip::tcp::resolver resolver;
std::unique_ptr<socket_type> socket; std::unique_ptr<socket_type> socket;
std::mutex socket_mutex; std::mutex socket_mutex;
std::string host; std::string host;
unsigned short port; unsigned short port;
ClientBase( const std::string& host_port, unsigned short default_port ) : resolver( io_service ) { ClientBase(const std::string& host_port, unsigned short default_port) : resolver(io_service) {
auto parsed_host_port = parse_host_port( host_port, default_port ); auto parsed_host_port=parse_host_port(host_port, default_port);
host = parsed_host_port.first; host=parsed_host_port.first;
port = parsed_host_port.second; port=parsed_host_port.second;
} }
std::pair<std::string, unsigned short> parse_host_port( const std::string &host_port, unsigned short default_port ) { std::pair<std::string, unsigned short> parse_host_port(const std::string &host_port, unsigned short default_port) {
std::pair<std::string, unsigned short> parsed_host_port; std::pair<std::string, unsigned short> parsed_host_port;
size_t host_end = host_port.find( ':' ); size_t host_end=host_port.find(':');
if( host_end == std::string::npos ) { if(host_end==std::string::npos) {
parsed_host_port.first = host_port; parsed_host_port.first=host_port;
parsed_host_port.second = default_port; parsed_host_port.second=default_port;
} }
else { else {
parsed_host_port.first = host_port.substr( 0, host_end ); parsed_host_port.first=host_port.substr(0, host_end);
parsed_host_port.second = static_cast<unsigned short>( stoul( host_port.substr( host_end + 1 ) ) ); parsed_host_port.second=static_cast<unsigned short>(stoul(host_port.substr(host_end+1)));
} }
return parsed_host_port; return parsed_host_port;
} }
virtual void connect() = 0; virtual void connect()=0;
std::shared_ptr< asio::deadline_timer > get_timeout_timer() { std::shared_ptr< asio::basic_waitable_timer< std::chrono::steady_clock > > get_timeout_timer() {
if( config.timeout == 0 ) if(config.timeout==0)
return nullptr; return nullptr;
auto timer = std::make_shared< asio::deadline_timer >( io_service ); auto timer=std::make_shared< asio::basic_waitable_timer< std::chrono::steady_clock > >(io_service);
timer->expires_from_now( std::chrono::seconds( config.timeout ) ); timer->expires_from_now( std::chrono::seconds( config.timeout ) );
timer->async_wait( [this]( const std::error_code& ec ) { timer->async_wait([this](const std::error_code& ec) {
if( !ec ) { if(!ec) {
close(); close();
} }
} ); });
return timer; return timer;
} }
void parse_response_header( const std::shared_ptr<Response> &response ) const { void parse_response_header(const std::shared_ptr<Response> &response) const {
std::string line; std::string line;
getline( response->content, line ); getline(response->content, line);
size_t version_end = line.find( ' ' ); size_t version_end=line.find(' ');
if( version_end != std::string::npos ) { if(version_end!=std::string::npos) {
if( 5<line.size() ) if(5<line.size())
response->http_version = line.substr( 5, version_end - 5 ); response->http_version=line.substr(5, version_end-5);
if( ( version_end + 1 )<line.size() ) if((version_end+1)<line.size())
response->status_code = line.substr( version_end + 1, line.size() - ( version_end + 1 ) - 1 ); response->status_code=line.substr(version_end+1, line.size()-(version_end+1)-1);
getline( response->content, line ); getline(response->content, line);
size_t param_end; size_t param_end;
while( ( param_end = line.find( ':' ) ) != std::string::npos ) { while((param_end=line.find(':'))!=std::string::npos) {
size_t value_start = param_end + 1; size_t value_start=param_end+1;
if( ( value_start )<line.size() ) { if((value_start)<line.size()) {
if( line[value_start] == ' ' ) if(line[value_start]==' ')
value_start++; value_start++;
if( value_start<line.size() ) if(value_start<line.size())
response->header.insert( std::make_pair( line.substr( 0, param_end ), line.substr( value_start, line.size() - value_start - 1 ) ) ); response->header.insert(std::make_pair(line.substr(0, param_end), line.substr(value_start, line.size()-value_start-1)));
} }
getline( response->content, line ); getline(response->content, line);
} }
} }
} }
std::shared_ptr<Response> request_read() { std::shared_ptr<Response> request_read() {
std::shared_ptr<Response> response( new Response() ); std::shared_ptr<Response> response(new Response());
asio::streambuf chunked_streambuf; asio::streambuf chunked_streambuf;
auto timer = get_timeout_timer(); auto timer=get_timeout_timer();
asio::async_read_until( *socket, response->content_buffer, "\r\n\r\n", asio::async_read_until(*socket, response->content_buffer, "\r\n\r\n",
[this, &response, &chunked_streambuf, timer]( const std::error_code& ec, size_t bytes_transferred ) { [this, &response, &chunked_streambuf, timer](const std::error_code& ec, size_t bytes_transferred) {
if( timer ) if(timer)
timer->cancel(); timer->cancel();
if( !ec ) { if(!ec) {
size_t num_additional_bytes = response->content_buffer.size() - bytes_transferred; size_t num_additional_bytes=response->content_buffer.size()-bytes_transferred;
parse_response_header( response ); parse_response_header(response);
auto header_it = response->header.find( "Content-Length" ); auto header_it=response->header.find("Content-Length");
if( header_it != response->header.end() ) { if(header_it!=response->header.end()) {
auto content_length = stoull( header_it->second ); auto content_length=stoull(header_it->second);
if( content_length>num_additional_bytes ) { if(content_length>num_additional_bytes) {
auto timer = get_timeout_timer(); auto timer=get_timeout_timer();
asio::async_read( *socket, response->content_buffer, asio::async_read(*socket, response->content_buffer,
asio::transfer_exactly( static_cast< size_t >( content_length - num_additional_bytes ) ), asio::transfer_exactly(content_length-num_additional_bytes),
[this, timer]( const std::error_code& ec, size_t /*bytes_transferred*/ ) { [this, timer](const std::error_code& ec, size_t /*bytes_transferred*/) {
if( timer ) if(timer)
timer->cancel(); timer->cancel();
if( ec ) { if(ec) {
std::lock_guard<std::mutex> lock( socket_mutex ); std::lock_guard<std::mutex> lock(socket_mutex);
this->socket = nullptr; this->socket=nullptr;
throw std::system_error( ec ); throw std::system_error(ec);
} }
} ); });
} }
} }
else if( ( header_it = response->header.find( "Transfer-Encoding" ) ) != response->header.end() && header_it->second == "chunked" ) { else if((header_it=response->header.find("Transfer-Encoding"))!=response->header.end() && header_it->second=="chunked") {
request_read_chunked( response, chunked_streambuf ); request_read_chunked(response, chunked_streambuf);
} }
} }
else { else {
std::lock_guard<std::mutex> lock( socket_mutex ); std::lock_guard<std::mutex> lock(socket_mutex);
socket = nullptr; socket=nullptr;
throw std::system_error( ec ); throw std::system_error(ec);
} }
} ); });
io_service.reset(); io_service.reset();
io_service.run(); io_service.run();
return response; return response;
} }
void request_read_chunked( const std::shared_ptr<Response> &response, asio::streambuf &streambuf ) { void request_read_chunked(const std::shared_ptr<Response> &response, asio::streambuf &streambuf) {
auto timer = get_timeout_timer(); auto timer=get_timeout_timer();
asio::async_read_until( *socket, response->content_buffer, "\r\n", asio::async_read_until(*socket, response->content_buffer, "\r\n",
[this, &response, &streambuf, timer]( const std::error_code& ec, size_t bytes_transferred ) { [this, &response, &streambuf, timer](const std::error_code& ec, size_t bytes_transferred) {
if( timer ) if(timer)
timer->cancel(); timer->cancel();
if( !ec ) { if(!ec) {
std::string line; std::string line;
getline( response->content, line ); getline(response->content, line);
bytes_transferred -= line.size() + 1; bytes_transferred-=line.size()+1;
line.pop_back(); line.pop_back();
std::streamsize length = stol( line, 0, 16 ); std::streamsize length=stol(line, 0, 16);
auto num_additional_bytes = response->content_buffer.size() - bytes_transferred; auto num_additional_bytes=static_cast<std::streamsize>(response->content_buffer.size()-bytes_transferred);
auto post_process = [this, &response, &streambuf, length] { auto post_process=[this, &response, &streambuf, length] {
std::ostream stream( &streambuf ); std::ostream stream(&streambuf);
if( length>0 ) { if(length>0) {
std::vector<char> buffer( static_cast<size_t>( length ) ); std::vector<char> buffer(static_cast<size_t>(length));
response->content.read( &buffer[0], length ); response->content.read(&buffer[0], length);
stream.write( &buffer[0], length ); stream.write(&buffer[0], length);
} }
//Remove "\r\n" //Remove "\r\n"
response->content.get(); response->content.get();
response->content.get(); response->content.get();
if( length>0 ) if(length>0)
request_read_chunked( response, streambuf ); request_read_chunked(response, streambuf);
else { else {
std::ostream response_stream( &response->content_buffer ); std::ostream response_stream(&response->content_buffer);
response_stream << stream.rdbuf(); response_stream << stream.rdbuf();
} }
}; };
if( ( 2 + length )>num_additional_bytes ) { if((2+length)>num_additional_bytes) {
auto timer = get_timeout_timer(); auto timer=get_timeout_timer();
asio::async_read( *socket, response->content_buffer, asio::async_read(*socket, response->content_buffer,
asio::transfer_exactly( static_cast< size_t >( 2 + length - num_additional_bytes ) ), asio::transfer_exactly(2+length-num_additional_bytes),
[this, post_process, timer]( const std::error_code& ec, size_t /*bytes_transferred*/ ) { [this, post_process, timer](const std::error_code& ec, size_t /*bytes_transferred*/) {
if( timer ) if(timer)
timer->cancel(); timer->cancel();
if( !ec ) { if(!ec) {
post_process(); post_process();
} }
else { else {
std::lock_guard<std::mutex> lock( socket_mutex ); std::lock_guard<std::mutex> lock(socket_mutex);
this->socket = nullptr; this->socket=nullptr;
throw std::system_error( ec ); throw std::system_error(ec);
} }
} ); });
} }
else else
post_process(); post_process();
} }
else { else {
std::lock_guard<std::mutex> lock( socket_mutex ); std::lock_guard<std::mutex> lock(socket_mutex);
socket = nullptr; socket=nullptr;
throw std::system_error( ec ); throw std::system_error(ec);
} }
} ); });
} }
}; };
template<class socket_type> template<class socket_type>
class Client : public ClientBase<socket_type> {}; class Client : public ClientBase<socket_type> {};
typedef asio::ip::tcp::socket HTTP; typedef asio::ip::tcp::socket HTTP;
template<> template<>
class Client<HTTP> : public ClientBase<HTTP> { class Client<HTTP> : public ClientBase<HTTP> {
public: public:
Client( const std::string& server_port_path ) : ClientBase<HTTP>::ClientBase( server_port_path, 80 ) {} Client(const std::string& server_port_path) : ClientBase<HTTP>::ClientBase(server_port_path, 80) {}
protected: protected:
void connect() { void connect() {
if( !socket || !socket->is_open() ) { if(!socket || !socket->is_open()) {
std::unique_ptr<asio::ip::tcp::resolver::query> query; std::unique_ptr<asio::ip::tcp::resolver::query> query;
if( config.proxy_server.empty() ) if(config.proxy_server.empty())
query = std::unique_ptr<asio::ip::tcp::resolver::query>( new asio::ip::tcp::resolver::query( host, std::to_string( port ) ) ); query=std::unique_ptr<asio::ip::tcp::resolver::query>(new asio::ip::tcp::resolver::query(host, std::to_string(port)));
else { else {
auto proxy_host_port = parse_host_port( config.proxy_server, 8080 ); auto proxy_host_port=parse_host_port(config.proxy_server, 8080);
query = std::unique_ptr<asio::ip::tcp::resolver::query>( new asio::ip::tcp::resolver::query( proxy_host_port.first, std::to_string( proxy_host_port.second ) ) ); query=std::unique_ptr<asio::ip::tcp::resolver::query>(new asio::ip::tcp::resolver::query(proxy_host_port.first, std::to_string(proxy_host_port.second)));
} }
resolver.async_resolve( *query, [this]( const std::error_code &ec, resolver.async_resolve(*query, [this](const std::error_code &ec,
asio::ip::tcp::resolver::iterator it ) { asio::ip::tcp::resolver::iterator it){
if( !ec ) { if(!ec) {
{ {
std::lock_guard<std::mutex> lock( socket_mutex ); std::lock_guard<std::mutex> lock(socket_mutex);
socket = std::unique_ptr<HTTP>( new HTTP( io_service ) ); socket=std::unique_ptr<HTTP>(new HTTP(io_service));
} }
auto timer = get_timeout_timer(); auto timer=get_timeout_timer();
asio::async_connect( *socket, it, [this, timer] asio::async_connect(*socket, it, [this, timer]
( const std::error_code &ec, asio::ip::tcp::resolver::iterator /*it*/ ) { (const std::error_code &ec, asio::ip::tcp::resolver::iterator /*it*/){
if( timer ) if(timer)
timer->cancel(); timer->cancel();
if( !ec ) { if(!ec) {
asio::ip::tcp::no_delay option( true ); asio::ip::tcp::no_delay option(true);
this->socket->set_option( option ); this->socket->set_option(option);
} }
else { else {
std::lock_guard<std::mutex> lock( socket_mutex ); std::lock_guard<std::mutex> lock(socket_mutex);
this->socket = nullptr; this->socket=nullptr;
throw std::system_error( ec ); throw std::system_error(ec);
} }
} ); });
} }
else { else {
std::lock_guard<std::mutex> lock( socket_mutex ); std::lock_guard<std::mutex> lock(socket_mutex);
socket = nullptr; socket=nullptr;
throw std::system_error( ec ); throw std::system_error(ec);
} }
} ); });
io_service.reset(); io_service.reset();
io_service.run(); io_service.run();
} }
} }
}; };
} }
#endif /* CLIENT_HTTP_HPP */ #endif /* CLIENT_HTTP_HPP */

View file

@ -162,7 +162,7 @@ bool Core::Scripting::ScriptLoader::isModuleLoaded( std::string name )
for( auto it = m_scriptMap.begin(); it != m_scriptMap.end(); ++it ) for( auto it = m_scriptMap.begin(); it != m_scriptMap.end(); ++it )
{ {
if( Util::toLowerCopy( it->second->library_name) == Util::toLowerCopy( name ) ) if( Util::toLowerCopy( it->second->library_name ) == Util::toLowerCopy( name ) )
return true; return true;
} }