예제 #1
0
파일: NetworkMgr.cs 프로젝트: niuniuzhu/KOW
        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);
            }
        }
예제 #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);
        }
예제 #3
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;
            }
            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);
        }
예제 #4
0
 public void Push(NetEvent netEvent)
 {
     this._queue.Push(netEvent);
 }
예제 #5
0
 public void Push(NetEvent netEvent)
 {
     this._pool.Enqueue(netEvent);
 }
예제 #6
0
파일: NetworkMgr.cs 프로젝트: niuniuzhu/KOW
 public void PushEvent(NetEvent netEvent) => this._eventQueue.Push(netEvent);
예제 #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);
                    }
                }
            }
        }