private static void OnReceiveHandler(IAsyncResult ar) { if (ar == null) { return; } BaseClient baseClient = null; try { baseClient = (BaseClient)ar.AsyncState; int numBytes = baseClient.Socket.EndReceive(ar); if (numBytes > 0 || (numBytes <= 0 && DisconnectOnNullByte == false)) { Log.Tcp(baseClient.GetIp(), baseClient.ReceiveBuffer, 0, numBytes); int bufferSize = baseClient.ReceiveBufferOffset + numBytes; byte[] packetStream = new byte[bufferSize]; Buffer.BlockCopy(baseClient.ReceiveBuffer, 0, packetStream, 0, bufferSize); baseClient.ReceiveBufferOffset = 0; baseClient.OnReceive(packetStream); baseClient.BeginReceive(); } else { Log.Debug("BaseClient", "disconnection of client (" + baseClient.GetIp() + "), received bytes=" + numBytes); baseClient.Server.Disconnect(baseClient, "Exiting"); } } catch (ObjectDisposedException) { if (baseClient != null) { baseClient.Server.Disconnect(baseClient, "ObjectDisposedException in OnReceiveHandler"); } } catch (SocketException e) { if (baseClient != null) { Log.Debug("BaseClient", string.Format("{0} {1}", baseClient.GetIp(), e.Message)); baseClient.Server.Disconnect(baseClient, $"OnReceiveHandler: { Enum.GetName(typeof(SocketError), e.ErrorCode) } ({ e.Message })"); } } catch (Exception e) { Log.Error("BaseClient", e.ToString()); if (baseClient != null) { baseClient.Server.Disconnect(baseClient, "Exception in OnReceiveHandler"); } } }
private static void OnReceiveHandler(IAsyncResult ar) { if (ar == null) { return; } BaseClient baseClient = null; try { baseClient = (BaseClient)ar.AsyncState; int numBytes = baseClient.Socket.EndReceive(ar); if (numBytes > 0 || (numBytes <= 0 && DisconnectOnNullByte == false)) { Log.Tcp(baseClient.GetIp, baseClient.ReceiveBuffer, 0, numBytes); byte[] buffer = baseClient.ReceiveBuffer; int bufferSize = baseClient.ReceiveBufferOffset + numBytes; baseClient.ReceiveBufferOffset = 0; byte[] Packet = new byte[bufferSize]; Buffer.BlockCopy(buffer, 0, Packet, 0, bufferSize); baseClient.OnReceive(Packet); baseClient.BeginReceive(); } else { Log.Debug("BaseClient", "disconnection of client (" + baseClient.GetIp + "), received bytes=" + numBytes); baseClient._srvr.Disconnect(baseClient); } } catch (ObjectDisposedException) { if (baseClient != null) { baseClient._srvr.Disconnect(baseClient); } } catch (SocketException e) { if (baseClient != null) { Log.Info("BaseClient", string.Format("{0} {1}", baseClient.GetIp, e.Message)); baseClient._srvr.Disconnect(baseClient); } } catch (Exception e) { Log.Error("BaseClient", e.ToString()); if (baseClient != null) { baseClient._srvr.Disconnect(baseClient); } } }