Listen() public method

public Listen ( string host, int port ) : void
host string
port int
return void
示例#1
0
        public static IDisposable Create(AppDelegate app, int port, string path)
        {
            app = ErrorPage.Middleware(app);

            var effectivePath = path;

            var endpoint = new IPEndPoint(IPAddress.Any, port);
            var context = Context.Create();
            var httpServer = new HttpServer(
                context,
                transaction =>
                {
                    var cts = new CancellationTokenSource();

                    var requestPathBase = effectivePath;
                    if (requestPathBase == "/" || requestPathBase == null)
                        requestPathBase = "";

                    var requestPath = transaction.Request.Path;
                    if (requestPath.StartsWith(requestPathBase, StringComparison.OrdinalIgnoreCase))
                        requestPath = requestPath.Substring(requestPathBase.Length);

                    var requestQueryString = RequestQueryString(transaction.Request.QueryData);

                    var requestHeaders = transaction.Request.Headers.Keys.ToDictionary(k => k, k => transaction.Request.Headers[k], StringComparer.OrdinalIgnoreCase);
                    var env = new Dictionary<string, object>
                    {
                        {OwinConstants.Version, "1.0"},
                        {OwinConstants.RequestMethod, transaction.Request.Method.ToString().Substring(5)},
                        {OwinConstants.RequestScheme, "http"},
                        {OwinConstants.RequestPathBase, requestPathBase},
                        {OwinConstants.RequestPath, requestPath},
                        {OwinConstants.RequestQueryString, requestQueryString},
                        {OwinConstants.RequestHeaders, requestHeaders},
                        {OwinConstants.RequestBody, RequestBody(transaction.Request.PostBody, transaction.Request.ContentEncoding)},
                        {"Manos.Http.IHttpTransaction", transaction},
                        {"server.CLIENT_IP", transaction.Request.Socket.RemoteEndpoint.Address.ToString()},
                        {"System.Threading.CancellationToken", cts.Token}
                    };

                    app(
                        env,
                        (status, headers, body) =>
                        {
                            transaction.Response.StatusCode = int.Parse(status.Substring(0, 3));
                            foreach (var header in headers)
                            {
                                if (string.Equals(header.Key, "Set-Cookie", StringComparison.OrdinalIgnoreCase))
                                {
                                    // use a header-injection to avoid re-parsing values into Manos HttpCookie structure
                                    transaction.Response.SetHeader(header.Key, string.Join("\r\nSet-Cookie: ", header.Value.ToArray()));
                                }
                                else
                                {
                                    transaction.Response.SetHeader(header.Key, string.Join(",", header.Value.ToArray()));
                                }
                            }

                            body(
                                data =>
                                {
                                    var duplicate = new byte[data.Count];
                                    Array.Copy(data.Array, data.Offset, duplicate, 0, data.Count);
                                    transaction.Response.Write(duplicate);
                                    return false;
                                },
                                _ => false,
                                ex => transaction.Response.End(),
                                cts.Token);
                        },
                        ex =>
                        {
                            // This should never be called
                            throw new NotImplementedException();
                        });
                },
                context.CreateTcpServerSocket(endpoint.AddressFamily),
                true);
            httpServer.Listen(endpoint.Address.ToString(), port);

            var thread = new Thread(context.Start);
            thread.Start();

            return new Disposable(() =>
            {
                context.Stop();
                thread.Join(250);
                //httpServer.Dispose();
            });
        }
示例#2
0
        public static void Start(ManosApp application)
        {
            if (application == null)
                throw new ArgumentNullException ("application");

            app = application;

            started = true;
            server = new HttpServer (HandleTransaction, ioloop);

            server.Listen (IPAddress.ToString (), port);

            ioloop.Start ();
        }
示例#3
0
文件: AppHost.cs 项目: restrepo/manos
        public static void Start(ManosApp application)
        {
            if (application == null)
                throw new ArgumentNullException ("application");

            app = application;

            app.StartInternal ();

            started = true;

            foreach (var ep in listenEndPoints) {

                var server = new HttpServer (IOLoop, HandleTransaction, IOLoop.CreateSocket());
                server.Listen (ep.Address.ToString (), ep.Port);

                servers.Add (server);
            }
            foreach (var ep in secureListenEndPoints.Keys) {
                var keypair = secureListenEndPoints [ep];
                var socket = IOLoop.CreateSecureSocket (keypair.Item1, keypair.Item2);
                var server = new HttpServer (ioloop, HandleTransaction, socket);
                server.Listen (ep.Address.ToString (), ep.Port);

                servers.Add (server);
            }

            ioloop.Start ();
        }
示例#4
0
        public static void Initialize(ManosApp application)
        {
            if (application == null)
                throw new ArgumentNullException ("application");

            app = application;

            app.StartInternal ();

            foreach (var ep in listenEndPoints) {
                var server = new HttpServer (Context, HandleTransaction, Context.CreateTcpServerSocket (ep.AddressFamily));
                server.Listen (ep.Address.ToString (), ep.Port);

                servers.Add (server);
            }
            foreach (var ep in secureListenEndPoints.Keys) {
            //				var keypair = secureListenEndPoints [ep];
            //				var socket = Context.CreateSecureSocket (keypair.Item1, keypair.Item2);
            //				var server = new HttpServer (context, HandleTransaction, socket);
            //				server.Listen (ep.Address.ToString (), ep.Port);
            //
            //				servers.Add (server);
            }
        }