private void AcceptCallback(IAsyncResult ar) { // Signal the main thread to continue. allDone.Set(); // Get the socket that handles the client request. var listener = (Socket)ar.AsyncState; if (cancellationRequested) { return; } Socket socket = listener.EndAccept(ar); AddSocket(socket); // Create the state object. var xc = new SocketTransceiver(socket); while (true) { try { IList <MemoryStream> request = xc.ReadBuffers(); IList <MemoryStream> response = responder.Respond(request, xc); xc.WriteBuffers(response); } catch (ObjectDisposedException) { break; } catch (SocketException) { break; } catch (AvroRuntimeException) { break; } catch (Exception) { break; } } try { xc.Disconnect(); } catch (Exception) { } RemoveSocket(socket); }
//TODO: apparently this doesn't compile in Mono - investigate //public Action<Exception, IAsyncResult> ExceptionHandler { get; set; } protected void HttpListenerCallback(IAsyncResult result) { try { HttpListener listener = (HttpListener)result.AsyncState; if (_listener != listener) //the server which began this callback was stopped - just exit { return; } HttpListenerContext context = listener.EndGetContext(result); listener.BeginGetContext(HttpListenerCallback, listener); //spawn listening for next request so it can be processed while we are dealing with this one //process this request if (!context.Request.HttpMethod.Equals("POST")) { throw new AvroRuntimeException("HTTP method must be POST"); } if (!context.Request.ContentType.Equals("avro/binary")) { throw new AvroRuntimeException("Content-type must be avro/binary"); } byte[] intBuffer = new byte[4]; var buffers = HttpTransceiver.ReadBuffers(context.Request.InputStream, intBuffer); buffers = _responder.Respond(buffers); context.Response.ContentType = "avro/binary"; context.Response.ContentLength64 = HttpTransceiver.CalculateLength(buffers); HttpTransceiver.WriteBuffers(buffers, context.Response.OutputStream); context.Response.OutputStream.Close(); context.Response.Close(); } catch (Exception ex) { //TODO: apparently this doesn't compile in Mono - investigate //if (ExceptionHandler != null) // ExceptionHandler(ex, result); //else // Debug.Print("Exception occured while processing a request, no exception handler was provided - ignoring", ex); Debug.Print("Exception occured while processing a web request, skipping this request: ", ex); } }
public override IList <MemoryStream> Transceive(IList <MemoryStream> request) { return(responder.Respond(request)); }