Skip to content

boinred/FastPortSharp

Repository files navigation

πŸš€ FastPortSharp

High-Performance Asynchronous TCP Socket Server/Client Framework

English | ν•œκ΅­μ–΄

A scalable network library based on .NET 10, designed for applications requiring large-scale concurrent connections, such as game servers and real-time communication systems.


πŸ“‹ Table of Contents


🎯 Project Overview

FastPortSharp is a framework for high-performance network communication. It achieves high throughput with minimal memory allocation using SocketAsyncEventArgs-based asynchronous I/O patterns and efficient buffer management.

Motivation

  • Reliable network processing in large-scale concurrent connection environments
  • Modular and reusable network component design
  • Efficient serialization/deserialization using Protocol Buffers

✨ Key Features

Feature Description
Async I/O High concurrency processing with SocketAsyncEventArgs-based IOCP pattern
Circular Buffer Minimized GC pressure through memory reuse
Protocol Buffers Efficient message serialization based on Google Protobuf
Session Management Flexible session creation and management based on the Factory pattern
Keep-Alive Connection state monitoring via TCP Keep-Alive settings
BackgroundService Service lifecycle management based on .NET Generic Host
Latency Statistics Real-time measurement of RTT, server processing time, and network delay

πŸ›  Tech Stack

Domain Technology
Language C# 14 / .NET 10
Async Pattern SocketAsyncEventArgs (IOCP)
Serialization Google Protocol Buffers
DI Container Microsoft.Extensions.DependencyInjection
Hosting Microsoft.Extensions.Hosting
Concurrency Channel<T>, .NET 10 Lock
Testing MSTest, BenchmarkDotNet

πŸ“Š Performance Benchmarks

Environment: Windows 11, Intel Core i5-14600K 3.50GHz, .NET 10

Key Performance Indicators

Item Result Note
CircularBuffer Write 244~670 ns 64B~8KB data
CircularBuffer vs QueueBuffer 20x faster Based on 4KB data
Channel vs BufferBlock 4x faster 69% memory savings
.NET 10 Lock vs lock 9% faster Based on 10,000 iterations

πŸ“ˆ Detailed Benchmark Results

πŸ‘‰ View Full Benchmark Results

Running Benchmarks

dotnet run -c Release --project FastPortBenchmark

πŸ“‘ Reports

Performance Test Reports

Report Description Link
Pre-optimization Performance Report Latency performance test results before optimization πŸ“„ View
Lock-optimized Performance Report Performance test after applying ArrayPool + .NET 10 Lock πŸ“„ View
Channel-optimized Performance Report Performance test after applying full optimizations πŸ“„ View
Benchmark Results Component-specific performance measurement based on BenchmarkDotNet πŸ“„ View

Optimization Summary

Metric Before Final (Channel applied) Improvement
Average RTT 96.03 ms 55.68 ms 42.0%↓
Server Processing Time 0.234 ms 0.002 ms 99.1%↓
Max RTT 434.40 ms 83.13 ms 80.9%↓
Throughput ~489/min ~1,080/min 2.2x↑

πŸ— Architecture

System Structure

flowchart TB
    subgraph Client ["FastPortClient"]
        CC[FastPortConnector]
        CSS[FastPortServerSession]
        CBS[BackgroundService]
    end
    
    subgraph Server ["FastPortServer"]
        FS[FastPortServer]
        FCS[FastPortClientSession]
        FSM[SessionManager]
        SBS[BackgroundService]
    end
    
    subgraph LibNetworks ["LibNetworks"]
        BL[BaseListener]
        BC[BaseConnector]
        BS[BaseSession]
        SEP[SocketEventsPool]
    end
    
    subgraph LibCommons ["LibCommons"]
        CB[CircularBuffer]
        BP[BasePacket]
        IDG[IDGenerator]
        LS[LatencyStats]
    end
    
    subgraph Protocols ["Protocols"]
        PB[Protobuf Messages]
    end
    
    CC --> BC
    CSS --> BS
    FS --> BL
    FCS --> BS
    
    BS --> CB
    BS --> BP
    
    FCS --> PB
    CSS --> PB
    
    CBS --> CC
    SBS --> FS
    
    CSS --> LS
Loading

Server Connection Flow

sequenceDiagram
    participant C as Client
    participant L as BaseListener
    participant SF as SessionFactory
    participant S as ClientSession
    participant B as CircularBuffer
    
    C->>L: TCP Connect
    L->>L: AcceptAsync()
    L->>SF: Create(socket)
    SF->>S: new ClientSession()
    S->>B: Initialize Buffers
    S->>S: OnAccepted()
    
    loop Message Processing
        C->>S: Send Data
        S->>B: Write(data)
        B->>S: TryGetBasePackets()
        S->>S: OnReceived(packet)
        S->>C: SendMessage(response)
    end
    
    C->>S: Disconnect
    S->>S: OnDisconnected()
Loading

πŸ“ Project Structure

FastPortSharp/
β”œβ”€β”€ πŸ“‚ LibCommons/                 # Common utility library
β”‚   β”œβ”€β”€ BaseCircularBuffers.cs     # Circular buffer implementation (.NET 10 Lock)
β”‚   β”œβ”€β”€ ArrayPoolCircularBuffers.cs # ArrayPool-based circular buffer
β”‚   β”œβ”€β”€ BasePacket.cs              # Packet structure
β”‚   β”œβ”€β”€ LatencyStats.cs            # Latency statistics collection
β”‚   └── IBuffers.cs                # Buffer interface
β”‚
β”œβ”€β”€ πŸ“‚ LibNetworks/                # Network core library
β”‚   β”œβ”€β”€ BaseListener.cs            # TCP listener base
β”‚   β”œβ”€β”€ BaseConnector.cs           # TCP connector base
β”‚   β”œβ”€β”€ SocketEventsPool.cs        # SocketAsyncEventArgs pool
β”‚   └── πŸ“‚ Sessions/
β”‚       β”œβ”€β”€ BaseSession.cs         # Session core logic (Channel<T>)
β”‚       └── IClientSessionFactory.cs
β”‚
β”œβ”€β”€ πŸ“‚ FastPortServer/             # TCP server application
β”œβ”€β”€ πŸ“‚ FastPortClient/             # TCP client application
β”œβ”€β”€ πŸ“‚ Protocols/                  # Protocol Buffers definitions
β”œβ”€β”€ πŸ“‚ FastPortBenchmark/          # Performance benchmarks
β”œβ”€β”€ πŸ“‚ LibCommonTest/              # Unit tests
└── πŸ“‚ docs/                       # Documentation
    β”œβ”€β”€ latency-performance-report.md           # Pre-optimization performance report
    β”œβ”€β”€ latency-performance-report-after-lock.md # Lock-optimized report
    β”œβ”€β”€ baseline-benchmark-results.md           # Benchmark results
    └── FastPortSharp-Optimization-Guide-Confluence.md

πŸ”§ Core Implementation

1. Circular Buffer

Efficiently handles continuous data streams without memory reallocation.

public class BaseCircularBuffers : IBuffers, IDisposable
{
    private byte[] m_Buffers;
    private int m_Head = 0;  // Read position
    private int m_Tail = 0;  // Write position
    
    // Uses .NET 10 lightweight Lock
    private readonly Lock m_Lock = new();
    
    public int Write(byte[] buffers, int offset, int count)
    {
        lock (m_Lock)
        {
            // Auto-expansion when capacity is low
            // Circular write logic for memory efficiency
        }
    }
}

2. Channel<T> Based Packet Processing

Uses Channel<T> for high-performance asynchronous message delivery.

// 4x faster and 69% memory savings compared to BufferBlock<T>
private readonly Channel<BasePacket> m_ReceivedPackets = 
    Channel.CreateBounded<BasePacket>(new BoundedChannelOptions(1000)
    {
        FullMode = BoundedChannelFullMode.Wait,
        SingleReader = true,
        SingleWriter = true
    });

// Packet processing loop
await foreach (var packet in m_ReceivedPackets.Reader.ReadAllAsync(cancellationToken))
{
    OnReceived(packet);
}

3. Factory Pattern Based Session Creation

public interface IClientSessionFactory
{
    BaseSessionClient Create(Socket socket);
}

public class FastPortClientSessionFactory : IClientSessionFactory
{
    public BaseSessionClient Create(Socket socket)
    {
        return new FastPortClientSession(_logger, socket, 
            new ArrayPoolCircularBuffers(8192), 
            new ArrayPoolCircularBuffers(8192));
    }
}

4. Protocol Buffers Message Processing

protected void RequestSendMessage<T>(int packetId, IMessage<T> message) 
    where T : IMessage<T>
{
    Span<byte> packetIdBuffers = BitConverter.GetBytes(packetId);
    ReadOnlySpan<byte> messageBuffers = message.ToByteArray();
    
    byte[] packetBuffers = new byte[packetIdBuffers.Length + messageBuffers.Length];
    packetIdBuffers.CopyTo(packetBuffers);
    messageBuffers.CopyTo(packetBuffers.AsSpan(packetIdBuffers.Length));
    
    RequestSendBuffers(packetBuffers);
}

5. Latency Statistics Collection

// appsettings.json configuration
{
  "LatencyStats": {
    "EnableConsoleOutput": true,
    "EnableFileOutput": true,
    "OutputDirectory": "Stats",
    "OutputFilePrefix": "latency_stats"
  }
}

πŸš€ Getting Started

Prerequisites

  • .NET 10 SDK
  • Visual Studio 2022 or VS Code

Build and Run

# Build solution
dotnet build FastPortSharp.sln -c Release

# Run server
dotnet run --project FastPortServer -c Release

# Run client (in a new terminal)
dotnet run --project FastPortClient -c Release

Run Tests

dotnet test LibCommonTest

πŸ“ License

This project is licensed under the MIT License.


πŸ‘€ Developer

boinred

GitHub


πŸ’‘ This project is continuously improving. Feedbacks and contributions are welcome!

About

High-performance async TCP socket server/client framework for .NET 10 with IOCP, Channel<T>, ArrayPool, and Protocol Buffers. Optimized for game servers and real-time systems.

Topics

Resources

Stars

Watchers

Forks

Contributors