Example #1
0
        public virtual void Close()
        {
            if (handle == IntPtr.Zero)
            {
                return;
            }

            DisableReading();
            DisableWriting();

            read_watcher.Dispose();
            write_watcher.Dispose();
            timeout_watcher.Dispose();

            read_watcher    = null;
            write_watcher   = null;
            timeout_watcher = null;
            handle          = IntPtr.Zero;

            foreach (IWriteOperation op in write_ops)
            {
                op.Dispose();
            }
            write_ops.Clear();

            if (Closed != null)
            {
                Closed(this, EventArgs.Empty);
            }
            Closed        = null;
            read_callback = null;

            GC.SuppressFinalize(this);
        }
Example #2
0
        public override void Close()
        {
            base.Close();
            if (Handle != IntPtr.Zero)
            {
                PauseReading();
                PauseWriting();

                readWatcher.Dispose();
                writeWatcher.Dispose();

                if (readTimeoutWatcher != null)
                {
                    readTimeoutWatcher.Dispose();
                }
                if (writeTimeoutWatcher != null)
                {
                    writeTimeoutWatcher.Dispose();
                }

                readWatcher         = null;
                writeWatcher        = null;
                readTimeoutWatcher  = null;
                writeTimeoutWatcher = null;

                Handle = IntPtr.Zero;
            }
        }
Example #3
0
        protected override void Dispose(bool disposing)
        {
            if (Handle != IntPtr.Zero)
            {
                PauseReading();
                PauseWriting();

                readWatcher.Dispose();
                writeWatcher.Dispose();

                if (readTimeoutWatcher != null)
                {
                    readTimeoutWatcher.Dispose();
                }
                if (writeTimeoutWatcher != null)
                {
                    writeTimeoutWatcher.Dispose();
                }

                readWatcher         = null;
                writeWatcher        = null;
                readTimeoutWatcher  = null;
                writeTimeoutWatcher = null;

                Handle = IntPtr.Zero;
            }
            base.Dispose(disposing);
        }
Example #4
0
        public override void Connect(string host, int port, Action callback)
        {
            if (state != Socket.SocketState.Invalid)
            {
                throw new InvalidOperationException("Socket already in use");
            }

            int error;
            var fd = manos_socket_connect(host, port, out error);

            if (fd < 0)
            {
                throw new Exception(String.Format("An error occurred while trying to connect to {0}:{1} errno: {2}", host, port, error));
            }

            stream = new PlainSocketStream(this, new IntPtr(fd));

            var connectWatcher = new IOWatcher(new IntPtr(fd), EventTypes.Write, Context.Loop, (watcher, revents) => {
                watcher.Stop();
                watcher.Dispose();

                this.address = host;
                this.port    = port;

                this.state = Socket.SocketState.Open;

                callback();
            });

            connectWatcher.Start();
        }
Example #5
0
 void HandleReadReady(Loop loop, IOWatcher watcher, EventTypes revents)
 {
     if (readTimeoutContinuation == null)
     {
         readTimeoutContinuation = DateTime.Now;
     }
     HandleRead();
 }
Example #6
0
 void HandleWriteReady(Loop loop, IOWatcher watcher, EventTypes revents)
 {
     if (writeTimeoutContinuation == null)
     {
         writeTimeoutContinuation = DateTime.Now;
     }
     HandleWrite();
 }
Example #7
0
        public override void Connect(IPEndPoint endpoint, Action callback, Action <Exception> error)
        {
            CheckDisposed();

            if (endpoint == null)
            {
                throw new ArgumentNullException("endpoint");
            }
            if (callback == null)
            {
                throw new ArgumentNullException("callback");
            }
            if (error == null)
            {
                throw new ArgumentNullException("error");
            }

            if (localname != null && localname.AddressFamily != endpoint.AddressFamily)
            {
                throw new ArgumentException();
            }
            if (IsConnected)
            {
                throw new InvalidOperationException();
            }

            int             err;
            ManosIPEndpoint ep = endpoint;

            err = SocketFunctions.manos_socket_connect_ip(fd, ref ep, out err);
            if (err != 0)
            {
                throw Errors.SocketFailure("Connect failure", err);
            }
            else
            {
                var connectWatcher = new IOWatcher(new IntPtr(fd), EventTypes.Write, Context.Loop, (watcher, revents) => {
                    watcher.Stop();
                    watcher.Dispose();

                    var result = SocketFunctions.manos_socket_peername_ip(fd, out ep, out err);
                    if (result < 0)
                    {
                        error(Errors.SocketFailure("Connect failure", err));
                    }
                    else
                    {
                        peername = endpoint;

                        IsConnected = true;

                        callback();
                    }
                });
                connectWatcher.Start();
            }
        }
Example #8
0
        private void OnConnected()
        {
            SetHandle(IOWatcher.GetHandle(socket));
            state = SocketState.Open;

            if (Connected != null)
            {
                Connected(this);
            }
        }
Example #9
0
        private void HandleIOWriteEvent(Loop loop, IOWatcher watcher, EventTypes revents)
        {
            // Happens after a close
            if (handle == IntPtr.Zero)
            {
                return;
            }

            Expires = DateTime.UtcNow + TimeOut;
            HandleWrite();
        }
Example #10
0
        public SocketStream(Socket socket, IOLoop ioloop) : base(ioloop)
        {
            this.socket = socket;

            if (socket != null)
            {
                socket.Blocking = false;
                SetHandle(IOWatcher.GetHandle(socket));
                state = SocketState.Open;
            }
        }
Example #11
0
        public void Listen( string host, int port )
        {
            int error;
            fd = manos_dgram_socket_listen( host, port, out error );

            if (fd < 0)
                throw new Exception (String.Format ("An error occurred while trying to connect to {0}:{1} errno: {2}", host, port, error));

            watcher = new IOWatcher( new IntPtr( fd ), EventTypes.Read, loop.EventLoop, (l, w, r) => onRead() );
            watcher.Start();
        }
Example #12
0
        public void Connect(string host, int port)
        {
            socket = CreateSocket();
            socket.Connect(host, port);

            IntPtr    handle    = IOWatcher.GetHandle(socket);
            IOWatcher iowatcher = new IOWatcher(handle, EventTypes.Write, IOLoop.EventLoop, (l, w, r) => {
                w.Stop();
                OnConnected();
            });

            iowatcher.Start();
        }
Example #13
0
        protected EventedStream(Context context, IntPtr handle)
            : base(context)
        {
            if (handle == IntPtr.Zero)
            {
                throw new ArgumentException("handle");
            }

            this.Handle = handle;

            this.readWatcher  = new IOWatcher(Handle, EventTypes.Read, Context.Loop, HandleReadReady);
            this.writeWatcher = new IOWatcher(Handle, EventTypes.Write, Context.Loop, HandleWriteReady);
        }
Example #14
0
        public void Listen(IPEndPoint endpoint)
        {
            socket = CreateSocket();

            socket.Bind(endpoint);
            socket.Listen(128);

            SetHandle(IOWatcher.GetHandle(socket));

            DisableTimeout();
            EnableReading();
            state = SocketState.AcceptingConnections;
        }
Example #15
0
        public void SetHandle(IntPtr handle)
        {
            if (this.handle != IntPtr.Zero && this.handle != handle)
            {
                Close();
            }

            this.handle     = handle;
            read_watcher    = new IOWatcher(handle, EventTypes.Read, ioloop.EventLoop, HandleIOReadEvent);
            write_watcher   = new IOWatcher(handle, EventTypes.Write, ioloop.EventLoop, HandleIOWriteEvent);
            timeout_watcher = new TimerWatcher(TimeOut, TimeOut, ioloop.EventLoop, HandleTimeoutEvent);

            timeout_watcher.Start();
        }
Example #16
0
 protected override void Dispose(bool disposing)
 {
     if (listener != null)
     {
         listener.Stop();
         listener.Dispose();
         listener = null;
     }
     if (stream != null)
     {
         stream.Close();
         stream = null;
     }
     base.Dispose(disposing);
 }
Example #17
0
        protected EventedStream(IOLoop loop, IntPtr handle)
        {
            if (loop == null)
            {
                throw new ArgumentNullException("loop");
            }
            if (handle == IntPtr.Zero)
            {
                throw new ArgumentException("handle");
            }

            this.Loop   = loop;
            this.Handle = handle;

            this.readWatcher  = new IOWatcher(Handle, EventTypes.Read, Loop.EVLoop, HandleReadReady);
            this.writeWatcher = new IOWatcher(Handle, EventTypes.Write, Loop.EVLoop, HandleWriteReady);
        }
Example #18
0
        public void Connect(string host, int port)
        {
            int error;

            fd = manos_socket_connect(host, port, out error);

            if (fd < 0)
            {
                throw new Exception(String.Format("An error occurred while trying to connect to {0}:{1} errno: {2}", host, port, error));
            }


            IOWatcher iowatcher = new IOWatcher(new IntPtr(fd), EventTypes.Write, IOLoop.EventLoop, (l, w, r) => {
                w.Stop();

                this.host = host;
                this.port = port;
                OnConnected();
            });

            iowatcher.Start();
        }
Example #19
0
        public void Listen(int backlog, Action <ITcpSocket> callback)
        {
            CheckDisposed();
            if (stream != null)
            {
                throw new InvalidOperationException();
            }

            if (listener != null)
            {
                listener.Stop();
                listener.Dispose();
            }

            int error;
            var result = SocketFunctions.manos_socket_listen(fd, backlog, out error);

            if (result < 0)
            {
                throw Errors.SocketFailure("Listen failure", error);
            }

            listener = new IOWatcher(new IntPtr(fd), EventTypes.Read, Context.Loop, delegate {
                ManosIPEndpoint ep;
                var client = SocketFunctions.manos_socket_accept(fd, out ep, out error);
                if (client < 0 && error != 0)
                {
                    throw new Exception(string.Format("Error while accepting: {0}", Errors.ErrorToString(error)));
                }
                else if (client > 0)
                {
                    var socket = new TcpSocket(Context, AddressFamily, client, LocalEndpoint, ep);
                    callback(socket);
                }
            });
            listener.Start();
        }
Example #20
0
        public override void Close()
        {
            IOWatcher.ReleaseHandle(socket, Handle);

            base.Close();
        }