MUN1Z/KingNetwork

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

Opened this issue · 3 comments

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?

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

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

@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.