19#ifndef STREAMSOCKET_H_
20#define STREAMSOCKET_H_
22#include <Atlas/Objects/ObjectsFwd.h>
23#include <Atlas/Negotiate.h>
25#include <boost/asio.hpp>
26#include <boost/asio/steady_timer.hpp>
27#include <boost/noncopyable.hpp>
54class StreamSocket:
public std::enable_shared_from_this<StreamSocket>,
private boost::noncopyable
90 const std::string& client_name,
91 Atlas::Bridge& bridge,
123 read_buffer_size = 2048
125 boost::asio::io_service& m_io_service;
126 Atlas::Bridge& _bridge;
127 Callbacks _callbacks;
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;
173 virtual void do_read() = 0;
174 virtual void negotiate_read() = 0;
175 void startNegotiation();
176 Atlas::Negotiate::State negotiate();
183template<
typename ProtocolT>
188 const std::string& client_name, Atlas::Bridge& bridge,
191 void connect(
const typename ProtocolT::endpoint& endpoint);
192 void write()
override;
193 typename ProtocolT::socket& getAsioSocket();
195 typename ProtocolT::socket m_socket;
196 void negotiate_read()
override;
197 void negotiate_write();
198 void do_read()
override;
204template<
typename ProtocolT>
209 const std::string& client_name, Atlas::Bridge& bridge,
211 void connectWithQuery(
const typename ProtocolT::resolver::query& query);
213 typename ProtocolT::resolver m_resolver;
Template specialization which uses boost::asio sockets.
void write() override
Send any unsent data.
Template specialization which uses boost::asio sockets with resolvers (i.e. TCP and UDP,...
Handles the internal socket instance, interacting with the asynchronous io_service calls.
void detach()
Detaches the callbacks.
std::unique_ptr< boost::asio::streambuf > mWriteBuffer
Atlas::Codec & getCodec()
Gets the codec object.
std::unique_ptr< Atlas::Net::StreamConnect > _sc
negotiation object (nullptr after connection!)
boost::asio::streambuf mReadBuffer
std::unique_ptr< boost::asio::streambuf > mSendBuffer
virtual void write()=0
Send any unsent data.
Atlas::Objects::ObjectsEncoder & getEncoder()
Gets the encoder object.
@ DISCONNECTING
clean disconnection in progress
@ NEGOTIATE_TIMEOUT
timeout when negotiating
@ NEGOTIATE_FAILED
failure when negotiating
@ DISCONNECTED
finished disconnection
@ CONNECTED
connection fully established
@ INVALID_STATUS
indicates an illegal state
@ CONNECTING_FAILED
failure when trying to establish a connection
@ CONNECTING_TIMEOUT
timeout when trying to establish a connection
@ CONNECTION_FAILED
connection failed
@ NEGOTIATE
Atlas negotiation in progress.
@ CONNECTING
stream / socket connection in progress
Methods that are used as callbacks.
std::function< void(Status)> stateChanged
Called when the internal state has changed.
std::function< void()> dispatch
Called when operations have arrived and needs dispatching.