예제 #1
0
        /// <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;
 }
예제 #3
0
 /// <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);
     }
 }
예제 #4
0
 /// <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);
     }
 }
예제 #5
0
 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;
 }
예제 #7
0
 /// <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)));
 }
예제 #8
0
 /// <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));
 }
예제 #9
0
 /// <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));
 }
예제 #10
0
 /// <summary>
 /// 引发客户端连接到服务器事件
 /// </summary>
 /// <param name="client">接入服务器的客户端</param>
 protected virtual void OnClienConnect(AsyncTCPClient client)
 {
     ClientConnected?.Invoke(this,
                             new TcpClientConnectedEventArgs(client));
 }