Applied proper disconnection sequence to V1 of the client.

This commit is contained in:
raufismayilov 2024-05-31 20:31:48 +04:00
parent 8870fe9acd
commit c49d365699

View file

@ -44,6 +44,7 @@ namespace SMBLibrary.Client
private uint m_serverMaxBufferSize; private uint m_serverMaxBufferSize;
private ushort m_maxMpxCount; private ushort m_maxMpxCount;
private int m_responseTimeoutInMilliseconds; private int m_responseTimeoutInMilliseconds;
private EventWaitHandle m_disconnectedEventHandle = new EventWaitHandle(false, EventResetMode.ManualReset);
private object m_incomingQueueLock = new object(); private object m_incomingQueueLock = new object();
private List<SMB1Message> m_incomingQueue = new List<SMB1Message>(); private List<SMB1Message> m_incomingQueue = new List<SMB1Message>();
@ -110,7 +111,7 @@ namespace SMBLibrary.Client
SessionPacket sessionResponsePacket = WaitForSessionResponsePacket(); SessionPacket sessionResponsePacket = WaitForSessionResponsePacket();
if (!(sessionResponsePacket is PositiveSessionResponsePacket)) if (!(sessionResponsePacket is PositiveSessionResponsePacket))
{ {
m_clientSocket.Disconnect(false); DisconnectSocket();
if (!ConnectSocket(serverAddress, port)) if (!ConnectSocket(serverAddress, port))
{ {
return false; return false;
@ -137,6 +138,7 @@ namespace SMBLibrary.Client
bool supportsDialect = NegotiateDialect(m_forceExtendedSecurity); bool supportsDialect = NegotiateDialect(m_forceExtendedSecurity);
if (!supportsDialect) if (!supportsDialect)
{ {
DisconnectSocket();
m_clientSocket.Close(); m_clientSocket.Close();
} }
else else
@ -149,6 +151,7 @@ namespace SMBLibrary.Client
private bool ConnectSocket(IPAddress serverAddress, int port) private bool ConnectSocket(IPAddress serverAddress, int port)
{ {
m_disconnectedEventHandle.Reset();
m_clientSocket = new Socket(serverAddress.AddressFamily, SocketType.Stream, ProtocolType.Tcp); m_clientSocket = new Socket(serverAddress.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
try try
@ -166,11 +169,18 @@ namespace SMBLibrary.Client
return true; return true;
} }
private void DisconnectSocket()
{
m_clientSocket.Shutdown(SocketShutdown.Send);
m_disconnectedEventHandle.WaitOne();
m_clientSocket.Shutdown(SocketShutdown.Receive);
}
public void Disconnect() public void Disconnect()
{ {
if (m_isConnected) if (m_isConnected)
{ {
m_clientSocket.Disconnect(false); DisconnectSocket();
m_clientSocket.Close(); m_clientSocket.Close();
m_connectionState.ReceiveBuffer.Dispose(); m_connectionState.ReceiveBuffer.Dispose();
m_isConnected = false; m_isConnected = false;
@ -424,12 +434,6 @@ namespace SMBLibrary.Client
ConnectionState state = (ConnectionState)ar.AsyncState; ConnectionState state = (ConnectionState)ar.AsyncState;
Socket clientSocket = state.ClientSocket; Socket clientSocket = state.ClientSocket;
if (!clientSocket.Connected)
{
state.ReceiveBuffer.Dispose();
return;
}
int numberOfBytesReceived = 0; int numberOfBytesReceived = 0;
try try
{ {
@ -438,18 +442,21 @@ namespace SMBLibrary.Client
catch (ArgumentException) // The IAsyncResult object was not returned from the corresponding synchronous method on this class. catch (ArgumentException) // The IAsyncResult object was not returned from the corresponding synchronous method on this class.
{ {
state.ReceiveBuffer.Dispose(); state.ReceiveBuffer.Dispose();
m_disconnectedEventHandle.Set();
return; return;
} }
catch (ObjectDisposedException) catch (ObjectDisposedException)
{ {
Log("[ReceiveCallback] EndReceive ObjectDisposedException"); Log("[ReceiveCallback] EndReceive ObjectDisposedException");
state.ReceiveBuffer.Dispose(); state.ReceiveBuffer.Dispose();
m_disconnectedEventHandle.Set();
return; return;
} }
catch (SocketException ex) catch (SocketException ex)
{ {
Log("[ReceiveCallback] EndReceive SocketException: " + ex.Message); Log("[ReceiveCallback] EndReceive SocketException: " + ex.Message);
state.ReceiveBuffer.Dispose(); state.ReceiveBuffer.Dispose();
m_disconnectedEventHandle.Set();
return; return;
} }
@ -457,6 +464,7 @@ namespace SMBLibrary.Client
{ {
m_isConnected = false; m_isConnected = false;
state.ReceiveBuffer.Dispose(); state.ReceiveBuffer.Dispose();
m_disconnectedEventHandle.Set();
} }
else else
{ {
@ -464,8 +472,6 @@ namespace SMBLibrary.Client
buffer.SetNumberOfBytesReceived(numberOfBytesReceived); buffer.SetNumberOfBytesReceived(numberOfBytesReceived);
ProcessConnectionBuffer(state); ProcessConnectionBuffer(state);
if (clientSocket.Connected)
{
try try
{ {
clientSocket.BeginReceive(buffer.Buffer, buffer.WriteOffset, buffer.AvailableLength, SocketFlags.None, new AsyncCallback(OnClientSocketReceive), state); clientSocket.BeginReceive(buffer.Buffer, buffer.WriteOffset, buffer.AvailableLength, SocketFlags.None, new AsyncCallback(OnClientSocketReceive), state);
@ -473,15 +479,16 @@ namespace SMBLibrary.Client
catch (ObjectDisposedException) catch (ObjectDisposedException)
{ {
m_isConnected = false; m_isConnected = false;
buffer.Dispose();
Log("[ReceiveCallback] BeginReceive ObjectDisposedException"); Log("[ReceiveCallback] BeginReceive ObjectDisposedException");
buffer.Dispose();
m_disconnectedEventHandle.Set();
} }
catch (SocketException ex) catch (SocketException ex)
{ {
m_isConnected = false; m_isConnected = false;
buffer.Dispose();
Log("[ReceiveCallback] BeginReceive SocketException: " + ex.Message); Log("[ReceiveCallback] BeginReceive SocketException: " + ex.Message);
} buffer.Dispose();
m_disconnectedEventHandle.Set();
} }
} }
} }