|
|
|
@ -16,6 +16,12 @@ namespace client
@@ -16,6 +16,12 @@ namespace client
|
|
|
|
|
{ |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
void BOBCommandSession::Terminate () |
|
|
|
|
{ |
|
|
|
|
m_Socket.close (); |
|
|
|
|
m_IsOpen = false; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
void BOBCommandSession::Receive () |
|
|
|
|
{ |
|
|
|
|
m_Socket.async_read_some (boost::asio::buffer(m_ReceiveBuffer + m_ReceiveBufferOffset, BOB_COMMAND_BUFFER_SIZE - m_ReceiveBufferOffset), |
|
|
|
@ -26,7 +32,10 @@ namespace client
@@ -26,7 +32,10 @@ namespace client
|
|
|
|
|
void BOBCommandSession::HandleReceived (const boost::system::error_code& ecode, std::size_t bytes_transferred) |
|
|
|
|
{ |
|
|
|
|
if (ecode) |
|
|
|
|
{ |
|
|
|
|
LogPrint ("BOB command channel read error: ", ecode.message ()); |
|
|
|
|
Terminate (); |
|
|
|
|
} |
|
|
|
|
else |
|
|
|
|
{ |
|
|
|
|
size_t size = m_ReceiveBufferOffset + bytes_transferred; |
|
|
|
@ -55,18 +64,67 @@ namespace client
@@ -55,18 +64,67 @@ namespace client
|
|
|
|
|
else |
|
|
|
|
{ |
|
|
|
|
LogPrint (eLogError, "Malformed input of the BOB command channel"); |
|
|
|
|
return; |
|
|
|
|
Terminate (); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
void BOBCommandSession::Send (size_t len) |
|
|
|
|
{ |
|
|
|
|
boost::asio::async_write (m_Socket, boost::asio::buffer (m_SendBuffer, len), |
|
|
|
|
boost::asio::transfer_all (), |
|
|
|
|
std::bind(&BOBCommandSession::HandleSent, shared_from_this (), |
|
|
|
|
std::placeholders::_1, std::placeholders::_2)); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
void BOBCommandSession::HandleSent (const boost::system::error_code& ecode, std::size_t bytes_transferred) |
|
|
|
|
{ |
|
|
|
|
if (ecode) |
|
|
|
|
{ |
|
|
|
|
LogPrint ("BOB command channel send error: ", ecode.message ()); |
|
|
|
|
Terminate (); |
|
|
|
|
} |
|
|
|
|
else |
|
|
|
|
{ |
|
|
|
|
if (m_IsOpen) |
|
|
|
|
Receive (); |
|
|
|
|
else |
|
|
|
|
Terminate (); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
void BOBCommandSession::SendReplyOK (const char * msg) |
|
|
|
|
{ |
|
|
|
|
#ifdef _MSC_VER |
|
|
|
|
size_t len = sprintf_s (m_SendBuffer, BOB_COMMAND_BUFFER_SIZE, BOB_REPLY_OK, msg); |
|
|
|
|
#else |
|
|
|
|
size_t len = snprintf (m_SendBuffer, BOB_COMMAND_BUFFER_SIZE, BOB_REPLY_OK, msg); |
|
|
|
|
#endif |
|
|
|
|
Send (len); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
void BOBCommandSession::SendReplyError (const char * msg) |
|
|
|
|
{ |
|
|
|
|
#ifdef _MSC_VER |
|
|
|
|
size_t len = sprintf_s (m_SendBuffer, BOB_COMMAND_BUFFER_SIZE, BOB_REPLY_ERROR, msg); |
|
|
|
|
#else |
|
|
|
|
size_t len = snprintf (m_SendBuffer, BOB_COMMAND_BUFFER_SIZE, BOB_REPLY_ERROR, msg); |
|
|
|
|
#endif |
|
|
|
|
Send (len); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
void BOBCommandSession::ZapCommandHandler (const char * operand, size_t len) |
|
|
|
|
{ |
|
|
|
|
LogPrint (eLogDebug, "BOB: zap"); |
|
|
|
|
Terminate (); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
void BOBCommandSession::QuitCommandHandler (const char * operand, size_t len) |
|
|
|
|
{ |
|
|
|
|
LogPrint (eLogDebug, "BOB: quit"); |
|
|
|
|
m_IsOpen = false; |
|
|
|
|
SendReplyOK ("Bye!"); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
BOBCommandChannel::BOBCommandChannel (int port): |
|
|
|
@ -74,6 +132,7 @@ namespace client
@@ -74,6 +132,7 @@ namespace client
|
|
|
|
|
m_Acceptor (m_Service, boost::asio::ip::tcp::endpoint(boost::asio::ip::tcp::v4(), port)) |
|
|
|
|
{ |
|
|
|
|
m_CommandHandlers[BOB_COMMAND_ZAP] = &BOBCommandSession::ZapCommandHandler; |
|
|
|
|
m_CommandHandlers[BOB_COMMAND_QUIT] = &BOBCommandSession::QuitCommandHandler; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
BOBCommandChannel::~BOBCommandChannel () |
|
|
|
|