Пример #1
0
        public TcpServer(IOLoop io_loop_=null, object ssl_options_=null)
        {
            io_loop = io_loop_;
            ssl_options = ssl_options_;
            _sockets = new Dictionary<int, Socket>(); // fd -> socket object
            _pending_sockets = new List<Socket>();
            _started = false;
  
            // Verify the SSL options. Otherwise we don't get errors until clients
            // connect. This doesn't verify that the keys are legitimate, but
            // the SSL module doesn't do that until there is a connected socket
            // which seems like too much work 

            if (ssl_options != null)
            {
                //todo
                /* Only certfile is required: it can contain both keys
                if 'certfile' not in self.ssl_options:
                    raise KeyError('missing key "certfile" in ssl_options')

                if not os.path.exists(self.ssl_options['certfile']):
                    raise ValueError('certfile "%s" does not exist' %
                        self.ssl_options['certfile'])
                if ('keyfile' in self.ssl_options and
                        not os.path.exists(self.ssl_options['keyfile'])):
                    raise ValueError('keyfile "%s" does not exist' %
                        self.ssl_options['keyfile'])*/
            }
        }
Пример #2
0
 public IOStream(Socket socket_, IOLoop io_loop_, int max_buffer_size_=104857600, int read_chunk_size_=4096)
 {
     socket = socket_;
     socket.Blocking = false;
     io_loop = io_loop_ ?? IOLoop.instance();
     max_buffer_size = max_buffer_size_;
     read_chunk_size = read_chunk_size_;
     error = null;
     _read_buffer = new LinkedList<byte[]>(); // collections.deque();
     _write_buffer = new LinkedList<byte[]>(); //collections.deque();
     _read_buffer_size = 0;
     _write_buffer_frozen = false;
     _read_delimiter = null;
     _read_regex = null;
     _read_bytes = 0; 
     _read_until_close = false;
     _read_callback = null;
     _streaming_callback = null;
     _write_callback = null;
     _close_callback = null;
     _connect_callback = null;
     _connecting = false;
     _state = 0;
     _pending_callbacks = 0;
 }
Пример #3
0
        public void install()
        {
            /* Installs this IOloop object as the singleton instance.

            This is normally not necessary as `instance()` will create
            an IOLoop on demand, but you may want to call `install` to use
            a custom subclass of IOLoop.
            */

            Debug.Assert(!initialized());
            IOLoop._instance = this;
        }
Пример #4
0
        public static IOLoop instance()
        {
            /*Returns a global IOLoop instance.

            Most single-threaded applications have a single, global IOLoop.
            Use this method instead of passing around IOLoop instances
            throughout your code.

            A common pattern for classes that depend on IOLoops is to use
            a default argument to enable programs with multiple IOLoops
            but not require the argument for simpler applications::

                class MyClass(object):
                    def __init__(self, io_loop=None):
                        self.io_loop = io_loop or IOLoop.instance()
            */

            if(_instance == null)
                _instance = new IOLoop();

            return _instance;
        }
Пример #5
0
 public HTTPServer(Func<HTTPRequest, RequestHandler> request_callback_, bool no_keep_alive_ = false, IOLoop io_loop_ = null, bool xheaders_ = false, object ssl_options_ = null)
     : base(io_loop_, ssl_options_)
 {
     request_callback = request_callback_;
     no_keep_alive = no_keep_alive_;
     xheaders = xheaders_;
 }
Пример #6
0
        public void add_accept_handler(Socket sock, Action<Socket, IPEndPoint> callback, IOLoop io_loop = null)
        {
            /*Adds an ``IOLoop`` event handler to accept new connections on ``sock``.

            When a connection is accepted, ``callback(connection, address)`` will
            be run (``connection`` is a socket object, and ``address`` is the
            address of the other end of the connection).  Note that this signature
            is different from the ``callback(fd, events)`` signature used for
            ``IOLoop`` handlers.
            */

            if (io_loop == null)
                io_loop = IOLoop.instance();

            Action<int, int> accept_handler = (fd, events) =>
            {
                while (true)
                {
                    Socket connection = null;

                    try
                    {
                        connection = sock.Accept();
                    }
                    catch (SocketException ex)
                    {
                        if (ex.SocketErrorCode == SocketError.WouldBlock || ex.SocketErrorCode == SocketError.TryAgain)
                            return;

                        throw;
                    }

                    callback(connection, connection.RemoteEndPoint as IPEndPoint);
                }
            };

            io_loop.add_handler(sock, sock.fileno(), accept_handler, IOLoop.READ);
        }
Пример #7
0
        public void add_sockets(List<Socket> sockets)
        {
            /*Makes this server start accepting connections on the given sockets.

            The ``sockets`` parameter is a list of socket objects such as
            those returned by `bind_sockets`.
            `add_sockets` is typically used in combination with that
            method and `tornado.process.fork_processes` to provide greater
            control over the initialization of a multi-process server.
            */
            if(io_loop == null)
                io_loop = IOLoop.instance();

            foreach(var sock in sockets)
            {
                _sockets[sock.fileno()] = sock;
                add_accept_handler(sock, _handle_connection, io_loop);
            }
        }