eris
1.4.0
A WorldForge client library.
|
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
31 using namespace boost::asio;
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 {
void hardDisconnect(bool emit)
@ DISCONNECTING
clean disconnection in progress
virtual int connectRemote(const std::string &host, short port)
@ NEGOTIATE_TIMEOUT
timeout when negotiating
virtual ~BaseConnection()
destructor, will perform a hard disconnect if necessary
virtual void onConnect()
derived-class notification when connection and negotiation is completed
sigc::signal< void > Disconnected
final disconnect (or hard disocnnect) notifcation
@ NEGOTIATE
Atlas negotiation in progress.
Template specialization which uses boost::asio sockets with resolvers (i.e. TCP and UDP,...
Status _status
current status of the connection
sigc::signal< void > Connected
sent on successful negotiation of a game server connection
virtual void setStatus(Status sc)
update the connection status and generate signals
@ DISCONNECTED
finished disconnection
std::string _host
the host name we're connected to
@ CONNECTING
stream / socket connection in progress
@ NEGOTIATE
Atlas negotiation in progress.
@ CONNECTING
stream / socket connection in progress
@ CONNECTING_FAILED
failure when trying to establish a connection
@ NEGOTIATE_FAILED
failure when negotiating
Template specialization which uses boost::asio sockets.
virtual void handleFailure(const std::string &msg)=0
derived-class notification when a failure occurs
@ CONNECTION_FAILED
connection failed
Methods that are used as callbacks.
@ DISCONNECTING
clean disconnection in progress
@ CONNECTING_TIMEOUT
timeout when trying to establish a connection
std::function< void(Status)> stateChanged
Called when the internal state has changed.
@ CONNECTED
connection fully established
const std::string & getHost() const
std::function< void()> dispatch
Called when operations have arrived and needs dispatching.
short _port
the port we're connected to
std::string _clientName
the client identified used during connection
virtual int connectLocal(const std::string &socket)
Status
possible states for the connection
@ CONNECTED
connection fully established