Пример #1
0
        private void StartListen()
        {
            try
            {
                _socket.BeginAccept(asyncResult =>
                {
                    try
                    {
                        Socket newSocket = _socket.EndAccept(asyncResult);

                        //马上进行下一轮监听,增加吞吐量
                        if (_isListen)
                        {
                            StartListen();
                        }

                        TcpSocketConnection newConnection = new TcpSocketConnection(newSocket, this, RecLength)
                        {
                            HandleRecMsg      = HandleRecMsg == null ? null : new Action <TcpSocketServer, TcpSocketConnection, byte[]>(HandleRecMsg),
                            HandleClientClose = HandleClientClose == null ? null : new Action <TcpSocketServer, TcpSocketConnection>(HandleClientClose),
                            HandleSendMsg     = HandleSendMsg == null ? null : new Action <TcpSocketServer, TcpSocketConnection, byte[]>(HandleSendMsg),
                            HandleException   = HandleException == null ? null : new Action <Exception>(HandleException)
                        };
                        newConnection.HandleRecMsg += new Action <TcpSocketServer, TcpSocketConnection, byte[]>((a, b, c) =>
                        {
                            _sendCheckMsg.RecMsg(b.ConnectionId, c);
                        });
                        AddConnection(newConnection);
                        newConnection.StartRecMsg();
                        HandleNewClientConnected?.Invoke(this, newConnection);
                    }
                    catch (Exception ex)
                    {
                        AccessException(ex);
                    }
                }, null);
            }
            catch (Exception ex)
            {
                AccessException(ex);
            }
        }
Пример #2
0
        private void StartListen()
        {
            try
            {
                _socket.BeginAccept(asyncResult =>
                {
                    try
                    {
                        Socket newSocket = _socket.EndAccept(asyncResult);

                        //马上进行下一轮监听,增加吞吐量
                        if (_isListen)
                        {
                            StartListen();
                        }

                        TcpSocketConnection newConnection = new TcpSocketConnection(newSocket, this)
                        {
                            HandleRecMsg      = HandleRecMsg == null ? null : new Action <TcpSocketServer, TcpSocketConnection, byte[]>(HandleRecMsg),
                            HandleClientClose = HandleClientClose == null ? null : new Action <TcpSocketServer, TcpSocketConnection>(HandleClientClose),
                            HandleSendMsg     = HandleSendMsg == null ? null : new Action <TcpSocketServer, TcpSocketConnection, byte[]>(HandleSendMsg),
                            HandleException   = HandleException == null ? null : new Action <Exception>(HandleException)
                        };

                        AddConnection(newConnection);
                        newConnection.StartRecMsg();
                        HandleNewClientConnected?.BeginInvoke(this, newConnection, null, null);
                    }
                    catch (Exception ex)
                    {
                        HandleException?.BeginInvoke(ex, null, null);
                    }
                }, null);
            }
            catch (Exception ex)
            {
                HandleException?.BeginInvoke(ex, null, null);
            }
        }