/// <summary> /// 触发异常事件 /// </summary> /// <param name="state"></param> private void RaiseOtherException(YTcpSrvClientState state, string descrip) { if (OtherException != null) { OtherException(this, new YTcpSrvEventArgs(descrip, state)); } }
/// <summary> /// 触发数据发送完毕的事件 /// </summary> /// <param name="state"></param> private void RaiseCompletedSend(YTcpSrvClientState state) { if (CompletedSend != null) { CompletedSend(this, new YTcpSrvEventArgs(state)); } }
/// <summary> /// 触发网络错误事件 /// </summary> /// <param name="state"></param> private void RaiseNetError(YTcpSrvClientState state) { if (NetError != null) { NetError(this, new YTcpSrvEventArgs(state)); } }
/// <summary> /// 触发客户端连接断开事件 /// </summary> /// <param name="client"></param> private void RaiseClientDisconnected(YTcpSrvClientState state, string ip) { if (ClientDisconnected != null) { ClientDisconnected(this, new YTcpSrvEventArgs(ip)); } }
/// <summary> /// 触发发送数据前的事件 /// </summary> /// <param name="state"></param> private void RaisePrepareSend(YTcpSrvClientState state) { if (PrepareSend != null) { PrepareSend(this, new YTcpSrvEventArgs(state)); } }
/// <summary> /// 触发客户端连接事件 /// </summary> /// <param name="state"></param> private void RaiseClientConnected(YTcpSrvClientState state) { if (ClientConnected != null) { ClientConnected(this, new YTcpSrvEventArgs(state)); } }
private void RaiseDataReceived(YTcpSrvClientState state) { try { DataReceived?.Invoke(this, new YTcpSrvEventArgs(state)); } catch (Exception e) { Logger.Error($"处理{state.TcpClientIP}的一包数据逻辑出先异常", e, 3600); } }
/// <summary> /// 关闭一个与客户端之间的会话 /// </summary> /// <param name="state">需要关闭的客户端会话对象</param> public void Close(YTcpSrvClientState state) { if (state != null) { state.Close(); lock (ClientLock) { clients.Remove(state); ClientCount--; } //TODO 触发关闭事件 } }
/// <summary> /// 数据接受回调函数 /// </summary> /// <param name="ar"></param> private void HandleDataReceived(IAsyncResult ar) { if (IsRunning) { YTcpSrvClientState state = (YTcpSrvClientState)ar.AsyncState; NetworkStream stream = state.NetworkStream; int recv = 0; try { recv = stream.EndRead(ar); } catch { recv = 0; } if (recv == 0) { // connection has been closed string ip = state.TcpClientIP; try { lock (ClientLock) { clients.Remove(state); } //触发客户端连接断开事件 RaiseClientDisconnected(state, ip); } catch (Exception e) { Logger.Error("客户端移除异常", e); } return; } // received byte and trigger event notification byte[] buff = new byte[recv]; Buffer.BlockCopy(state.Buffer, 0, buff, 0, recv); state.BufferCount = recv; //触发数据收到事件 : RaiseDataReceived(state); // continue listening for tcp datagram packets try { stream.BeginRead(state.Buffer, 0, state.Buffer.Length, HandleDataReceived, state); } catch (Exception e) { Logger.Error($"Tcp Begin Read [{state?.TcpClientIP}] 异常: ", e, 36000); } } }
/// <summary> /// 处理客户端连接的函数 /// </summary> /// <param name="ar"></param> private void HandleTcpClientAccepted(IAsyncResult ar) { if (IsRunning) { try { TcpClient client = listener.EndAcceptTcpClient(ar); byte[] buffer = new byte[client.ReceiveBufferSize]; YTcpSrvClientState state = new YTcpSrvClientState(client, buffer); lock (ClientLock) { clients.Add(state); } RaiseClientConnected(state); NetworkStream stream = state.NetworkStream; //开始异步读取数据 stream.BeginRead(state.Buffer, 0, state.Buffer.Length, HandleDataReceived, state); listener.BeginAcceptTcpClient( new AsyncCallback(HandleTcpClientAccepted), ar.AsyncState); } catch { } } }
public YTcpSrvEventArgs(string msg, YTcpSrvClientState state) { this.Msg = msg; this.State = state; IsHandled = false; }
public YTcpSrvEventArgs(YTcpSrvClientState state) { this.State = state; IsHandled = false; }
private void RaiseOtherException(YTcpSrvClientState state) { RaiseOtherException(state, ""); }
/// <summary> /// 发送数据 /// </summary> /// <param name="state">接收数据的客户端会话</param> /// <param name="data">数据报文</param> public void Send(YTcpSrvClientState state, byte[] data) { RaisePrepareSend(state); Send(state.TcpClient, data); }