Skip to content

shigenobu/OrangeCabinet

Repository files navigation

OrangeCabinet - C# .NET async udp server & client

nuget .NET CI codecov License

feature

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

  • Callback is below.
    • 'IncomingAsync' (received)
    • 'TimeoutAsync' (timeout)
    • 'ShutdownAsync' (shutdown)
  • Can store user value in remote.
  • Check timeout at regular intervals by last receive time.
  • Client bind too, not connect. So, previously known client port.

how to use

callback

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

    public override async Task IncomingAsync(OcRemote remote, byte[] message)
    {
        Console.WriteLine($"Received: {Encoding.UTF8.GetString(message)} ({remote})");

        var inc = remote.GetValue<int>(Key);
        inc++;
        remote.SetValue(Key, inc);

        await remote.SendAsync($"{inc}");
        if (inc > 10)
        {
            remote.ClearValue(Key);
            remote.Escape();
        }
    }

    public override Task TimeoutAsync(OcRemote remote)
    {
        Console.WriteLine($"Timeout: {remote}");
        return Task.CompletedTask;
    }

    public override Task ShutdownAsync(OcRemote remote)
    {
        Console.WriteLine($"Shutdown: {remote}");
        return Task.CompletedTask;
    }
}

for server (ip v4)

public static async Task Main(string[] args)
{
    var serverBinder = new OcBinder(new SampleCallback())
    {
        BindPort = 8710,
    };
    var server = new OcLocal(serverBinder);
    server.Start();
    await server.SendToAsync("0", new IPEndPoint(IPAddress.Parse("127.0.0.1"), 8710));  // Send from server to some endpoint what you hope.
    server.WaitFor();
    // ...
    server.Shutdown();
}

for client (ip v4)

public static async Task Main(string[] args)
{
    using var clientBinder = new OcBinder(new Callback())
    {
        BindPort = 18710,
    };
    var client = new OcRemote(clientBinder, "127.0.0.1", 8710);
    for (int j = 0; j < 3; j++)
    {
        await client.SendAsync($"{j}");
    }
}

for server (ip v6)

public static async Task Main(string[] args)
{
    var serverBinder = new OcBinder(new SampleCallback())
    {
        SocketAddressFamily = OcSocketAddressFamily.Ipv6,
        BindPort = 8710,
    };
    var server = new OcLocal(serverBinder);
    server.Start();
    await server.SendToAsync("0", new IPEndPoint(IPAddress.Parse("::1"), 8710));  // Send from server to some endpoint what you hope.
    server.WaitFor();
    // ...
    server.Shutdown();
}

for client (ip v6)

public static async Task Main(string[] args)
{
    using var clientBinder = new OcBinder(new Callback())
    {
        SocketAddressFamily = OcSocketAddressFamily.Ipv6,
        BindPort = 18710,
    };
    var client = new OcRemote(clientBinder, "::1", 8710);
    for (int j = 0; j < 3; j++)
    {
        await client.SendAsync($"{j}");
    }
}

About

Async udp server and client library.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published