Eris  1.4.0
Connection.h
1 #ifndef ERIS_CONNECTION_H
2 #define ERIS_CONNECTION_H
3 
4 #include "BaseConnection.h"
5 #include "ServerInfo.h"
6 
7 #include <Atlas/Objects/Decoder.h>
8 #include <Atlas/Objects/ObjectsFwd.h>
9 #include <Atlas/Objects/RootOperation.h>
10 
11 #include <deque>
12 #include <map>
13 #include <unordered_map>
14 #include <memory>
15 
19 namespace Eris
20 {
21 
22 // Forward declarations
23 class Timeout;
24 class PollData;
25 class TypeService;
26 class Router;
27 class Redispatch;
28 class ResponseTracker;
29 class TestInjector;
30 class EventService;
31 
33 
36 class Connection :
37  public BaseConnection,
38  public Atlas::Objects::ObjectsDecoder
39 {
40 public:
42 
46  Connection(boost::asio::io_service& io_service, EventService& eventService, const std::string &cnm, const std::string& host, short port);
47 
52  Connection(boost::asio::io_service& io_service, EventService& eventService, const std::string &cnm, const std::string& socket);
53 
54  ~Connection() override;
55 
59  int connect();
60 
62  int disconnect();
63 
64  TypeService* getTypeService() const
65  { return m_typeService.get(); }
66 
67  ResponseTracker* getResponder() const
68  { return m_responder.get(); }
69 
70  EventService& getEventService();
71 
73 
76  virtual void send(const Atlas::Objects::Root &obj);
77 
78  void setDefaultRouter(Router* router);
79 
80  void clearDefaultRouter();
81 
82  void registerRouterForTo(Router* router, const std::string& toId);
83  void unregisterRouterForTo(Router* router, const std::string& toId);
84 
85  void registerRouterForFrom(Router* router, const std::string& fromId);
86  void unregisterRouterForFrom(Router* router, const std::string& fromId);
87 
92  void lock();
93 
96  void unlock();
97 
104  void refreshServerInfo();
105 
111  void getServerInfo(ServerInfo&) const;
112 
113  sigc::signal<void> GotServerInfo;
114 
116 
120  sigc::signal<bool> Disconnecting;
121 
128  sigc::signal<void, const std::string&> Failure;
129 
131 
134  sigc::signal<void, Status> StatusChanged;
135 
136 protected:
139  void setStatus(Status sc) override;
140 
142  void handleFailure(const std::string &msg) override;
143 
144  void handleTimeout(const std::string& msg) override;
145 
146  void onConnect() override;
147 
148  void objectArrived(const Atlas::Objects::Root& obj) override;
149 
150  EventService& _eventService;
151 
152  const std::string _host;
153  const short _port;
154  const std::string _localSocket;
155 
156  friend class Redispatch;
157  friend class TestInjector;
158 
161  void postForDispatch(const Atlas::Objects::Root& obj);
162 
163  void cleanupRedispatch(Redispatch* r);
164 
165  void dispatch() override;
166 
167  void dispatchOp(const Atlas::Objects::Operation::RootOperation& op);
168  void handleServerInfo(const Atlas::Objects::Operation::RootOperation& op);
169 
170  void onDisconnectTimeout();
171 
172  typedef std::deque<Atlas::Objects::Operation::RootOperation> OpDeque;
173  OpDeque m_opDeque;
174 
175  std::unique_ptr<TypeService> m_typeService;
176  Router* m_defaultRouter; // need several of these?
177 
178  typedef std::unordered_map<std::string, Router*> IdRouterMap;
179  IdRouterMap m_toRouters;
180  IdRouterMap m_fromRouters;
181 
182  int m_lock;
183 
184  std::vector<Redispatch*> m_finishedRedispatches;
185  ServerInfo m_info;
186 
187  std::unique_ptr<ResponseTracker> m_responder;
188 };
189 
191 long getNewSerialno();
192 
193 } // of Eris namespace
194 
195 #endif
196 
void onConnect() override
derived-class notification when connection and negotiation is completed
Definition: Connection.cpp:353
Definition: Redispatch.h:17
void setStatus(Status sc) override
update the connection status (and emit the appropriate signal)
Definition: Connection.cpp:323
Status
possible states for the connection
Definition: BaseConnection.h:52
void handleFailure(const std::string &msg) override
Process failures (to track when reconnection should be permitted)
Definition: Connection.cpp:329
Information about a specific game server, retrieved via the Meta-server and anonymous GETs...
Definition: ServerInfo.h:22
int disconnect()
Initiate disconnection from the server.
Definition: Connection.cpp:86
void postForDispatch(const Atlas::Objects::Root &obj)
Inject a local operation into the dispatch queue.
Definition: Connection.cpp:366
OpDeque m_opDeque
store of all the received ops waiting to be dispatched
Definition: Connection.h:173
Connection(boost::asio::io_service &io_service, EventService &eventService, const std::string &cnm, const std::string &host, short port)
Create an unconnected instance.
Definition: Connection.cpp:35
sigc::signal< bool > Disconnecting
Emitted when the disconnection process is initiated.
Definition: Connection.h:120
long getNewSerialno()
operation serial number sequencing
Definition: Connection.cpp:388
Every Eris class and type lives inside the Eris namespace; certain utility functions live in the Util...
Definition: Account.cpp:34
void unlock()
Unlock the connection (permit status change).
Definition: Connection.cpp:217
Underlying Atlas connection, providing a send interface, and receive (dispatch) system.
Definition: Connection.h:36
Handles polling of the IO system as well as making sure that registered handlers are run on the main ...
Definition: EventService.h:43
virtual void send(const Atlas::Objects::Root &obj)
Transmit an Atlas::Objects instance to the server.
Definition: Connection.cpp:147
sigc::signal< void, Status > StatusChanged
indicates a status change on the connection
Definition: Connection.h:134
const short _port
port of the server
Definition: Connection.h:153
int connect()
If the underlying socket cannot be opened, connect will return an error number immediately.
Definition: Connection.cpp:76
A service class querying and caching types.
Definition: TypeService.h:24
Definition: Response.h:61
sigc::signal< void, const std::string & > Failure
Emitted when a non-fatal error occurs.
Definition: Connection.h:128
void refreshServerInfo()
Update the information stored about the current server.
Definition: Connection.cpp:245
Underlying Atlas connection, providing a send interface, and receive (dispatch) system.
Definition: BaseConnection.h:36
void lock()
Lock then connection&#39;s state.
Definition: Connection.cpp:212
abstract interface for objects that can route Atlas data.
Definition: Router.h:10
void getServerInfo(ServerInfo &) const
Retrive the current server information.
Definition: Connection.cpp:240