Eris  1.4.0
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/noncopyable.hpp>
27 
28 #include <memory>
29 
30 namespace Atlas
31 {
32 class Bridge;
33 class Codec;
34 namespace Net
35 {
36 class StreamConnect;
37 }
38 namespace Objects
39 {
40 class ObjectsEncoder;
41 }
42 }
43 
44 namespace Eris
45 {
46 
53 class StreamSocket: public std::enable_shared_from_this<StreamSocket>, private boost::noncopyable
54 {
55 public:
56 
57  typedef enum
58  {
59  INVALID_STATUS = 0,
69  DISCONNECTING
70  } Status;
71 
75  struct Callbacks
76  {
80  std::function<void()> dispatch;
81 
85  std::function<void(Status)> stateChanged;
86  };
87 
88  StreamSocket(boost::asio::io_service& io_service,
89  const std::string& client_name, Atlas::Bridge& bridge,
90  Callbacks& callbacks);
91  virtual ~StreamSocket();
92 
98  void detach();
99 
105  Atlas::Codec& getCodec();
106 
112  Atlas::Objects::ObjectsEncoder& getEncoder();
113 
117  virtual void write() = 0;
118 protected:
119  enum
120  {
121  read_buffer_size = 2048
122  };
123  boost::asio::io_service& m_io_service;
124  Atlas::Bridge& _bridge;
125  Callbacks _callbacks;
126 
131  boost::asio::streambuf* mWriteBuffer;
132 
137  boost::asio::streambuf* mSendBuffer;
138 
142  boost::asio::streambuf mReadBuffer;
143 
147  std::istream mInStream;
148 
152  std::ostream mOutStream;
153 
158 
163 
164  Atlas::Net::StreamConnect* _sc;
165  boost::asio::deadline_timer _negotiateTimer;
166  boost::asio::deadline_timer _connectTimer;
167  Atlas::Codec* m_codec;
168  Atlas::Objects::ObjectsEncoder * m_encoder;
169  bool m_is_connected;
170 
171  virtual void do_read() = 0;
172  virtual void negotiate_read() = 0;
173  void startNegotiation();
174  Atlas::Negotiate::State negotiate();
175 
176 };
177 
181 template<typename ProtocolT>
183 {
184 public:
185  AsioStreamSocket(boost::asio::io_service& io_service,
186  const std::string& client_name, Atlas::Bridge& bridge,
187  StreamSocket::Callbacks& callbacks);
188  virtual ~AsioStreamSocket();
189  void connect(const typename ProtocolT::endpoint& endpoint);
190  virtual void write();
191  typename ProtocolT::socket& getAsioSocket();
192 protected:
193  typename ProtocolT::socket m_socket;
194  virtual void negotiate_read();
195  void negotiate_write();
196  virtual void do_read();
197 };
198 
202 template<typename ProtocolT>
204 {
205 public:
206  ResolvableAsioStreamSocket(boost::asio::io_service& io_service,
207  const std::string& client_name, Atlas::Bridge& bridge,
208  StreamSocket::Callbacks& callbacks);
209  void connectWithQuery(const typename ProtocolT::resolver::query& query);
210 protected:
211  typename ProtocolT::resolver m_resolver;
212 };
213 
214 }
215 #endif /* STREAMSOCKET_H_ */
boost::asio::streambuf mReadBuffer
Buffer for data being read from the socket.
Definition: StreamSocket.h:142
finished disconnection
Definition: StreamSocket.h:68
connection fully established
Definition: StreamSocket.h:66
bool mShouldSend
True if we should send again as soon as an ongoing async_write operation completes.
Definition: StreamSocket.h:157
Handles the internal socket instance, interacting with the asynchronous io_service calls...
Definition: StreamSocket.h:53
stream / socket connection in progress
Definition: StreamSocket.h:60
Atlas::Net::StreamConnect * _sc
negotiation object (NULL after connection!)
Definition: StreamSocket.h:164
failure when trying to establish a connection
Definition: StreamSocket.h:62
Methods that are used as callbacks.
Definition: StreamSocket.h:75
Every Eris class and type lives inside the Eris namespace; certain utility functions live in the Util...
Definition: Account.cpp:34
connection failed
Definition: StreamSocket.h:67
boost::asio::streambuf * mWriteBuffer
Buffer used to write data to be sent.
Definition: StreamSocket.h:131
std::ostream mOutStream
Stream for data being sent out.
Definition: StreamSocket.h:152
Template specialization which uses boost::asio sockets.
Definition: StreamSocket.h:182
std::istream mInStream
Stream for data being received.
Definition: StreamSocket.h:147
Template specialization which uses boost::asio sockets with resolvers (i.e.
Definition: StreamSocket.h:203
bool mIsSending
True if we&#39;re currently sending through an async_write (and thus shouldn&#39;t touch mSendBuffer).
Definition: StreamSocket.h:162
std::function< void(Status)> stateChanged
Called when the internal state has changed.
Definition: StreamSocket.h:85
boost::asio::streambuf * mSendBuffer
Buffer of data which is being sent.
Definition: StreamSocket.h:137
std::function< void()> dispatch
Called when operations have arrived and needs dispatching.
Definition: StreamSocket.h:80
failure when negotiating
Definition: StreamSocket.h:65
timeout when trying to establish a connection
Definition: StreamSocket.h:61
Status
Definition: StreamSocket.h:57
timeout when negotiating
Definition: StreamSocket.h:64
Definition: BaseConnection.h:18
Atlas negotiation in progress.
Definition: StreamSocket.h:63