eris  1.4.0
A WorldForge client library.
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 // Forward declarations
22 class Timeout;
23 
24 class PollData;
25 
26 class TypeService;
27 
28 class Router;
29 
30 class Redispatch;
31 
32 class ResponseTracker;
33 
34 class TestInjector;
35 
36 class EventService;
37 
39 
42 struct ConnectionDecoder;
43 
44 class Connection :
45  public BaseConnection{
46 public:
47  friend struct ConnectionDecoder;
49 
53  Connection(boost::asio::io_service& io_service,
54  EventService& eventService,
55  std::string clientName,
56  const std::string& host,
57  short port);
58 
63  Connection(boost::asio::io_service& io_service,
64  EventService& eventService,
65  std::string clientName,
66  std::string socket);
67 
68  ~Connection() override;
69 
73  int connect();
74 
76  int disconnect();
77 
78  TypeService& getTypeService() const { return *m_typeService; }
79 
80  ResponseTracker& getResponder() const { return *m_responder; }
81 
82  EventService& getEventService();
83 
85 
88  virtual void send(const Atlas::Objects::Root& obj);
89 
90  void setDefaultRouter(Router* router);
91 
92  void clearDefaultRouter();
93 
94  void registerRouterForTo(Router* router, const std::string& toId);
95 
96  void unregisterRouterForTo(Router* router, const std::string& toId);
97 
98  void registerRouterForFrom(Router* router, const std::string& fromId);
99 
100  void unregisterRouterForFrom(const std::string& fromId);
101 
106  void lock();
107 
110  void unlock();
111 
118  void refreshServerInfo();
119 
125  void getServerInfo(ServerInfo&) const;
126 
127  sigc::signal<void> GotServerInfo;
128 
130 
134  sigc::signal<bool> Disconnecting;
135 
142  sigc::signal<void, const std::string&> Failure;
143 
145 
148  sigc::signal<void, Status> StatusChanged;
149 
150 protected:
153  void setStatus(Status sc) override;
154 
156  void handleFailure(const std::string& msg) override;
157 
158  void handleTimeout(const std::string& msg) override;
159 
160  void onConnect() override;
161 
162  virtual void objectArrived(Atlas::Objects::Root obj);
163 
164  std::unique_ptr<ConnectionDecoder> m_decoder;
165 
166  EventService& _eventService;
167 
168  const std::string _host;
169  const short _port;
170  const std::string _localSocket;
171 
172  friend class Redispatch;
173 
174  friend class TestInjector;
175 
178  void postForDispatch(const Atlas::Objects::Root& obj);
179 
180  void cleanupRedispatch(Redispatch* r);
181 
182  void dispatch() override;
183 
184  void dispatchOp(const Atlas::Objects::Operation::RootOperation& op);
185 
186  void handleServerInfo(const Atlas::Objects::Operation::RootOperation& op);
187 
188  void onDisconnectTimeout();
189 
190  typedef std::deque<Atlas::Objects::Operation::RootOperation> OpDeque;
191  OpDeque m_opDeque;
192 
193  std::unique_ptr<TypeService> m_typeService;
194  Router* m_defaultRouter; // need several of these?
195 
196  typedef std::unordered_map<std::string, Router*> IdRouterMap;
197  IdRouterMap m_toRouters;
198  IdRouterMap m_fromRouters;
199 
200  int m_lock;
201 
202  std::vector<std::unique_ptr<Redispatch>> m_finishedRedispatches;
203  ServerInfo m_info;
204 
205  std::unique_ptr<ResponseTracker> m_responder;
206 };
207 
209 std::int64_t getNewSerialno();
210 
211 } // of Eris namespace
212 
213 #endif
214 
void onConnect() override
derived-class notification when connection and negotiation is completed
Definition: Connection.cpp:359
void setStatus(Status sc) override
Definition: Connection.cpp:331
Status
possible states for the connection
void handleFailure(const std::string &msg) override
Process failures (to track when reconnection should be permitted)
Definition: Connection.cpp:336
int disconnect()
Initiate disconnection from the server.
Definition: Connection.cpp:105
void postForDispatch(const Atlas::Objects::Root &obj)
Definition: Connection.cpp:370
void getServerInfo(ServerInfo &) const
Definition: Connection.cpp:242
OpDeque m_opDeque
store of all the received ops waiting to be dispatched
Definition: Connection.h:191
sigc::signal< bool > Disconnecting
Definition: Connection.h:134
Connection(boost::asio::io_service &io_service, EventService &eventService, std::string clientName, const std::string &host, short port)
Create an unconnected instance.
Definition: Connection.cpp:48
Definition: Account.cpp:33
Handles polling of the IO system as well as making sure that registered handlers are run on the main ...
Definition: EventService.h:42
virtual void send(const Atlas::Objects::Root &obj)
Transmit an Atlas::Objects instance to the server.
Definition: Connection.cpp:160
std::int64_t getNewSerialno()
operation serial number sequencing
Definition: Connection.cpp:390
sigc::signal< void, Status > StatusChanged
indicates a status change on the connection
Definition: Connection.h:148
const short _port
port of the server
Definition: Connection.h:169
sigc::signal< void, const std::string & > Failure
Definition: Connection.h:142
void refreshServerInfo()
Definition: Connection.cpp:246
Underlying Atlas connection, providing a send interface, and receive (dispatch) system.