Beispiel #1
0
        private void FireEvents()
        {
            this._eventQueue.Switch();
            while (!this._eventQueue.isEmpty)
            {
                NetEvent    netEvent = this._eventQueue.Pop();
                INetSession session  = netEvent.session;

                if (netEvent.type == NetEvent.Type.Establish)
                {
                    session._OnEstablish();
                }
                else
                {
                    if (!this._sessionsToRemove.Contains(session) &&
                        this._idToSession.ContainsKey(session.id))                             //可能在处理时间前session已关闭
                    {
                        switch (netEvent.type)
                        {
                        case NetEvent.Type.ConnErr:
                            session._OnConnError(netEvent.error);
                            break;

                        case NetEvent.Type.Error:
                            session._OnError(netEvent.error);
                            break;

                        case NetEvent.Type.Recv:
                            session._OnRecv(netEvent.data, 0, netEvent.data.Length);
                            break;

                        case NetEvent.Type.Send:
                            session._OnSend();
                            break;
                        }
                    }
                }
                this._eventPool.Push(netEvent);
            }
        }
Beispiel #2
0
        private void ProcessConnect(SocketAsyncEventArgs connectEventArgs)
        {
            if (connectEventArgs.SocketError != SocketError.Success)
            {
                this.OnError($"socket connect error, address:{this._ip}:{this._port}, code:{connectEventArgs.SocketError}");
                this.Close();
                return;
            }

            TCPConnection tcpConnection = ( TCPConnection )this.session.connection;

            tcpConnection.socket         = new SocketWrapper(this.socket);
            tcpConnection.remoteEndPoint = this.socket.RemoteEndPoint;
            tcpConnection.recvBufSize    = this.recvBufSize;
            tcpConnection.StartReceive();
            this.socket = null;

            NetEvent netEvent = NetworkMgr.instance.PopEvent();

            netEvent.type    = NetEvent.Type.Establish;
            netEvent.session = this.session;
            NetworkMgr.instance.PushEvent(netEvent);
        }
        private void ProcessConnect(SocketAsyncEventArgs connectEventArgs)
        {
            if (connectEventArgs.SocketError != SocketError.Success)
            {
                this.OnError($"socket connect error, address:{this._ip}:{this._port}, code:{connectEventArgs.SocketError}");
                this.Close();
                return;
            }
            this.session.connection.socket              = this.socket;
            this.session.connection.localEndPoint       = this.socket.LocalEndPoint;
            this.session.connection.remoteEndPoint      = this.socket.RemoteEndPoint;
            this.session.connection.recvBufSize         = this.recvBufSize;
            this.session.connection.packetEncodeHandler = this.packetEncodeHandler;
            this.session.connection.packetDecodeHandler = this.packetDecodeHandler;
            this.session.connection.StartReceive();
            this.socket = null;

            NetEvent netEvent = NetEventMgr.instance.pool.Pop();

            netEvent.type    = NetEvent.Type.Establish;
            netEvent.session = this.session;
            NetEventMgr.instance.Push(netEvent);
        }
Beispiel #4
0
 public void Push(NetEvent netEvent)
 {
     this._queue.Push(netEvent);
 }
Beispiel #5
0
 public void Push(NetEvent netEvent)
 {
     this._pool.Enqueue(netEvent);
 }
Beispiel #6
0
 public void PushEvent(NetEvent netEvent) => this._eventQueue.Push(netEvent);
Beispiel #7
0
        protected override void ProcessData(StreamBuffer cache)
        {
            while (true)
            {
                if (cache.length == 0)
                {
                    break;
                }

                if (!this._handshakeComplete)
                {
                    WSHttpRequest request = WSHelper.ProcessHandShakeData(cache.GetBuffer(), 0, cache.length);
                    if (request == null)
                    {
                        break;
                    }

                    //Logger.Log( request );
                    string subProtocol  = WSHelper.Negotiate(this.subProtocols, request.subProtocols);
                    byte[] responseData = WSHelper.ProcessHybi13Handshake(request, subProtocol);
                    if (responseData == null)
                    {
                        break;
                    }

                    this._handshakeComplete = true;
                    this.SendWithoutHeader(responseData, 0, responseData.Length);

                    cache.Clear();

                    NetEvent netEvent = NetworkMgr.instance.PopEvent();
                    netEvent.type    = NetEvent.Type.Establish;
                    netEvent.session = this.session;
                    NetworkMgr.instance.PushEvent(netEvent);

                    break;
                }
                {
                    bool isEof = WSHelper.ProcessClientData(cache.GetBuffer(), 0, cache.length, this._readState, out int len, out WSOPCode op);
                    if (len < 0)  //载体没有读取完
                    {
                        break;
                    }

                    //截断当前缓冲区
                    cache.Strip(len, cache.length - len);
                    if (isEof)  //分片已结束
                    {
                        if (op == WSOPCode.Close)
                        {
                            //到这里代表连接关闭了
                            this._readState.Clear();
                            this.OnError("client closed");
                            break;
                        }

                        byte[] data = this._readState.ToArray();
                        this._readState.Clear();

                        NetEvent netEvent = NetworkMgr.instance.PopEvent();
                        netEvent.type    = NetEvent.Type.Recv;
                        netEvent.session = this.session;
                        netEvent.data    = data;
                        NetworkMgr.instance.PushEvent(netEvent);
                    }
                }
            }
        }