private static SocketAwaitable DoSendAsync(Socket socket, SocketAsyncEventArgs args, ReadOnlySequence <byte> buffer, string name) { if (buffer.IsSingleSegment) { return(DoSendAsync(socket, args, buffer.First, name)); } #if SOCKET_STREAM_BUFFERS if (!args.MemoryBuffer.IsEmpty) #else if (args.Buffer != null) #endif { args.SetBuffer(null, 0, 0); } args.BufferList = GetBufferList(args, buffer); Helpers.DebugLog(name, $"## {nameof(socket.SendAsync)} {buffer.Length}"); if (socket.SendAsync(args)) { Helpers.Incr(Counter.SocketSendAsyncMultiAsync); } else { Helpers.Incr(Counter.SocketSendAsyncMultiSync); SocketAwaitable.OnCompleted(args); } return(GetAwaitable(args)); }
private static SocketAwaitable DoSendAsync(Socket socket, SocketAsyncEventArgs args, ReadOnlyMemory <byte> memory, string name) { // The BufferList getter is much less expensive then the setter. if (args.BufferList != null) { args.BufferList = null; } #if SOCKET_STREAM_BUFFERS args.SetBuffer(MemoryMarshal.AsMemory(memory)); #else var segment = memory.GetArray(); args.SetBuffer(segment.Array, segment.Offset, segment.Count); #endif Helpers.DebugLog(name, $"## {nameof(socket.SendAsync)} {memory.Length}"); if (socket.SendAsync(args)) { Helpers.Incr(Counter.SocketSendAsyncSingleAsync); } else { Helpers.Incr(Counter.SocketSendAsyncSingleSync); SocketAwaitable.OnCompleted(args); } return(GetAwaitable(args)); }
private static SocketAwaitable ConnectAsync(Socket socket, SocketAsyncEventArgs args, EndPoint endpoint) { args.RemoteEndPoint = endpoint; if (!socket.ConnectAsync(args)) { SocketAwaitable.OnCompleted(args); } return(GetAwaitable(args)); }