Engine-Pong/Game/Network/NetworkClient.cs

35 lines
900 B
C#
Raw Normal View History

using System;
using System.Collections.Generic;
2024-02-01 23:38:35 +03:00
using LiteNetLib;
using Syntriax.Engine.Core;
using Syntriax.Engine.Core.Abstract;
2024-02-02 12:04:53 +03:00
namespace Pong.Network;
2024-02-01 23:38:35 +03:00
public class NetworkClient : BehaviourOverride, INetworkClient
2024-02-01 23:38:35 +03:00
{
public EventBasedNetListener Listener { get; private set; } = null!;
public NetManager Manager { get; private set; } = null!;
2024-02-01 23:38:35 +03:00
public NetworkClient()
{
Priority = 10;
2024-02-01 23:38:35 +03:00
Listener = new EventBasedNetListener();
Manager = new NetManager(Listener);
2024-02-01 23:38:35 +03:00
}
public void Connect(string address, int port, string? password = null)
{
Manager.Start();
Manager.Connect(address, port, password ?? string.Empty);
}
public void PollEvents() => Manager.PollEvents();
public void Stop() => Manager.Stop();
2024-02-01 23:38:35 +03:00
protected override void OnUpdate() => PollEvents();
protected override void OnFinalize() => Stop();
2024-02-01 23:38:35 +03:00
}