-
Notifications
You must be signed in to change notification settings - Fork 4
4.SslByteMessageClient‐Server
ReferenceType edited this page Jul 15, 2024
·
3 revisions
It is build upon SSLClient/Server, where only difference is messages are sent with 4 byte length header. This allows us to send messages without fragmentation.
For detailed info about underlying system, please refer SSLClient/Server
- Byte fragmentation occurs when message size exceeds max MTU. Which is around 1500 bytes and can be lower.
- Each receiver unit on server and client side has stateful message extractor where full messages are extracted from fragmented bytes by using length info.
- Messages are guaranteed to reach destination atomically without fragmentation(As long as connection is alive).
//var scert = new X509Certificate2("server.pfx", "greenpass");
//var cert = new X509Certificate2("client.pfx", "greenpass");
var scert = CertificateGenerator.GenerateSelfSignedCertificate();
var cert = CertificateGenerator.GenerateSelfSignedCertificate();
var server = new SslByteMessageServer(20008, scert);
server.RemoteCertificateValidationCallback += (_, _, _, _) => { return true; };
server.OnBytesReceived += ServerBytesReceived;
// since certificate is self-signed it will give chain errors, here we bypass it
server.RemoteCertificateValidationCallback += (_, _, _, _) => { return true; };
server.StartServer();
var client = new SslByteMessageClient(cert);
client.OnBytesReceived += ClientBytesReceived;
client.RemoteCertificateValidationCallback += (a, b, c, d) => true;
client.Connect("127.0.0.1", 20008);
client.SendAsync(UTF8Encoding.UTF8.GetBytes("Hello I'm a client!"));
// Callback handlers
void ServerBytesReceived(Guid clientId, byte[] bytes, int offset, int count)
{
Console.WriteLine(UTF8Encoding.UTF8.GetString(bytes, offset, count));
server.SendBytesToClient(clientId, UTF8Encoding.UTF8.GetBytes("Hello I'm the server"));
}
void ClientBytesReceived(byte[] bytes, int offset, int count)
{
Console.WriteLine(UTF8Encoding.UTF8.GetString(bytes, offset, count));
}