eris  1.4.0
A WorldForge client library.
StreamSocket.h
1 /*
2  Copyright (C) 2014 Erik Ogenvik
3 
4  This program is free software; you can redistribute it and/or modify
5  it under the terms of the GNU General Public License as published by
6  the Free Software Foundation; either version 2 of the License, or
7  (at your option) any later version.
8 
9  This program is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  GNU General Public License for more details.
13 
14  You should have received a copy of the GNU General Public License
15  along with this program; if not, write to the Free Software Foundation,
16  Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
17  */
18 
19 #ifndef STREAMSOCKET_H_
20 #define STREAMSOCKET_H_
21 
22 #include <Atlas/Objects/ObjectsFwd.h>
23 #include <Atlas/Negotiate.h>
24 
25 #include <boost/asio.hpp>
26 #include <boost/asio/steady_timer.hpp>
27 #include <boost/noncopyable.hpp>
28 
29 #include <memory>
30 
31 namespace Atlas
32 {
33 class Bridge;
34 class Codec;
35 namespace Net
36 {
37 class StreamConnect;
38 }
39 namespace Objects
40 {
41 class ObjectsEncoder;
42 }
43 }
44 
45 namespace Eris
46 {
47 
54 class StreamSocket: public std::enable_shared_from_this<StreamSocket>, private boost::noncopyable
55 {
56 public:
57 
58  typedef enum
59  {
71  } Status;
72 
76  struct Callbacks
77  {
81  std::function<void()> dispatch;
82 
86  std::function<void(Status)> stateChanged;
87  };
88 
89  StreamSocket(boost::asio::io_service& io_service,
90  const std::string& client_name,
91  Atlas::Bridge& bridge,
92  Callbacks callbacks);
93  virtual ~StreamSocket();
94 
100  void detach();
101 
107  Atlas::Codec& getCodec();
108 
114  Atlas::Objects::ObjectsEncoder& getEncoder();
115 
119  virtual void write() = 0;
120 protected:
121  enum
122  {
123  read_buffer_size = 2048
124  };
125  boost::asio::io_service& m_io_service;
126  Atlas::Bridge& _bridge;
127  Callbacks _callbacks;
128 
133  std::unique_ptr<boost::asio::streambuf> mWriteBuffer;
134 
139  std::unique_ptr<boost::asio::streambuf> mSendBuffer;
140 
144  boost::asio::streambuf mReadBuffer;
145 
149  std::istream mInStream;
150 
154  std::ostream mOutStream;
155 
160 
165 
166  std::unique_ptr<Atlas::Net::StreamConnect> _sc;
167  boost::asio::steady_timer _negotiateTimer;
168  boost::asio::steady_timer _connectTimer;
169  std::unique_ptr<Atlas::Codec> m_codec;
170  std::unique_ptr<Atlas::Objects::ObjectsEncoder> m_encoder;
171  bool m_is_connected;
172 
173  virtual void do_read() = 0;
174  virtual void negotiate_read() = 0;
175  void startNegotiation();
176  Atlas::Negotiate::State negotiate();
177 
178 };
179 
183 template<typename ProtocolT>
185 {
186 public:
187  AsioStreamSocket(boost::asio::io_service& io_service,
188  const std::string& client_name, Atlas::Bridge& bridge,
189  StreamSocket::Callbacks callbacks);
190  ~AsioStreamSocket() override;
191  void connect(const typename ProtocolT::endpoint& endpoint);
192  void write() override;
193  typename ProtocolT::socket& getAsioSocket();
194 protected:
195  typename ProtocolT::socket m_socket;
196  void negotiate_read() override;
197  void negotiate_write();
198  void do_read() override;
199 };
200 
204 template<typename ProtocolT>
206 {
207 public:
208  ResolvableAsioStreamSocket(boost::asio::io_service& io_service,
209  const std::string& client_name, Atlas::Bridge& bridge,
210  StreamSocket::Callbacks callbacks);
211  void connectWithQuery(const typename ProtocolT::resolver::query& query);
212 protected:
213  typename ProtocolT::resolver m_resolver;
214 };
215 
216 }
217 #endif /* STREAMSOCKET_H_ */
Eris::AsioStreamSocket::write
void write() override
Send any unsent data.
Definition: StreamSocket_impl.h:191
Eris::StreamSocket::DISCONNECTING
@ DISCONNECTING
clean disconnection in progress
Definition: StreamSocket.h:70
Eris::StreamSocket::NEGOTIATE_TIMEOUT
@ NEGOTIATE_TIMEOUT
timeout when negotiating
Definition: StreamSocket.h:65
Eris::StreamSocket::getEncoder
Atlas::Objects::ObjectsEncoder & getEncoder()
Gets the encoder object.
Definition: StreamSocket.cpp:128
Eris::StreamSocket::Status
Status
Definition: StreamSocket.h:58
Eris::StreamSocket::NEGOTIATE
@ NEGOTIATE
Atlas negotiation in progress.
Definition: StreamSocket.h:64
Eris::ResolvableAsioStreamSocket
Template specialization which uses boost::asio sockets with resolvers (i.e. TCP and UDP,...
Definition: StreamSocket.h:205
Eris::StreamSocket::mWriteBuffer
std::unique_ptr< boost::asio::streambuf > mWriteBuffer
Definition: StreamSocket.h:133
Eris::StreamSocket
Handles the internal socket instance, interacting with the asynchronous io_service calls.
Definition: StreamSocket.h:54
Eris::StreamSocket::_sc
std::unique_ptr< Atlas::Net::StreamConnect > _sc
negotiation object (nullptr after connection!)
Definition: StreamSocket.h:166
Eris::StreamSocket::mShouldSend
bool mShouldSend
Definition: StreamSocket.h:159
Eris::StreamSocket::INVALID_STATUS
@ INVALID_STATUS
indicates an illegal state
Definition: StreamSocket.h:60
Eris::StreamSocket::mInStream
std::istream mInStream
Definition: StreamSocket.h:149
Eris::StreamSocket::write
virtual void write()=0
Send any unsent data.
Eris::StreamSocket::CONNECTING
@ CONNECTING
stream / socket connection in progress
Definition: StreamSocket.h:61
Eris::StreamSocket::CONNECTING_FAILED
@ CONNECTING_FAILED
failure when trying to establish a connection
Definition: StreamSocket.h:63
Eris::StreamSocket::NEGOTIATE_FAILED
@ NEGOTIATE_FAILED
failure when negotiating
Definition: StreamSocket.h:66
Eris::StreamSocket::mReadBuffer
boost::asio::streambuf mReadBuffer
Definition: StreamSocket.h:144
Eris::AsioStreamSocket
Template specialization which uses boost::asio sockets.
Definition: StreamSocket.h:184
Eris::StreamSocket::mSendBuffer
std::unique_ptr< boost::asio::streambuf > mSendBuffer
Definition: StreamSocket.h:139
Eris::StreamSocket::getCodec
Atlas::Codec & getCodec()
Gets the codec object.
Definition: StreamSocket.cpp:123
Eris
Definition: Account.cpp:33
Eris::StreamSocket::CONNECTION_FAILED
@ CONNECTION_FAILED
connection failed
Definition: StreamSocket.h:68
Eris::StreamSocket::Callbacks
Methods that are used as callbacks.
Definition: StreamSocket.h:76
Eris::StreamSocket::CONNECTING_TIMEOUT
@ CONNECTING_TIMEOUT
timeout when trying to establish a connection
Definition: StreamSocket.h:62
Eris::StreamSocket::Callbacks::stateChanged
std::function< void(Status)> stateChanged
Called when the internal state has changed.
Definition: StreamSocket.h:86
Eris::StreamSocket::detach
void detach()
Detaches the callbacks.
Definition: StreamSocket.cpp:58
Eris::StreamSocket::mOutStream
std::ostream mOutStream
Definition: StreamSocket.h:154
Eris::StreamSocket::Callbacks::dispatch
std::function< void()> dispatch
Called when operations have arrived and needs dispatching.
Definition: StreamSocket.h:81
Eris::StreamSocket::DISCONNECTED
@ DISCONNECTED
finished disconnection
Definition: StreamSocket.h:69
Eris::StreamSocket::mIsSending
bool mIsSending
Definition: StreamSocket.h:164
Eris::StreamSocket::CONNECTED
@ CONNECTED
connection fully established
Definition: StreamSocket.h:67