private void AcceptCallback(IAsyncResult ar) { // Signal the main thread to continue. allDone.Set(); // Get the socket that handles the client request. Socket listener = (Socket)ar.AsyncState; Socket handler = listener.EndAccept(ar); // Create the state object. SocketStateObject state = new SocketStateObject(); state.WorkSocket = handler; handler.BeginReceive(state.Buffer, 0, state.BufferSize, 0, new AsyncCallback(ReadCallback), state); //todo use ReceiveAsync //fenergo only handler.BeginReceive(state.Buffer, 0, state.BufferSize, 0, new AsyncCallback(ReadCallback), state); //todo use ReceiveAsync }
private void ReadCallback(IAsyncResult ar) { // Retrieve the state object and the handler socket // from the asynchronous state object. SocketStateObject state = (SocketStateObject)ar.AsyncState; Socket handler = state.WorkSocket; // Read data from the client socket. int bytesRead = handler.EndReceive(ar); if (bytesRead > 0) { // There might be more data, so store the data received so far. string content = Encoding.ASCII.GetString(state.Buffer, 0, bytesRead); OnNewRequest?.Invoke(content); handler.BeginReceive(state.Buffer, 0, state.BufferSize, 0, new AsyncCallback(ReadCallback), state); //todo use ReceiveAsync return; string response = @"HTTP/1.1 200 OK Date: Mon, 27 Jul 2009 12:28:53 GMT Server: Apache / 2.2.14(Win32) Last - Modified: Wed, 22 Jul 2009 19:15:56 GMT Content - Length: 0 Connection: Closed " ; if (!String.IsNullOrEmpty(RedirectUrl)) { response = GetResponseFromRedirect(content); } SendReply(handler, response); } }