Esempio n. 1
0
 private static void OnDisconnectedClient(HttpWebSocket client)
 {
     try
     {
         ClientDisconnectedHandler handler;
         if (null != (handler = (ClientDisconnectedHandler)clientDisconnected))
         {
             handler(client);
         }
     }
     catch
     {
     }
 }
Esempio n. 2
0
 private static void OnNewClient(HttpWebSocket client)
 {
     try
     {
         ConnectedClientHandler handler;
         if (null != (handler = (ConnectedClientHandler)clientConnected))
         {
             handler(client);
         }
     }
     catch
     {
         client.Open = false;
     }
 }
Esempio n. 3
0
        private void Start(List <Assembly> assemblies)
        {
            if (this.root != null && !Directory.Exists(this.root))
            {
                throw new DirectoryNotFoundException("root folder not found");
            }

            this.Connections = new List <HttpConnection>();
            this.Sessions    = new Dictionary <string, HttpSession>();

            this.Cache = new HttpCache(this);
            HttpController.LoadControllers(assemblies);
            HttpWebSocket.LoadControllers(assemblies);

            this.connectionWaitHandle = new AutoResetEvent(false);

            this.listenerLoopThread = new Thread(this.ListenerLoop)
            {
                Name         = "HttpServer Listener",
                IsBackground = true
            };
            this.listenerLoopThread.Start();
        }
Esempio n. 4
0
        public HttpResponse(HttpRequest request)
        {
            this.request = request;

            try
            {
                if (this.request.Path == @"")
                {
                    if (HttpController.Primary == null)
                    {
                        throw new ApplicationException("Primary Controller Not Defined");
                    }
                    if (HttpController.Primary.PrimaryAction == null)
                    {
                        throw new ApplicationException("Primary Action Not Defined");
                    }

                    this.content = HttpController.RequestPrimary(this.request);
                }
                else if (this.request.IsWebSocket && HttpWebSocket.WebSocketControllerExists(this.request))
                {
                    var type = HttpWebSocket.GetWebSocketController(this.request);
                    this.request.WebSocket = (HttpWebSocket)Activator.CreateInstance(type, this.request);
                }
                else if (HttpController.ActionExists(this.request))
                {
                    this.content = HttpController.RequestAction(this.request);
                }
                else
                {
                    this.content = HttpContent.Read(this.request.Server, this.request.Path);
                }

                if (this.content is HttpExceptionContent)
                {
                    this.bytes = this.ConstructResponse(HttpStatusCode.SERVERERROR);
                }
                else if (this.request.IsWebSocket)
                {
                    this.bytes = this.ConstructResponse(HttpStatusCode.SWITCHING_PROTOCOLS);
                }
                else if (this.content.ETag == this.request.ETag && !this.content.ParsingRequired)
                {
                    this.bytes = this.ConstructResponse(HttpStatusCode.NOT_MODIFIED);
                }
                else
                {
                    this.bytes = this.ConstructResponse(HttpStatusCode.OK);
                }
            }
            catch (FileNotFoundException ex)
            {
                this.content = new HttpExceptionContent(ex);
                this.bytes   = this.ConstructResponse(HttpStatusCode.SERVERERROR);
            }
            catch (Exception ex)
            {
                this.content = new HttpExceptionContent(ex);
                this.bytes   = this.ConstructResponse(HttpStatusCode.SERVERERROR);
            }
        }