Skip to content

Commit

Permalink
Merge a8debbd into 45672e0
Browse files Browse the repository at this point in the history
  • Loading branch information
kuzux authored Jun 6, 2021
2 parents 45672e0 + a8debbd commit ba7ddb6
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 2 deletions.
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();
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);

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

0 comments on commit ba7ddb6

Please sign in to comment.