/// <summary> /// 触发异常事件 /// </summary> /// <param name="state"></param> private void RaiseOtherException(ZAsyncTcpClientState state, string descrip) { if (OtherException != null) { OtherException(this, new ZAsyncTcpEventArgs(descrip, state)); } }
/// <summary> /// 触发数据发送完毕的事件 /// </summary> /// <param name="state"></param> private void RaiseCompletedSend(ZAsyncTcpClientState state) { if (CompletedSend != null) { CompletedSend(this, new ZAsyncTcpEventArgs(state)); } }
/// <summary> /// 触发网络错误事件 /// </summary> /// <param name="state"></param> private void RaiseNetError(ZAsyncTcpClientState state) { if (NetError != null) { NetError(this, new ZAsyncTcpEventArgs(state)); } }
/// <summary> /// 触发发送数据前的事件 /// </summary> /// <param name="state"></param> private void RaisePrepareSend(ZAsyncTcpClientState state) { if (PrepareSend != null) { PrepareSend(this, new ZAsyncTcpEventArgs(state)); } }
private void RaiseDataReceived(ZAsyncTcpClientState state) { if (DataReceived != null) { DataReceived(this, new ZAsyncTcpEventArgs(state)); } }
/// <summary> /// 触发客户端连接断开事件 /// </summary> /// <param name="client"></param> private void RaiseClientDisconnected(ZAsyncTcpClientState state) { if (ClientDisconnected != null) { ClientDisconnected(this, new ZAsyncTcpEventArgs("连接断开")); } }
/// <summary> /// 关闭一个与客户端之间的会话 /// </summary> /// <param name="state">需要关闭的客户端会话对象</param> public void Close(ZAsyncTcpClientState state) { if (state != null) { state.Close(); _tcpClientStateList.Remove(state); //TODO 触发关闭事件 } }
/// <summary> /// 数据接受回调函数 /// </summary> /// <param name="ar"></param> private void BeginReadHandler(IAsyncResult ar) { int numberOfReadBytes = 0; ZAsyncTcpClientState clientState = (ZAsyncTcpClientState)ar.AsyncState; NetworkStream networkStream = clientState.Client.GetStream(); numberOfReadBytes = networkStream.EndRead(ar); if (numberOfReadBytes > 0) { //byte[] buffer = new byte[numberOfReadBytes]; //Buffer.BlockCopy(clientState.Buffer, 0, buffer, 0, numberOfReadBytes); RaiseDataReceived(clientState); } networkStream.BeginRead(clientState.Buffer, 0, clientState.Buffer.Length, BeginReadHandler, clientState); }
/// <summary> /// 处理客户端连接的函数 /// </summary> /// <param name="ar"></param> private void BeginAcceptTcpClientHandler(IAsyncResult ar) { TcpListener tcpListener = (TcpListener)ar.AsyncState; TcpClient tcpClient = tcpListener.EndAcceptTcpClient(ar); byte[] buffer = new byte[tcpClient.ReceiveBufferSize]; ZAsyncTcpClientState clientState = new ZAsyncTcpClientState(tcpClient, buffer); _tcpClientStateList.Add(clientState); RaiseClientConnected(clientState); NetworkStream networkStream = tcpClient.GetStream(); networkStream.BeginRead(clientState.Buffer, 0, clientState.Buffer.Length, BeginReadHandler, clientState); tcpListener.BeginAcceptTcpClient(BeginAcceptTcpClientHandler, tcpListener); }
public ZAsyncTcpEventArgs(string msg, ZAsyncTcpClientState state) { this._msg = msg; this._state = state; IsHandled = false; }
public ZAsyncTcpEventArgs(ZAsyncTcpClientState state) { this._state = state; IsHandled = false; }
private void RaiseOtherException(ZAsyncTcpClientState state) { RaiseOtherException(state, ""); }