Ejemplo n.º 1
0
        // an event from the listeners has happened which is HTTP, process it..

        private void ProcessHTTP(HttpListenerContext ctx)
        {
            try
            {
                ServerLog?.Invoke(ctx.Request.RequestInfo());

                NodeResponse res = httpresponder.Item1(ctx.Request, httpresponder.Item2);      // get response from method.  Always responds with data
                ctx.Response.ContentType     = res.ContentType;
                ctx.Response.ContentLength64 = res.Data.Length;
                if (res.Headers != null)
                {
                    ctx.Response.Headers.Add(res.Headers);
                }

#if false
                var x = ctx.Response.Headers.AllKeys;

                WebHeaderCollection headers = ctx.Response.Headers;
                foreach (string key in headers.AllKeys)
                {
                    string[] values = headers.GetValues(key);
                    if (values.Length > 0)
                    {
                        System.Diagnostics.Debug.WriteLine("The values of the " + key + " header are: ");
                        foreach (string value in values)
                        {
                            System.Diagnostics.Debug.WriteLine("   " + value);
                        }
                    }
                    else
                    {
                        System.Diagnostics.Debug.WriteLine("There is no value associated with the header.");
                    }
                }
#endif
                ctx.Response.OutputStream.Write(res.Data, 0, res.Data.Length);
            }
            catch (Exception e)
            {   // suppress any exceptions
                System.Diagnostics.Debug.WriteLine("Process HTTP exception " + e);
            }
            finally
            {
                // always close response - this sends the response back to the client
                ctx.Response.Close();
            }
        }
Ejemplo n.º 2
0
        // an event from the listeners has happened which is HTTP, process it..

        private void ProcessHTTP(HttpListenerContext ctx)
        {
            try
            {
                ServerLog?.Invoke(ctx.Request.RequestInfo());

                byte[] buf = httpresponder.Item1(ctx.Request, httpresponder.Item2);      // get response from method.  Always responds with data
                ctx.Response.ContentLength64 = buf.Length;
                ctx.Response.OutputStream.Write(buf, 0, buf.Length);
            }
            catch (Exception e)
            {   // suppress any exceptions
                System.Diagnostics.Debug.WriteLine("Process HTTP exception " + e);
            }
            finally
            {
                // always close response - this sends the response back to the client
                ctx.Response.Close();
            }
        }
Ejemplo n.º 3
0
        public override void Log(IServer server, ServerLogEventArgs e)
        {
            var httpLog = e as HttpServerLogEventArgs;

            CacheLog(e);
            ServerLog?.Invoke(server, e);
            if (Options.LogToConsole && (httpLog == null || httpLog.OutputConsole))
            {
                base.Log(server, e);
            }
            if (Options.WriteLog && (httpLog == null || httpLog.OutputFile))
            {
                mFileLog.Add(e.Type, e.Message);
            }
            ISession output = LogOutput;

            if (output != null && e.Session != output)
            {
                ActionResult log = new ActionResult();
                log.Data = new { LogType = e.Type.ToString(), Time = DateTime.Now.ToString("H:mm:ss"), Message = e.Message };
                CreateDataFrame(log).Send(output);
            }
        }
Ejemplo n.º 4
0
 protected virtual void OnServerLog(ServerStatusEventArgs e)
 {
     ServerLog?.Invoke(this, e);
 }
Ejemplo n.º 5
0
 /// <summary>
 ///		lanza el evento de recepción
 /// </summary>
 internal void RaiseEventReceived(string strFileName)
 {
     ServerLog?.Invoke(this, new ServerEvents.SmtpServerLogEventArgs("Received", strFileName));
 }
Ejemplo n.º 6
0
 /// <summary>
 ///		Lanza un evento de log
 /// </summary>
 internal void RaiseEventLog(string strAction, string strMessage)
 {
     ServerLog?.Invoke(this, new ServerEvents.SmtpServerLogEventArgs(strAction, strMessage));
 }
Ejemplo n.º 7
0
 internal void HandleServerLog(object sender, ServerLoggerEventArgs args)
 {
     ServerLog?.Invoke(sender, args);
 }
Ejemplo n.º 8
0
        // a web socket is attaching
        private async void ProcessWebSocket(HttpListenerContext ctx, string protocol, Tuple <WebSocketsResponderFunc, Object> responder, CancellationToken canceltk)
        {
            WebSocketContext wsctx;

            try
            {
                System.Diagnostics.Debug.WriteLine("WEBSOCKET Accepting on " + prefixesString + " protocol " + protocol);
                wsctx = await ctx.AcceptWebSocketAsync(protocol);
            }
            catch (Exception e)
            {
                // The upgrade process failed somehow. For simplicity lets assume it was a failure on the part of the server and indicate this using 500.
                ctx.Response.StatusCode = 500;
                ctx.Response.Close();
                System.Diagnostics.Debug.WriteLine("Exception: {0}", e);
                return;
            }

            ServerLog?.Invoke("WEBSOCKET accepted " + prefixesString);
            System.Diagnostics.Debug.WriteLine("WEBSOCKET accepted " + prefixesString);

            WebSocket webSocket = wsctx.WebSocket;

            lock (webSockets)
                webSockets.Add(webSocket);       // add to the pool of web sockets to throw data at.


            try
            {
                byte[] receiveBuffer = new byte[WebSocketMsgBufferSize];

                while (webSocket.State == WebSocketState.Open)
                {
                    WebSocketReceiveResult receiveResult = await webSocket.ReceiveAsync(new ArraySegment <byte>(receiveBuffer), canceltk);

                    ServerLog?.Invoke("WEBSOCKET request " + prefixesString + ": " + receiveResult.MessageType);
                    System.Diagnostics.Debug.WriteLine("WEBSOCKET request " + prefixesString + " type " + receiveResult.MessageType + " len " + receiveResult.Count);

                    if (receiveResult.MessageType == WebSocketMessageType.Close)
                    {
                        System.Diagnostics.Debug.WriteLine("WEBSOCKET close req " + prefixesString);
                        webSocket.CloseAsync(WebSocketCloseStatus.NormalClosure, "", CancellationToken.None).Wait();        // here we block until complete
                    }
                    else
                    {
                        responder.Item1(ctx.Request, webSocket, receiveResult, receiveBuffer, responder.Item2);
                    }
                }

                System.Diagnostics.Debug.WriteLine("WEBSOCKET closed on " + prefixesString);
            }
            catch (System.Threading.Tasks.TaskCanceledException)
            {
                // normal task canceled exception
            }
            catch (Exception e)
            {
                // Just log any exceptions to the console. Pretty much any exception that occurs when calling `SendAsync`/`ReceiveAsync`/`CloseAsync` is unrecoverable in that it will abort the connection and leave the `WebSocket` instance in an unusable state.
                System.Diagnostics.Debug.WriteLine("Exception: {0}", e);
            }
            finally
            {
                if (webSocket != null)      // Clean up by disposing the WebSocket once it is closed/aborted.
                {
                    lock (webSockets)
                        webSockets.Remove(webSocket);
                    webSocket.Dispose();
                }
            }

            System.Diagnostics.Debug.WriteLine("WEBSOCKET terminate " + prefixesString);
        }