public HttpContext(
             WebServer ownerServer,
            int recvBufferSize,
            int sendBufferSize)
        {
            this.EnableWebSocket = true;
            this.ownerServer = ownerServer;
            //each recvSendArgs is created for this connection session only ***
            //---------------------------------------------------------------------------------------------------------- 
            KeepAlive = true;
            //set buffer for newly created saArgs
            ownerServer.SetBufferFor(this.recvSendArgs = new SocketAsyncEventArgs());
            recvIO = new RecvIO(recvSendArgs, recvSendArgs.Offset, recvBufferSize, HandleReceive);
            sendIO = new SendIO(recvSendArgs, recvSendArgs.Offset + recvBufferSize, sendBufferSize, HandleSend);
            //----------------------------------------------------------------------------------------------------------  
            httpReq = new HttpRequest(this);
            httpResp = new HttpResponse(this, sendIO);

            //common(shared) event listener***
            recvSendArgs.Completed += (object sender, SocketAsyncEventArgs e) =>
            {
                switch (e.LastOperation)
                {
                    case SocketAsyncOperation.Receive:                         
                        recvIO.ProcessReceivedData();
                        break;
                    case SocketAsyncOperation.Send:
                        sendIO.ProcessWaitingData();
                        break;
                    default:
                        throw new ArgumentException("The last operation completed on the socket was not a receive or send");
                }
            };
        }
        public WebSocketContext(WebSocketServer webSocketServer, ReqRespHandler<WebSocketRequest, WebSocketResponse> webSocketReqHandler)
        {
            this.webSocketReqHandler = webSocketReqHandler;
            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(recvIO);

        }
 public RecvIOBufferStream(RecvIO recvIO)
 {
     this._latestRecvIO = recvIO;
     AutoClearPrevBufferBlock = true;
 }
        Opcode currentOpCode = Opcode.Cont;//use default 
        //-----------------------

        internal WebSocketProtocolParser(RecvIO recvIO)
        {
            this.recvIO = recvIO;
            myBufferStream = new RecvIOBufferStream(recvIO);

        }
        public Connection(ConnectionConfig userConfig)
        {
            config = userConfig;
            recvBufferSize = userConfig.recvBufferSize;
            sendBufferSize = userConfig.sendBufferSize;
            socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            //
            switch ((CharSets)config.charsetNumber)
            {
                case CharSets.UTF8_GENERAL_CI:
                    //_parser = new PacketParser(Encoding.UTF8);
                    _writer = new MySqlStreamWriter(Encoding.UTF8);
                    break;
                case CharSets.ASCII:
                    //_parser = new PacketParser(Encoding.ASCII);
                    _writer = new MySqlStreamWriter(Encoding.ASCII);
                    break;
                default:
                    throw new NotImplementedException();
            }

            //------------------
            //we share recvSendArgs between recvIO and sendIO
            //similar to simple http 
            //it is simple, (NOT  duplex like web socket)             
            //------------------
            recvSendArgs = new SocketAsyncEventArgs();
            recvSendArgs.SetBuffer(new byte[recvBufferSize + sendBufferSize], 0, recvBufferSize + sendBufferSize);
            recvIO = new RecvIO(recvSendArgs, recvSendArgs.Offset, recvBufferSize, HandleReceive);
            sendIO = new SendIO(recvSendArgs, recvSendArgs.Offset + recvBufferSize, sendBufferSize, HandleSend);
            //------------------
            //common(shared) event listener***
            recvSendArgs.Completed += (object sender, SocketAsyncEventArgs e) =>
            {
                switch (e.LastOperation)
                {
                    case SocketAsyncOperation.Receive:
                        recvIO.ProcessReceivedData();
                        break;
                    case SocketAsyncOperation.Send:
                        sendIO.ProcessWaitingData();
                        break;
                    default:
                        throw new ArgumentException("The last operation completed on the socket was not a receive or send");
                }
            };
            //------------------
            recvSendArgs.AcceptSocket = socket;
            _mysqlParserMx = new MySqlParserMx(config);

        }
 /// <summary>
 /// return true if not complete
 /// </summary>
 /// <param name="recvIO"></param>
 /// <returns></returns>
 public bool ParseData(RecvIO recvIO)
 {
     //we need to parse some data here 
     //load incomming data into ms 
     //load data from recv buffer into the ms
     //---------------
     //copy all to stream 
     //may not complete in first round ***  
     _mysqlStreamReader.AppendBuffer(recvIO, recvIO.BytesTransferred);
     currentPacketParser.Parse(_mysqlStreamReader);
     //-----------------------------------------------
     //some large table may not complete in first round
     ParseResult = currentPacketParser.ParseResult;
     return !(_isCompleted = !currentPacketParser.NeedMoreData);
     //--------------------
     //not need to wait here
     //just return *** 
     //--------------------
 }
 public RecvIOBufferStream(RecvIO recvIO)
 {
     this._latestRecvIO       = recvIO;
     AutoClearPrevBufferBlock = true;
 }