Пример #1
0
 protected void Callback_SocketStatusChanged(object sender, SocketStatusChangedArgs <TPackage> args)
 {
     if (OnSocketStatusChangedEvent != null)
     {
         OnSocketStatusChangedEvent(sender, args);
     }
 }
Пример #2
0
 /// <summary>
 /// 一个连接接收完成
 /// </summary>
 /// <param name="sender"></param>
 /// <param name="e"></param>
 private void OnAcceptCompleted(object sender, SocketAsyncEventArgs e)
 {
     if (m_state != (int)EnumNetworkStatus.Established)
     {
         return;
     }
     if (e.SocketError == SocketError.Success)
     {
         //和客户端关联的socket
         Socket socket = e.AcceptSocket;
         try
         {
             if (socket.Connected)
             {
                 var client = new SocketNetClient <TBuilder, TPackage>(socket,
                                                                       EnumNetworkType.TCP,
                                                                       NetworkGlobal.TCP_RECEIVE_BUFFER_SIZE,
                                                                       NetworkGlobal.TCP_SOCKET_RECEIVE_SIZE,
                                                                       NetworkGlobal.TCP_SOCKET_SEND_SIZE);
                 client.OnSocketStatusChangedEvent += Client_OnClientSocketStatusChangedEvent;
                 client.OnDataReceivedEvent        += Client_OnDataReceivedEvent;
                 client.OnExceptionHappenedEvent   += Client_OnExceptionHappenedEvent;
                 //更新上线
                 m_clients.AddOrUpdate((IPEndPoint)socket.RemoteEndPoint, client, (v, k) => { return(client); });
                 //启动收收数
                 client.StartReceive();
                 //上线通知
                 if (OnClientSocketStatusChangedEvent != null)
                 {
                     var args = new SocketStatusChangedArgs <TPackage>(EnumNetworkStatus.Established, client);
                     OnClientSocketStatusChangedEvent(this, args);
                 }
             }
         }
         catch (SocketException ex)
         {
             if (OnExceptionHappenedEvent != null)
             {
                 var args = new ExceptionHappenedArgs((IPEndPoint)socket.RemoteEndPoint, "处理客户端连接上线失败", ex);
                 OnExceptionHappenedEvent(this, args);
             }
         }
     }
     //投递下一个接受请求
     StartAcceptConnection(e);
 }
Пример #3
0
        private void Client_OnClientSocketStatusChangedEvent(object arg1, SocketStatusChangedArgs <TPackage> arg2)
        {
            switch (arg2.Status)
            {
            case EnumNetworkStatus.Shutdown:
            {
                SocketNetClient <TBuilder, TPackage> client = null;
                m_clients.TryRemove(arg2.Target.RemoteEndPoint, out client);
            }
            break;

            default:
                break;
            }
            if (OnClientSocketStatusChangedEvent != null)
            {
                OnClientSocketStatusChangedEvent(this, arg2);
            }
        }