コード例 #1
0
ファイル: SocketExtensions.cs プロジェクト: stjordanis/neo-fx
 public static SocketAwaitable ReceiveAsync(this Socket socket,
                                            SocketAwaitable awaitable)
 {
     awaitable.Reset();
     if (!socket.ReceiveAsync(awaitable.eventArgs))
     {
         awaitable.wasCompleted = true;
     }
     return(awaitable);
 }
コード例 #2
0
        public async Task ConnectAsync(IPEndPoint endpoint, CancellationToken token = default)
        {
            log.LogInformation("connecting to {host}:{port}", endpoint.Address, endpoint.Port);

            var args = new SocketAsyncEventArgs();

            args.RemoteEndPoint = endpoint;
            var awaitable = new SocketAwaitable(args);

            await socket.ConnectAsync(awaitable);

            Execute(token);
        }
コード例 #3
0
        private async Task StartSocketSend(CancellationToken token)
        {
            var reader    = sendPipe.Reader;
            var args      = new SocketAsyncEventArgs();
            var awaitable = new SocketAwaitable(args);

            while (!token.IsCancellationRequested)
            {
                var readResult = await reader.ReadAsync(token).ConfigureAwait(false);

                log.LogDebug("sendPipe read {length} bytes {IsCanceled} {IsCompleted}", readResult.Buffer.Length, readResult.IsCanceled, readResult.IsCompleted);

                if (readResult.IsCanceled)
                {
                    throw new OperationCanceledException();
                }

                var buffer = readResult.Buffer;
                if (buffer.Length > 0)
                {
                    foreach (var segment in buffer)
                    {
                        args.SetBuffer(MemoryMarshal.AsMemory(segment));
                        await socket.SendAsync(awaitable);

                        log.LogDebug("sent {length} via socket", segment.Length);
                    }
                }

                reader.AdvanceTo(buffer.End);
                log.LogDebug("sendPipe advanced to {end}", buffer.End.GetInteger());

                if (readResult.IsCompleted)
                {
                    break;
                }
            }
        }