2013-02-16 21:05:10 +00:00
|
|
|
#ifndef NETWORK_H_INCLUDED
|
|
|
|
#define NETWORK_H_INCLUDED
|
|
|
|
|
|
|
|
#include <string>
|
|
|
|
#include <map>
|
|
|
|
#include <vector>
|
|
|
|
#include "boost/shared_array.hpp"
|
|
|
|
#include "boost/property_tree/ptree.hpp"
|
|
|
|
#include "boost/foreach.hpp"
|
|
|
|
|
|
|
|
#include "boost/asio.hpp"
|
|
|
|
#include "boost/date_time/posix_time/posix_time.hpp"
|
2017-02-26 23:26:42 +00:00
|
|
|
#include "boost/signals2.hpp"
|
2013-02-16 21:05:10 +00:00
|
|
|
#include "boost/variant.hpp"
|
|
|
|
|
|
|
|
#include "include/Utils/Network/SignalSender.h"
|
|
|
|
|
|
|
|
namespace SE
|
|
|
|
{
|
|
|
|
|
|
|
|
const int CONST_CONNECTION_TIMEOUT_SECONDS = 300;
|
|
|
|
|
|
|
|
|
|
|
|
void SendPropertyTree(boost::asio::io_service& ioService, boost::asio::ip::tcp::socket& socket, boost::property_tree::ptree pTree);
|
|
|
|
|
|
|
|
|
2013-02-26 13:57:00 +00:00
|
|
|
class TDataReadSignalMap
|
|
|
|
{
|
|
|
|
protected:
|
2017-02-26 23:26:42 +00:00
|
|
|
std::map<std::string, std::shared_ptr<boost::signals2::signal<void(boost::property_tree::ptree)>>> SignalMap;
|
2013-02-26 13:57:00 +00:00
|
|
|
public:
|
|
|
|
|
|
|
|
void AddSlot(const std::string& nodeName, boost::function<void(boost::property_tree::ptree)> f);
|
|
|
|
|
|
|
|
bool SignalExists(const std::string& signalName);
|
|
|
|
|
|
|
|
void EmitSignal(const std::string& signalName, const boost::property_tree::ptree& pt);
|
|
|
|
|
|
|
|
void Clear();
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2013-02-16 21:05:10 +00:00
|
|
|
//Must be stored in shared_ptr only
|
2013-02-26 13:57:00 +00:00
|
|
|
class TDataReader : public boost::enable_shared_from_this<TDataReader>
|
2013-02-16 21:05:10 +00:00
|
|
|
{
|
2013-02-26 13:57:00 +00:00
|
|
|
protected:
|
|
|
|
bool Nonstop;
|
|
|
|
|
|
|
|
void InnerStartRead();
|
|
|
|
public:
|
|
|
|
|
2013-02-16 21:05:10 +00:00
|
|
|
boost::asio::ip::tcp::socket& Socket;
|
|
|
|
|
|
|
|
int DataSize;
|
|
|
|
|
|
|
|
std::vector<char> Data;
|
|
|
|
|
2013-02-26 13:57:00 +00:00
|
|
|
TDataReadSignalMap DataReadSignalMap;
|
|
|
|
|
2013-02-16 21:05:10 +00:00
|
|
|
TDataReader(boost::asio::ip::tcp::socket& socket);
|
|
|
|
|
2013-02-26 13:57:00 +00:00
|
|
|
void StartReadOnce();
|
|
|
|
|
|
|
|
void StartReadNonstop();
|
2013-02-16 21:05:10 +00:00
|
|
|
|
|
|
|
void HandleReadDataSize(const boost::system::error_code& error);
|
|
|
|
|
|
|
|
void HandleReadData(const boost::system::error_code& error);
|
2013-02-26 13:57:00 +00:00
|
|
|
|
2017-02-26 23:26:42 +00:00
|
|
|
boost::signals2::signal<void()> ErrorSignal;
|
2013-02-16 21:05:10 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class TAuthorizationInterface
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
virtual void Authorize() = 0;
|
2013-02-26 13:57:00 +00:00
|
|
|
|
|
|
|
virtual ~TAuthorizationInterface() { }
|
2013-02-16 21:05:10 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
class TSimpleAuthorization : public TAuthorizationInterface
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
|
|
|
|
boost::asio::io_service& IoService;
|
|
|
|
|
|
|
|
boost::asio::ip::tcp::socket& Socket;
|
|
|
|
|
|
|
|
std::string Login;
|
|
|
|
std::string Password;
|
|
|
|
|
|
|
|
|
|
|
|
TSimpleAuthorization(boost::asio::io_service& ioService, boost::asio::ip::tcp::socket& socket);
|
|
|
|
|
|
|
|
virtual void Authorize();
|
|
|
|
|
|
|
|
void HandleGetData(boost::property_tree::ptree pTree);
|
|
|
|
|
2017-02-26 23:26:42 +00:00
|
|
|
boost::signals2::signal<void()> AuthorizedSignal;
|
|
|
|
boost::signals2::signal<void(std::string, std::string)> SaveLoginPasswordSignal;
|
|
|
|
boost::signals2::signal<void()> ErrorSignal;
|
2013-02-16 21:05:10 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
typedef boost::variant<std::shared_ptr<TSimpleAuthorization>> TAuthorizationVariant;
|
|
|
|
|
2013-02-19 12:37:56 +00:00
|
|
|
|
2013-02-16 21:05:10 +00:00
|
|
|
class TClientSocket : public boost::enable_shared_from_this<TClientSocket>
|
|
|
|
{
|
|
|
|
protected:
|
|
|
|
|
|
|
|
boost::asio::io_service IoService; //IoService must be declared before Socket
|
|
|
|
|
|
|
|
boost::thread IoServiceThread;
|
|
|
|
|
|
|
|
boost::asio::ip::tcp::socket Socket;
|
|
|
|
|
|
|
|
int ReadDataLenLong;
|
|
|
|
|
|
|
|
std::vector<char> ReadData;
|
|
|
|
|
|
|
|
boost::shared_ptr<boost::asio::deadline_timer> ConnectionTimeoutTimer;
|
|
|
|
|
|
|
|
bool Opened;
|
|
|
|
|
|
|
|
public:
|
2017-02-26 23:26:42 +00:00
|
|
|
boost::signals2::signal<void()> OnAddressNotResolvedSignal;
|
|
|
|
boost::signals2::signal<void()> OnConnectedSignal;
|
|
|
|
boost::signals2::signal<void()> OnAutorizedSignal;
|
|
|
|
boost::signals2::signal<void()> OnDisconnectedSignal;
|
2013-02-26 13:57:00 +00:00
|
|
|
|
|
|
|
boost::shared_ptr<TDataReader> ClientDataReader;
|
2013-02-16 21:05:10 +00:00
|
|
|
|
|
|
|
TAuthorizationVariant Authorization;
|
|
|
|
|
|
|
|
TClientSocket();
|
|
|
|
~TClientSocket();
|
|
|
|
|
|
|
|
void IoServiceRun();
|
|
|
|
|
|
|
|
void Open(const std::string address, const std::string& port);
|
|
|
|
|
|
|
|
void Close();
|
|
|
|
|
|
|
|
void HandleConnectTimeout(const boost::system::error_code& error);
|
|
|
|
void HandleConnect(const boost::system::error_code& error);
|
|
|
|
|
|
|
|
void HandleAuthorized();
|
|
|
|
void HandleAuthorizationError();
|
|
|
|
|
2013-02-26 13:57:00 +00:00
|
|
|
void SendPropertyTree(boost::property_tree::ptree pTree);
|
2013-02-16 21:05:10 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
} //namespace SE
|
|
|
|
|
|
|
|
|
2017-02-26 23:26:42 +00:00
|
|
|
#endif
|