Added server + network stuff
This commit is contained in:
1 parent
01b58124ec
commit
5d13869324
14 files changed
+417
-40
No files matched your search
@@ -18,7 +18,7 @@ namespace SE
|
||||
|
||||
typedef unsigned int cardinal;
|
||||
typedef unsigned short int word;
|
||||
typedef unsigned char byte;
|
||||
//typedef unsigned char byte;
|
||||
|
||||
typedef cardinal* pcardinal;
|
||||
|
||||
|
||||
@@ -25,24 +25,50 @@ 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);
|
||||
|
||||
|
||||
//Must be stored in shared_ptr only
|
||||
struct TDataReader : public boost::enable_shared_from_this<TDataReader>
|
||||
class TDataReadSignalMap
|
||||
{
|
||||
protected:
|
||||
std::map<std::string, std::shared_ptr<boost::signal<void(boost::property_tree::ptree)>>> SignalMap;
|
||||
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();
|
||||
|
||||
};
|
||||
|
||||
|
||||
//Must be stored in shared_ptr only
|
||||
class TDataReader : public boost::enable_shared_from_this<TDataReader>
|
||||
{
|
||||
protected:
|
||||
bool Nonstop;
|
||||
|
||||
void InnerStartRead();
|
||||
public:
|
||||
|
||||
boost::asio::ip::tcp::socket& Socket;
|
||||
|
||||
int DataSize;
|
||||
|
||||
std::vector<char> Data;
|
||||
|
||||
TDataReadSignalMap DataReadSignalMap;
|
||||
|
||||
TDataReader(boost::asio::ip::tcp::socket& socket);
|
||||
|
||||
void StartRead();
|
||||
void StartReadOnce();
|
||||
|
||||
void StartReadNonstop();
|
||||
|
||||
void HandleReadDataSize(const boost::system::error_code& error);
|
||||
|
||||
void HandleReadData(const boost::system::error_code& error);
|
||||
|
||||
boost::signal<void(boost::property_tree::ptree)> DataReadSignal;
|
||||
|
||||
boost::signal<void()> ErrorSignal;
|
||||
};
|
||||
|
||||
@@ -52,6 +78,8 @@ class TAuthorizationInterface
|
||||
{
|
||||
public:
|
||||
virtual void Authorize() = 0;
|
||||
|
||||
virtual ~TAuthorizationInterface() { }
|
||||
};
|
||||
|
||||
class TSimpleAuthorization : public TAuthorizationInterface
|
||||
@@ -62,8 +90,6 @@ public:
|
||||
|
||||
boost::asio::ip::tcp::socket& Socket;
|
||||
|
||||
//std::shared_ptr<TDataReader> DataReader;
|
||||
|
||||
std::string Login;
|
||||
std::string Password;
|
||||
|
||||
@@ -105,6 +131,8 @@ public:
|
||||
boost::signal<void()> OnConnectedSignal;
|
||||
boost::signal<void()> OnAutorizedSignal;
|
||||
boost::signal<void()> OnDisconnectedSignal;
|
||||
|
||||
boost::shared_ptr<TDataReader> ClientDataReader;
|
||||
|
||||
TAuthorizationVariant Authorization;
|
||||
|
||||
@@ -122,16 +150,11 @@ public:
|
||||
|
||||
void HandleAuthorized();
|
||||
void HandleAuthorizationError();
|
||||
};
|
||||
|
||||
class TServerSocket
|
||||
{
|
||||
protected:
|
||||
public:
|
||||
void SendPropertyTree(boost::property_tree::ptree pTree);
|
||||
};
|
||||
|
||||
|
||||
|
||||
} //namespace SE
|
||||
|
||||
|
||||
|
||||
@@ -0,0 +1,127 @@
|
||||
#ifndef SERVER_H_INCLUDED
|
||||
#define SERVER_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"
|
||||
#include "boost/signal.hpp"
|
||||
#include "boost/variant.hpp"
|
||||
|
||||
#include "include/Utils/Network/SignalSender.h"
|
||||
|
||||
|
||||
namespace SE
|
||||
{
|
||||
|
||||
class TAutorizatorInterface
|
||||
{
|
||||
protected:
|
||||
public:
|
||||
virtual void StartListen() = 0;
|
||||
|
||||
virtual ~TAutorizatorInterface() { }
|
||||
|
||||
};
|
||||
|
||||
class TSimpleAutorizator : public TAutorizatorInterface
|
||||
{
|
||||
protected:
|
||||
|
||||
|
||||
static std::vector<std::string> UserPasswords;
|
||||
static boost::mutex UserPasswordsMutex;
|
||||
|
||||
public:
|
||||
|
||||
boost::asio::io_service& IoService;
|
||||
|
||||
boost::asio::ip::tcp::socket& Socket;
|
||||
|
||||
TSimpleAutorizator(boost::asio::io_service& ioService, boost::asio::ip::tcp::socket& socket);
|
||||
|
||||
virtual void StartListen();
|
||||
|
||||
void HandleGetData(boost::property_tree::ptree pTree);
|
||||
|
||||
boost::signal<void()> ErrorSignal;
|
||||
boost::signal<void(std::string)> AllowedSignal;
|
||||
boost::signal<void()> DeniedSignal;
|
||||
};
|
||||
|
||||
typedef boost::variant<std::shared_ptr<TSimpleAutorizator>> TAuthorizatorVariant;
|
||||
|
||||
class TServerSocket;
|
||||
|
||||
class TConnectedUser : public boost::enable_shared_from_this<TConnectedUser>
|
||||
{
|
||||
protected:
|
||||
|
||||
TServerSocket& Server;
|
||||
std::string Login; //Need to generalize this
|
||||
|
||||
public:
|
||||
boost::asio::ip::tcp::socket Socket;
|
||||
|
||||
TAuthorizatorVariant Autorizator;
|
||||
|
||||
boost::shared_ptr<TDataReader> UserDataReader;
|
||||
|
||||
TConnectedUser(TServerSocket& server);
|
||||
|
||||
~TConnectedUser();
|
||||
|
||||
std::string GetLogin() { return Login; }
|
||||
|
||||
void HandleAllowed(std::string login); //Need to generalize this
|
||||
|
||||
void SendPropertyTree(boost::property_tree::ptree pTree);
|
||||
|
||||
};
|
||||
|
||||
|
||||
class TServerSocket
|
||||
{
|
||||
protected:
|
||||
std::vector<boost::shared_ptr<TConnectedUser>> UserArr;
|
||||
|
||||
boost::thread ServiceThread;
|
||||
|
||||
std::shared_ptr<boost::asio::ip::tcp::acceptor> Acceptor;
|
||||
|
||||
|
||||
public:
|
||||
|
||||
boost::asio::io_service IoService;
|
||||
|
||||
|
||||
TServerSocket();
|
||||
~TServerSocket();
|
||||
|
||||
void Open(int port);
|
||||
void Close();
|
||||
|
||||
void UpdateInThread();
|
||||
void JoinServiceThread();
|
||||
|
||||
void StartAccept();
|
||||
void HandleAccept(boost::shared_ptr<TConnectedUser> user, const boost::system::error_code& error);
|
||||
|
||||
//Need to generalize this
|
||||
boost::signal<void(boost::shared_ptr<TConnectedUser>)> OnUserAuthorizedSignal;
|
||||
//boost::signal<void(std::string)> OnUserDisconnectedSignal;
|
||||
|
||||
};
|
||||
|
||||
|
||||
|
||||
|
||||
} //namespace SE
|
||||
|
||||
|
||||
#endif
|
||||
@@ -27,6 +27,7 @@ This code combines additional routines (such as console/log, exceptions, math ut
|
||||
#include "include/Utils/SimpleTimer.h"
|
||||
#include "include/Utils/ThreadUtils.h"
|
||||
#include "include/Utils/Network/Network.h"
|
||||
#include "include/Utils/Network/Server.h"
|
||||
|
||||
#ifdef TARGET_WIN32
|
||||
#include "WinApi/WinApi.h"
|
||||
|
||||
Reference in new issue
Block a user