Giter Site home page Giter Site logo

xt.mnet's Introduction

XT.MNet TCP Server/Client

NuGet

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:

Remarks on used technologies

  • 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

Simple Usage

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.

Creation of a TCP Server

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();

Connect / Disconnect (Connect event is after successful handshake)

server.OnConnect += (connection) => {
    ...
};

server.OnDisconnect += (connection) => {
    ...
};

Register event handler for raw bytes messages

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]));

});

Register Server Raw Data

server.On((buffer, connection) => {

    Console.WriteLine($" {DateTime.Now} Recieve: {Encoding.UTF8.GetString(buffer)} ");


});

Creation of a TCP Client

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();

Connect / Disconnect (Connect event is after successful handshake)

client.OnConnect += () => {
    ...
};

client.OnDisconnect += () => {
    ...
};

Register event handler for raw bytes messages

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]));

});

Register Raw

client.On(data =>
{
    var str = Encoding.UTF8.GetString(data);
    Console.WriteLine($"{DateTime.Now} {str}");
});

xt.mnet's People

Contributors

strongq avatar

Watchers

 avatar

xt.mnet's Issues

some question

hi.
i have a game server in vb.net.
i want to use your library.i have some questions.
1-i need to remove silent client .if client not send data over 10 min server most disconnect client from server automaticly.can this library do?
2-is it multi client server?i have about 3000 client at the same time.
3-is your library better than hpsocket and others?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.