Browse Source

more of SAM debug logging

pull/892/head
hypnosis-i2p 8 years ago
parent
commit
8e266058ae
  1. 43
      libi2pd_client/SAM.cpp
  2. 3
      libi2pd_client/SAM.h

43
libi2pd_client/SAM.cpp

@ -24,11 +24,12 @@ namespace client
SAMSocket::~SAMSocket () SAMSocket::~SAMSocket ()
{ {
Terminate (); Terminate ("~SAMSocket()");
} }
void SAMSocket::CloseStream () void SAMSocket::CloseStream (const char* reason)
{ {
LogPrint (eLogDebug, "SAMSocket::CloseStream, reason: ", reason);
if (m_Stream) if (m_Stream)
{ {
m_Stream->Close (); m_Stream->Close ();
@ -36,9 +37,9 @@ namespace client
} }
} }
void SAMSocket::Terminate () void SAMSocket::Terminate (const char* reason)
{ {
CloseStream (); CloseStream (reason);
switch (m_SocketType) switch (m_SocketType)
{ {
@ -82,7 +83,7 @@ namespace client
{ {
LogPrint (eLogError, "SAM: handshake read error: ", ecode.message ()); LogPrint (eLogError, "SAM: handshake read error: ", ecode.message ());
if (ecode != boost::asio::error::operation_aborted) if (ecode != boost::asio::error::operation_aborted)
Terminate (); Terminate ("SAM: handshake read error");
} }
else else
{ {
@ -130,7 +131,7 @@ namespace client
else else
{ {
LogPrint (eLogError, "SAM: handshake mismatch"); LogPrint (eLogError, "SAM: handshake mismatch");
Terminate (); Terminate ("SAM: handshake mismatch");
} }
} }
} }
@ -141,7 +142,7 @@ namespace client
{ {
LogPrint (eLogError, "SAM: handshake reply send error: ", ecode.message ()); LogPrint (eLogError, "SAM: handshake reply send error: ", ecode.message ());
if (ecode != boost::asio::error::operation_aborted) if (ecode != boost::asio::error::operation_aborted)
Terminate (); Terminate ("SAM: handshake reply send error");
} }
else else
{ {
@ -153,6 +154,8 @@ namespace client
void SAMSocket::SendMessageReply (const char * msg, size_t len, bool close) void SAMSocket::SendMessageReply (const char * msg, size_t len, bool close)
{ {
LogPrint (eLogDebug, "SAMSocket::SendMessageReply, close=",close?"true":"false", " reason: ", msg);
if (!m_IsSilent) if (!m_IsSilent)
boost::asio::async_write (m_Socket, boost::asio::buffer (msg, len), boost::asio::transfer_all (), boost::asio::async_write (m_Socket, boost::asio::buffer (msg, len), boost::asio::transfer_all (),
std::bind(&SAMSocket::HandleMessageReplySent, shared_from_this (), std::bind(&SAMSocket::HandleMessageReplySent, shared_from_this (),
@ -160,7 +163,7 @@ namespace client
else else
{ {
if (close) if (close)
Terminate (); Terminate ("SAMSocket::SendMessageReply(close=true)");
else else
Receive (); Receive ();
} }
@ -172,12 +175,12 @@ namespace client
{ {
LogPrint (eLogError, "SAM: reply send error: ", ecode.message ()); LogPrint (eLogError, "SAM: reply send error: ", ecode.message ());
if (ecode != boost::asio::error::operation_aborted) if (ecode != boost::asio::error::operation_aborted)
Terminate (); Terminate ("SAM: reply send error");
} }
else else
{ {
if (close) if (close)
Terminate (); Terminate ("SAMSocket::HandleMessageReplySent(close=true)");
else else
Receive (); Receive ();
} }
@ -189,7 +192,7 @@ namespace client
{ {
LogPrint (eLogError, "SAM: read error: ", ecode.message ()); LogPrint (eLogError, "SAM: read error: ", ecode.message ());
if (ecode != boost::asio::error::operation_aborted) if (ecode != boost::asio::error::operation_aborted)
Terminate (); Terminate ("SAM: read error");
} }
else if (m_SocketType == eSAMSocketTypeStream) else if (m_SocketType == eSAMSocketTypeStream)
HandleReceived (ecode, bytes_transferred); HandleReceived (ecode, bytes_transferred);
@ -243,13 +246,13 @@ namespace client
else else
{ {
LogPrint (eLogError, "SAM: unexpected message ", m_Buffer); LogPrint (eLogError, "SAM: unexpected message ", m_Buffer);
Terminate (); Terminate ("SAM: unexpected message");
} }
} }
else else
{ {
LogPrint (eLogError, "SAM: malformed message ", m_Buffer); LogPrint (eLogError, "SAM: malformed message ", m_Buffer);
Terminate (); Terminate ("malformed message");
} }
} }
@ -603,7 +606,7 @@ namespace client
if (m_BufferOffset >= SAM_SOCKET_BUFFER_SIZE) if (m_BufferOffset >= SAM_SOCKET_BUFFER_SIZE)
{ {
LogPrint (eLogError, "SAM: Buffer is full, terminate"); LogPrint (eLogError, "SAM: Buffer is full, terminate");
Terminate (); Terminate ("Buffer is full");
return; return;
} }
m_Socket.async_read_some (boost::asio::buffer(m_Buffer + m_BufferOffset, SAM_SOCKET_BUFFER_SIZE - m_BufferOffset), m_Socket.async_read_some (boost::asio::buffer(m_Buffer + m_BufferOffset, SAM_SOCKET_BUFFER_SIZE - m_BufferOffset),
@ -617,7 +620,7 @@ namespace client
{ {
LogPrint (eLogError, "SAM: read error: ", ecode.message ()); LogPrint (eLogError, "SAM: read error: ", ecode.message ());
if (ecode != boost::asio::error::operation_aborted) if (ecode != boost::asio::error::operation_aborted)
Terminate (); Terminate ("read error");
} }
else else
{ {
@ -632,7 +635,7 @@ namespace client
if (!ecode) if (!ecode)
s->Receive (); s->Receive ();
else else
s->m_Owner.GetService ().post ([s] { s->Terminate (); }); s->m_Owner.GetService ().post ([s] { s->Terminate ("AsyncSend failed"); });
}); });
} }
} }
@ -660,7 +663,7 @@ namespace client
std::bind (&SAMSocket::HandleWriteI2PData, shared_from_this (), std::placeholders::_1)); std::bind (&SAMSocket::HandleWriteI2PData, shared_from_this (), std::placeholders::_1));
} }
else // no more data else // no more data
Terminate (); Terminate ("no more data");
} }
} }
} }
@ -678,13 +681,13 @@ namespace client
else else
{ {
auto s = shared_from_this (); auto s = shared_from_this ();
m_Owner.GetService ().post ([s] { s->Terminate (); }); m_Owner.GetService ().post ([s] { s->Terminate ("stream read error"); });
} }
} }
else else
{ {
auto s = shared_from_this (); auto s = shared_from_this ();
m_Owner.GetService ().post ([s] { s->Terminate (); }); m_Owner.GetService ().post ([s] { s->Terminate ("stream read error (op aborted)"); });
} }
} }
else else
@ -700,7 +703,7 @@ namespace client
{ {
LogPrint (eLogError, "SAM: socket write error: ", ecode.message ()); LogPrint (eLogError, "SAM: socket write error: ", ecode.message ());
if (ecode != boost::asio::error::operation_aborted) if (ecode != boost::asio::error::operation_aborted)
Terminate (); Terminate ("socket write error at HandleWriteI2PData");
} }
else else
I2PReceive (); I2PReceive ();

3
libi2pd_client/SAM.h

@ -80,7 +80,7 @@ namespace client
SAMSocket (SAMBridge& owner); SAMSocket (SAMBridge& owner);
~SAMSocket (); ~SAMSocket ();
void CloseStream (); // TODO: implement it better void CloseStream (const char* reason); // TODO: implement it better
boost::asio::ip::tcp::socket& GetSocket () { return m_Socket; }; boost::asio::ip::tcp::socket& GetSocket () { return m_Socket; };
void ReceiveHandshake (); void ReceiveHandshake ();
@ -90,6 +90,7 @@ namespace client
void Terminate (); void Terminate ();
private: private:
void Terminate (const char* reason);
void HandleHandshakeReceived (const boost::system::error_code& ecode, std::size_t bytes_transferred); void HandleHandshakeReceived (const boost::system::error_code& ecode, std::size_t bytes_transferred);
void HandleHandshakeReplySent (const boost::system::error_code& ecode, std::size_t bytes_transferred); void HandleHandshakeReplySent (const boost::system::error_code& ecode, std::size_t bytes_transferred);
void HandleMessage (const boost::system::error_code& ecode, std::size_t bytes_transferred); void HandleMessage (const boost::system::error_code& ecode, std::size_t bytes_transferred);

Loading…
Cancel
Save