瀏覽代碼

Client: Fixed a bug related to localhost communication

Tal Aloni 5 年之前
父節點
當前提交
2caf390c41
共有 2 個文件被更改,包括 13 次插入21 次删除
  1. 7 11
      SMBLibrary/Client/SMB1Client.cs
  2. 6 10
      SMBLibrary/Client/SMB2Client.cs

+ 7 - 11
SMBLibrary/Client/SMB1Client.cs

@@ -1,4 +1,4 @@
-/* Copyright (C) 2014-2018 Tal Aloni <tal.aloni.il@gmail.com>. All rights reserved.
+/* Copyright (C) 2014-2019 Tal Aloni <tal.aloni.il@gmail.com>. All rights reserved.
  * 
  * You can redistribute this program and/or modify it under the terms of
  * the GNU Lesser Public License as published by the Free Software Foundation,
@@ -31,7 +31,6 @@ namespace SMBLibrary.Client
         private bool m_isConnected;
         private bool m_isLoggedIn;
         private Socket m_clientSocket;
-        private IAsyncResult m_currentAsyncResult;
         private bool m_forceExtendedSecurity;
         private bool m_unicode;
         private bool m_largeFiles;
@@ -87,7 +86,7 @@ namespace SMBLibrary.Client
 
                 ConnectionState state = new ConnectionState();
                 NBTConnectionReceiveBuffer buffer = state.ReceiveBuffer;
-                m_currentAsyncResult = m_clientSocket.BeginReceive(buffer.Buffer, buffer.WriteOffset, buffer.AvailableLength, SocketFlags.None, new AsyncCallback(OnClientSocketReceive), state);
+                m_clientSocket.BeginReceive(buffer.Buffer, buffer.WriteOffset, buffer.AvailableLength, SocketFlags.None, new AsyncCallback(OnClientSocketReceive), state);
                 bool supportsDialect = NegotiateDialect(m_forceExtendedSecurity);
                 if (!supportsDialect)
                 {
@@ -358,13 +357,6 @@ namespace SMBLibrary.Client
 
         private void OnClientSocketReceive(IAsyncResult ar)
         {
-            if (ar != m_currentAsyncResult)
-            {
-                // We ignore calls for old sockets which we no longer use
-                // See: http://rajputyh.blogspot.co.il/2010/04/solve-exception-message-iasyncresult.html
-                return;
-            }
-
             ConnectionState state = (ConnectionState)ar.AsyncState;
 
             if (!m_clientSocket.Connected)
@@ -377,6 +369,10 @@ namespace SMBLibrary.Client
             {
                 numberOfBytesReceived = m_clientSocket.EndReceive(ar);
             }
+            catch (ArgumentException) // The IAsyncResult object was not returned from the corresponding synchronous method on this class.
+            {
+                return;
+            }
             catch (ObjectDisposedException)
             {
                 Log("[ReceiveCallback] EndReceive ObjectDisposedException");
@@ -400,7 +396,7 @@ namespace SMBLibrary.Client
 
                 try
                 {
-                    m_currentAsyncResult = m_clientSocket.BeginReceive(buffer.Buffer, buffer.WriteOffset, buffer.AvailableLength, SocketFlags.None, new AsyncCallback(OnClientSocketReceive), state);
+                    m_clientSocket.BeginReceive(buffer.Buffer, buffer.WriteOffset, buffer.AvailableLength, SocketFlags.None, new AsyncCallback(OnClientSocketReceive), state);
                 }
                 catch (ObjectDisposedException)
                 {

+ 6 - 10
SMBLibrary/Client/SMB2Client.cs

@@ -32,7 +32,6 @@ namespace SMBLibrary.Client
         private bool m_isConnected;
         private bool m_isLoggedIn;
         private Socket m_clientSocket;
-        private IAsyncResult m_currentAsyncResult;
 
         private object m_incomingQueueLock = new object();
         private List<SMB2Command> m_incomingQueue = new List<SMB2Command>();
@@ -79,7 +78,7 @@ namespace SMBLibrary.Client
 
                 ConnectionState state = new ConnectionState();
                 NBTConnectionReceiveBuffer buffer = state.ReceiveBuffer;
-                m_currentAsyncResult = m_clientSocket.BeginReceive(buffer.Buffer, buffer.WriteOffset, buffer.AvailableLength, SocketFlags.None, new AsyncCallback(OnClientSocketReceive), state);
+                m_clientSocket.BeginReceive(buffer.Buffer, buffer.WriteOffset, buffer.AvailableLength, SocketFlags.None, new AsyncCallback(OnClientSocketReceive), state);
                 bool supportsDialect = NegotiateDialect();
                 if (!supportsDialect)
                 {
@@ -246,13 +245,6 @@ namespace SMBLibrary.Client
 
         private void OnClientSocketReceive(IAsyncResult ar)
         {
-            if (ar != m_currentAsyncResult)
-            {
-                // We ignore calls for old sockets which we no longer use
-                // See: http://rajputyh.blogspot.co.il/2010/04/solve-exception-message-iasyncresult.html
-                return;
-            }
-
             ConnectionState state = (ConnectionState)ar.AsyncState;
 
             if (!m_clientSocket.Connected)
@@ -265,6 +257,10 @@ namespace SMBLibrary.Client
             {
                 numberOfBytesReceived = m_clientSocket.EndReceive(ar);
             }
+            catch (ArgumentException) // The IAsyncResult object was not returned from the corresponding synchronous method on this class.
+            {
+                return;
+            }
             catch (ObjectDisposedException)
             {
                 Log("[ReceiveCallback] EndReceive ObjectDisposedException");
@@ -288,7 +284,7 @@ namespace SMBLibrary.Client
 
                 try
                 {
-                    m_currentAsyncResult = m_clientSocket.BeginReceive(buffer.Buffer, buffer.WriteOffset, buffer.AvailableLength, SocketFlags.None, new AsyncCallback(OnClientSocketReceive), state);
+                    m_clientSocket.BeginReceive(buffer.Buffer, buffer.WriteOffset, buffer.AvailableLength, SocketFlags.None, new AsyncCallback(OnClientSocketReceive), state);
                 }
                 catch (ObjectDisposedException)
                 {