-
Notifications
You must be signed in to change notification settings - Fork 1
/
NetworkController.cpp
83 lines (63 loc) · 2.13 KB
/
NetworkController.cpp
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
#include "NetworkController.h"
#include "cuboid_server.h"
#include "enet/enet.h"
#include <sstream>
NetworkController::NetworkController(CuboidServer* ParentEngine) :
root(ParentEngine)
{ initEnet(); };
NetworkController::~NetworkController() {
enet_host_destroy(enetHost);
enet_deinitialize();
}
void NetworkController::doTick() {
ENetEvent Event;
while( enet_host_service(enetHost, &Event, 0) > 0 ){
switch( Event.type ) {
case ENET_EVENT_TYPE_RECEIVE:
//we have recieved a packet
processPacket(Event.packet);
break;
case ENET_EVENT_TYPE_CONNECT:
break;
case ENET_EVENT_TYPE_DISCONNECT:
//TODO handle these
break;
};
}
}
void NetworkController::initEnet() {
if( enet_initialize() != 0){
root->fatalError("NetController", "Cannot initialise ENet");
};
root->sout << "Attempting to create host..." << endl;
if( root->confMgr.varExists("host_address") ){
enet_address_set_host( &enetAddress, root->confMgr.getVarS("host_address").c_str() );
root->sout << "Binding to configured address " << root->confMgr.getVarS("host_address") <<endl;
}else{
enetAddress.host = ENET_HOST_ANY;
root->sout << "Binding to any available address" << endl;
}
if( root->confMgr.varExists("host_port") ){
enetAddress.port = root->confMgr.getVarI("host_port");
}else{
enetAddress.port = HOST_PORT_DEFAULT;
}
root->sout << "Binding to port " << enetAddress.port << endl;
enetHost = enet_host_create(&enetAddress, (root->confMgr.varExists("max_clients")) ? (root->confMgr.getVarI("max_clients")) : MAX_CLIENTS_DEFAULT, 2, 0, 0);
if(enetHost == NULL) {
root->fatalError("NetController", "Cannot create ENet host");
}
}
void NetworkController::processPacket(ENetPacket* packet) {
uchar* data = packet->data;
CuboidNetProtocol type = static_cast<CuboidNetProtocol>(data[0]);
//TODO packet processing
/*if(type == NET_REQUEST_JOIN){
string name;
//string format is it's length followed by the string of chars
name.copy( (char*)&data[2], (size_t)data[1] );
root->sout << "Recieved NET_REQUEST_JOIN with name " << name << endl;
}else{
root->sout << "Recieved unknown packet." << endl;
}*/
}