mirror of
https://github.com/TalAloni/SMBLibrary.git
synced 2025-04-30 10:47:48 +02:00
Applied proper disconnection sequence to V1 of the client.
This commit is contained in:
parent
8870fe9acd
commit
c49d365699
1 changed files with 33 additions and 26 deletions
|
@ -44,6 +44,7 @@ namespace SMBLibrary.Client
|
|||
private uint m_serverMaxBufferSize;
|
||||
private ushort m_maxMpxCount;
|
||||
private int m_responseTimeoutInMilliseconds;
|
||||
private EventWaitHandle m_disconnectedEventHandle = new EventWaitHandle(false, EventResetMode.ManualReset);
|
||||
|
||||
private object m_incomingQueueLock = new object();
|
||||
private List<SMB1Message> m_incomingQueue = new List<SMB1Message>();
|
||||
|
@ -110,7 +111,7 @@ namespace SMBLibrary.Client
|
|||
SessionPacket sessionResponsePacket = WaitForSessionResponsePacket();
|
||||
if (!(sessionResponsePacket is PositiveSessionResponsePacket))
|
||||
{
|
||||
m_clientSocket.Disconnect(false);
|
||||
DisconnectSocket();
|
||||
if (!ConnectSocket(serverAddress, port))
|
||||
{
|
||||
return false;
|
||||
|
@ -137,6 +138,7 @@ namespace SMBLibrary.Client
|
|||
bool supportsDialect = NegotiateDialect(m_forceExtendedSecurity);
|
||||
if (!supportsDialect)
|
||||
{
|
||||
DisconnectSocket();
|
||||
m_clientSocket.Close();
|
||||
}
|
||||
else
|
||||
|
@ -149,6 +151,7 @@ namespace SMBLibrary.Client
|
|||
|
||||
private bool ConnectSocket(IPAddress serverAddress, int port)
|
||||
{
|
||||
m_disconnectedEventHandle.Reset();
|
||||
m_clientSocket = new Socket(serverAddress.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
|
||||
|
||||
try
|
||||
|
@ -166,11 +169,18 @@ namespace SMBLibrary.Client
|
|||
return true;
|
||||
}
|
||||
|
||||
private void DisconnectSocket()
|
||||
{
|
||||
m_clientSocket.Shutdown(SocketShutdown.Send);
|
||||
m_disconnectedEventHandle.WaitOne();
|
||||
m_clientSocket.Shutdown(SocketShutdown.Receive);
|
||||
}
|
||||
|
||||
public void Disconnect()
|
||||
{
|
||||
if (m_isConnected)
|
||||
{
|
||||
m_clientSocket.Disconnect(false);
|
||||
DisconnectSocket();
|
||||
m_clientSocket.Close();
|
||||
m_connectionState.ReceiveBuffer.Dispose();
|
||||
m_isConnected = false;
|
||||
|
@ -424,12 +434,6 @@ namespace SMBLibrary.Client
|
|||
ConnectionState state = (ConnectionState)ar.AsyncState;
|
||||
Socket clientSocket = state.ClientSocket;
|
||||
|
||||
if (!clientSocket.Connected)
|
||||
{
|
||||
state.ReceiveBuffer.Dispose();
|
||||
return;
|
||||
}
|
||||
|
||||
int numberOfBytesReceived = 0;
|
||||
try
|
||||
{
|
||||
|
@ -438,18 +442,21 @@ namespace SMBLibrary.Client
|
|||
catch (ArgumentException) // The IAsyncResult object was not returned from the corresponding synchronous method on this class.
|
||||
{
|
||||
state.ReceiveBuffer.Dispose();
|
||||
m_disconnectedEventHandle.Set();
|
||||
return;
|
||||
}
|
||||
catch (ObjectDisposedException)
|
||||
{
|
||||
Log("[ReceiveCallback] EndReceive ObjectDisposedException");
|
||||
state.ReceiveBuffer.Dispose();
|
||||
m_disconnectedEventHandle.Set();
|
||||
return;
|
||||
}
|
||||
catch (SocketException ex)
|
||||
{
|
||||
Log("[ReceiveCallback] EndReceive SocketException: " + ex.Message);
|
||||
state.ReceiveBuffer.Dispose();
|
||||
m_disconnectedEventHandle.Set();
|
||||
return;
|
||||
}
|
||||
|
||||
|
@ -457,6 +464,7 @@ namespace SMBLibrary.Client
|
|||
{
|
||||
m_isConnected = false;
|
||||
state.ReceiveBuffer.Dispose();
|
||||
m_disconnectedEventHandle.Set();
|
||||
}
|
||||
else
|
||||
{
|
||||
|
@ -464,8 +472,6 @@ namespace SMBLibrary.Client
|
|||
buffer.SetNumberOfBytesReceived(numberOfBytesReceived);
|
||||
ProcessConnectionBuffer(state);
|
||||
|
||||
if (clientSocket.Connected)
|
||||
{
|
||||
try
|
||||
{
|
||||
clientSocket.BeginReceive(buffer.Buffer, buffer.WriteOffset, buffer.AvailableLength, SocketFlags.None, new AsyncCallback(OnClientSocketReceive), state);
|
||||
|
@ -473,15 +479,16 @@ namespace SMBLibrary.Client
|
|||
catch (ObjectDisposedException)
|
||||
{
|
||||
m_isConnected = false;
|
||||
buffer.Dispose();
|
||||
Log("[ReceiveCallback] BeginReceive ObjectDisposedException");
|
||||
buffer.Dispose();
|
||||
m_disconnectedEventHandle.Set();
|
||||
}
|
||||
catch (SocketException ex)
|
||||
{
|
||||
m_isConnected = false;
|
||||
buffer.Dispose();
|
||||
Log("[ReceiveCallback] BeginReceive SocketException: " + ex.Message);
|
||||
}
|
||||
buffer.Dispose();
|
||||
m_disconnectedEventHandle.Set();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue