void xnet_handshake_message_handler::append_uuid(const xstring& uuid) { try { xuuid_string_generator uuid_str_gen; xdebug_info(xchar_format(xtr(_X("Adding UUID \"{1}\" to acceptable UUID list."))) % uuid); acceptable_uuids().insert(uuid_str_gen(uuid)); } catch (...) { xdebug_info(xchar_format(xtr(_X("Failed to add \"{1}\" to acceptable UUID list."))) % uuid); } }
void xtcp_server::start_accept() { xdebug_info(_X("Accepting connection...")); xtcp_io_object_ptr io_object(new xtcp_io_object(io_service())); acceptor_.async_accept(io_object->socket(), xbind(&xtcp_server::on_accept, this, io_object, xplaceholders::error)); }
void xtcp_server::on_accept(xtcp_io_object_ptr& io_object, const xerror_code& error_code) { if (error_code) { xdebug_info((xchar_format(xtr(_X("Failed to accept connection with error \"{1}\"."))) % match_str<xstring, std::string>::apply(error_code.message()))); // TODO: What should be done here } else { xdebug_info((xchar_format(xtr(_X("Accepted one connection from ({1}:{2})."))) % match_str<xstring, std::string>::apply(io_object->peer_endpoint().address().to_string()) % io_object->peer_endpoint().port())); // Start next accept start_accept(); // Issue the signal connection_established_sig_(io_object); } }
void xnet_handshake_message::set_uuid(const xstring& uuid) { try { xuuid_string_generator uuid_str_gen; uuid_ = uuid_str_gen(uuid); } catch (...) { xdebug_info(xchar_format(xtr(_X("Failed to create UUID from string \"{1}\"."))) % uuid); } }
void xnet_handshake_message_handler::handle_message(xnet_message_ptr message, xnet_message_handler_context_ptr context) { xdebug_info(_X("Handling handshake message...")); xassert(message); xassert(context); // Call this only once since this should not change once initialized on startup static const xuuid_set& acceptable_uuids = xnet_handshake_message_handler::acceptable_uuids(); xnet_handshake_message_ptr handshake_message = xdynamic_pointer_cast<xnet_handshake_message>(message); xassert(handshake_message); // If the UUID in the handshake message in the acceptable UUID set, then handshake accepted bool accepted = acceptable_uuids.find(handshake_message->uuid()) != acceptable_uuids.end(); xdebug_info((xchar_format(xtr(_X("The UUID received from peer side is \"{1}\", which is {2}."))) % xuuid_to_xstring(handshake_message->uuid()) % (accepted ? _X("acceptable") : _X("not acceptable")) )); // If accept this handshake, first accept the heartbeat parameters if (accepted) { context->accept_heartbeat_params(handshake_message->heartbeat_interval(), handshake_message->heartbeat_threshold()); } // Then handle the result context->handle_handshake_result(accepted); }
bool xtcp_server::start(const xtcp_endpoint& endpoint) { xdebug_info((xchar_format(xtr(_X("Starting TCP server at endpoint ({1}:{2})..."))) % match_str<xstring, std::string>::apply(endpoint.address().to_string()) % endpoint.port())); xerror_code error_code; acceptor_.open(endpoint.protocol(), error_code); if (error_code) { xlog_error(xchar_format(xtr(_X("Failed to open acceptor with endpoint ({1}:{2}) with error \"{3}\"."))) % match_str<xstring, std::string>::apply(endpoint.address().to_string()) % endpoint.port() % match_str<xstring, std::string>::apply(error_code.message())); return false; } // acceptor_.bind(endpoint, error_code); if (error_code) { xlog_error(xchar_format(xtr(_X("Failed to bind acceptor with endpoint ({1}:{2}) with error \"{3}\"."))) % match_str<xstring, std::string>::apply(endpoint.address().to_string()) % endpoint.port() % match_str<xstring, std::string>::apply(error_code.message())); return false; } // acceptor_.listen(MAX_CONNECTIONS, error_code); if (error_code) { xlog_error(xchar_format(xtr(_X("Failed to listen to endpoint ({1}:{2}) with error \"{3}\"."))) % match_str<xstring, std::string>::apply(endpoint.address().to_string()) % endpoint.port() % match_str<xstring, std::string>::apply(error_code.message())); return false; } start_accept(); return true; }
xtcp_server::~xtcp_server() { xdebug_info(_X("Deleting xtcp_server...")); }
xtcp_server::xtcp_server(xio_service& io_service) : io_service_(io_service), acceptor_(io_service) { xdebug_info(_X("Creating xtcp_server...")); }