public PlainWebSocketConn(bool asClient) : base(asClient)
        {
            //-------------------
            //send,resp
            _sockAsyncSender = new SocketAsyncEventArgs();
            _sockAsyncSender.SetBuffer(new byte[RECV_BUFF_SIZE], 0, RECV_BUFF_SIZE);
            _sendIO = new SendIO(_sockAsyncSender, 0, RECV_BUFF_SIZE, HandleSendCompleted);
            _sockAsyncSender.Completed += new EventHandler <SocketAsyncEventArgs>((s, e) =>
            {
                switch (e.LastOperation)
                {
                default:
                    {
                    }
                    break;

                case SocketAsyncOperation.Send:
                    {
                        _sendIO.ProcessWaitingData();
                    }
                    break;

                case SocketAsyncOperation.Receive:
                    {
                    }
                    break;
                }
            });
            _webSocketResp = new WebSocketResponse(_connectionId, asClient, this);

            //------------------------------------------------------------------------------------
            //recv,req ,new socket
            _sockAsyncListener = new SocketAsyncEventArgs();
            _sockAsyncListener.SetBuffer(new byte[RECV_BUFF_SIZE], 0, RECV_BUFF_SIZE);
            _recvIO = new RecvIO(_sockAsyncListener, 0, RECV_BUFF_SIZE, HandleReceivedData);


            RecvIOBufferStream recvIOStream = new RecvIOBufferStream();

            _webSocketReqParser = new WebSocketProtocolParser(this, recvIOStream);
            _webSocketReqParser.SetNewParseResultHandler(req =>
            {
                WebSocketReqInputQueue.Enqueue(new WebSocketReqQueueItem(this, req));
            });
            _sockAsyncListener.Completed += new EventHandler <SocketAsyncEventArgs>((s, e) =>
            {
                switch (e.LastOperation)
                {
                default:
                    {
                    }
                    break;

                case SocketAsyncOperation.Send:
                    {
                    }
                    break;

                case SocketAsyncOperation.Receive:
                    {
                        //copy data and write to recvIO stream

                        int recvByteCount = e.BytesTransferred;
                        if (recvByteCount > 0)
                        {
                            //TODO
                            byte[] tmp1 = new byte[recvByteCount];
                            Buffer.BlockCopy(e.Buffer, 0, tmp1, 0, recvByteCount);
                            recvIOStream.WriteData(tmp1, recvByteCount);
                            _recvIO.ProcessReceivedData();
                        }
                    }
                    break;
                }
            });
            //------------------------------------------------------------------------------------
        }
예제 #2
0
 public SecureWebSocketConn(bool asClient)
     : base(asClient)
 {
     _webSocketResp = new WebSocketResponse(_connectionId, asClient, this);
 }
예제 #3
0
        public WebSocketContext(WebSocketServer webSocketServer)
        {
            this.webSocketServer = webSocketServer;
            connectionId         = System.Threading.Interlocked.Increment(ref connectionIdTotal);
            //-------------------
            //send,resp
            sockAsyncSender = new SocketAsyncEventArgs();
            sockAsyncSender.SetBuffer(new byte[RECV_BUFF_SIZE], 0, RECV_BUFF_SIZE);
            sendIO = new SendIO(sockAsyncSender, 0, RECV_BUFF_SIZE, sendIO_SendCompleted);
            sockAsyncSender.Completed += new EventHandler <SocketAsyncEventArgs>((s, e) =>
            {
                switch (e.LastOperation)
                {
                default:
                    {
                    }
                    break;

                case SocketAsyncOperation.Send:
                    {
                        sendIO.ProcessWaitingData();
                    }
                    break;

                case SocketAsyncOperation.Receive:
                    {
                    }
                    break;
                }
            });
            webSocketResp = new WebSocketResponse(this, sendIO);

            //------------------------------------------------------------------------------------
            //recv,req ,new socket
            sockAsyncListener = new SocketAsyncEventArgs();
            sockAsyncListener.SetBuffer(new byte[RECV_BUFF_SIZE], 0, RECV_BUFF_SIZE);
            recvIO = new RecvIO(sockAsyncListener, 0, RECV_BUFF_SIZE, HandleReceivedData);
            sockAsyncListener.Completed += new EventHandler <SocketAsyncEventArgs>((s, e) =>
            {
                switch (e.LastOperation)
                {
                default:
                    {
                    }
                    break;

                case SocketAsyncOperation.Send:
                    {
                    }
                    break;

                case SocketAsyncOperation.Receive:
                    {
                        recvIO.ProcessReceivedData();
                    }
                    break;
                }
            });
            //------------------------------------------------------------------------------------
            this.webSocketReqParser = new WebSocketProtocolParser(this, recvIO);
        }