Skip to content

Commit

Permalink
command channel acceptor
Browse files Browse the repository at this point in the history
  • Loading branch information
orignal committed Dec 2, 2014
1 parent e7f05cc commit 3ab65bb
Show file tree
Hide file tree
Showing 2 changed files with 101 additions and 1 deletion.
67 changes: 66 additions & 1 deletion BOB.cpp
Original file line number Diff line number Diff line change
@@ -1,15 +1,80 @@
#include "Log.h"
#include "BOB.h"

namespace i2p
{
namespace client
{
BOBCommandChannel::BOBCommandChannel (int port)
BOBDataStream::BOBDataStream (std::shared_ptr<boost::asio::ip::tcp::socket> socket,
std::shared_ptr<i2p::stream::Stream> stream): m_Socket (socket), m_Stream (stream)
{
}

BOBCommandChannel::BOBCommandChannel (int port):
m_IsRunning (false), m_Thread (nullptr),
m_Acceptor (m_Service, boost::asio::ip::tcp::endpoint(boost::asio::ip::tcp::v4(), port))
{
}

BOBCommandChannel::~BOBCommandChannel ()
{
Stop ();
}

void BOBCommandChannel::Start ()
{
Accept ();
m_IsRunning = true;
m_Thread = new std::thread (std::bind (&BOBCommandChannel::Run, this));
}

void BOBCommandChannel::Stop ()
{
m_DataStreams.clear ();
m_IsRunning = false;
m_Service.stop ();
if (m_Thread)
{
m_Thread->join ();
delete m_Thread;
m_Thread = nullptr;
}
}

void BOBCommandChannel::Run ()
{
while (m_IsRunning)
{
try
{
m_Service.run ();
}
catch (std::exception& ex)
{
LogPrint (eLogError, "BOB: ", ex.what ());
}
}
}

void BOBCommandChannel::Accept ()
{
auto newSocket = std::make_shared<boost::asio::ip::tcp::socket> (m_Service);
m_Acceptor.async_accept (*newSocket, std::bind (&BOBCommandChannel::HandleAccept, this,
std::placeholders::_1, newSocket));
}

void BOBCommandChannel::HandleAccept(const boost::system::error_code& ecode, std::shared_ptr<boost::asio::ip::tcp::socket> socket)
{
if (ecode != boost::asio::error::operation_aborted)
Accept ();

if (!ecode)
{
LogPrint (eLogInfo, "New BOB command connection from ", socket->remote_endpoint ());
// TODO:
}
else
LogPrint (eLogError, "BOB accept error: ", ecode.message ());
}
}
}
Expand Down
35 changes: 35 additions & 0 deletions BOB.h
Original file line number Diff line number Diff line change
@@ -1,17 +1,52 @@
#ifndef BOB_H__
#define BOB_H__

#include <thread>
#include <memory>
#include <list>
#include <boost/asio.hpp>
#include "Streaming.h"

namespace i2p
{
namespace client
{
class BOBDataStream: public std::enable_shared_from_this<BOBDataStream>
{
public:

BOBDataStream (std::shared_ptr<boost::asio::ip::tcp::socket> socket,
std::shared_ptr<i2p::stream::Stream> stream);

private:

std::shared_ptr<boost::asio::ip::tcp::socket> m_Socket;
std::shared_ptr<i2p::stream::Stream> m_Stream;
};

class BOBCommandChannel
{
public:

BOBCommandChannel (int port);
~BOBCommandChannel ();

void Start ();
void Stop ();

private:

void Run ();
void Accept ();
void HandleAccept(const boost::system::error_code& ecode, std::shared_ptr<boost::asio::ip::tcp::socket> socket);

private:

bool m_IsRunning;
std::thread * m_Thread;
boost::asio::io_service m_Service;
boost::asio::ip::tcp::acceptor m_Acceptor;
std::list<std::shared_ptr<BOBDataStream> > m_DataStreams;
};
}
}
Expand Down

0 comments on commit 3ab65bb

Please sign in to comment.