From 10603d336d14219b141ba95cde43266d79fc077d Mon Sep 17 00:00:00 2001 From: Mike Nolan Date: Wed, 23 Nov 2022 06:55:49 -0600 Subject: [PATCH] fixed a bug with messages over 4096 bytes and added easier way to use this --- Tesses.WebServer.WebSocket/Class1.cs | 113 ++++++++++++++---- .../Tesses.WebServer.WebSocket.csproj | 6 +- 2 files changed, 93 insertions(+), 26 deletions(-) diff --git a/Tesses.WebServer.WebSocket/Class1.cs b/Tesses.WebServer.WebSocket/Class1.cs index 00ff99c..7451b17 100644 --- a/Tesses.WebServer.WebSocket/Class1.cs +++ b/Tesses.WebServer.WebSocket/Class1.cs @@ -13,6 +13,67 @@ using System.IO; namespace Tesses.WebServer { + + public abstract class EasyWebSocketServer + { + System.Timers.Timer timer; + Func wsm; + Func ping; + bool canEnable; + public bool Enabled { + get{ + if(!canEnable) return false; + return timer.Enabled; + } + + set{ + if(canEnable) timer.Enabled = value; + }} + internal async Task Opened(Func sendWsm,Func ping,CancellationToken token) + { + this.wsm=sendWsm; + this.ping =ping; + timer=new System.Timers.Timer(); + timer.Elapsed += async(sender,e)=>{ + try{ + await Ping(); + }catch(Exception ex) + { + _=ex; + } + }; + timer.Interval = 10000; + canEnable=true; + await OnConnectionStarted(token); + } + public abstract Task OnConnectionStarted(CancellationToken token); + public async Task Ping() + { + await Ping(new byte[]{ (byte)'P', (byte)'i', (byte)'n', (byte)'g' }); + } + public async Task Ping(byte[] data) + { + await ping(data); + } + public abstract Task OnReceiveMessage(WebSocketMessage msg); + public async Task SendMessage(WebSocketMessage msg) + { + await wsm(msg); + } + public void Close(bool clean) + { + canEnable=false; + timer.Enabled=false; + timer.Dispose(); + + OnConnectionEnded(clean); + } + protected virtual void OnConnectionEnded(bool clean) + { + + } + + } public static class WebSocketExtensions { @@ -44,6 +105,10 @@ namespace Tesses.WebServer },c)),async(m)=>await Task.Run(()=>arrived(m)),closed); Task.Run(()=>t).Wait(); } + public static async Task StartEasyWebSocketConnectionAsync(this ServerContext ctx,EasyWebSocketServer wss) + { + await ctx.StartWebSocketConnectionAsync(wss.Opened,wss.OnReceiveMessage,wss.Close); + } public static async Task StartWebSocketConnectionAsync(this ServerContext ctx,Func,Func,CancellationToken,Task> opened,Func arrived,Action closed) { WebSocketServer server=new WebSocketServer(ctx); @@ -122,21 +187,7 @@ namespace Tesses.WebServer.WebSocket { Text=JsonConvert.SerializeObject(data); } - internal IEnumerable<(byte[] array,int)> GetPackets() - { - int read=0; - int offset=0; - byte[] buffer=new byte[4096]; - do - { - read = Math.Min(buffer.Length,data.Length-offset); - Array.Copy(data,offset,buffer,0,read); - yield return (buffer,read); - offset+=read; - }while(read>0); - } - public string Text {get{return Encoding.UTF8.GetString(Data);} private set{data=Encoding.UTF8.GetBytes(value); Binary=false;}} } @@ -159,6 +210,7 @@ namespace Tesses.WebServer.WebSocket public class WebSocketServer { + Mutex mtx=new Mutex(); bool hasInit=false; ServerContext context; public WebSocketServer(ServerContext ctx) @@ -194,30 +246,42 @@ namespace Tesses.WebServer.WebSocket public async Task SendMessageAsync(WebSocketMessage msg) { + while(!hasInit) ; + mtx.WaitOne(); int opCode = msg.Binary ? 0x2 : 0x1; - (byte[] buff,int len)[] parts = msg.GetPackets().ToArray(); + - for(int i = 0;i get_short() { @@ -286,6 +352,7 @@ namespace Tesses.WebServer.WebSocket } private async Task<(byte[] data,long len)> read_packet_async(byte len) { + int realLen=len & 127; bool masked=(len & 0b10000000) > 0; long realLen2 = realLen >= 126 ? realLen > 126 ? await get_long() : await get_short() : realLen; diff --git a/Tesses.WebServer.WebSocket/Tesses.WebServer.WebSocket.csproj b/Tesses.WebServer.WebSocket/Tesses.WebServer.WebSocket.csproj index 3a2ed03..02a45a5 100644 --- a/Tesses.WebServer.WebSocket/Tesses.WebServer.WebSocket.csproj +++ b/Tesses.WebServer.WebSocket/Tesses.WebServer.WebSocket.csproj @@ -5,9 +5,9 @@ Tesses.WebServer.WebSocket Mike Nolan Tesses - 1.0.0 - 1.0.0 - 1.0.0 + 1.0.1 + 1.0.1 + 1.0.1 WebSockets for Tesses.WebServer MIT HTTP, WebServer, Website, WebSockets