Eris  1.3.21
BaseConnection.h
1 #ifndef ERIS_BASE_CONNECTION_H
2 #define ERIS_BASE_CONNECTION_H
3 
4 #include <Atlas/Objects/ObjectsFwd.h>
5 
6 #include <sigc++/trackable.h>
7 #include <sigc++/signal.h>
8 
9 #include <string>
10 
11 class tcp_socket_stream;
12 
13 namespace Atlas
14 {
15  class Bridge;
16  class Codec;
17  namespace Net
18  {
19  class StreamConnect;
20  }
21 }
22 
23 namespace Eris
24 {
25 
26 // Forward declarations
27 class Timeout;
28 
30 class BaseConnection : virtual public sigc::trackable
31 {
32 public:
34  virtual ~BaseConnection();
35 
38  virtual int connect(const std::string &host, short port);
39 
41  typedef enum {
48 
49  // doesn't really belong here, but enums aren't subclassable
51  } Status;
52 
54  Status getStatus() const
55  { return _status; }
56 
58  bool isConnected() const
59  { return (_status == CONNECTED) || (_status == DISCONNECTING);}
60 
64  int getFileDescriptor();
65 
71  const std::string& getHost() const;
72 
78  short getPort() const;
79 
81  sigc::signal<void> Connected;
82 
84  sigc::signal<void> Disconnected;
85 protected:
87 
90  BaseConnection(const std::string &cnm, const std::string &id, Atlas::Bridge *br);
91 
93  void recv();
94 
96  virtual void setStatus(Status sc);
97 
99  virtual void onConnect();
100 
102  virtual void handleFailure(const std::string &msg) = 0;
103 
104  virtual void handleTimeout(const std::string& msg) = 0;
105 
106  void onConnectTimeout();
107  void onNegotiateTimeout();
108 
111  void hardDisconnect(bool emit);
112 
114  void nonblockingConnect();
115 
117  void pollNegotiation();
118 
119  Atlas::Objects::ObjectsEncoder* _encode;
120  Atlas::Net::StreamConnect* _sc;
121  Atlas::Codec* m_codec;
122 
124  const std::string _id;
125 
126  tcp_socket_stream* _stream;
127  std::string _clientName;
128 
131  Atlas::Bridge* _bridge;
133 
134  std::string _host;
135  short _port;
136 };
137 
138 }
139 
140 #endif
141 
tcp_socket_stream * _stream
the underlying iostream channel
Definition: BaseConnection.h:126
Atlas::Objects::ObjectsEncoder * _encode
the objects encoder, bound to _codec
Definition: BaseConnection.h:119
meta-query performing GET operation
Definition: BaseConnection.h:50
Status
possible states for the connection
Definition: BaseConnection.h:41
void recv()
perform a blocking read from the underlying socket
Definition: BaseConnection.cpp:156
void hardDisconnect(bool emit)
performs and instant disconnection from the server specified whether the change of state should be s...
Definition: BaseConnection.cpp:110
std::string _clientName
the client identified used during connection
Definition: BaseConnection.h:127
connection fully established
Definition: BaseConnection.h:45
Atlas::Net::StreamConnect * _sc
negotiation object (NULL after connection!)
Definition: BaseConnection.h:120
int getFileDescriptor()
get the underlyinmg file descriptor (socket).
Definition: BaseConnection.cpp:285
stream / socket connection in progress
Definition: BaseConnection.h:44
void pollNegotiation()
track negotation of the Atlas codecs / stream
Definition: BaseConnection.cpp:225
Status getStatus() const
get the current status of the connection
Definition: BaseConnection.h:54
Timeout * _timeout
network level timeouts
Definition: BaseConnection.h:132
clean disconnection in progress
Definition: BaseConnection.h:47
virtual ~BaseConnection()
destructor, will perform a hard disconnect if necessary
Definition: BaseConnection.cpp:69
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
Definition: BaseConnection.cpp:280
const std::string & getHost() const
Gets the host of the connection.
Definition: BaseConnection.cpp:292
finished disconnection
Definition: BaseConnection.h:46
virtual int connect(const std::string &host, short port)
open a connection to the specified host/port; invokes the failure handler if the connection could not...
Definition: BaseConnection.cpp:77
const std::string _id
a unique identifier for this connection
Definition: BaseConnection.h:124
Atlas::Bridge * _bridge
the connection bridge (i.e something implementing objectArrived()) : this can be the derived class it...
Definition: BaseConnection.h:131
BaseConnection(const std::string &cnm, const std::string &id, Atlas::Bridge *br)
create an unconnected instance
Definition: BaseConnection.cpp:44
sigc::signal< void > Connected
sent on successful negotiation of a game server connection
Definition: BaseConnection.h:81
Atlas negotiation in progress.
Definition: BaseConnection.h:43
Status _status
current status of the connection
Definition: BaseConnection.h:123
short _port
the port we're connected to
Definition: BaseConnection.h:135
Timeout.
Definition: Timeout.h:12
bool isConnected() const
Ascertain whether or not the connection is usable for transport.
Definition: BaseConnection.h:58
void nonblockingConnect()
complete the connection state and start negotiation
Definition: BaseConnection.cpp:193
short getPort() const
Gets the port of the connection.
Definition: BaseConnection.cpp:297
virtual void onConnect()
derived-class notification when connection and negotiation is completed
Definition: BaseConnection.cpp:260
std::string _host
the host name we're connected to
Definition: BaseConnection.h:134
Underlying Atlas connection, providing a send interface, and receive (dispatch) system.
Definition: BaseConnection.h:30
sigc::signal< void > Disconnected
final disconnect (or hard disocnnect) notifcation
Definition: BaseConnection.h:84
indicates an illegal state
Definition: BaseConnection.h:42