-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Client.cs
121 lines (100 loc) · 3.42 KB
/
Client.cs
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
/**
* Copyright (C) 2020 Quaint Studios, Kristopher Ali (Makosai) <kristopher.ali.dev@gmail.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
namespace Sustenet.Clients
{
using System.Net;
using System.Collections.Generic;
using Transport;
using Network;
/// <summary>
/// A standard client that connects to a server.
/// </summary>
public class Client : BaseClient
{
public IPAddress ip;
public ushort port;
private Packet receivedData;
protected delegate string PacketHandler(Packet packet);
protected static Dictionary<int, PacketHandler> packetHandlers;
public Client(string _ip = "127.0.0.1", ushort _port = 6256, bool debug = true) : base(0, debug)
{
ip = IPAddress.Parse(_ip);
port = _port;
tcp.onConnected.Run += () => {
receivedData = new Packet();
};
tcp.onReceived.Run += (data) => {
receivedData.Reset(HandleData(data));
};
InitializeClientData();
}
/// <summary>
/// Connects to the currently assigned IP and port.
/// </summary>
public void Connect()
{
tcp.Connect(ip, port);
}
#region Data Functions
private bool HandleData(byte[] data)
{
int packetLength = 0;
receivedData.SetBytes(data);
if(receivedData.UnreadLength() >= 4)
{
packetLength = receivedData.ReadInt();
if(packetLength <= 0)
{
return true;
}
}
while(packetLength > 0 && packetLength <= receivedData.UnreadLength())
{
byte[] packetBytes = receivedData.ReadBytes(packetLength);
ThreadManager.ExecuteOnMainThread(() => {
using(Packet packet = new Packet(packetBytes))
{
int packetId = packet.ReadInt();
packetHandlers[packetId](packet);
}
});
packetLength = 0;
if(receivedData.UnreadLength() >= 4)
{
packetLength = receivedData.ReadInt();
if(packetLength <= 0)
{
return true;
}
}
}
if(packetLength <= 1)
{
return true;
}
return false;
}
protected virtual void InitializeClientData()
{
packetHandlers = new Dictionary<int, PacketHandler>()
{
{ (int)ServerPackets.welcome, this.Welcome }
};
}
#endregion
}
}