Exemplo n.º 1
0
 internal void innerAddClient(PowerLineWebsocketClient client)
 {
     lock (this.websocketClientLock)
     {
         this.websocketClients.Add(client);
     }
 }
Exemplo n.º 2
0
        public PowerLineContext(JObject message, Stream requestPayload, int pathIndex, string[] path, PowerLineWebsocketClient client)
        {
            this.RemoteAddress = client.RemoteAddress;
            this.RemotePort    = client.RemotePort;
            this.LocalPort     = client.LocalPort;
            this.RemoteAddress = client.RemoteAddress;

            this.WebsocketClient     = client;
            this.IsWebSocket         = true;
            this.context             = null;
            this.request             = null;
            this.response            = null;
            this.PathIndex           = pathIndex;
            this.Path                = path;
            this._requestJsonPayload = message;

            if (message.TryGetValue("method", out string requestMethod))
            {
                this.RequestMethod = requestMethod;
            }
            else
            {
                throw new Exception("Invaild websocket request");
            }

            this.RequestHeaders  = message.ReadHeaders();
            this.ResponseHeader  = new Dictionary <string, string>();
            this.RequestPayload  = requestPayload;
            this.ResponsePayload = null;
        }
Exemplo n.º 3
0
 internal bool innerRemoveClient(PowerLineWebsocketClient client)
 {
     this.innerUnsbscribeAllEvents(client);
     lock (this.websocketClientLock)
     {
         return(this.websocketClients.Remove(client));
     }
 }
Exemplo n.º 4
0
 internal bool innerSubscribeEvent(PowerLineWebsocketClient client, PowerLineEvent powerLineEvent)
 {
     if (powerLineEvent.SubscribeWithCheck(client))
     {
         client.SubscribeEvent(powerLineEvent);
         return(true);
     }
     else
     {
         return(false);
     }
 }
Exemplo n.º 5
0
 internal bool UnsubscribeClient(PowerLineWebsocketClient websocket)
 {
     this.clientsLock.WaitAsync();
     try
     {
         return(this.clients.Remove(websocket));
     }
     finally
     {
         this.clientsLock.Release();
     }
 }
Exemplo n.º 6
0
 internal void SubscribeClient(PowerLineWebsocketClient websocket)
 {
     this.clientsLock.WaitAsync();
     try
     {
         this.clients.Add(websocket);
     }
     finally
     {
         this.clientsLock.Release();
     }
 }
Exemplo n.º 7
0
 internal void innerUnsbscribeAllEvents(PowerLineWebsocketClient client)
 {
     lock (client.eventsLock)
     {
         foreach (KeyValuePair <string, PowerLineEvent> singleEvent in client.events)
         {
             lock (singleEvent.Value.clientsLock)
             {
                 singleEvent.Value.clients.Remove(client);
             }
         }
         client.events.Clear();
     }
 }
Exemplo n.º 8
0
        internal bool SubscribeWithCheck(PowerLineWebsocketClient client)
        {
            PowerLineSubscriptionEventArgs args = new PowerLineSubscriptionEventArgs(client);

            Subscription?.Invoke(this, args);
            if (args.cancel)
            {
                return(false);
            }
            else
            {
                this.SubscribeClient(client);
                return(true);
            }
        }
Exemplo n.º 9
0
        internal async Task HandleWebsocketMessageAsync(PowerLineWebsocketClient client, Stream message)
        {
            JObject mainMessage;

            using (StreamReader reader = new StreamReader(message))
            {
                mainMessage = JObject.Parse(reader.ReadToEnd());
            }

            if (!mainMessage.TryGetValue("url", out string requestUrl))
            {
                throw new Exception("Invaild request url");
            }
            if (!mainMessage.TryGetValue("websocketId", out int websocketId))
            {
                throw new Exception("Invaild request url");
            }
            string[]         UrlPath = requestUrl.Split('/');
            PowerLineContext context = new PowerLineContext(mainMessage, message, 0, UrlPath, client);

            PowerLineEndPointExecutionResult result = await GetHandleResultAsync(context);

            switch (result.ResultType)
            {
            case PowerLinExecutionResultType.EndPointNotFound:
                result.Context.SetResponse(404);
                result.Context.SetResponseHttpString("Not Found");
                break;

            case PowerLinExecutionResultType.HandlerException:
                result.Context.SetResponse(500);
                result.Context.SetResponseHttpString(result.Exception.Message);
                break;

            case PowerLinExecutionResultType.HttpMethodNotFound:
                result.Context.SetResponse(404);
                result.Context.SetResponseHttpString("Not Found [HttpMethod]");
                break;
            }

            await client.SendResponseAsync(context, websocketId);
        }
 public PowerLineSubscriptionEventArgs(PowerLineWebsocketClient client)
 {
     this.Client = client;
     this.cancel = false;
 }
Exemplo n.º 11
0
 internal void innerUnsbscribeEvent(PowerLineWebsocketClient client, PowerLineEvent powerLineEvent)
 {
     client.UnsubscribeEvent(powerLineEvent);
     powerLineEvent.UnsubscribeClient(client);
 }
Exemplo n.º 12
0
 private void handleAsyncWebsocket(HttpListenerContext context)
 {
     // just create a client, he will manage eveyrthing else (like adding to the server client list)
     PowerLineWebsocketClient client = new PowerLineWebsocketClient(this, context, this.cancelToken);
 }