/// <summary> /// Executes the request on the base handle. /// </summary> public void Connect() { var addr = SockAddr.FromIpEndPoint(this.EndPoint); Libuv.EnsureSuccess(Libuv.uv_tcp_connect(this, this.BaseHandle, ref addr, _UvConnectCallback)); this.EndPoint = null; }
/// <summary> /// Start listening for incoming connections. /// </summary> /// <param name="backlog">Indicates the number of connections the kernel might queue.</param> /// <param name="callback">Callback to be called when a new incoming connection is received.</param> /// <param name="state">State to be passed to the callback.</param> public void Listen(int backlog, ListenCallbackDelegate callback, object state = null) { this.EnsureCallingThread(); if (this._ListenVitality.IsAllocated) { throw new InvalidOperationException("Listen may not be called more than once."); } try { this._UserListenCallback = callback; this._UserListenState = state; this._ListenVitality = GCHandle.Alloc(this, GCHandleType.Normal); Libuv.EnsureSuccess(Libuv.uv_listen(this, backlog, _UvListenCallback)); } catch { this._UserListenCallback = null; this._UserListenState = null; if (this._ListenVitality.IsAllocated) { this._ListenVitality.Free(); } throw; } }
/// <summary> /// Starts reading data from an incoming stream. /// The <paramref name="readCallback"/> will be executed several times until the stream closes or <see cref="ReadStop"/> method is called. /// </summary> /// <param name="allocCallback">Allocation callback.</param> /// <param name="readCallback">Read callback.</param> /// <param name="state">State to be passed to the callbacks.</param> public void ReadStart(AllocCallbackDelegate allocCallback, ReadCallbackDelegate readCallback, object state = null) { this.EnsureCallingThread(); if (this._ReadVitality.IsAllocated) { throw new InvalidOperationException("ReadStop must be called before ReadStart may be called again."); } try { this._ReadVitality = GCHandle.Alloc(this, GCHandleType.Normal); this._UserAllocCallback = allocCallback; this._UserReadCallback = readCallback; this._UserReadState = state; Libuv.EnsureSuccess(Libuv.uv_read_start(this, _UvAllocCallback, _UvReadCallback)); } catch (Exception) { this._UserAllocCallback = null; this._UserReadCallback = null; this._UserReadState = null; if (this._ReadVitality.IsAllocated) { this._ReadVitality.Free(); } throw; } }
/// <summary> /// Binds the handle to the specified end point. /// </summary> /// <param name="endPoint"></param> public void Bind(IPEndPoint endPoint) { this.EnsureCallingThread(); var addr = SockAddr.FromIpEndPoint(endPoint); Libuv.EnsureSuccess(Libuv.uv_tcp_bind(this, ref addr, 0)); }
/// <summary> /// Initializes a new instance of the <see cref="UvAsync"/> handle. /// </summary> /// <param name="loop">Loop, on which this handle will be initialized.</param> /// <param name="callback">A callback, which will be executed after a loop will receive an async signal from this handle.</param> /// <param name="state">A state object to be stored in the handle for later use inside the callback.</param> public UvAsync(UvLoop loop, Action <UvAsync> callback, object state = null) : base(loop, UvHandleType.Async) { if (callback == null) { throw new ArgumentNullException(nameof(callback)); } this._Callback = callback; this._State = state; Libuv.EnsureSuccess(Libuv.uv_async_init(loop, this, _UvAsyncCallback)); this.NeedsToBeClosed = true; }
/// <summary> /// Initializes a new instance of the <see cref="UvPrepare"/> handle. /// </summary> /// <param name="loop">Loop, on which this handle will be initialized.</param> /// <param name="callback">Callback, which will be invoked every loop iteration.</param> /// <param name="state">A state object to be stored in the handle for later use inside the callback.</param> public UvPrepare(UvLoop loop, Action <UvPrepare> callback, object state) : base(loop, UvHandleType.Prepare) { if (callback == null) { throw new ArgumentNullException(nameof(callback)); } this._Callback = callback; this._State = state; Libuv.EnsureSuccess(Libuv.uv_prepare_init(loop, this)); this.NeedsToBeClosed = true; }
/// <summary> /// Stops data reading for the stream. The <see cref="ReadCallbackDelegate"/> callback will no longer be called. /// </summary> public void ReadStop() { this.EnsureCallingThread(); if (!this._ReadVitality.IsAllocated) { throw new InvalidOperationException("ReadStart must be called before ReadStop may be called."); } this._UserAllocCallback = null; this._UserReadCallback = null; this._UserReadState = null; this._ReadVitality.Free(); Libuv.EnsureSuccess(Libuv.uv_read_stop(this)); }
/// <summary> /// Get the address of the peer connected to the handle. /// </summary> /// <returns></returns> public IPEndPoint GetSockEndPoint() { this.EnsureCallingThread(); SockAddr socketAddress; #if DOTNET_CORE int namelen = Marshal.SizeOf <SockAddr>(); #else int namelen = Marshal.SizeOf(typeof(SockAddr)); #endif Libuv.EnsureSuccess(Libuv.uv_tcp_getsockname(this, out socketAddress, ref namelen)); return(socketAddress.ToIpEndPoint()); }
/// <summary> /// Stop the handle, the callback will no longer be called. /// </summary> public void Stop() { this.EnsureCallingThread(); Libuv.EnsureSuccess(Libuv.uv_prepare_stop(this)); }
/// <summary> /// Initializes a new instance of <see cref="UvTcp"/> handle on the given event loop. /// </summary> /// <param name="loop"></param> public UvTcp(UvLoop loop) : base(loop, UvHandleType.Tcp) { Libuv.EnsureSuccess(Libuv.uv_tcp_init(loop, this)); this.NeedsToBeClosed = true; }
/// <summary> /// /// </summary> /// <returns></returns> public int PendingCount() { this.EnsureCallingThread(); return(Libuv.EnsureSuccess(Libuv.uv_pipe_pending_count(this))); }
/// <summary> /// Bind the pipe to a file path (Unix) or a name (Windows). /// </summary> /// <param name="name"></param> public void Bind(string name) { this.EnsureCallingThread(); Libuv.EnsureSuccess(Libuv.uv_pipe_bind(this, name)); }
/// <summary> /// Initializes a new instance of <see cref="UvPipe"/> handle on the given event loop. /// </summary> /// <param name="loop"></param> /// <param name="ipc">Indicates if this pipe will be used for handle passing between processes.</param> public UvPipe(UvLoop loop, bool ipc = false) : base(loop, UvHandleType.NamedPipe) { this.Ipc = ipc; Libuv.EnsureSuccess(Libuv.uv_pipe_init(loop, this, ipc ? 1 : 0)); this.NeedsToBeClosed = true; }
/// <summary> /// Runs the event loop in specified mode. /// </summary> /// <param name="mode">Loop run mode.</param> /// <returns>Run status (depends on specified mode).</returns> public int Run(UvLoopRunMode mode = UvLoopRunMode.RunDefault) { this.EnsureCallingThread(); return(Libuv.EnsureSuccess(Libuv.uv_run(this, mode))); }
/// <summary> /// Wakeup the event loop and call the async handle’s callback. /// It’s safe to call this function from any thread. The callback will be called on the loop thread. /// </summary> public void Send() { //this.EnsureCallingThread(); this method is thread safe Libuv.EnsureSuccess(Libuv.uv_async_send(this)); }
/// <summary> /// Enables / disables Nagle’s algorithm. /// </summary> /// <param name="enable"></param> public void NoDelay(bool enable) { this.EnsureCallingThread(); Libuv.EnsureSuccess(Libuv.uv_tcp_nodelay(this, enable ? 1 : 0)); }
/// <summary> /// Starts the handle with the callback specified in the constructor. /// </summary> public void Start() { this.EnsureCallingThread(); Libuv.EnsureSuccess(Libuv.uv_prepare_start(this, _UvPrepareCallback)); }
/// <summary> /// Executes the request on the base handle. /// </summary> public void Shutdown() { this.EnsureCallingThread(); Libuv.EnsureSuccess(Libuv.uv_shutdown(this, this.BaseHandle, _UvShutdownCallback)); }
/// <summary> /// Starts the handle with the callback specified in the constructor. /// </summary> public void Start() { this.EnsureCallingThread(); Libuv.EnsureSuccess(Libuv.uv_check_start(this, _UvCheckCallback)); }
/// <summary> /// Opens an existing file descriptor or SOCKET as a TCP handle. /// </summary> /// <param name="hSocket"></param> public void Open(IntPtr hSocket) { this.EnsureCallingThread(); Libuv.EnsureSuccess(Libuv.uv_tcp_open(this, hSocket)); }
/// <summary> /// This call is used in conjunction with <see cref="Listen"/> to accept incoming connections. /// Call this function after receiving a <see cref="ListenCallbackDelegate"/> to accept the connection. /// </summary> /// <param name="client"></param> public void Accept(UvStream client) { this.EnsureCallingThread(); Libuv.EnsureSuccess(Libuv.uv_accept(this, client)); }
/// <summary> /// Initializes a new instance of <see cref="UvLoop"/> on current thread. /// All calls to this loop and it's handles must be invoked from the same thread. /// </summary> public UvLoop() : base(_CalculateSize(), Thread.CurrentThread.ManagedThreadId) { Libuv.EnsureSuccess(Libuv.uv_loop_init(this)); this.NeedsToBeClosed = true; }
internal override void Write(UvBuffer[] buffers) { Libuv.EnsureSuccess(Libuv.uv_write2(this, this.BaseHandle, buffers, buffers.Length, this.HandleToSend, _UvWriteCallback)); this.Buffers = null; this.HandleToSend = null; }
/// <inheritdoc/> protected override void CloseHandle() { this.EnsureCallingThread(); Libuv.EnsureSuccess(Libuv.uv_loop_close(this)); this.Dispose(); }
internal virtual void Write(UvBuffer[] buffers) { Libuv.EnsureSuccess(Libuv.uv_write(this, this.BaseHandle, buffers, buffers.Length, _UvWriteCallback)); this.Buffers = null; }