engine/include/Utils/Network/Server.h

136 lines
2.8 KiB
C
Raw Permalink Normal View History

2013-02-26 13:57:00 +00:00
#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
{
2013-05-27 13:24:27 +00:00
2013-02-26 13:57:00 +00:00
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;
2013-05-27 13:24:27 +00:00
static std::string GenerateRandomPassword();
2013-02-26 13:57:00 +00:00
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();
2013-05-27 13:24:27 +00:00
void HandleGetRegisterRequest(boost::property_tree::ptree pTree);
void HandleGetLoginRequest(boost::property_tree::ptree pTree);
void CreateNewUser();
2013-02-26 13:57:00 +00:00
boost::signal<void()> ErrorSignal;
boost::signal<void(std::string)> AllowedSignal;
2013-05-27 13:24:27 +00:00
//boost::signal<void()> DeniedSignal; //Simple autorizator does not deny
2013-02-26 13:57:00 +00:00
};
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);
2013-02-27 14:03:45 +00:00
void DisconnectSlots();
2013-02-26 13:57:00 +00:00
};
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);
2013-02-27 14:03:45 +00:00
void DeleteUser(boost::shared_ptr<TConnectedUser> user);
2013-02-26 13:57:00 +00:00
//Need to generalize this
boost::signal<void(boost::shared_ptr<TConnectedUser>)> OnUserAuthorizedSignal;
2013-02-27 14:03:45 +00:00
boost::signal<void(boost::shared_ptr<TConnectedUser>)> OnUserDisconnectedSignal;
2013-02-26 13:57:00 +00:00
};
} //namespace SE
#endif