From 60463fdafa97ba68f41430c78bb946ff41bbc1d7 Mon Sep 17 00:00:00 2001
From: Jeff Becker <jeff@i2p.rocks>
Date: Tue, 24 Apr 2018 11:11:48 -0400
Subject: [PATCH] shut down socket and don't allocate buffer for each write in
 WriteI2PData

---
 libi2pd_client/SAM.cpp | 21 +++++++++++++--------
 1 file changed, 13 insertions(+), 8 deletions(-)

diff --git a/libi2pd_client/SAM.cpp b/libi2pd_client/SAM.cpp
index 71b5bea1..b0ffda51 100644
--- a/libi2pd_client/SAM.cpp
+++ b/libi2pd_client/SAM.cpp
@@ -25,8 +25,7 @@ namespace client
 
 	SAMSocket::~SAMSocket ()
 	{
-		m_Stream.reset ();	
-		if (m_Socket.is_open()) m_Socket.close ();
+		m_Stream.reset ();
 	}	
 
 	void SAMSocket::Terminate (const char* reason)
@@ -59,7 +58,11 @@ namespace client
 				;
 		}
 		m_SocketType = eSAMSocketTypeTerminated;
-		if (m_Socket.is_open()) m_Socket.close ();
+		if (m_Socket.is_open ())
+		{
+			m_Socket.shutdown ();
+			m_Socket.close ();
+		}
 		m_Owner.RemoveSocket(this);
 	}
 
@@ -742,9 +745,11 @@ namespace client
 	
 	void SAMSocket::WriteI2PData(size_t sz)
 	{
-		uint8_t * sendbuff = new uint8_t[sz];
-		memcpy(sendbuff, m_StreamBuffer, sz);
-		WriteI2PDataImmediate(sendbuff, sz);
+		boost::asio::async_write (
+			m_Socket,
+			boost::asio::buffer (m_StreamBuffer, sz),
+			boost::asio::transfer_all(),
+			std::bind(&SAMSocket::HandleWriteI2PData, shared_from_this(), std::placeholders::_1));
 	}
 	
 	void SAMSocket::HandleI2PReceive (const boost::system::error_code& ecode, std::size_t bytes_transferred)
@@ -778,7 +783,8 @@ namespace client
 				{
 					WriteI2PData(bytes_transferred);
 				}
-				I2PReceive();
+				else
+					I2PReceive();
 			}
 		}
 	}
@@ -897,7 +903,6 @@ namespace client
 	
 	SAMSession::~SAMSession ()
 	{
-		CloseStreams();
 		i2p::client::context.DeleteLocalDestination (localDestination);
 	}