/// <summary> /// 开始处理数据接收 /// </summary> /// <param name="args">事件参数</param> private void ProcessReceives(SocketAsyncEventArgs args) { var client = args.UserToken as ClientInfo; if (client == null) { return; } if (args.BytesTransferred > 0) { if (args.SocketError != SocketError.Success) { //这里可能会产生一系列的问题,比如操作取消之类 DropClient(client); return; } try { var dbRecieved = args.BytesTransferred; var unpack = new DataUnpack(this, client, null); unpack.ProcessBinaryData(args.Buffer, args.Offset, dbRecieved, ref client.lastDataBuf); WaitForData(args); } catch (ObjectDisposedException ex) { //在被释放的资源上进行操作 //WSO.Tools.ErrorLogger.LogException(ex); //_serverInfoSetter.AppendText = ex.Message; } catch (SocketException socketEx) { if (socketEx.ErrorCode == 10054) { DropClient(client); //移除客户端 } else { ErrorLogger.LogException(socketEx); } } } else { //客户端断开连接 DropClient(client); } }
private void Tcp_ProcessDataRecieved(IAsyncResult result) { var state = (StateObject)result.AsyncState; if (client == null || !client.Connected) { //没有连接的话,则不处理 return; } try { var recievedCount = client.EndReceive(result); if (recievedCount > 0) { var unpack = new DataUnpack(this, null, null); unpack.ProcessBinaryData(state.buffer, 0, recievedCount, ref state.lastDataBuf); Tcp_WaitForData(state); } else { //断开连接 Disconnect(); } } catch (ObjectDisposedException ex) { //在被释放的资源上进行操作 //WSO.Tools.ErrorLogger.LogException(ex); //_serverInfoSetter.AppendText = ex.Message; } catch (SocketException socketEx) { if (socketEx.ErrorCode != 10054) { ErrorLogger.LogException(socketEx); } Disconnect(); //移除客户端 } catch (Exception ex) { ErrorLogger.LogException(ex); Disconnect(); } }
/// <summary> /// 处理UDP数据,UDP仅接收PSP数据 /// </summary> /// <param name="result"></param> private void Udp_ProcessDataRecieved(IAsyncResult result) { var state = (StateObject)result.AsyncState; if (!isUdp || udpSocket == null) { return; } try { EndPoint temp = new IPEndPoint(IPAddress.Any, 0); var recievedCount = udpSocket.EndReceiveFrom(result, ref temp); if (recievedCount > 0) { var unpack = new DataUnpack(this, null, null); unpack.ProcessBinaryData(state.buffer, 0, recievedCount, ref state.lastDataBuf); Udp_WaitForData(state); } } catch (ObjectDisposedException ex) { //在被释放的资源上进行操作 //WSO.Tools.ErrorLogger.LogException(ex); //_serverInfoSetter.AppendText = ex.Message; } catch (SocketException socketEx) { if (socketEx.ErrorCode != 10054) { ErrorLogger.LogException(socketEx); } SendUdpEnd(); } catch (Exception ex) { ErrorLogger.LogException(ex); SendUdpEnd(); } }