/// <summary> /// 客户端接入服务器的回调函数 /// </summary> /// <param name="ar"></param> private void AsyncAcceptCallBack(IAsyncResult ar) { TcpListener listener; AsyncTCPClient asyncClient; try { listener = ar.AsyncState as TcpListener; TcpClient client = listener.EndAcceptTcpClient(ar); asyncClient = new AsyncTCPClient(client, this.Encoding); lock (connections) { connections.Add(asyncClient); } OnClienConnect(asyncClient); } catch (Exception ex) { CommonModules.Notifier.NotifyHelper.Notify(Notifier.NotifyLevel.FATAL, "接受客户端接入时发生致命性错误", ex); throw ex; } //开始异步接受数据 byte[] buffer = new byte[tcplistener.Server.ReceiveBufferSize]; asyncClient.Client.GetStream().BeginRead(buffer, 0, buffer.Length, BeginReceiveCallBack, new TCPClientState(asyncClient, buffer)); //继续等待客户端连接 if (connections.Count < this.MaxConnectCount) { tcplistener.BeginAcceptTcpClient(AsyncAcceptCallBack, listener); } }
public TcpClientConnectedEventArgs(AsyncTCPClient client) { if (client == null) { throw new ArgumentNullException("TcpClient"); } this.TcpClient = client; }
/// <summary> /// 发送数据 /// </summary> /// <param name="client"></param> /// <param name="data"></param> public void Send(AsyncTCPClient client, byte[] data) { try { client.Send(data); } catch (Exception ex) { Notifier.NotifyHelper.Notify(Notifier.NotifyLevel.ERROR, "服务器发送数据出错.", ex); } }
/// <summary> /// 发送数据 /// </summary> /// <param name="client"></param> /// <param name="data"></param> public void Send(AsyncTCPClient client, string data) { try { Send(client, this.m_Encoding.GetBytes(data)); } catch (Exception ex) { Notifier.NotifyHelper.Notify(Notifier.NotifyLevel.ERROR, "服务器发送数据出错.", ex); } }
public TCPClientState(AsyncTCPClient client, byte[] buffer) { TcpClient = client; Buffer = buffer; }
/// <summary> /// 接收报文数据事件参数 /// </summary> /// <param name="tcpClient">客户端</param> /// <param name="datagram">报文数据</param> public TCPDatagramReceiveEventArgs(AsyncTCPClient tcpClient, T datagram) { this.TcpClient = tcpClient; this.DataGram = datagram; }
/// <summary> /// 接收数据报文明文事件 /// </summary> /// <param name="sender"></param> /// <param name="datagram"></param> private void OnPlaintextReceived(AsyncTCPClient sender, byte[] datagram) { PlaintextReceived?.Invoke(this, new TCPDatagramReceiveEventArgs <string>( sender, this.Encoding.GetString(datagram))); }
/// <summary> /// 接收数据报文事件 /// </summary> /// <param name="sender"></param> /// <param name="datagram"></param> private void OnDatagramReceived(AsyncTCPClient sender, byte[] datagram) { DatagramReceived?.Invoke(this, new TCPDatagramReceiveEventArgs <byte[]>(sender, datagram)); }
/// <summary> /// 服务器接受客户端报文数据事件 /// </summary> /// <param name="client">客户端</param> /// <param name="datagram">数据报文</param> protected virtual void OnServerReceiveDatagram(AsyncTCPClient client, byte[] datagram) { ServerReceiveDatagram?.Invoke(this, new TCPDatagramReceiveEventArgs <byte[]>(client, datagram)); }
/// <summary> /// 引发客户端连接到服务器事件 /// </summary> /// <param name="client">接入服务器的客户端</param> protected virtual void OnClienConnect(AsyncTCPClient client) { ClientConnected?.Invoke(this, new TcpClientConnectedEventArgs(client)); }