Ledon.Sockets.Server 0.0.1

dotnet add package Ledon.Sockets.Server --version 0.0.1
                    
NuGet\Install-Package Ledon.Sockets.Server -Version 0.0.1
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Ledon.Sockets.Server" Version="0.0.1" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Ledon.Sockets.Server" Version="0.0.1" />
                    
Directory.Packages.props
<PackageReference Include="Ledon.Sockets.Server" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add Ledon.Sockets.Server --version 0.0.1
                    
#r "nuget: Ledon.Sockets.Server, 0.0.1"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package Ledon.Sockets.Server@0.0.1
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=Ledon.Sockets.Server&version=0.0.1
                    
Install as a Cake Addin
#tool nuget:?package=Ledon.Sockets.Server&version=0.0.1
                    
Install as a Cake Tool

Ledon.Sockets.Server

协议服务端:TCP/UDP 监听、连接管理、类型化分发。与 Ledon.Protocols(解析)、 Ledon.Sockets.Client(客户端)分开——按需引用,互不拖累。

builder.Services.AddLedonSocketServer(
    o => { o.Port = 9000; o.BufferSize = 8192; },
    new LengthPrefixedFrameCodec(),
    new MyHandler());

连接处理器

public class MyHandler : ConnectionHandlerBase
{
    public override async Task OnConnectedAsync(ConnectionContext connection, CancellationToken ct)
    {
        // 登录态、限流计数都放 Items 里
        connection.Items["loginAt"] = DateTimeOffset.UtcNow;
        await connection.SendPacketAsync(new Welcome("hi"), ct);
    }

    public override async Task OnFrameAsync(ConnectionContext connection, ReadOnlyMemory<byte> frame, CancellationToken ct)
    {
        var packet = PacketSerializer<LoginRequest>.Deserialize(frame.Span);
        await connection.SendPacketAsync(new LoginReply(true), ct);
    }

    public override Task OnDisconnectedAsync(ConnectionContext connection, Exception? error, CancellationToken ct)
    {
        // 资源清理点(连接级状态在这里收尾)
        return Task.CompletedTask;
    }
}
  • 每连接一个独立读取循环 + 独立 DI 作用域(connection.Services 拿 DbContext/仓储);
  • 帧按到达顺序处理;单帧处理失败只记录不断开;ProtocolException 断开该连接;
  • 半包自动拼接;缓冲上限保护(默认 1MB,超过视为对端异常);
  • connection.SendPacketAsync(packet):包编解码 + 帧编码一步完成。

类型化分发(按包类型路由)

[PacketType(1)]
public struct LoginRequest { /* [PacketField] ... */ }

public class LoginRequestHandler : IPacketHandler<LoginRequest>
{
    public Task HandleAsync(ConnectionContext connection, LoginRequest packet, CancellationToken ct) { ... }
}

// 启动时
builder.Services.AddLedonSocketServer(o => o.Port = 9000, new LengthPrefixedFrameCodec(), new LoggingConnectionHandler(...));
builder.Services.AddLedonPacketHandlers();   // 扫描 + 注册 + 编译解码委托

帧布局约定:前 2 字节 = 类型标识(小端),其余 = 包体。协议不同就自己实现 IPacketDispatcher。

UDP 服务端

builder.Services.AddLedonSocketUdpServer(o => o.Port = 9001, new MyDatagramHandler());

public class MyDatagramHandler : IUdpHandler
{
    public Task OnDatagramAsync(UdpContext datagram, CancellationToken ct = default)
    {
        // UDP 无连接、无顺序保证:本方法并发调用,实现须线程安全
        return datagram.ReplyAsync(datagram.Data, ct);   // 回显
    }
}

完整文档:https://github.com/wosledon/ledon-framework

Product Compatible and additional computed target framework versions.
.NET net10.0 is compatible.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages (1)

Showing the top 1 NuGet packages that depend on Ledon.Sockets.Server:

Package Downloads
Ledon.Sip.Server

Ledon SIP 服务端:基于 Socket 库的 SIP over UDP/TCP 监听与应答。

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
0.0.1 49 10/28/2026