Skip to content

Commit

Permalink
Fix of WebSocket close status frame
Browse files Browse the repository at this point in the history
  • Loading branch information
chronoxor committed Nov 19, 2023
1 parent c6d3cfa commit bc5f529
Show file tree
Hide file tree
Showing 10 changed files with 13 additions and 4 deletions.
2 changes: 1 addition & 1 deletion examples/WsChatServer/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public override void OnWsReceived(byte[] buffer, long offset, long size)

// If the buffer starts with '!' the disconnect the current session
if (message == "!")
Close(1000);
Close();
}

protected override void OnError(SocketError error)
Expand Down
2 changes: 1 addition & 1 deletion examples/WssChatServer/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public override void OnWsReceived(byte[] buffer, long offset, long size)

// If the buffer starts with '!' the disconnect the current session
if (message == "!")
Close(1000);
Close();
}

protected override void OnError(SocketError error)
Expand Down
2 changes: 1 addition & 1 deletion source/NetCoreServer/NetCoreServer.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<Version>8.0.1.0</Version>
<Version>8.0.2.0</Version>
<Authors>Ivan Shynkarenka</Authors>
<Copyright>Copyright (c) 2019-2023 Ivan Shynkarenka</Copyright>
<RepositoryUrl>https://github.com/chronoxor/NetCoreServer</RepositoryUrl>
Expand Down
3 changes: 2 additions & 1 deletion source/NetCoreServer/WebSocket.cs
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,8 @@ public bool PerformServerUpgrade(HttpRequest request, HttpResponse response)
/// <param name="status">WebSocket status (default is 0)</param>
public void PrepareSendFrame(byte opcode, bool mask, ReadOnlySpan<byte> buffer, int status = 0)
{
bool storeStatus = (opcode & WS_CLOSE) == WS_CLOSE;
// Check if we need to store additional 2 bytes of close status frame
bool storeStatus = ((opcode & WS_CLOSE) == WS_CLOSE) && ((buffer.Length > 0) || (status != 0));
long size = storeStatus ? (buffer.Length + 2) : buffer.Length;

// Clear the previous WebSocket send buffer
Expand Down
2 changes: 2 additions & 0 deletions source/NetCoreServer/WsClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,14 @@ public class WsClient : HttpClient, IWebSocket

public override bool Connect() { _syncConnect = true; return base.Connect(); }
public override bool ConnectAsync() { _syncConnect = false; return base.ConnectAsync(); }
public virtual bool Close() => Close(0, Span<byte>.Empty);
public virtual bool Close(int status) => Close(status, Span<byte>.Empty);
public virtual bool Close(int status, string text) => Close(status, Encoding.UTF8.GetBytes(text));
public virtual bool Close(int status, ReadOnlySpan<char> text) => Close(status, Encoding.UTF8.GetBytes(text.ToArray()));
public virtual bool Close(int status, byte[] buffer) => Close(status, buffer.AsSpan());
public virtual bool Close(int status, byte[] buffer, long offset, long size) => Close(status, buffer.AsSpan((int)offset, (int)size));
public virtual bool Close(int status, ReadOnlySpan<byte> buffer) { SendClose(status, buffer); base.Disconnect(); return true; }
public virtual bool CloseAsync() => CloseAsync(0, Span<byte>.Empty);
public virtual bool CloseAsync(int status) => CloseAsync(status, Span<byte>.Empty);
public virtual bool CloseAsync(int status, string text) => CloseAsync(status, Encoding.UTF8.GetBytes(text));
public virtual bool CloseAsync(int status, ReadOnlySpan<char> text) => CloseAsync(status, Encoding.UTF8.GetBytes(text.ToArray()));
Expand Down
1 change: 1 addition & 0 deletions source/NetCoreServer/WsServer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ public class WsServer : HttpServer, IWebSocket

#region Session management

public virtual bool CloseAll() => CloseAll(0, Span<byte>.Empty);
public virtual bool CloseAll(int status) => CloseAll(status, Span<byte>.Empty);
public virtual bool CloseAll(int status, string text) => CloseAll(status, Encoding.UTF8.GetBytes(text));
public virtual bool CloseAll(int status, ReadOnlySpan<char> text) => CloseAll(status, Encoding.UTF8.GetBytes(text.ToArray()));
Expand Down
1 change: 1 addition & 0 deletions source/NetCoreServer/WsSession.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ public class WsSession : HttpSession, IWebSocket
public WsSession(WsServer server) : base(server) { WebSocket = new WebSocket(this); }

// WebSocket connection methods
public virtual bool Close() => Close(0, Span<byte>.Empty);
public virtual bool Close(int status) => Close(status, Span<byte>.Empty);
public virtual bool Close(int status, string text) => Close(status, Encoding.UTF8.GetBytes(text));
public virtual bool Close(int status, ReadOnlySpan<char> text) => Close(status, Encoding.UTF8.GetBytes(text.ToArray()));
Expand Down
2 changes: 2 additions & 0 deletions source/NetCoreServer/WssClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,14 @@ public class WssClient : HttpsClient, IWebSocket

public override bool Connect() { _syncConnect = true; return base.Connect(); }
public override bool ConnectAsync() { _syncConnect = false; return base.ConnectAsync(); }
public virtual bool Close() => Close(0, Span<byte>.Empty);
public virtual bool Close(int status) => Close(status, Span<byte>.Empty);
public virtual bool Close(int status, string text) => Close(status, Encoding.UTF8.GetBytes(text));
public virtual bool Close(int status, ReadOnlySpan<char> text) => Close(status, Encoding.UTF8.GetBytes(text.ToArray()));
public virtual bool Close(int status, byte[] buffer) => Close(status, buffer.AsSpan());
public virtual bool Close(int status, byte[] buffer, long offset, long size) => Close(status, buffer.AsSpan((int)offset, (int)size));
public virtual bool Close(int status, ReadOnlySpan<byte> buffer) { SendClose(status, buffer); base.Disconnect(); return true; }
public virtual bool CloseAsync() => CloseAsync(0, Span<byte>.Empty);
public virtual bool CloseAsync(int status) => CloseAsync(status, Span<byte>.Empty);
public virtual bool CloseAsync(int status, string text) => CloseAsync(status, Encoding.UTF8.GetBytes(text));
public virtual bool CloseAsync(int status, ReadOnlySpan<char> text) => CloseAsync(status, Encoding.UTF8.GetBytes(text.ToArray()));
Expand Down
1 change: 1 addition & 0 deletions source/NetCoreServer/WssServer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ public class WssServer : HttpsServer, IWebSocket

#region Session management

public virtual bool CloseAll() => CloseAll(0, Span<byte>.Empty);
public virtual bool CloseAll(int status) => CloseAll(status, Span<byte>.Empty);
public virtual bool CloseAll(int status, string text) => CloseAll(status, Encoding.UTF8.GetBytes(text));
public virtual bool CloseAll(int status, ReadOnlySpan<char> text) => CloseAll(status, Encoding.UTF8.GetBytes(text.ToArray()));
Expand Down
1 change: 1 addition & 0 deletions source/NetCoreServer/WssSession.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ public class WssSession : HttpsSession, IWebSocket
public WssSession(WssServer server) : base(server) { WebSocket = new WebSocket(this); }

// WebSocket connection methods
public virtual bool Close() => Close(0, Span<byte>.Empty);
public virtual bool Close(int status) => Close(status, Span<byte>.Empty);
public virtual bool Close(int status, string text) => Close(status, Encoding.UTF8.GetBytes(text));
public virtual bool Close(int status, ReadOnlySpan<char> text) => Close(status, Encoding.UTF8.GetBytes(text.ToArray()));
Expand Down

0 comments on commit bc5f529

Please sign in to comment.