-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathServer.cs
More file actions
113 lines (94 loc) · 3.91 KB
/
Server.cs
File metadata and controls
113 lines (94 loc) · 3.91 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
using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
using System.Net.Sockets;
namespace GameServer
{
class Server
{
public static int MaxPlayers { get; private set; }
public static int Port { get; private set; }
public static Dictionary<int, Client> clients = new Dictionary<int, Client>();
private static TcpListener tcpListener;
private static UdpClient udpListener;
public delegate void PacketHandler(int _fromClient, Packet _packet);
public static Dictionary<int, PacketHandler> packetHandlers;
public static void Start(int _maxPlayers, int _port)
{
MaxPlayers = _maxPlayers;
Port = _port;
Console.WriteLine("Starting...");
InitializeServerData();
tcpListener = new TcpListener(IPAddress.Any, Port);
tcpListener.Start();
tcpListener.BeginAcceptTcpClient(new AsyncCallback(TCPConnectCallBack), null);
udpListener = new UdpClient(Port);
udpListener.BeginReceive(UDPReceiveCallback, null);
Console.WriteLine($"Server started on Port: {Port}");
}
private static void TCPConnectCallBack(IAsyncResult _result)
{
TcpClient _client = tcpListener.EndAcceptTcpClient(_result);
tcpListener.BeginAcceptTcpClient(new AsyncCallback(TCPConnectCallBack), null);
Console.WriteLine($"Incoming connection from {_client.Client.RemoteEndPoint}");
for (int i = 1; i < MaxPlayers; i++)
{
if (clients[i].tcp.socket == null)
{
clients[i].tcp.Connect(_client);
return;
}
}
Console.WriteLine($"{_client.Client.RemoteEndPoint} failed to connect: Server full!");
}
private static void UDPReceiveCallback(IAsyncResult _result){
try {
IPEndPoint _clientEndPoint = new IPEndPoint(IPAddress.Any, 0);
byte[] _data = udpListener.EndReceive(_result, ref _clientEndPoint);
udpListener.BeginReceive(UDPReceiveCallback, null);
if(_data.Length < 4) {
return;
}
using(Packet _packet = new Packet(_data)) {
int _clientId = _packet.ReadInt();
if(_clientId == 0) {
return;
}
if(clients[_clientId].udp.endPoint == null) {
clients[_clientId].udp.Connect(_clientEndPoint);
return;
}
if(clients[_clientId].udp.endPoint.ToString() == _clientEndPoint.ToString()) {
clients[_clientId].udp.HandleData(_packet);
}
}
}
catch (Exception e){
Console.WriteLine($"Error recieving data via UDP: {e}");
}
}
public static void SendUDPData(IPEndPoint _clientEndPoint, Packet _packet) {
try {
if(_clientEndPoint != null) {
udpListener.BeginSend(_packet.ToArray(), _packet.Length(), _clientEndPoint, null, null);
}
}
catch(Exception e) {
Console.WriteLine($"Error sending data to {_clientEndPoint} via UDP: {e}");
}
}
private static void InitializeServerData()
{
for (int i = 1; i <= MaxPlayers; i++)
{
clients.Add(i, new Client(i));
}
packetHandlers = new Dictionary<int, PacketHandler>() {
{(int)ClientPackets.welcomeReceived, ServerHandle.WelcomeReceived},
{(int)ClientPackets.playerMovement, ServerHandle.PlayerMovement},
};
Console.WriteLine("Packets initialized");
}
}
}