forked from LeagueSandbox/GameServer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPacketServer.cs
94 lines (87 loc) · 3.63 KB
/
PacketServer.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
using ENet;
using GameServerCore;
using GameServerCore.Packets.Enums;
using GameServerCore.Packets.Handlers;
using GameServerCore.Packets.Interfaces;
using GameServerCore.Packets.PacketDefinitions;
using PacketDefinitions420.Exceptions;
using System;
using System.Collections.Generic;
namespace PacketDefinitions420
{
/// <summary>
/// Class responsible for the server's networking of the game.
/// </summary>
public class PacketServer
{
private Host _server;
private uint _serverHost = Address.IPv4HostAny;
private IGame _game;
protected const int PEER_MTU = 996;
/// <summary>
/// Networking encryption method.
/// </summary>
public BlowFish Blowfish { get; private set; }
/// <summary>
/// Interface of all functions related to sending and receiving packets.
/// </summary>
public IPacketHandlerManager PacketHandlerManager { get; private set; }
/// <summary>
/// Creates and sets up the host for the server as well as the packet handler manager.
/// </summary>
/// <param name="port">Port the server will be hosted on.</param>
/// <param name="blowfishKeys">Unique blowfish keys the server will use for encryption for each player.</param>
/// <param name="game">Game instance.</param>
/// <param name="netReq">Network request handler instance.</param>
/// <param name="netResp">Network response handler instance.</param>
public void InitServer(ushort port, Dictionary<long, string> blowfishKeys, IGame game, NetworkHandler<ICoreRequest> netReq, NetworkHandler<ICoreResponse> netResp)
{
_game = game;
_server = new Host();
_server.Create(new Address(_serverHost,port), 32, 32, 0, 0);
Dictionary<long, BlowFish> blowfishes = new Dictionary<long, BlowFish>();
foreach(var rawKey in blowfishKeys)
{
var key = Convert.FromBase64String(rawKey.Value);
if (key.Length <= 0)
{
throw new InvalidKeyException($"Invalid blowfish key supplied ({key})");
}
blowfishes.Add(rawKey.Key, new BlowFish(key));
}
PacketHandlerManager = new PacketHandlerManager(blowfishes, _server, game, netReq, netResp);
}
/// <summary>
/// The core networking loop which fires for connections, received packets, and disconnects.
/// </summary>
public void NetLoop()
{
while (_server.Service(0, out var enetEvent) > 0)
{
switch (enetEvent.Type)
{
case EventType.Connect:
{
// Set some defaults
enetEvent.Peer.Mtu = PEER_MTU;
enetEvent.Data = 0;
}
break;
case EventType.Receive:
{
var channel = (Channel)enetEvent.ChannelID;
PacketHandlerManager.HandlePacket(enetEvent.Peer, enetEvent.Packet, channel);
// Clean up the packet now that we're done using it.
enetEvent.Packet.Dispose();
}
break;
case EventType.Disconnect:
{
PacketHandlerManager.HandleDisconnect(enetEvent.Peer);
}
break;
}
}
}
}
}