/// <summary> /// Called when an <see cref="T:ExitGames.Net.Sockets.AsyncSocketReceiver"/> receives a message. /// </summary> /// <param name="sender">The <see cref="T:ExitGames.Net.Sockets.AsyncSocketReceiver"/>.</param> /// <param name="e">The event args.</param> private void OnReaderReceive(AsyncSocketReceiver sender, SocketAsyncEventArgs e) { try { if (e.SocketError == SocketError.Success) { Interlocked.Exchange(ref bytesReceived, e.BytesTransferred); OnReceive(sender.RemoteEndpoint, e.Buffer, e.Offset, e.BytesTransferred); sender.BeginReceive(); return; } SocketError error = e.SocketError; if (error != SocketError.OperationAborted) { if (error == SocketError.ConnectionReset) { if (log.IsDebugEnabled) { log.DebugFormat("Connection was reset by remote peer {0}.", sender.RemoteEndpoint); } } else if (error == SocketError.Disconnecting) { if (log.IsDebugEnabled) { log.DebugFormat("Disconnecting on remote endpoint {0}.", sender.RemoteEndpoint); } } else { if (log.IsErrorEnabled) { log.ErrorFormat("Receive failed on {0}: SocketError={1}", sender.RemoteEndpoint, e.SocketError); } } } else { if (log.IsDebugEnabled) { log.DebugFormat("OperationAborted for remote endpoint {0}.", sender.RemoteEndpoint); } } RemoveSocketReceiver(sender); OnDisconnected(sender.RemoteEndpoint, e.SocketError); } catch (ThreadAbortException) { throw; } catch (OutOfMemoryException) { throw; } catch (Exception exception) { log.Error(exception); } }
/// <summary> /// Removes an <see cref="T:ExitGames.Net.Sockets.AsyncSocketReceiver"/>. /// </summary> /// <param name="socketReceiver">The <see cref="T:ExitGames.Net.Sockets.AsyncSocketReceiver"/>.</param> private void RemoveSocketReceiver(AsyncSocketReceiver socketReceiver) { lock (readerDictionary) { readerDictionary.Remove(socketReceiver.RemoteEndpoint); socketReceiver.Dispose(); } }