PurpleSofa 0.3.0

dotnet add package PurpleSofa --version 0.3.0
                    
NuGet\Install-Package PurpleSofa -Version 0.3.0
                    
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="PurpleSofa" Version="0.3.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="PurpleSofa" Version="0.3.0" />
                    
Directory.Packages.props
<PackageReference Include="PurpleSofa" />
                    
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 PurpleSofa --version 0.3.0
                    
#r "nuget: PurpleSofa, 0.3.0"
                    
#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.
#addin nuget:?package=PurpleSofa&version=0.3.0
                    
Install PurpleSofa as a Cake Addin
#tool nuget:?package=PurpleSofa&version=0.3.0
                    
Install PurpleSofa as a Cake Tool

PurpleSofa - C# .NET async tcp server & client

nuget .NET CI codecov License

feature

PurpleSofa is 'Asynchronous Programming Model (APM)' socket wrapper library,
with 'Task-based Asynchronous Pattern (TAP)' at callback methods.
Otherwise, APM and TAP mixed.

  • Callback for
    • 'OnOpenAsync' (accepted or connected)
    • 'OnMessageAsync' (received)
    • 'OnCloseAsync' (received none).
  • Can store user value in session.
  • Check timeout at regular intervals by last receive time. It's useful to detect 'half close'.
  • 'OnCloseAsync' execution is taken via queue in order to avoid simultaneously many 'close'.

how to use

callback

public class Callback : PsCallback
{
    private const string Key = "inc";

    public override async Task OnOpenAsync(PsSession session)
    {
        Console.WriteLine($"OnOpen {session}");
        session.SetValue(Key, 0);
        session.ChangeIdleMilliSeconds(5000);

        var inc = session.GetValue<int>(Key);
        await session.SendAsync($"inc:{inc}");
    }

    public override async Task OnMessageAsync(PsSession session, byte[] message)
    {
        Console.WriteLine($"OnMessage {session} {Encoding.UTF8.GetString(message)}");
        var inc = session.GetValue<int>(Key);
        inc++;
        session.SetValue(Key, inc);
        await session.SendAsync($"inc: {inc}");
        if (inc > 3) session.Close();
    }

    public override Task OnCloseAsync(PsSession session, PsCloseReason closeReason)
    {
        session.ClearValue(Key);
        var inc = session.GetValue<int>(Key);
        Console.WriteLine($"OnClose {session} {closeReason}, inc:{inc}");
        return Task.CompletedTask;
    }
}

for server (ip v4)

public static void Main(string[] args)
{
    var server = new PsServer(new Callback());
    server.Start();
    server.WaitFor();
    // --- another thread
    // server.Shutdown();
}

for client (ip v4)

public static void Main(string[] args)
{
    var client = new PsClient(new Callback(), "127.0.0.1", 8710);
    client.Connect();
    // ...
    client.Disconnect();
}

for server (ip v6)

public static void Main(string[] args)
{
    var server = new PsServer(new Callback())
    {
        SocketAddressFamily = PsSocketAddressFamily.Ipv6
    };
    server.Start();
    server.WaitFor();
    // --- another thread
    // server.Shutdown();
}
  • Ipv4 socket is treated as ipv6 socket.
  • If host address 0.0.0.0, changed to ::.

for client (ip v6)

public static void Main(string[] args)
{
    var client = new PsClient(new Callback(), PsSocketAddressFamily.Ipv6, "::1", 8710);
    // Below is no problem
    // var client = new PsClient(new Callback(), "127.0.0.1", 8710);
    client.Connect();
    // ...
    client.Disconnect();
}
  • Ipv4 socket is treated as ipv6 socket.
  • If server is listening on ipv6, client is able to connect to server like v4.

for multi client (ip v4, v6)

var multiClient = new PsMultiClient(new Callback());
multiClient.InitBundle();
for (int i = 0; i < 3; i++)
{
    // assume that server listening on 8710, 8711, 8712
    var clientConnection = multiClient.Connect("127.0.0.1", 8710 + i);
    // ip v6
    // var clientConnection = multiClient.Connect(PsSocketAddressFamily.Ipv6, "::1", 8710 + i);
    :
    multiClient.Disconnect(clientConnection);
}
multiClient.DestroyBundle();
Product Compatible and additional computed target framework versions.
.NET net8.0 is compatible.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed.  net9.0 is compatible.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 was computed.  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.
  • net8.0

    • No dependencies.
  • net9.0

    • No dependencies.

NuGet packages (1)

Showing the top 1 NuGet packages that depend on PurpleSofa:

Package Downloads
FluentNetting

Fluent forwarded server.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
0.3.0 140 5/5/2025
0.2.2 104 11/27/2024
0.2.1 105 11/22/2024
0.2.0 207 4/15/2024
0.1.1 466 12/8/2023
0.1.0 500 9/5/2023
0.0.9 660 1/3/2023
0.0.8 667 12/20/2022
0.0.7 785 12/12/2022
0.0.6 1,030 1/24/2022
0.0.5 1,030 1/13/2022
0.0.4 654 12/21/2021
0.0.3 678 12/10/2021
0.0.2 751 12/2/2021
0.0.1 734 12/2/2021