-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathClient.cs
More file actions
239 lines (208 loc) · 7.88 KB
/
Client.cs
File metadata and controls
239 lines (208 loc) · 7.88 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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
using System.Collections.Generic;
using System.Threading;
using ENet;
using Packet = tbsgNetLib.Packet;
namespace tbsgNetLib{
internal struct NetEvent
{
public NetEvent(EventType type, Packet packet)
{
this.type = type;
this.packet = packet;
}
public NetEvent(EventType type) : this()
{
this.type = type;
this.packet = new Packet();
}
public ENet.EventType type;
public Packet packet;
};
public abstract class Client
{
protected Client()
{
this.netEventQueue = new Queue<NetEvent>();
}
public void Init()
{
Library.Initialize();
this.host = new Host();
readThread = new Thread(new ThreadStart(ReceivePackets));
readThread.IsBackground = true;
}
public void DeInt()
{
StopThread();
}
private void StopThread()
{
if (readThread.IsAlive)
{
readThread.Abort();
}
Library.Deinitialize();
}
public void Connect(string ip,ushort port,ulong connectionId){
host.Create(null, 1);
var address = new Address();
address.SetHost(ip);
address.Port = port;
peer = host.Connect(address, 200, 1234);
Log("[NetLib] connect to server "+ip+":"+port);
readThread.Start();
}
public void Disconnect(){
if (IsConnected) {
peer.DisconnectNow(0);
}
}
public void SendPacket(NetCommands command){
Packet sending = new Packet();
sending.Write((uint)command);
peer.Send(0, sending.Bytes);
}
public void SendPacket(NetCommands command,Packet packet)
{
Packet sending = new Packet();
sending.Write((uint) command);
sending.Write(packet);
peer.Send(0, sending.Bytes);
}
public void ReceivePackets()
{
while (host.Service(1) >= 0)
{
Event @event;
while (host.CheckEvents(out @event) > 0)
{
Log("[NetLib] Client: revived " + @event.Type.ToString());
switch (@event.Type)
{
case ENet.EventType.Connect:
Log("[NetLib] Connect");
isConnected = true;
netEventQueue.Enqueue(new NetEvent(@event.Type));
break;
case ENet.EventType.Disconnect:
Log("[NetLib] Disconnect");
isConnected = false;
netEventQueue.Enqueue(new NetEvent(@event.Type));
break;
case ENet.EventType.Receive:
var data = @event.Packet.GetBytes();
Packet packet = new Packet(data);
netEventQueue.Enqueue(new NetEvent(@event.Type, packet));
@event.Packet.Dispose();
break;
}
}
}
}
public void HandleEvents()
{
lock (netEventQueue)
{
while (netEventQueue.Count != 0)
{
NetEvent netEvent = netEventQueue.Dequeue();
switch (netEvent.type)
{
case ENet.EventType.None:
break;
case ENet.EventType.Connect:
{
isConnected = true;
OnConnect();
OnConnectEvent?.Invoke();
}
break;
case ENet.EventType.Receive:
{
HandleAnyPacket(netEvent.packet);
}
break;
case ENet.EventType.Disconnect:
{
isConnected = false;
OnDisconnect();
OnDisconnectEvent?.Invoke();
}
break;
}
}
}
}
public void HandleAnyPacket(Packet packet)
{
uint commandInt = packet.ReadUint();
var command = (NetCommands)commandInt;
if (command == NetCommands.CustomCommand)
{
uint customCommand = packet.ReadUint();
Log("[NetLib] handling customCommand " + customCommand.ToString());
HandleCustomPacket(customCommand, packet);
}
else
{
Log("[NetLib] handling NetCommands " + ((NetCommands)command).ToString());
HandlePacket(command, packet);
}
}
private void HandlePacket(NetCommands command, Packet packet)
{
switch (command)
{
case NetCommands.IdentifySuccessful:
isIdentified = true;
Log("[NetLib] Identify successful");
OnIdentificationSuccess();
OnIdentificationSuccessEvent?.Invoke();
break;
case NetCommands.IdentifyFailure:
isIdentified = false;
Log("[NetLib] Identify failure: The server didn't accept your access token! It might be already in use or it has expired. Please retry logging in.");
var failuerCode = (IdentifyResponse) packet.ReadUint();
OnIdentificationFailure(failuerCode);
OnIdentificationFailureEvent?.Invoke(failuerCode);
break;
case NetCommands.HandshakeSuccess:
Log("[NetLib] Handshake successful");
var identificationPacket = GetIdentity();
SendPacket(NetCommands.Identify, identificationPacket);
Log("[NetLib] Identifying...");
break;
default:
Log("[NetLib] command was not implemented");
break;
}
}
public abstract void OnConnect();
public abstract void OnDisconnect();
public abstract void OnIdentificationSuccess();
public abstract void OnIdentificationFailure(IdentifyResponse identifyResponse);
protected abstract Packet GetIdentity();
protected abstract void HandleCustomPacket(uint customCommand, Packet packet);
protected abstract void Log(string msg);
public bool IsConnected => isConnected;
public bool IsIdentified => isIdentified;
private Host host;
private Peer peer;
private Connection connection;
private Queue<NetEvent> netEventQueue;
private Thread readThread;
private bool isConnected = false;
private bool isIdentified = false;
//events:
public struct Delegates {
public delegate void OnConnect();
public delegate void OnDisconnect();
public delegate void OnIdentificationSuccess();
public delegate void OnIdentificationFailure(IdentifyResponse identifyResponse);
}
public static event Delegates.OnConnect OnConnectEvent;
public static event Delegates.OnDisconnect OnDisconnectEvent;
public static event Delegates.OnIdentificationSuccess OnIdentificationSuccessEvent;
public static event Delegates.OnIdentificationFailure OnIdentificationFailureEvent;
}
}