/// <summary> /// Function is called by our read threads whenever data is received. This function calls the /// OnRecvData function, which is the one that should be overriden by the user. /// </summary> /// <param name="ar"></param> private void ReceiveComplete(object sender, SocketAsyncEventArgs e) { if (Client == null) { return; } if (e == null) { throw new Exception("SocketAsyncEventArgs was null"); } byte[] bData = (byte[])e.Buffer; int nLen = e.BytesTransferred; if (nLen == 0) { if (UserInitiatedDisconnect == false) { OnDisconnect("Graceful Disconnect"); } return; } if (bData == null) { throw new Exception("bData is null in SocketClient.OnRecvDataAll"); } try { byte[] bPassIn = new byte[nLen]; Array.Copy(bData, 0, bPassIn, 0, nLen); /// Check in our buffer, prevent pinning if (m_BufferPool != null) { m_BufferPool.Checkin(bData); } OnRecvData(bPassIn, nLen); } catch (System.NullReferenceException exnull) { throw new Exception("Something is null here... not sure what", exnull); } }
/// <summary> /// Function is called by our read threads whenever data is received. This function calls the /// OnRecvData function, which is the one that should be overriden by the user. /// </summary> /// <param name="ar"></param> private void OnRecvDataAll(IAsyncResult ar) { if (Client == null) { return; } if (ar == null) { throw new Exception("IAsyncResult was null"); } byte[] bData = (byte[])ar.AsyncState; int nLen = 0; try { if (SslStream != null) { nLen = SslStream.EndRead(ar); } else { nLen = NetworkStream.EndRead(ar); } //nLen = Client.EndReceive(ar); // //System.Net.Sockets.NetworkStream networkStream = ar.AsyncState as System.Net.Sockets.NetworkStream; // //nLen = networkStream.EndRead(ar); } catch (SocketException esock) { if (m_Logger != null) { m_Logger.LogError(ToString(), MessageImportance.Highest, esock.ToString()); } OnDisconnect(esock.ToString()); /// Check in our buffer, prevent pinning if (m_BufferPool != null) { m_BufferPool.Checkin(bData); } return; } catch (System.IO.IOException e) /// socket was closed { if (m_Logger != null) { m_Logger.LogError(ToString(), MessageImportance.Highest, e.ToString()); } FireDisconnectHandler(new System.EventArgs()); //OnDisconnect(e.ToString()); // don't call this here, socket is already closed /// Check in our buffer, prevent pinning if (m_BufferPool != null) { m_BufferPool.Checkin(bData); } return; } catch (System.ObjectDisposedException e2) // network stream closed { if (m_Logger != null) { m_Logger.LogError(ToString(), MessageImportance.Highest, e2.ToString()); } FireDisconnectHandler(new System.EventArgs()); //OnDisconnect(e.ToString()); // don't call this here, socket is already closed /// Check in our buffer, prevent pinning if (m_BufferPool != null) { m_BufferPool.Checkin(bData); } return; } catch (System.Exception e3) { if (m_Logger != null) { m_Logger.LogError(ToString(), MessageImportance.Highest, e3.ToString()); } OnDisconnect(e3.ToString()); /// Check in our buffer, prevent pinning if (m_BufferPool != null) { m_BufferPool.Checkin(bData); } return; } HandleReceiveData(bData, nLen); }