28 lines
916 B
C#
28 lines
916 B
C#
using LiteNetLib.Utils;
|
|
|
|
namespace Syntriax.Engine.Network;
|
|
|
|
public class NetworkClient : NetworkBase, INetworkCommunicatorClient
|
|
{
|
|
private readonly NetDataWriter netDataWriter = new();
|
|
|
|
public void Connect(string address, int port, string? password = null)
|
|
{
|
|
if (!IsInUniverse)
|
|
throw new($"{nameof(NetworkClient)} must be in an universe to connect");
|
|
|
|
Manager.Start();
|
|
Manager.Connect(address, port, password ?? string.Empty);
|
|
}
|
|
|
|
public void SendToServer(INetworkPacket packet)
|
|
{
|
|
if (packet is not INetSerializable netSerializable)
|
|
throw new($"Packet trying to be sent to the server is not compatible with LiteNetLib");
|
|
|
|
netDataWriter.Reset();
|
|
netPacketProcessor.WriteNetSerializable(netDataWriter, ref netSerializable);
|
|
Manager.FirstPeer.Send(netDataWriter, LiteNetLib.DeliveryMethod.ReliableOrdered);
|
|
}
|
|
}
|