MUN1Z / KingNetwork

KingNetwork is an open source library to facilitate the creation and communication of clients and servers via TCP, UDP, WebSocket and RUDP sockets.
https://github.com/Mun1z/KingNetwork
MIT License
99 stars 21 forks source link

Can a client use both unreliable and reliable UDP in the same instance? #9

Open bag7dad opened 8 months ago

bag7dad commented 8 months ago

In Unity, most data syncing occurs unreliably, but sometimes there is a need to send reliable data to the same client within the same room. I think it's better to add options with the send() method, such as sendOptions.reliable and sendOptions.unreliable. Also, is sendOptions.reliable sequenced?

MUN1Z commented 8 months ago

Hello @medozeus, yes you can use the RUDP mode, and use unreliable in the same time, the RUDP implementation run two sockets in the same time, one in TCP and another in UDP. You can see the example in Reame RUDP section, observe the Enum "RudpMessageType.Reliable".

Using the RUDP connection on KingServer

// create and start the server
var server = new KingServer(listenerType: NetworkListenerType.RUDP, port: 7171);
server.MessageReceivedHandler = OnMessageReceived;

//ASync execution
await server.StartAsync(); //You can pass a out var cancellationToken in parameter
//Sync execution
server.Start();

// implements the callback for MessageReceivedHandler
private void OnMessageReceived(IClient client, IKingBufferReader reader)
{
    Console.WriteLine($"Received data from client {client.Id}, data length {reader.Length()}");
}

// send a message to all clients
using(var writer = KingBufferWriter.Create())
{
    writer.Write("Test message!");
    //You can use RudpMessageType.Reliable to send Reliable messages and RudpMessageType.Unreliable to send Unreliable messages
    server.SendMessageToAll(writer, RudpMessageType.Reliable);
}

// stop the server when you don't need it anymore
server.Stop();

Using the RUDP connection on KingClient

// create and connect the client
var client = new KingClient(listenerType: NetworkListenerType.RUDP);
client.MessageReceivedHandler = OnMessageReceived;
client.Connect("127.0.0.1", 7171);

// implements the callback for MessageReceivedHandler
private void OnMessageReceived(IKingBufferReader reader)
{
    Console.WriteLine($"Received data from server, data length {reader.Length()}");
}

/// send a message to server
using(var writer = KingBufferWriter.Create()
{
    writer.Write("Test message!");
    //You can use RudpMessageType.Reliable to send Reliable messages and RudpMessageType.Unreliable to send Unreliable messages
    client.SendMessage(writer, RudpMessageType.Reliable);
}

// disconnect from the server when we are done
client.Disconnect();
bag7dad commented 8 months ago

This method consumes significant resources, requiring two servers and two clients.

MUN1Z commented 8 months ago

@medozeus Creating a socket instance consumes practically nothing of the machine, which makes something consume is to be traveling packages. And that you would already do being a connected or one, the amount of data you will travel would be the same in both occasions, only changes that part of them is in one connection and another part in another connection.