-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLobby.cpp
81 lines (74 loc) · 1.6 KB
/
Lobby.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
#include <iostream>
#include <cstring>
#include "MessageIdentifiers.h"
#include "Lobby.hh"
Lobby::Lobby(bool isServer) : mIsServer(isServer),
mPeer(RakNet::RakPeerInterface::GetInstance())
{
if (mIsServer)
{
mSocket.port = 4242;
}
else
{
mSocket.port = 4243;
}
}
Lobby::~Lobby()
{
RakNet::RakPeerInterface::DestroyInstance(mPeer);
}
void Lobby::start()
{
mPeer->Startup(1, &mSocket, 1);
if (mIsServer)
{
mPeer->SetMaximumIncomingConnections(1);
waitForClient();
}
else
{
searchForLobby();
}
}
void Lobby::searchForLobby()
{
bool foundLobby = false;
RakNet::Packet *packet;
while (!foundLobby)
{
mPeer->Ping("255.255.255.255", 4242, false);
for (packet = mPeer->Receive(); packet; mPeer->DeallocatePacket(packet), packet = mPeer->Receive())
{
switch(packet->data[0])
{
case ID_UNCONNECTED_PONG:
std::cout << "Lobby found ! : " << packet->systemAddress.ToString() << std::endl;
foundLobby = true;
break;
default :
std::cout << "UnIdentified packet : " << packet->systemAddress.ToString() << std::endl;
}
}
}
}
void Lobby::waitForClient()
{
bool foundClient = false;
RakNet::Packet *packet;
while (!foundClient)
{
for (packet = mPeer->Receive();packet;mPeer->DeallocatePacket(packet), packet = mPeer->Receive())
{
switch(packet->data[0])
{
case ID_UNCONNECTED_PING:
std::cout << "Client found !" << std::endl;
foundClient = true;
break;
default :
std::cout << "UnIdentified packet" << std::endl;
}
}
}
}