public void Start()
        {
            try
            {
                server          = new HttpServer(provider);
                tcpListener     = new TcpListener(IPAddress.Any, 11000);
                tcpListenerAdpt = new TcpListenerAdapter(tcpListener);

                server.Use(tcpListenerAdpt);
                server.Use((context, next) =>
                {
                    Debug.WriteLine("Receiver a HTTP request!================================");

                    var request_method   = context.Request.Method.ToString();
                    var request_protocol = context.Request.Protocol;
                    var request_uri      = context.Request.Uri.ToString();
                    var request_content  = Encoding.UTF8.GetString(context.Request.Post.Raw);
                    var headers          = context.Request.Headers;

                    Debug.WriteLine("Method: " + request_method);
                    Debug.WriteLine("Protocol: " + request_protocol);
                    Debug.WriteLine("Uri: " + request_uri);
                    Debug.WriteLine("Body: " + request_content);

                    if (request_method.ToUpper() == "POST")
                    {
                        var obj = new NotificationObject(request_content);

                        Debug.WriteLine("Path[" + obj.Path + "] receive data: " + obj.Content);

                        var resource_path = obj.Path;
                        var resource_name = obj.ResourceName;
                        var msg_id        = context.Request.Headers.GetByName("X-M2M-RI");

                        //call handler

                        var list_headers = new List <KeyValuePair <string, string> >();

                        list_headers.Add(new KeyValuePair <string, string>("X-M2M-RSC", "2001"));
                        list_headers.Add(new KeyValuePair <string, string>("X-M2M-RI", msg_id));

                        var response_headers = new ListHttpHeaders(list_headers);

                        context.Response = uhttpsharp.StringHttpResponse.Create("", HttpResponseCode.Created, "text/html", true, response_headers);
                    }

                    return(Task.Factory.GetCompleted());
                });

                server.Start();

                IsActived = true;
            }
            catch (Exception exp)
            {
                IsActived = false;
                throw exp;
            }
        }
Beispiel #2
0
        /// <summary>
        /// Initializes a HttpListener.
        /// </summary>
        /// <param name="address"></param>
        /// <param name="port"></param>
        public HttpListener(IPAddress address, int port) : this()
        {
            LocalEndpoint = new IPEndPoint(
                address,
                port);

            _tcpListener = new TcpListenerAdapter(LocalEndpoint);
        }
Beispiel #3
0
 /// <summary>
 /// Initializes a HttpListener with an IPEndPoint.
 /// </summary>
 /// <param name="endpoint"></param>
 public HttpListener(IPEndPoint endpoint) : this()
 {
     LocalEndpoint = endpoint;
     _tcpListener  = new TcpListenerAdapter(LocalEndpoint);
 }