-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathClient.cs
More file actions
132 lines (113 loc) · 3.53 KB
/
Client.cs
File metadata and controls
132 lines (113 loc) · 3.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
using System.Net;
using System.Net.Sockets;
using LiteNetLib;
using SharpEngine.Network.Internal;
namespace SharpEngine.Network;
/// <summary>
/// SharpEngine Network Client
/// </summary>
public class Client
{
/// <summary>
/// Delegate for PacketReceived Event
/// </summary>
public delegate void ReceivePacket(dynamic packet);
/// <summary>
/// Delegate for PeerConnected Event
/// </summary>
public delegate void Connected();
/// <summary>
/// Delegate for ErrorReceived Event
/// </summary>
public delegate void NetworkError(IPEndPoint endPoint, SocketError error);
/// <summary>
/// Delegate for PeerDisconnected Event
/// </summary>
public delegate void Disconnected(NetPeer peer, DisconnectInfo info);
/// <summary>
/// List of all packets unknown by Client
/// </summary>
public List<Type> PacketTypes { get; } = [];
/// <summary>
/// If Client is Running
/// </summary>
public bool IsRunning { get; set; }
/// <summary>
/// Event when packet is received
/// </summary>
public event ReceivePacket? PacketReceived;
/// <summary>
/// Event when client is connected
/// </summary>
public event Connected? PeerConnected;
/// <summary>
/// Event when client is disconnected
/// </summary>
public event Disconnected? PeerDisconnected;
/// <summary>
/// Event when error is received
/// </summary>
public event NetworkError? ErrorReceived;
private readonly EventBasedNetListener _listener = new();
private readonly NetManager _client;
private readonly NetPeer _server;
/// <summary>
/// Create Client
/// </summary>
/// <param name="ip">Ip</param>
/// <param name="port">Port</param>
/// <param name="key">Connection Key</param>
public Client(string ip, int port, string key = "")
{
_client = new NetManager(_listener);
_client.Start();
_server = _client.Connect(ip, port, key);
_listener.NetworkReceiveEvent += ReceiveEvent;
_listener.PeerConnectedEvent += _ => PeerConnected?.Invoke();
_listener.NetworkErrorEvent += (endPoint, error) => ErrorReceived?.Invoke(endPoint, error);
_listener.PeerDisconnectedEvent += (peer, info) => PeerDisconnected?.Invoke(peer, info);
IsRunning = true;
}
/// <summary>
/// Send Packet to Server
/// </summary>
/// <param name="packet">Packet</param>
/// <typeparam name="T">Type of Packet</typeparam>
/// <exception cref="UnknownPacketException">Exception thrown when packet is unknown</exception>
public void SendPacket<T>(T packet)
where T : notnull
{
if (!PacketTypes.Contains(packet.GetType()))
throw new UnknownPacketException($"Unknown Packet : {packet.GetType()}");
Common.SendPacket(_server, packet);
}
/// <summary>
/// Update Client
/// </summary>
public void Update() => _client.PollEvents();
/// <summary>
/// Shutdown Client
/// </summary>
public void Shutdown()
{
_client.Stop();
IsRunning = false;
}
private void ReceiveEvent(
NetPeer peer,
NetPacketReader reader,
byte channel,
DeliveryMethod deliveryMethod
)
{
var packetType = reader.GetString();
foreach (var type in PacketTypes)
{
if (packetType == type.Name)
{
PacketReceived?.Invoke(Common.ReadPacket(reader, packetType, type));
break;
}
}
}
}