/// <summary> /// 对读取到的数据进行处理--Client异步读取完后会执行这个事件方法 /// </summary> /// <param name="client">当前客户端类</param> /// <param name="readData">读取到的数据</param> private void Client_OnReadData(AsyncSocketClient client, byte[] readData, int offset, int count) { _listReadEvent.PutObj(new SocketEventDeal(client, EN_SocketDealEvent.read)); //读下一条 try { var param = new SocketEventParam(client.ConnectSocket, EN_SocketEvent.read) { ClientInfo = client.ClientInfo, Data = readData, Offset = offset, Count = count }; if (client.ConnectSocket.Connected) { _socketEventPool.PutObj(param); lock (this) { ReadByteCount += readData.Length; } } } catch (Exception ex) { NetLogger.Log($@"Client_OnReadData 异常 {ex.Message}***{ex.StackTrace}"); } }
/// <summary> /// 作为客户端连接后要对AsyncSocketClient的处理--事件方法 /// </summary> /// <param name="param"></param> /// <param name="client"></param> private void SocketConnectEvent(SocketEventParam param, AsyncSocketClient client) { try { if (param.Socket == null || client == null) //连接失败 { } else { lock (_clientGroup) { _clientGroup[client.ConnectSocket] = client; } client.OnSocketClose += Client_OnSocketClose; client.OnReadData += Client_OnReadData; client.OnSendData += Client_OnSendData; _listReadEvent.PutObj(new SocketEventDeal(client, EN_SocketDealEvent.read)); } _socketEventPool.PutObj(param); } catch (Exception ex) { NetLogger.Log($"SocketConnectEvent 异常 {ex.Message}***", ex); } }
/// <summary> /// Listener 生成的 AsyncSocketClient 回调方法 /// </summary> /// <param name="listenParam">客户端参数</param> /// <param name="client">生成的客户端 实体类</param> private void Listener_OnAcceptSocketClient(ListenParam listenParam, AsyncSocketClient client) { try { lock (_clientGroup) { _clientGroup[client.ConnectSocket] = client; } //给Client添加消息处理事件 client.OnSocketClose += Client_OnSocketClose; client.OnReadData += Client_OnReadData; client.OnSendData += Client_OnSendData; client.OnSocketHeart += Client_OnSocketHeart; _listReadEvent.PutObj(new SocketEventDeal(client, EN_SocketDealEvent.read)); var param = new SocketEventParam(client.ConnectSocket, EN_SocketEvent.accept) { ClientInfo = client.ClientInfo }; _socketEventPool.PutObj(param); } catch (Exception ex) { NetLogger.Log(@"Listener_OnAcceptSocket 异常", ex); } }
private void Client_OnSendData(AsyncSocketClient client, int sendCount) { //发送下一条 _listSendEvent.PutObj(new SocketEventDeal(client, EN_SocketDealEvent.send)); lock (this) { SendByteCount += sendCount; } }
private void Client_OnSocketClose(AsyncSocketClient client) { try { SocketEventParam param = new SocketEventParam(client.ConnectSocket, EN_SocketEvent.close); param.ClientInfo = client.ClientInfo; //Console.WriteLine("放入关闭命令"); _socketEventPool.PutObj(param); } catch (Exception ex) { NetLogger.Log($"Client_OnSocketClose 异常 {ex.Message}***", ex); } }
/// <summary> /// 收到包长度异常的时间方法 /// </summary> /// <param name="socket"></param> /// <param name="buffer"></param> /// <param name="offset"></param> /// <param name="packetLen"></param> internal void OnRcvPacketLenError(Socket socket, byte[] buffer, int offset, int packetLen) { try { lock (_clientGroup) { if (!_clientGroup.Keys.Any(a => a == socket)) { Debug.Assert(false); return; } AsyncSocketClient client = _clientGroup[socket]; client.CloseSocket(); } } catch (Exception ex) { NetLogger.Log("OnRcvPacketLenError 异常", ex); } }
/// <summary> /// 根据连接请求建立AsyncSocketClient /// </summary> /// <param name="acceptEventArgs"></param> private void ProcessAccept(SocketAsyncEventArgs acceptEventArgs) { try { using (acceptEventArgs) { if (acceptEventArgs.AcceptSocket != null) { AsyncSocketClient client = new AsyncSocketClient(acceptEventArgs.AcceptSocket); client.CreateClientInfo(this); _newSocketClientQueue.Enqueue(client); _acceptEvent.Set(); } } } catch (Exception ex) { NetLogger.Log($@"ProcessAccept {ex.Message}***", ex); } }
/// <summary> /// 放到发送缓冲 /// </summary> /// <param name="socket"></param> /// <param name="data"></param> /// <returns></returns> public EN_SendDataResult SendData(Socket socket, byte[] data) { if (socket != null && _clientGroup.Keys.Any(a => a == socket)) { AsyncSocketClient client = null; lock (_clientGroup) { client = _clientGroup[socket]; } EN_SendDataResult result = client.PutSendData(data); if (result == EN_SendDataResult.ok) { //发送下一条 _listSendEvent.PutObj(new SocketEventDeal(client, EN_SocketDealEvent.send)); } return(result); } return(EN_SendDataResult.no_client); }
private void Client_OnSocketHeart(AsyncSocketClient obj) { SendData(obj.ConnectSocket, System.Text.Encoding.Default.GetBytes("Heart beat test, please reply!")); }
/// <summary> /// 检查client是不是有问题 /// </summary> /// <param name="client"></param> private void DealNewAccept(AsyncSocketClient client) { client.SendBufferByteCount = NetServer.SendBufferBytePerClient; OnAcceptSocket?.Invoke(ListenParam, client); }