From a8debbdd709e2b4d5f422a9fd26cacb40e88db05 Mon Sep 17 00:00:00 2001 From: Arda Cinar Date: Sun, 6 Jun 2021 14:48:29 +0000 Subject: [PATCH] Sending an ArraySegment object --- src/Fleck/Handlers/ComposableHandler.cs | 8 ++++++- src/Fleck/Handlers/Hybi13Handler.cs | 32 ++++++++++++++++++++++++- src/Fleck/Interfaces/IHandler.cs | 2 ++ src/Fleck/WebSocketConnection.cs | 5 ++++ 4 files changed, 45 insertions(+), 2 deletions(-) diff --git a/src/Fleck/Handlers/ComposableHandler.cs b/src/Fleck/Handlers/ComposableHandler.cs index ffd197b5..31e8d033 100644 --- a/src/Fleck/Handlers/ComposableHandler.cs +++ b/src/Fleck/Handlers/ComposableHandler.cs @@ -8,6 +8,7 @@ public class ComposableHandler : IHandler public Func Handshake = s => new byte[0]; public Func TextFrame = x => new byte[0]; public Func BinaryFrame = x => new byte[0]; + public Func, byte[]> BinarySegmentFrame = x => new byte[0]; public Action> ReceiveData = delegate { }; public Func PingFrame = i => new byte[0]; public Func PongFrame = i => new byte[0]; @@ -36,7 +37,12 @@ public byte[] FrameBinary(byte[] bytes) { return BinaryFrame(bytes); } - + + public byte[] FrameBinary(ArraySegment bytes) + { + return BinarySegmentFrame(bytes); + } + public byte[] FramePing(byte[] bytes) { return PingFrame(bytes); diff --git a/src/Fleck/Handlers/Hybi13Handler.cs b/src/Fleck/Handlers/Hybi13Handler.cs index 39403f31..669837f8 100644 --- a/src/Fleck/Handlers/Hybi13Handler.cs +++ b/src/Fleck/Handlers/Hybi13Handler.cs @@ -17,6 +17,7 @@ public static IHandler Create(WebSocketHttpRequest request, Action 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(), FrameType.Close), @@ -47,7 +48,36 @@ public static byte[] FrameData(byte[] payload, FrameType frameType) return memoryStream.ToArray(); } - + + public static byte[] FrameData(ArraySegment 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(); + memoryStream.Write(lengthBytes, 0, lengthBytes.Length); + } + else if (payload.Count > 125) + { + memoryStream.WriteByte(126); + var lengthBytes = payload.Count.ToBigEndianBytes(); + 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 data, ReadState readState, Action processFrame) { diff --git a/src/Fleck/Interfaces/IHandler.cs b/src/Fleck/Interfaces/IHandler.cs index 5bd26625..c6dbbc0f 100644 --- a/src/Fleck/Interfaces/IHandler.cs +++ b/src/Fleck/Interfaces/IHandler.cs @@ -1,4 +1,5 @@ using System.Collections.Generic; +using System; namespace Fleck { @@ -8,6 +9,7 @@ public interface IHandler void Receive(IEnumerable data); byte[] FrameText(string text); byte[] FrameBinary(byte[] bytes); + byte[] FrameBinary(ArraySegment bytes); byte[] FramePing(byte[] bytes); byte[] FramePong(byte[] bytes); byte[] FrameClose(int code); diff --git a/src/Fleck/WebSocketConnection.cs b/src/Fleck/WebSocketConnection.cs index 5aba2740..3e0debc7 100644 --- a/src/Fleck/WebSocketConnection.cs +++ b/src/Fleck/WebSocketConnection.cs @@ -67,6 +67,11 @@ public Task Send(byte[] message) return Send(message, Handler.FrameBinary); } + public Task Send(ArraySegment message) + { + return Send(message, Handler.FrameBinary); + } + public Task SendPing(byte[] message) { return Send(message, Handler.FramePing);