Ejemplo n.º 1
0
        public void Resolve(string node, string service, Action <UvArgs <IPEndPoint[]> > callback = null)
        {
            var hints    = addrinfo.CreateHints();
            var hintsPtr = this.Loop.Allocs.Alloc(Marshal.SizeOf(typeof(addrinfo)));

            Marshal.StructureToPtr(hints, hintsPtr, fDeleteOld: false);

            try
            {
                _resolveReq = this.Loop.Requests.Create(uv_req_type.UV_GETADDRINFO);
                CheckError(Uvi.uv_getaddrinfo(this.Loop.Handle, _resolveReq, _resolveDelegate, node, service, hintsPtr));
                this.Status      = HandleStatus.Resolving;
                _resolveCallback = new UvEndPointsCallback(this, callback);
            }
            catch (Exception)
            {
                this.Loop.Requests.Delete(_resolveReq);
                _connectCallback = null;
                throw;
            }
            finally
            {
                this.Loop.Allocs.Free(hintsPtr);
            }
        }
Ejemplo n.º 2
0
        internal IntPtr AllocHandle(uv_handle_type handleType)
        {
            var ret = Alloc(Uvi.uv_handle_size(handleType));

            _allocatedHandles++;
            return(ret);
        }
Ejemplo n.º 3
0
        private int FreeRequest(IntPtr req)
        {
            var ret = Uvi.uv_fs_req_result(req);

            Uvi.uv_fs_req_cleanup(req);
            this.Loop.Requests.Delete(req);
            return(ret);
        }
Ejemplo n.º 4
0
        private UvStatArgs FreeStatRequest(IntPtr req)
        {
            var ret  = Uvi.uv_fs_req_result(req);
            var stat = UvStat.Create(ret == 0 ? Uvi.uv_fs_req_stat(req) : IntPtr.Zero);

            Uvi.uv_fs_req_cleanup(req);
            this.Loop.Requests.Delete(req);
            return(new UvStatArgs(ret, stat));
        }
Ejemplo n.º 5
0
        private UvDataArgs FreeReadRequest(IntPtr req)
        {
            var ret = Uvi.uv_fs_req_result(req);

            Uvi.uv_fs_req_cleanup(req);
            var data = this.Loop.Requests.CopyAndDelete(req, (int)ret);

            return(new UvDataArgs(ret, data));
        }
Ejemplo n.º 6
0
        /// <summary>
        /// Closes the stream
        /// </summary>
        public void Close(bool dispose = false, Action <UvArgs> callback = null)
        {
            if (this.Status != HandleStatus.Open)
            {
                return;
            }

            _disposeAfterClose = dispose;
            Uvi.uv_close(this.Handle, _closeDelegate);
            this.Status    = HandleStatus.Closing;
            _closeCallback = new UvCallback(this, callback);
        }
Ejemplo n.º 7
0
 public void Connect(IntPtr address, Action <UvArgs> callback = null)
 {
     try
     {
         _address = address;
         CheckError(Uvi.uv_tcp_connect(this.Connection, this.Handle, _address, _connectDelegate));
         this.Status      = HandleStatus.Opening;
         _connectCallback = new UvCallback(this, callback);
     }
     catch (Exception)
     {
         _address         = this.Loop.Allocs.Free(_address);
         _connectCallback = null;
         throw;
     }
 }
Ejemplo n.º 8
0
        public void Write(byte[] data, int offset, int length, Action <UvDataArgs> callback = null)
        {
            IntPtr req = IntPtr.Zero;

            try
            {
                req = this.Loop.Requests.Create(uv_req_type.UV_WRITE, data, offset, length);
                CheckError((Uvi.uv_write(req, this.Handle, new[] { this.Loop.Requests[req] }, 1, _writeDelegate)));
                _writeCallbacks.Add(req, new UvDataCallback(this, callback, data));
            }
            catch (Exception)
            {
                this.Loop.Requests.Delete(req);
                throw;
            }
        }
Ejemplo n.º 9
0
        public void RemoveDirectory(string path, Action <UvArgs> callback = null)
        {
            IntPtr req = IntPtr.Zero;

            try
            {
                req = this.CreateRequest();
                CheckError(Uvi.uv_fs_rmdir(this.Loop.Handle, req, path, _rmdirDelegate));
                _rmdirCallback = new UvCallback(this, callback);
            }
            catch (Exception)
            {
                this.FreeRequest(req);
                throw;
            }
        }
Ejemplo n.º 10
0
        public void Delete(string path, Action <UvArgs> callback = null)
        {
            IntPtr req = IntPtr.Zero;

            try
            {
                req = this.CreateRequest();
                CheckError(Uvi.uv_fs_unlink(this.Loop.Handle, req, path, _deleteDelegate));
                _deleteCallback = new UvCallback(this, callback);
            }
            catch (Exception)
            {
                this.FreeRequest(req);
                throw;
            }
        }
Ejemplo n.º 11
0
 public void StartListening(IPEndPoint endpoint, Action <UvArgs <TcpServerSocket> > callback = null)
 {
     try
     {
         _address = this.Loop.Allocs.Free(_address);
         _address = TcpSocket.AllocSocketAddress(endpoint, this.Loop);
         CheckError(Uvi.uv_tcp_bind(this.Handle, _address, 0));
         CheckError(Uvi.uv_listen(this.Handle, this.BackLog, _connectionDelegate));
         this.Status      = HandleStatus.Open;
         _connectCallback = new UvTcpServerSocketCallback(this, callback);
     }
     catch (Exception)
     {
         _address = this.Loop.Allocs.Free(_address);
     }
 }
Ejemplo n.º 12
0
        protected virtual void Dispose(bool disposing)
        {
            if (this.IsDisposed)
            {
                return;
            }

            //we don't delete the default's loop handle
            if (this.Handle != DefaultLoop.Handle)
            {
                if (this.Handle != IntPtr.Zero)
                {
                    Uvi.uv_loop_delete(this.Handle);
                    this.Handle = IntPtr.Zero;
                }
            }

            GC.SuppressFinalize(this);
            this.IsDisposed = true;
        }
Ejemplo n.º 13
0
        internal LoopWork(Loop loop, Action run, Action after, Action <LoopWork> completed)
        {
            _run         = new uv_work_cb(this.Run);
            _after       = new uv_after_work_cb(this.After);
            _runAction   = run;
            _afterAction = after;
            _completed   = completed;
            _loop        = loop;
            _work        = _loop.Requests.Create(uv_req_type.UV_WORK);

            try
            {
                _loop.CheckError(Uvi.uv_queue_work(_loop.Handle, _work, _run, _after));
            }
            catch (Exception)
            {
                _work = _loop.Requests.Delete(_work);
                throw;
            }
        }
Ejemplo n.º 14
0
        public void Write(byte[] data, int offset, int length, Action <UvDataArgs> callback = null)
        {
            if (this.Status != FileStatus.Open)
            {
                throw new InvalidOperationException("File handle must be open in order to write data");
            }

            IntPtr req = IntPtr.Zero;

            try
            {
                req = this.CreateRequest(data, offset, length);
                CheckError(Uvi.uv_fs_write(this.Loop.Handle, req, _file, new[] { this.Loop.Requests[req] }, 1, -1, _writeDelegate));
                _writeCallback = new UvDataCallback(this, callback, data);
            }
            catch (Exception)
            {
                this.FreeRequest(req);
                throw;
            }
        }
Ejemplo n.º 15
0
        /// <summary>
        /// Read from the file
        /// </summary>
        public void Read(uint length, Action <UvDataArgs> callback = null)
        {
            if (this.Status != FileStatus.Open)
            {
                throw new InvalidOperationException("File handle must be open in order to read data");
            }

            IntPtr req = IntPtr.Zero;

            try
            {
                req = this.CreateRequest(length);
                CheckError(Uvi.uv_fs_read(this.Loop.Handle, req, _file, new[] { this.Loop.Requests[req] }, 1, -1, _readDelegate));
                _readCallback = new UvDataCallback(this, callback);
            }
            catch (Exception)
            {
                this.FreeRequest(req);
                throw;
            }
        }
Ejemplo n.º 16
0
        public void Shutdown(Action <UvArgs> callback = null)
        {
            IntPtr req = IntPtr.Zero;

            try
            {
                if (_isReading)
                {
                    this.ReadStop();
                }

                req = this.Loop.Requests.Create(uv_req_type.UV_SHUTDOWN);
                CheckError(Uvi.uv_shutdown(req, this.Handle, _shutdownDelegate));
                _shutdownCallback = new UvCallback(this, callback);
            }
            catch (Exception)
            {
                this.Loop.Requests.Delete(req);
                throw;
            }
        }
Ejemplo n.º 17
0
        internal static IntPtr AllocSocketAddress(IPEndPoint endpoint, Loop loop)
        {
            IntPtr ret;

            switch (endpoint.AddressFamily)
            {
            case AddressFamily.InterNetwork:
                ret = loop.Allocs.Alloc(Uvi.sockaddr_in_size);
                loop.CheckError(Uvi.uv_ip4_addr(endpoint.Address.ToString(), endpoint.Port, ret));
                break;

            case AddressFamily.InterNetworkV6:
                ret = loop.Allocs.Alloc(Uvi.sockaddr_in6_size);
                loop.CheckError(Uvi.uv_ip6_addr(endpoint.Address.ToString(), endpoint.Port, ret));
                break;

            default:
                throw new ArgumentException(String.Format("AddressFamily {0} not supported", endpoint.AddressFamily));
            }

            return(ret);
        }
Ejemplo n.º 18
0
        /// <summary>
        /// Closes the stream. After this call the stream will not be valid
        /// </summary>
        public void Close(Action <UvArgs> callback = null)
        {
            if (this.Status != FileStatus.Open)
            {
                throw new InvalidOperationException(String.Format("Cannot close the file handle while the status is {0}", this.Status));
            }

            IntPtr req = IntPtr.Zero;

            try
            {
                req = this.CreateRequest();
                CheckError(Uvi.uv_fs_close(this.Loop.Handle, req, _file, _closeDelegate));
                this.Status    = FileStatus.Closing;
                _closeCallback = new UvCallback(this, callback);
            }
            catch (Exception)
            {
                this.FreeRequest(req);
                throw;
            }
        }
Ejemplo n.º 19
0
        private void OnResolve(IntPtr resolver, int status, IntPtr addrinfo)
        {
            var callback = _resolveCallback;

            _resolveCallback = null;

            try
            {
                IPEndPoint[] value = null;
                if (status == 0)
                {
                    var info = ((addrinfo)Marshal.PtrToStructure(addrinfo, typeof(addrinfo)));
                    value = info.EndPoints.ToArray();
                }

                callback.Invoke(status, value, this.OnResolve, this.Resolved);
            }
            finally
            {
                Uvi.uv_freeaddrinfo(addrinfo);
                this.Loop.Requests.Delete(_resolveReq);
            }
        }
Ejemplo n.º 20
0
 /// <summary>
 ///
 /// </summary>
 /// <param name="loop"></param>
 /// <remarks>Handle type is <typeparamref name="Libuv.uv_tcp_t"/> that is a subclass of <typeparamref name="Libuv.uv_stream_t"/></remarks>
 protected TcpSocket(Loop loop)
     : base(loop, uv_handle_type.UV_TCP)
 {
     CheckError(Uvi.uv_tcp_init(this.Loop.Handle, this.Handle));
 }
Ejemplo n.º 21
0
 public void Stop()
 {
     CheckError(Uvi.uv_timer_stop(this.Handle));
     _callback = null;
 }
Ejemplo n.º 22
0
 /// <summary>
 /// This function polls for new events without blocking
 /// </summary>
 public void RunOnce()
 {
     CheckError(Uvi.uv_run(this.Handle, Uvi.uv_run_mode.UV_RUN_ONCE));
 }
Ejemplo n.º 23
0
 /// <summary>
 /// This function starts the event loop. It blocks until the reference count
 /// of the loop drops to zero.
 /// </summary>
 public void Run()
 {
     CheckError(Uvi.uv_run(this.Handle, Uvi.uv_run_mode.UV_RUN_DEFAULT));
 }
Ejemplo n.º 24
0
 public Loop()
     : this(Uvi.uv_loop_new())
 {
 }
Ejemplo n.º 25
0
 public void Again()
 {
     CheckError(Uvi.uv_timer_again(this.Handle));
 }
Ejemplo n.º 26
0
 internal IntPtr AllocRequest(uv_req_type requestType)
 {
     return(Alloc(Uvi.uv_req_size(requestType)));
 }
Ejemplo n.º 27
0
 internal uv_buf_t CreateBuffer(IntPtr data, uint size)
 {
     return(Uvi.uv_buf_init(data, size));
 }
Ejemplo n.º 28
0
 public void ReadStop()
 {
     CheckError(Uvi.uv_read_stop(this.Handle));
     _isReading    = false;
     _readCallback = null;
 }
Ejemplo n.º 29
0
 public void ReadStart(Action <UvDataArgs> callback = null)
 {
     CheckError(Uvi.uv_read_start(this.Handle, _allocDelegate, _readDelegate));
     _isReading    = true;
     _readCallback = new UvDataCallback(this, callback);
 }
Ejemplo n.º 30
0
 public TcpServer(Loop loop)
     : base(loop, uv_handle_type.UV_TCP)
 {
     this.BackLog = DefaultBackLog;
     CheckError(Uvi.uv_tcp_init(this.Loop.Handle, this.Handle));
 }