Created
November 14, 2023 22:26
-
-
Save jacobslusser/55497df65d90db75a67a2f6ae693b8f9 to your computer and use it in GitHub Desktop.
Proper DoSend pipeline loop
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
while (true) | |
{ | |
// Get data from the send buffer | |
var result = await reader.ReadAsync().ConfigureAwait(false); | |
if (result.IsCanceled) | |
{ | |
break; | |
} | |
// Write to the socket | |
var buffers = result.Buffer; | |
if (!buffers.IsEmpty) | |
{ | |
int bytesSent; | |
if (buffers.IsSingleSegment) | |
{ | |
bytesSent = await _socket!.SendAsync(buffers.First, SocketFlags.None).ConfigureAwait(false); | |
} | |
else | |
{ | |
// HELP!!! | |
bytesSent = 0; | |
} | |
reader.AdvanceTo(buffers.GetPosition(bytesSent)); | |
if (result.IsCompleted) | |
{ | |
break; | |
} | |
} | |
} |
I ended up following the SendMultiSegmentAsync
approach used in SignalR. A little more basic than I expected, but I guess if it is good enough for SignalR....
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@davidfowl if you have a moment, I'm trying to understand and replicate the
DoSend
andDoRecieve
methods of theSocketConnection
class in Kestrel. In that implementation there is a complicatedSocketAsyncEventArgs
derived class that I'm trying to avoid by using the standardSocket
extension methods. However, there is not a good way or example I can find for sending aReadOnlySequence<byte>
instead of a simpleReadOnlyMemory<byte>
-- noted by theHELP!!!
comment above.This question has been discussed in:
I'm not looking for a first-class implementation, just some guidance on the best way to send multiple buffers to avoid copies and loops (if possible).
Thx