PurpleSofa is 'Asynchronous Programming Model (APM)' socket wrapper library,
with 'Task-based Asynchronous Pattern (TAP)' at callback methods.
Otherwise, APM and TAP mixed.
Sync methods (OnOpen, OnMessage and OnClose) are disallowed for async override.
If you want to use 'async',
Async methods (OnOpenAsync, OnMessageAsync and OnCloseAsync) are override with 'UseAsyncCallback = true'.
- Callback for
- 'OnOpen or OnOpenAsync' (accepted or connected)
- 'OnMessage or OnMessageAsync' (received)
- 'OnClose or 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'.
- 'OnClose or OnCloseAsync' execution is taken via queue in order to avoid simultaneously many 'close'.
public class Callback : PsCallback
{
private const string Key = "inc";
public override void OnOpen(PsSession session)
{
Console.WriteLine($"OnOpen {session}");
session.SetValue(Key, 0);
session.ChangeIdleMilliSeconds(5000);
int inc = session.GetValue<int>(Key);
session.Send($"inc: {inc}");
}
public override void OnMessage(PsSession session, byte[] message)
{
Console.WriteLine($"OnMessage {session} {Encoding.UTF8.GetString(message)}");
int inc = session.GetValue<int>(Key);
inc++;
session.SetValue(Key, inc);
session.Send($"inc: {inc}");
if (inc > 3) session.Close();
}
public override void OnClose(PsSession session, PsCloseReason closeReason)
{
session.ClearValue(Key);
int inc = session.GetValue<int>(Key);
Console.WriteLine($"OnClose {session} {closeReason}, inc:{inc}");
}
}
public class AsyncCallback : PsCallback
{
private const string Key = "inc";
public override bool UseAsyncCallback { get; init; } = true;
public override async Task OnOpenAsync(PsSession session)
{
Console.WriteLine($"OnOpen {session}");
session.SetValue(Key, 0);
session.ChangeIdleMilliSeconds(5000);
int 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)}");
int 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);
int inc = session.GetValue<int>(Key);
Console.WriteLine($"OnClose {session} {closeReason}, inc:{inc}");
return Task.CompletedTask;
}
}
public static void Main(string[] args)
{
var server = new PsServer(new Callback());
server.Start();
server.WaitFor();
// --- another thread
// server.Shutdown();
}
public static void Main(string[] args)
{
var client = new PsClient(new Callback(), "127.0.0.1", 8710);
client.Connect();
// ...
client.Disconnect();
}
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::
.
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 enable to connect to server like v4.
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();