Ejemplo n.º 1
0
        private void InitServer()
        {
            _tcpSocketServer.HandleServerStarted = theServer =>
            {
                HandleServerStarted?.Invoke(this);
            };

            _tcpSocketServer.HandleException = ex =>
            {
                HandleException?.Invoke(ex);
            };

            _tcpSocketServer.HandleClientClose = (theServer, theCon) =>
            {
                var webCon = GetConnection(theCon.ConnectionId);
                CloseConnection(webCon);
                HandleClientClose?.Invoke(this, webCon);
            };

            _tcpSocketServer.HandleNewClientConnected = (theServer, theCon) =>
            {
                WebSocketConnection newCon = new WebSocketConnection(this, theCon)
                {
                    HandleClientClose = HandleClientClose == null ? null : new Action <WebSocketServer, WebSocketConnection>(HandleClientClose),
                    HandleSendMsg     = HandleSendMsg == null ? null : new Action <WebSocketServer, WebSocketConnection, string>(HandleSendMsg)
                };

                AddConnection(newCon);

                HandleNewClientConnected?.Invoke(this, newCon);
            };

            _tcpSocketServer.HandleRecMsg = (thServer, theCon, bytes) =>
            {
                string recStr = bytes.ToString(Encoding.UTF8);
                if (IsHandshake(recStr))
                {
                    string res = GetWebSocketResponse(recStr);

                    theCon.Send(res);
                }
                else
                {
                    int opcode = new string(bytes[0].ToBinString().Copy(4, 4).ToArray()).ToInt_FromBinString();

                    //为关闭连接
                    if (opcode == 8)
                    {
                        GetConnection(theCon.ConnectionId).Close();
                    }
                    else
                    {
                        string recData = AnalyticData(bytes);
                        HandleRecMsg?.Invoke(this, GetConnection(theCon.ConnectionId), recData);
                    }
                }
            };
        }
Ejemplo n.º 2
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);
            }
        }
Ejemplo n.º 3
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);
            }
        }
Ejemplo n.º 4
0
        private void StartListen()
        {
            try
            {
                _socket.BeginAccept(asyncResult =>
                {
                    try
                    {
                        Socket newSocket = _socket.EndAccept(asyncResult);
                        if (_isListen)
                        {
                            StartListen();
                        }

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

                        newClient.StartRecMsg();
                        ClientList.AddLast(newClient);

                        HandleNewClientConnected?.Invoke(this, newClient);
                    }
                    catch (Exception ex)
                    {
                        HandleException?.Invoke(ex);
                    }
                }, null);
            }
            catch (Exception ex)
            {
                HandleException?.Invoke(ex);
            }
        }