void CreateContextPool(int maxNumberOfConnnections) { int recvSize = 1024; int sendSize = 1024; bufferMan = new BufferManager((recvSize + sendSize) * maxNumberOfConnnections, (recvSize + sendSize)); //Allocate memory for buffers. We are using a separate buffer space for //receive and send, instead of sharing the buffer space, like the Microsoft //example does. this.contextPool = new SharedResoucePool<HttpContext>(maxNumberOfConnnections); //------------------------------------------------------------------ //It is NOT mandatory that you preallocate them or reuse them. But, but it is //done this way to illustrate how the API can // easily be used to create ***reusable*** objects to increase server performance. //------------------------------------------------------------------ //connection session: socket async = 1:1 for (int i = maxNumberOfConnnections - 1; i >= 0; --i) { var context = new HttpContext(this, recvSize, sendSize); context.BindReqHandler(this.reqHandler); //client handler this.contextPool.Push(context); } }
internal void ReleaseChildConn(HttpContext httpConn) { if (httpConn != null) { httpConn.Reset(); this.contextPool.Push(httpConn); newConnListener.NotifyFreeAcceptQuota(); } }
internal HttpRequest(HttpContext context) { this.context = context; bodyMs = new MemoryStream(); }
internal bool CheckWebSocketUpgradeRequest(HttpContext httpConn) { if (webSocketServer == null) { return false; } HttpRequest httpReq = httpConn.HttpReq; HttpResponse httpResp = httpConn.HttpResp; string upgradeKey = httpReq.GetHeaderKey("Upgrade"); if (upgradeKey != null && upgradeKey == "websocket") { string sec_websocket_key = httpReq.GetHeaderKey("Sec-WebSocket-Key"); Socket clientSocket = httpConn.RemoteSocket; httpConn.UnBindSocket(false);//unbind but not close client socket webSocketServer.RegisterNewWebSocket(clientSocket, sec_websocket_key);//the bind client to websocket server return true; } return false; }
internal HttpResponse(HttpContext context, SendIO sendIO) { this.context = context; bodyMs = new MemoryStream(); StatusCode = 200; //init this.sendIO = sendIO; this.ContentTypeCharSet = WebServers.TextCharSet.Utf8; }