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
31namespace Atlas
32{
33class Bridge;
34class Codec;
35namespace Net
36{
37class StreamConnect;
38}
39namespace Objects
40{
41class ObjectsEncoder;
42}
43}
44
45namespace Eris
46{
47
54class StreamSocket: public std::enable_shared_from_this<StreamSocket>, private boost::noncopyable
55{
56public:
57
58 typedef enum
59 {
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;
120protected:
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
183template<typename ProtocolT>
185{
186public:
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();
194protected:
195 typename ProtocolT::socket m_socket;
196 void negotiate_read() override;
197 void negotiate_write();
198 void do_read() override;
199};
200
204template<typename ProtocolT>
206{
207public:
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);
212protected:
213 typename ProtocolT::resolver m_resolver;
214};
215
216}
217#endif /* STREAMSOCKET_H_ */
Template specialization which uses boost::asio sockets.
Definition: StreamSocket.h:185
void write() override
Send any unsent data.
Template specialization which uses boost::asio sockets with resolvers (i.e. TCP and UDP,...
Definition: StreamSocket.h:206
Handles the internal socket instance, interacting with the asynchronous io_service calls.
Definition: StreamSocket.h:55
void detach()
Detaches the callbacks.
std::istream mInStream
Definition: StreamSocket.h:149
std::unique_ptr< boost::asio::streambuf > mWriteBuffer
Definition: StreamSocket.h:133
Atlas::Codec & getCodec()
Gets the codec object.
std::unique_ptr< Atlas::Net::StreamConnect > _sc
negotiation object (nullptr after connection!)
Definition: StreamSocket.h:166
std::ostream mOutStream
Definition: StreamSocket.h:154
boost::asio::streambuf mReadBuffer
Definition: StreamSocket.h:144
std::unique_ptr< boost::asio::streambuf > mSendBuffer
Definition: StreamSocket.h:139
virtual void write()=0
Send any unsent data.
Atlas::Objects::ObjectsEncoder & getEncoder()
Gets the encoder object.
@ DISCONNECTING
clean disconnection in progress
Definition: StreamSocket.h:70
@ NEGOTIATE_TIMEOUT
timeout when negotiating
Definition: StreamSocket.h:65
@ NEGOTIATE_FAILED
failure when negotiating
Definition: StreamSocket.h:66
@ DISCONNECTED
finished disconnection
Definition: StreamSocket.h:69
@ CONNECTED
connection fully established
Definition: StreamSocket.h:67
@ INVALID_STATUS
indicates an illegal state
Definition: StreamSocket.h:60
@ CONNECTING_FAILED
failure when trying to establish a connection
Definition: StreamSocket.h:63
@ CONNECTING_TIMEOUT
timeout when trying to establish a connection
Definition: StreamSocket.h:62
@ CONNECTION_FAILED
connection failed
Definition: StreamSocket.h:68
@ NEGOTIATE
Atlas negotiation in progress.
Definition: StreamSocket.h:64
@ CONNECTING
stream / socket connection in progress
Definition: StreamSocket.h:61
Definition: Account.cpp:33
Methods that are used as callbacks.
Definition: StreamSocket.h:77
std::function< void(Status)> stateChanged
Called when the internal state has changed.
Definition: StreamSocket.h:86
std::function< void()> dispatch
Called when operations have arrived and needs dispatching.
Definition: StreamSocket.h:81