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); }
public Connection(ConnectionConfig userConfig) { _config = userConfig; _recvBufferSize = userConfig.recvBufferSize; _sendBufferSize = userConfig.sendBufferSize; _socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); _writer = new MySqlStreamWriter(_config.GetEncoding()); //------------------ //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*** _recvSendArgsCompleted = (object sender, SocketAsyncEventArgs e) => { if (_isDisposed) { //it should not occur return; } 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.Completed += _recvSendArgsCompleted; //------------------ _recvSendArgs.AcceptSocket = _socket; _mysqlParserMx = new MySqlParserMx(_config); }
public HttpContext( WebServer ownerServer, int recvBufferSize, int sendBufferSize) { this.EnableWebSocket = true; this.ownerServer = ownerServer; //each recvSendArgs is created for this connection session only *** //---------------------------------------------------------------------------------------------------------- KeepAlive = false; //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 HttpContext( HttpWebServer ownerServer, int recvBufferSize, int sendBufferSize) { this.EnableWebSocket = true; _ownerServer = ownerServer; //each recvSendArgs is created for this connection session only *** //---------------------------------------------------------------------------------------------------------- KeepAlive = false; //set buffer for newly created saArgs _recv_a = new SocketAsyncEventArgs(); _send_a = new SocketAsyncEventArgs(); _recv_a.SetBuffer(new byte[RECV_BUFF_SIZE], 0, RECV_BUFF_SIZE); _send_a.SetBuffer(new byte[RECV_BUFF_SIZE], 0, RECV_BUFF_SIZE); //ownerServer.SetBufferFor(); //ownerServer.SetBufferFor(); _recvIO = new RecvIO(_recv_a, _recv_a.Offset, recvBufferSize, HandleReceive); _sendIO = new SendIO(_send_a, _send_a.Offset, sendBufferSize, HandleSend); //---------------------------------------------------------------------------------------------------------- _httpReq = new HttpRequestImpl(this); _httpReq.SetLargeUploadFilePolicyHandler(_ownerServer.LargeFileUploadPermissionReqHandler); _httpResp = new HttpResponseImpl(this); //common(shared) event listener*** _recv_a.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"); } }; _send_a.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 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; } }); //------------------------------------------------------------------------------------ }
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); }