Пример #1
0
 internal async Task <PowerLineEndPointExecutionResult> OnRequestAsync(int index, string[] requestPath, PowerLineContext context)
 {
     if (index >= requestPath.Length)
     {
         return(await this.OnSelfRequestAsync(index, requestPath, context));
     }
     else
     {
         if (this.childEndPoints.TryGetValue(requestPath[index], out PowerLineEndPoint endpoint))
         {
             return(await endpoint.OnRequestAsync(index + 1, requestPath, context));
         }
         else if (this.Dynamic)
         {
             return(await this.OnSelfRequestAsync(index, requestPath, context));
         }
         else
         {
             PowerLineEndPoint dynamicChildEndPoint = this.GetDyanmicEndpoint();
             if (dynamicChildEndPoint != null)
             {
                 return(await dynamicChildEndPoint.OnRequestAsync(index + 1, requestPath, context));
             }
             return(new PowerLineEndPointExecutionResult(context, PowerLinExecutionResultType.EndPointNotFound, this));
         }
     }
 }
Пример #2
0
 public PowerLineEndPoint RemoveEndpoint(PowerLineEndPoint endpoint)
 {
     lock (this.endpointsLock)
     {
         this.endPoints.Remove(endpoint.EndPointName);
         return(endpoint);
     }
 }
 public PowerLineEndPointExecutionResult(PowerLinExecutionResultType resultType, PowerLineContext context, Exception exception, PowerLineEndPoint endpoint, PowerLineHandler handler)
 {
     this.ResultType = resultType;
     this.Context    = context;
     this.Exception  = exception;
     this.EndPoint   = endpoint;
     this.Handler    = handler;
 }
Пример #4
0
 private bool GetEndPoint(string name, out PowerLineEndPoint endpoint)
 {
     lock (this.endpointsLock)
     {
         if (this.endPoints.TryGetValue(name, out endpoint))
         {
             return(true);
         }
         else
         {
             return(false);
         }
     }
 }
Пример #5
0
 public PowerLineEndPoint AddEndpoint(PowerLineEndPoint endpoint)
 {
     lock (this.endpointsLock)
     {
         if (this.endPoints.ContainsKey(endpoint.EndPointName))
         {
             this.endPoints[endpoint.EndPointName] = endpoint;
         }
         else
         {
             this.endPoints.Add(endpoint.EndPointName, endpoint);
         }
         return(endpoint);
     }
 }
Пример #6
0
 private async Task <PowerLineEndPointExecutionResult> GetHandleResultAsync(PowerLineContext context)
 {
     context.ResponseHeader["Server"] = "PowerLine powered by ";
     if (this.GetEndPoint(context.Path[1], out PowerLineEndPoint endpoint))
     {
         return(await endpoint.OnRequestAsync(2, context.Path, context));
     }
     else
     {
         PowerLineEndPoint dyanmicEndPoint = this.GetDynamicEndpoint(context.ReqeustUri);
         if (dyanmicEndPoint != null)
         {
             return(await dyanmicEndPoint.OnRequestAsync(2, context.Path, context));
         }
         return(new PowerLineEndPointExecutionResult(context, PowerLinExecutionResultType.EndPointNotFound, null));
     }
 }
Пример #7
0
        public PowerLineServer(IPAddress bindAddress, int bindPort, bool allowEvents = false, IEnumerable <PowerLineEndPoint> endpoints = null)
        {
            this.BindAddress         = bindAddress;
            this.BindPort            = bindPort;
            this.endpointsLock       = new object();
            this.upnpEngine          = new Upnp.UpnpEngine(this);
            this.upnpEndpoint        = Upnp.PowerLineUpnpHttpHandle.GetEndpoint(this.upnpEngine);
            this.websocketEventLock  = new object();
            this.websocketEvents     = new Dictionary <string, PowerLineEvent>();
            this.websocketClientLock = new object();
            this.websocketClients    = new List <PowerLineWebsocketClient>();
            this.serverStopped       = new EventWaitHandle(true, EventResetMode.ManualReset);

            this.mainListener = new HttpListener();
            this.mainListener.Prefixes.Add(this.BuildBindUrl());

            this.endPoints = (endpoints == null) ? new Dictionary <string, PowerLineEndPoint>() : new Dictionary <string, PowerLineEndPoint>(endpoints.Select((item) => new KeyValuePair <string, PowerLineEndPoint>(item.EndPointName, item)));
            if (allowEvents)
            {
                this.AddEndpoint(PowerLineEventHandler.GetEndPoint(this));
            }
        }
Пример #8
0
        internal async Task <PowerLineEndPointExecutionResult> SafeHandleRequestAsync(PowerLineEndPoint endpoint, int index, string[] requestPath, PowerLineContext context)
        {
            try
            {
                context.PathIndex = index;
                await this.HandleRequest(context);

                return(new PowerLineEndPointExecutionResult(context, endpoint, this));
            }
            catch (Exception ex)
            {
                return(new PowerLineEndPointExecutionResult(context, endpoint, this, ex));
            }
        }
Пример #9
0
 public PowerLineEndPoint AddEndPoint(PowerLineEndPoint endpoint)
 {
     this.childEndPoints.Add(endpoint.EndPointName, endpoint);
     return(endpoint);
 }
 public PowerLineEndPointExecutionResult(PowerLineContext context, PowerLineEndPoint endPoint, PowerLineHandler handler) : this(PowerLinExecutionResultType.OK, context, null, endPoint, handler)
 {
 }
 public PowerLineEndPointExecutionResult(PowerLineContext context, PowerLineEndPoint endPoint, PowerLineHandler handler, Exception exception) : this(PowerLinExecutionResultType.HandlerException, context, exception, endPoint, handler)
 {
 }
 public PowerLineEndPointExecutionResult(PowerLineContext context, PowerLinExecutionResultType resultType, PowerLineEndPoint endPoint) : this(resultType, context, null, endPoint, null)
 {
 }