change mutex to semaphoreslim

This commit is contained in:
Mike Nolan 2023-03-17 23:02:25 -05:00
parent f7ece07299
commit 26725d0215
2 changed files with 10 additions and 10 deletions

View File

@ -210,7 +210,7 @@ namespace Tesses.WebServer.WebSocket
public class WebSocketServer public class WebSocketServer
{ {
Mutex mtx=new Mutex(); SemaphoreSlim semaphoreSlim=new SemaphoreSlim(1,1);
bool hasInit=false; bool hasInit=false;
ServerContext context; ServerContext context;
public WebSocketServer(ServerContext ctx) public WebSocketServer(ServerContext ctx)
@ -248,7 +248,7 @@ namespace Tesses.WebServer.WebSocket
{ {
while(!hasInit) ; while(!hasInit) ;
mtx.WaitOne(); await semaphoreSlim.WaitAsync();
int opCode = msg.Binary ? 0x2 : 0x1; int opCode = msg.Binary ? 0x2 : 0x1;
@ -276,12 +276,12 @@ namespace Tesses.WebServer.WebSocket
Array.Copy(msg.Data,i*4096,message,1+b.Length,r); Array.Copy(msg.Data,i*4096,message,1+b.Length,r);
await context.NetworkStream.WriteAsync(message,0,message.Length); await context.NetworkStream.WriteAsync(message,0,message.Length);
} }
mtx.ReleaseMutex(); semaphoreSlim.Release();
} }
private async Task PongSend(byte[] msg,long len) private async Task PongSend(byte[] msg,long len)
{ {
mtx.WaitOne(); await semaphoreSlim.WaitAsync();
int finField = 0b10000000 ; int finField = 0b10000000 ;
byte firstByte= (byte)(finField | 0xA); byte firstByte= (byte)(finField | 0xA);
@ -291,7 +291,7 @@ namespace Tesses.WebServer.WebSocket
Array.Copy(b,0,message,1,b.Length); Array.Copy(b,0,message,1,b.Length);
Array.Copy(msg,0,message,1+b.Length,len); Array.Copy(msg,0,message,1+b.Length,len);
await context.NetworkStream.WriteAsync(message,0,message.Length); await context.NetworkStream.WriteAsync(message,0,message.Length);
mtx.ReleaseMutex(); semaphoreSlim.Release();
} }
private string get_Sec_WebSocketAccept(string headerVal) private string get_Sec_WebSocketAccept(string headerVal)
{ {
@ -328,7 +328,7 @@ namespace Tesses.WebServer.WebSocket
public async Task Ping(byte[] ping) public async Task Ping(byte[] ping)
{ {
mtx.WaitOne(); await semaphoreSlim.WaitAsync();
int finField = 0b10000000 ; int finField = 0b10000000 ;
byte firstByte= (byte)(finField | 0x9); byte firstByte= (byte)(finField | 0x9);
@ -338,7 +338,7 @@ namespace Tesses.WebServer.WebSocket
Array.Copy(b,0,message,1,b.Length); Array.Copy(b,0,message,1,b.Length);
Array.Copy(ping,0,message,1+b.Length,ping.Length); Array.Copy(ping,0,message,1+b.Length,ping.Length);
await context.NetworkStream.WriteAsync(message,0,message.Length); await context.NetworkStream.WriteAsync(message,0,message.Length);
mtx.ReleaseMutex(); semaphoreSlim.Release();
} }
private async Task<short> get_short() private async Task<short> get_short()
{ {

View File

@ -5,9 +5,9 @@
<PackageId>Tesses.WebServer.WebSocket</PackageId> <PackageId>Tesses.WebServer.WebSocket</PackageId>
<Author>Mike Nolan</Author> <Author>Mike Nolan</Author>
<Company>Tesses</Company> <Company>Tesses</Company>
<Version>1.0.1</Version> <Version>1.0.1.1</Version>
<AssemblyVersion>1.0.1</AssemblyVersion> <AssemblyVersion>1.0.1.1</AssemblyVersion>
<FileVersion>1.0.1</FileVersion> <FileVersion>1.0.1.1</FileVersion>
<Description>WebSockets for Tesses.WebServer</Description> <Description>WebSockets for Tesses.WebServer</Description>
<PackageLicenseExpression>MIT</PackageLicenseExpression> <PackageLicenseExpression>MIT</PackageLicenseExpression>
<PackageTags>HTTP, WebServer, Website, WebSockets</PackageTags> <PackageTags>HTTP, WebServer, Website, WebSockets</PackageTags>