Fork From MarvinDrude
Just a small lightweight library for TCP Communication in .NET/C#. It utilizes some techniques from internal kestrel sockets for some performance benefits. Some notes on things used:
- Uses some "stackalloc" and "MemoryPool.Shared" for less heap allocations
- Usage of "System.IO.Pipelines" for better buffering
- Custom schedulers for Tasks
- Custom "PinnedBlockMemoryPool" from kestrel
- Expression Compilation for better performance of events
- For Secure Ssl falls back to SslStream under the hood, but still amazingly fast
You should always first register all the event handlers before calling Server.Start/Client.Connect, in order for you to not miss any instant messages.
var server = new TcpServer(new TcpServerOptions() {
Address = "127.0.0.1",
Port = 43434,
Logger = debugLogger, // ILogger of your liking, default is just console one
SpecialChar='#' // only work for raw data
});
server.Start();
server.OnConnect += (connection) => {
...
};
server.OnDisconnect += (connection) => {
...
};
server.On("test-bytes", (buffer, connection) => {
// important, will only work by using ReadOnlyMemory<byte> here, not byte[], Memory<byte> etc.
Console.WriteLine("Length: " + buffer.Length);
// send a raw bytes message (important for sending must be of type Memory<byte>)
connection.Send("test-bytes", new Memory<byte>([0, 2, 3, 5]));
});
server.On((buffer, connection) => {
Console.WriteLine($" {DateTime.Now} Recieve: {Encoding.UTF8.GetString(buffer)} ");
});
var client = new TcpClient(new TcpClientOptions() {
Address = "127.0.0.1",
Port = 43434,
Logger = debugLogger, // ILogger of your liking, default is just console one
});
client.Connect();
client.OnConnect += () => {
...
};
client.OnDisconnect += () => {
...
};
client.On("test-bytes", (buffer) => {
// important, will only work by using ReadOnlyMemory<byte> here, not byte[], Memory<byte> etc.
Console.WriteLine("Length: " + buffer.Length);
// send a raw bytes message (important for sending must be of type Memory<byte>)
client.Send("test-bytes", new Memory<byte>([0, 2, 3, 5]));
});
client.On(data =>
{
var str = Encoding.UTF8.GetString(data);
Console.WriteLine($"{DateTime.Now} {str}");
});