i2pd/BOB.h

77 lines
1.7 KiB
C
Raw Normal View History

2014-12-02 16:34:02 +01:00
#ifndef BOB_H__
#define BOB_H__
2014-12-02 21:47:44 +01:00
#include <inttypes.h>
2014-12-02 17:42:35 +01:00
#include <thread>
#include <memory>
2014-12-02 21:47:44 +01:00
#include <map>
#include <string>
2014-12-02 17:42:35 +01:00
#include <boost/asio.hpp>
2014-12-02 21:47:44 +01:00
#include "I2PTunnel.h"
2014-12-02 17:42:35 +01:00
2014-12-02 16:34:02 +01:00
namespace i2p
{
namespace client
{
2014-12-02 21:47:44 +01:00
const size_t BOB_COMMAND_BUFFER_SIZE = 1024;
const char BOB_COMMAND_ZAP[] = "zap";
class BOBCommandChannel;
class BOBCommandSession: public std::enable_shared_from_this<BOBCommandSession>
2014-12-02 17:42:35 +01:00
{
public:
2014-12-02 21:47:44 +01:00
BOBCommandSession (BOBCommandChannel& owner);
~BOBCommandSession ();
boost::asio::ip::tcp::socket& GetSocket () { return m_Socket; };
void Receive ();
void HandleReceived (const boost::system::error_code& ecode, std::size_t bytes_transferred);
// command handlers
void ZapCommandHandler (const char * operand, size_t len);
2014-12-02 17:42:35 +01:00
private:
2014-12-02 21:47:44 +01:00
BOBCommandChannel& m_Owner;
boost::asio::ip::tcp::socket m_Socket;
char m_ReceiveBuffer[BOB_COMMAND_BUFFER_SIZE + 1];
size_t m_ReceiveBufferOffset;
bool m_IsOpen;
};
typedef void (BOBCommandSession::*BOBCommandHandler)(const char * operand, size_t len);
2014-12-02 16:34:02 +01:00
class BOBCommandChannel
{
public:
BOBCommandChannel (int port);
~BOBCommandChannel ();
2014-12-02 17:42:35 +01:00
void Start ();
void Stop ();
2014-12-02 21:47:44 +01:00
boost::asio::io_service& GetService () { return m_Service; };
std::map<std::string, BOBCommandHandler>& GetCommandHandlers () { return m_CommandHandlers; };
2014-12-02 17:42:35 +01:00
private:
void Run ();
void Accept ();
2014-12-02 21:47:44 +01:00
void HandleAccept(const boost::system::error_code& ecode, std::shared_ptr<BOBCommandSession> session);
2014-12-02 17:42:35 +01:00
private:
bool m_IsRunning;
std::thread * m_Thread;
boost::asio::io_service m_Service;
boost::asio::ip::tcp::acceptor m_Acceptor;
2014-12-02 21:47:44 +01:00
std::map<std::string, I2PTunnel *> m_Tunnels;
std::map<std::string, BOBCommandHandler> m_CommandHandlers;
2014-12-02 16:34:02 +01:00
};
}
}
#endif