2014-01-25 20:29:45 +01:00
|
|
|
/*
|
|
|
|
|
Copyright (C) 2014 Erik Ogenvik
|
|
|
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or modify
|
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
|
the Free Software Foundation; either version 2 of the License, or
|
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
|
along with this program; if not, write to the Free Software
|
|
|
|
|
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
|
|
|
|
*/
|
|
|
|
|
|
2014-01-27 22:00:11 +01:00
|
|
|
#ifndef COMMASIOCLIENT_H_
|
|
|
|
|
#define COMMASIOCLIENT_H_
|
2014-01-25 20:29:45 +01:00
|
|
|
|
|
|
|
|
#include "common/Link.h"
|
2014-01-27 22:00:11 +01:00
|
|
|
#include "common/CommSocket.h"
|
2014-01-25 20:29:45 +01:00
|
|
|
|
|
|
|
|
#include <Atlas/Objects/Decoder.h>
|
|
|
|
|
#include <Atlas/Objects/ObjectsFwd.h>
|
|
|
|
|
#include <Atlas/Codec.h>
|
|
|
|
|
#include <Atlas/Negotiate.h>
|
|
|
|
|
|
|
|
|
|
#include <boost/asio.hpp>
|
|
|
|
|
#include <boost/asio/buffer.hpp>
|
2014-02-03 22:00:46 +01:00
|
|
|
#include <boost/asio/deadline_timer.hpp>
|
2014-01-25 20:29:45 +01:00
|
|
|
|
|
|
|
|
#include <memory>
|
|
|
|
|
#include <sstream>
|
|
|
|
|
#include <deque>
|
|
|
|
|
|
2014-01-29 09:05:07 +01:00
|
|
|
template<typename ProtocolT>
|
2014-01-27 22:00:11 +01:00
|
|
|
class CommAsioClient: public Atlas::Objects::ObjectsDecoder,
|
|
|
|
|
public CommSocket,
|
2014-01-29 09:05:07 +01:00
|
|
|
public std::enable_shared_from_this<CommAsioClient<ProtocolT> >
|
2014-01-25 20:29:45 +01:00
|
|
|
{
|
|
|
|
|
public:
|
2014-01-27 22:00:11 +01:00
|
|
|
CommAsioClient(const std::string & name,
|
|
|
|
|
boost::asio::io_service& io_service);
|
|
|
|
|
virtual ~CommAsioClient();
|
2014-01-25 20:29:45 +01:00
|
|
|
|
2014-01-29 09:05:07 +01:00
|
|
|
typename ProtocolT::socket& getSocket();
|
2014-01-25 20:29:45 +01:00
|
|
|
|
2014-01-27 22:00:11 +01:00
|
|
|
void startAccept(Link * connection);
|
|
|
|
|
void startConnect(Link * connection);
|
2014-01-25 20:29:45 +01:00
|
|
|
int send(const Atlas::Objects::Operation::RootOperation &);
|
|
|
|
|
|
|
|
|
|
/// \brief STL deque of pointers to operation objects.
|
|
|
|
|
typedef std::deque<Atlas::Objects::Operation::RootOperation> DispatchQueue;
|
|
|
|
|
|
|
|
|
|
virtual void disconnect();
|
|
|
|
|
|
|
|
|
|
virtual int flush();
|
|
|
|
|
|
|
|
|
|
protected:
|
2014-01-29 09:05:07 +01:00
|
|
|
typename ProtocolT::socket mSocket;
|
2014-01-25 20:29:45 +01:00
|
|
|
|
2014-01-25 23:50:00 +01:00
|
|
|
boost::asio::streambuf mReadBuffer;
|
2014-02-06 13:38:32 +01:00
|
|
|
boost::asio::streambuf* mWriteBuffer;
|
2014-01-25 23:50:00 +01:00
|
|
|
std::iostream mStream;
|
2014-02-03 22:00:46 +01:00
|
|
|
boost::asio::deadline_timer mNegotiateTimer;
|
2014-01-25 20:29:45 +01:00
|
|
|
|
2014-01-27 22:00:11 +01:00
|
|
|
enum
|
|
|
|
|
{
|
|
|
|
|
read_buffer_size = 16384
|
|
|
|
|
};
|
2014-01-25 20:29:45 +01:00
|
|
|
|
|
|
|
|
/// \brief Queue of operations that have been decoded by not dispatched.
|
|
|
|
|
DispatchQueue m_opQueue;
|
|
|
|
|
/// \brief Atlas codec that handles encoding and decoding traffic.
|
|
|
|
|
Atlas::Codec * m_codec;
|
|
|
|
|
/// \brief high level encoder passes data to the codec for transmission.
|
|
|
|
|
Atlas::Objects::ObjectsEncoder * m_encoder;
|
|
|
|
|
/// \brief Atlas negotiator for handling codec negotiation.
|
|
|
|
|
Atlas::Negotiate * m_negotiate;
|
|
|
|
|
/// \brief Server side object for handling connection level operations.
|
|
|
|
|
Link * m_link;
|
2014-01-27 22:00:11 +01:00
|
|
|
|
|
|
|
|
const std::string mName;
|
|
|
|
|
|
2014-02-01 22:17:03 +01:00
|
|
|
void do_read();
|
|
|
|
|
|
2014-02-06 13:38:32 +01:00
|
|
|
void write();
|
2014-02-01 22:17:03 +01:00
|
|
|
|
|
|
|
|
void dispatch();
|
|
|
|
|
|
2014-01-27 22:00:11 +01:00
|
|
|
void startNegotiation();
|
2014-01-25 20:29:45 +01:00
|
|
|
|
|
|
|
|
/// \brief Handle socket data related to codec negotiation.
|
|
|
|
|
int negotiate();
|
|
|
|
|
|
2014-01-25 23:50:00 +01:00
|
|
|
void negotiate_read();
|
|
|
|
|
|
|
|
|
|
void negotiate_write();
|
|
|
|
|
|
2014-01-25 20:29:45 +01:00
|
|
|
int operation(const Atlas::Objects::Operation::RootOperation &);
|
|
|
|
|
|
|
|
|
|
virtual void objectArrived(const Atlas::Objects::Root & obj);
|
|
|
|
|
};
|
|
|
|
|
|
2014-01-27 22:00:11 +01:00
|
|
|
#endif /* COMMASIOCLIENT_H_ */
|