7 #include "BaseConnection.h" 9 #include "Exceptions.h" 11 #include "StreamSocket_impl.h" 13 #include <Atlas/Codec.h> 14 #include <Atlas/Net/Stream.h> 15 #include <Atlas/Objects/Encoder.h> 17 #include <sigc++/slot.h> 20 #include <Atlas/Objects/Factories.h> 21 #include "CustomEntities.h" 26 #define snprintf _snprintf 38 BaseConnection::BaseConnection(io_service& io_service,
39 std::string clientName,
41 _io_service(io_service),
42 _factories(new
Atlas::Objects::Factories()),
43 _status(DISCONNECTED),
45 _clientName(
std::move(clientName)),
48 if (!_factories->hasFactory(
"sys")) {
49 Atlas::Objects::Entity::SYS_NO = _factories->addFactory(
"sys",
50 &Atlas::Objects::factory<Atlas::Objects::Entity::SysData>, &Atlas::Objects::defaultInstance<Atlas::Objects::Entity::SysData>);
73 callbacks.
dispatch = [&] {this->dispatch();};
80 this->stateChanged(state);};
83 _socket.reset(socket);
86 ip::tcp::resolver::query query(host, ss.str());
88 socket->connectWithQuery(query);
89 }
catch (
const std::exception& e) {
90 error() <<
"Error when trying to connect to " << host <<
" on port " 91 << port <<
": " << e.what();
109 callbacks.
dispatch = [&] {this->dispatch();};
114 _socket.reset(socket);
116 socket->connect(local::stream_protocol::endpoint(filename));
117 }
catch (
const std::exception& e) {
145 onNegotiateTimeout();
184 void BaseConnection::onConnectTimeout()
186 std::ostringstream os;
187 os <<
"Connect to " <<
_host <<
':' <<
_port <<
" timed out";
188 handleTimeout(os.str());
192 void BaseConnection::onNegotiateTimeout()
194 handleTimeout(
"Atlas negotiation timed out");
213 Atlas::Objects::Factories& BaseConnection::getFactories() {
217 const Atlas::Objects::Factories& BaseConnection::getFactories()
const {
Status
possible states for the connection
virtual int connectLocal(const std::string &socket)
connection fully established
void hardDisconnect(bool emit)
stream / socket connection in progress
virtual int connectRemote(const std::string &host, short port)
std::string _clientName
the client identified used during connection
connection fully established
failure when trying to establish a connection
stream / socket connection in progress
Methods that are used as callbacks.
clean disconnection in progress
clean disconnection in progress
virtual ~BaseConnection()
destructor, will perform a hard disconnect if necessary
virtual void handleFailure(const std::string &msg)=0
derived-class notification when a failure occurs
virtual void setStatus(Status sc)
update the connection status and generate signals
const std::string & getHost() const
Template specialization which uses boost::asio sockets.
Template specialization which uses boost::asio sockets with resolvers (i.e. TCP and UDP...
sigc::signal< void > Connected
sent on successful negotiation of a game server connection
Atlas negotiation in progress.
std::function< void(Status)> stateChanged
Called when the internal state has changed.
Status _status
current status of the connection
short _port
the port we're connected to
std::function< void()> dispatch
Called when operations have arrived and needs dispatching.
virtual void onConnect()
derived-class notification when connection and negotiation is completed
timeout when trying to establish a connection
std::string _host
the host name we're connected to
sigc::signal< void > Disconnected
final disconnect (or hard disocnnect) notifcation
Atlas negotiation in progress.