-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathNetworkHandler.h
65 lines (52 loc) · 1.79 KB
/
NetworkHandler.h
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
#pragma once
#include "IrrHandling.h"
#include <thread>
#include "Packet.h"
class NetworkHandler
{
public:
NetworkHandler();
bool initialize(); // Initialize ENet handling
bool shutdown(); // Shutdown networking system, disconnect from servers, stop hosting etc.
void handle(IrrHandling* m); // Handle events if initialized
void setVerbose(bool enable); // Enable/disable networking warnings
std::unordered_map<enet_uint16, ENetPeer*>& getPeerMap();
// Server/Hosting
void hostServer(std::string ip, int port, int maxClients, int maxChannels); // Calls NetworkServer.OnHosted on completion, NetworkServer.OnHostFail on fail
bool stopHosting();
void setBandwidthLimit(int incoming, int outgoing);
bool isHosting();
int getServerIP();
int getPort();
void setUseRangeEncoder(bool enable);
ENetHost* getHost();
ENetHost* getClient();
ENetPeer* getPeer();
int getPeerState(int peerID);
int getPeerPing(int peerID);
void forceDisconnectClient(int peerID, int reasonCode);
// Client
bool createClient(int outgoing, int channels);
void connectClient(std::string address, int port, int channels);
void disconnectClient();
bool destroyClient();
bool isClientConnected();
// Packets
void sendPacketToServer(const Packet& p, int channel, bool tcp);
void sendPacketToPeer(int peerID, const Packet& p, int channel, bool tcp);
void sendPacketToAll(const Packet& p, int channel, bool tcp);
bool initialized = false;
bool verbose = false;
bool finished = false;
private:
// Server
ENetHost* server = nullptr;
// Client
ENetHost* client = nullptr;
ENetPeer* peer = nullptr;
std::thread netServerThread;
std::thread netClientThread;
std::unordered_map<enet_uint16, ENetPeer*> peerMap;
};
void netBodyServer(NetworkHandler* n, IrrHandling* m);
void netBodyClient(NetworkHandler* n, IrrHandling* m);