Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sending an ArraySegment object #314

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion src/Fleck/Handlers/ComposableHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ public class ComposableHandler : IHandler
public Func<string, byte[]> Handshake = s => new byte[0];
public Func<string, byte[]> TextFrame = x => new byte[0];
public Func<byte[], byte[]> BinaryFrame = x => new byte[0];
public Func<ArraySegment<byte>, byte[]> BinarySegmentFrame = x => new byte[0];
public Action<List<byte>> ReceiveData = delegate { };
public Func<byte[], byte[]> PingFrame = i => new byte[0];
public Func<byte[], byte[]> PongFrame = i => new byte[0];
Expand Down Expand Up @@ -36,7 +37,12 @@ public byte[] FrameBinary(byte[] bytes)
{
return BinaryFrame(bytes);
}


public byte[] FrameBinary(ArraySegment<byte> bytes)
{
return BinarySegmentFrame(bytes);
}

public byte[] FramePing(byte[] bytes)
{
return PingFrame(bytes);
Expand Down
32 changes: 31 additions & 1 deletion src/Fleck/Handlers/Hybi13Handler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ public static IHandler Create(WebSocketHttpRequest request, Action<string> onMes
Handshake = sub => Hybi13Handler.BuildHandshake(request, sub),
TextFrame = s => Hybi13Handler.FrameData(Encoding.UTF8.GetBytes(s), FrameType.Text),
BinaryFrame = s => Hybi13Handler.FrameData(s, FrameType.Binary),
BinarySegmentFrame = s => Hybi13Handler.FrameData(s, FrameType.Binary),
PingFrame = s => Hybi13Handler.FrameData(s, FrameType.Ping),
PongFrame = s => Hybi13Handler.FrameData(s, FrameType.Pong),
CloseFrame = i => Hybi13Handler.FrameData(i.ToBigEndianBytes<ushort>(), FrameType.Close),
Expand Down Expand Up @@ -47,7 +48,36 @@ public static byte[] FrameData(byte[] payload, FrameType frameType)

return memoryStream.ToArray();
}


public static byte[] FrameData(ArraySegment<byte> payload, FrameType frameType)
{
var memoryStream = new MemoryStream();
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Another allocation that could be skipped is if the MemoryStream is initialized with a starting buffer then memoryStream.toArray will just return the underlying array. To determine the buffer length, we'd just need to defer creating it until after calculating lengthBytes.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The problem with that is, the returned array from MemoryStream.ToArray() still has the length of the length of the underlying buffer (See https://replit.com/@kuzux/memorystreamarray#main.cs). The length of that array is then used by the SendBytes function. If we want it to function correctly, we'll need to initialize the underlying buffer for each message.

Unless you were talking about the extra allocation that happens after writing a large amount of bytes to the stream.

byte op = (byte)((byte)frameType + 128);

memoryStream.WriteByte(op);

if (payload.Count > UInt16.MaxValue)
{
memoryStream.WriteByte(127);
var lengthBytes = payload.Count.ToBigEndianBytes<ulong>();
memoryStream.Write(lengthBytes, 0, lengthBytes.Length);
}
else if (payload.Count > 125)
{
memoryStream.WriteByte(126);
var lengthBytes = payload.Count.ToBigEndianBytes<ushort>();
memoryStream.Write(lengthBytes, 0, lengthBytes.Length);
}
else
{
memoryStream.WriteByte((byte)payload.Count);
}

memoryStream.Write(payload.Array, 0, payload.Count);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should 0 be payload.Offset so it uses the correct range from the Segment's array?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep, fixed that


return memoryStream.ToArray();
}

public static void ReceiveData(List<byte> data, ReadState readState, Action<FrameType, byte[]> processFrame)
{

Expand Down
2 changes: 2 additions & 0 deletions src/Fleck/Interfaces/IHandler.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Collections.Generic;
using System;

namespace Fleck
{
Expand All @@ -8,6 +9,7 @@ public interface IHandler
void Receive(IEnumerable<byte> data);
byte[] FrameText(string text);
byte[] FrameBinary(byte[] bytes);
byte[] FrameBinary(ArraySegment<byte> bytes);
byte[] FramePing(byte[] bytes);
byte[] FramePong(byte[] bytes);
byte[] FrameClose(int code);
Expand Down
5 changes: 5 additions & 0 deletions src/Fleck/WebSocketConnection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,11 @@ public Task Send(byte[] message)
return Send(message, Handler.FrameBinary);
}

public Task Send(ArraySegment<byte> message)
{
return Send(message, Handler.FrameBinary);
}

public Task SendPing(byte[] message)
{
return Send(message, Handler.FramePing);
Expand Down