Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 28 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
# NetCoreServer
# NetCoreServer (customized fork)

> **Note:** This is a customized fork of the well-known
> [NetCoreServer](https://github.com/chronoxor/NetCoreServer) library written by my colleague
> [Ivan Shynkarenka (chronoxor)](https://github.com/chronoxor). All credit for the original design and
> implementation goes to the original author. This fork adds a few features and performance
> improvements on top of it — see [What's different in this fork](#whats-different-in-this-fork).

[![Awesome .NET](https://awesome.re/badge.svg)](https://github.com/quozd/awesome-dotnet)
[![License](https://img.shields.io/badge/License-MIT-green.svg)](LICENSE)
Expand All @@ -19,6 +25,7 @@ Has integration with high-level message protocol based on [Fast Binary Encoding]
[NetCoreServer downloads](https://github.com/chronoxor/NetCoreServer/releases)<br/>

# Contents
* [What's different in this fork](#whats-different-in-this-fork)
* [Features](#features)
* [Requirements](#requirements)
* [How to build?](#how-to-build)
Expand Down Expand Up @@ -71,6 +78,24 @@ Has integration with high-level message protocol based on [Fast Binary Encoding]
* [SSL Client certificate](#ssl-client-certificate)
* [Diffie-Hellman key exchange](#diffie-hellman-key-exchange)

# What's different in this fork
This fork tracks upstream [chronoxor/NetCoreServer](https://github.com/chronoxor/NetCoreServer) and adds:

**WebSocket engine extensibility**
* `WebSocket.WsHandshaked`, `WebSocket.WsSendLock` and `WebSocket.WsSendBuffer` are now `public`, so
external hosts can inspect handshake state and drive the WebSocket send pipeline directly

**WebSocket performance**
* Fast frame masking/unmasking using 32-bit chunked XOR
* Direct memory copy for unmasked server-to-client frames
* Batched byte appends while parsing frame headers instead of byte-by-byte loops
* Fast base64 validation of the `Sec-WebSocket-Accept` handshake header

**Buffer and HTTP robustness**
* `Buffer.Reserve` uses a minimum allocation floor to avoid reallocation churn
* Null/empty checks for span and array appends in `Buffer`
* Safe `BodyBytes` copy in `HttpRequest` and `HttpResponse`

# Features
* Cross platform (Linux, MacOS, Windows)
* Asynchronous communication
Expand All @@ -86,7 +111,7 @@ Has integration with high-level message protocol based on [Fast Binary Encoding]
* Linux
* MacOS
* Windows
* [.NET 6.0](https://dotnet.microsoft.com/en-us/download/dotnet/6.0)
* [.NET 10.0](https://dotnet.microsoft.com/en-us/download/dotnet/10.0)
* [7-Zip](https://www.7-zip.org)
* [cmake](https://www.cmake.org)
* [git](https://git-scm.com)
Expand All @@ -99,7 +124,7 @@ Optional:

### Setup repository
```shell
git clone https://github.com/chronoxor/NetCoreServer.git
git clone https://github.com/sergiovision/NetCoreServer.git
cd NetCoreServer
```

Expand Down
22 changes: 17 additions & 5 deletions source/NetCoreServer/Buffer.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System;
using System;
using System.Diagnostics;
using System.Text;

Expand Down Expand Up @@ -123,8 +123,10 @@ public void Reserve(long capacity)

if (capacity > Capacity)
{
byte[] data = new byte[Math.Max(capacity, 2 * Capacity)];
Array.Copy(_data, 0, data, 0, _size);
long newCapacity = Math.Max(capacity, Math.Max(256, 2 * Capacity));
byte[] data = new byte[newCapacity];
if (_size > 0)
Array.Copy(_data, 0, data, 0, _size);
_data = data;
}
}
Expand Down Expand Up @@ -173,6 +175,8 @@ public long Append(byte value)
/// <returns>Count of append bytes</returns>
public long Append(byte[] buffer)
{
if (buffer == null || buffer.Length == 0)
return 0;
Reserve(_size + buffer.Length);
Array.Copy(buffer, 0, _data, _size, buffer.Length);
_size += buffer.Length;
Expand All @@ -188,8 +192,10 @@ public long Append(byte[] buffer)
/// <returns>Count of append bytes</returns>
public long Append(byte[] buffer, long offset, long size)
{
if (buffer == null || size <= 0)
return 0;
Reserve(_size + size);
Array.Copy(buffer, offset, _data, _size, size);
Array.Copy(buffer, (int)offset, _data, (int)_size, (int)size);
_size += size;
return size;
}
Expand All @@ -201,6 +207,8 @@ public long Append(byte[] buffer, long offset, long size)
/// <returns>Count of append bytes</returns>
public long Append(ReadOnlySpan<byte> buffer)
{
if (buffer.IsEmpty)
return 0;
Reserve(_size + buffer.Length);
buffer.CopyTo(new Span<byte>(_data, (int)_size, buffer.Length));
_size += buffer.Length;
Expand All @@ -212,7 +220,7 @@ public long Append(ReadOnlySpan<byte> buffer)
/// </summary>
/// <param name="buffer">Buffer to append</param>
/// <returns>Count of append bytes</returns>
public long Append(Buffer buffer) => Append(buffer.AsSpan());
public long Append(Buffer buffer) => buffer != null ? Append(buffer.AsSpan()) : 0;

/// <summary>
/// Append the given text in UTF-8 encoding
Expand All @@ -221,6 +229,8 @@ public long Append(ReadOnlySpan<byte> buffer)
/// <returns>Count of append bytes</returns>
public long Append(string text)
{
if (string.IsNullOrEmpty(text))
return 0;
int length = Encoding.UTF8.GetMaxByteCount(text.Length);
Reserve(_size + length);
long result = Encoding.UTF8.GetBytes(text, 0, text.Length, _data, (int)_size);
Expand All @@ -235,6 +245,8 @@ public long Append(string text)
/// <returns>Count of append bytes</returns>
public long Append(ReadOnlySpan<char> text)
{
if (text.IsEmpty)
return 0;
int length = Encoding.UTF8.GetMaxByteCount(text.Length);
Reserve(_size + length);
long result = Encoding.UTF8.GetBytes(text, new Span<byte>(_data, (int)_size, length));
Expand Down
12 changes: 10 additions & 2 deletions source/NetCoreServer/HttpRequest.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Text;
Expand Down Expand Up @@ -87,7 +87,15 @@ public HttpRequest(string method, string url, string protocol = "HTTP/1.1")
/// <summary>
/// Get the HTTP request body as byte array
/// </summary>
public byte[] BodyBytes { get { return _cache.Data[_bodyIndex..(_bodyIndex + _bodySize)]; } }
public byte[] BodyBytes
{
get
{
var result = new byte[_bodySize];
Array.Copy(_cache.Data, _bodyIndex, result, 0, _bodySize);
return result;
}
}
/// <summary>
/// Get the HTTP request body as byte span
/// </summary>
Expand Down
12 changes: 10 additions & 2 deletions source/NetCoreServer/HttpResponse.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Text;
Expand Down Expand Up @@ -156,7 +156,15 @@ public HttpResponse(int status, string statusPhrase, string protocol)
/// <summary>
/// Get the HTTP request body as byte array
/// </summary>
public byte[] BodyBytes { get { return _cache.Data[_bodyIndex..(_bodyIndex + _bodySize)]; } }
public byte[] BodyBytes
{
get
{
var result = new byte[_bodySize];
Array.Copy(_cache.Data, _bodyIndex, result, 0, _bodySize);
return result;
}
}
/// <summary>
/// Get the HTTP request body as read-only byte span
/// </summary>
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>net10.0</TargetFramework>
<Version>10.0.0.0</Version>
<Version>10.0.1.0</Version>
<Authors>Ivan Shynkarenka</Authors>
<Copyright>Copyright (c) 2019-2026 Ivan Shynkarenka</Copyright>
<RepositoryUrl>https://github.com/chronoxor/NetCoreServer</RepositoryUrl>
Expand Down
Loading