public override Task ConnectAsync(EndPoint remoteAddress, EndPoint localAddress) { NativeChannel ch = this.Channel; if (!ch.Open) { return(this.CreateClosedChannelExceptionTask()); } ConnectRequest request = null; try { if (ch.connectPromise != null) { throw new InvalidOperationException("connection attempt already made"); } ch.connectPromise = new TaskCompletionSource(remoteAddress); if (localAddress != null) { ch.DoBind(localAddress); } request = new TcpConnect(this, (IPEndPoint)remoteAddress); return(ch.connectPromise.Task); } catch (Exception ex) { request?.Dispose(); this.CloseIfClosed(); return(TaskEx.FromException(this.AnnotateConnectException(ex, remoteAddress))); } }
void FinishRead(ReadOperation operation) { NativeChannel ch = this.Channel; IChannelPipeline pipeline = ch.Pipeline; IRecvByteBufAllocatorHandle allocHandle = this.RecvBufAllocHandle; IByteBuffer buffer = operation.Buffer; allocHandle.LastBytesRead = operation.Status; if (allocHandle.LastBytesRead <= 0) { // nothing was read -> release the buffer. buffer.SafeRelease(); } else { buffer.SetWriterIndex(buffer.WriterIndex + operation.Status); pipeline.FireChannelRead(buffer); } allocHandle.IncMessagesRead(1); allocHandle.ReadComplete(); pipeline.FireChannelReadComplete(); if (operation.Error != null) { pipeline.FireExceptionCaught(operation.Error); } if (operation.Error != null || operation.EndOfStream) { ch.DoClose(); } }
void FinishConnect(ConnectRequest request) { NativeChannel ch = this.Channel; TaskCompletionSource promise = ch.connectPromise; try { if (request.Error != null) { promise.TrySetException(request.Error); this.CloseIfClosed(); } else { ch.DoFinishConnect(); promise.TryComplete(); } } catch (Exception exception) { promise.TrySetException(exception); this.CloseIfClosed(); } finally { request.Dispose(); ch.connectPromise = null; } }
void INativeUnsafe.FinishConnect(ConnectRequest request) { NativeChannel ch = this.Channel; if (ch.EventLoop.InEventLoop) { this.FinishConnect(request); } else { ch.EventLoop.Execute(ConnectCallbackAction, this, request); } }
ReadOperation INativeUnsafe.PrepareRead() { NativeChannel ch = this.Channel; IChannelConfiguration config = ch.Configuration; IByteBufferAllocator allocator = config.Allocator; IRecvByteBufAllocatorHandle allocHandle = this.RecvBufAllocHandle; allocHandle.Reset(config); IByteBuffer buffer = allocHandle.Allocate(allocator); allocHandle.AttemptedBytesRead = buffer.WritableBytes; return(new ReadOperation(this, buffer)); }
protected NativeChannelUnsafe(NativeChannel channel) : base(channel) { }