forked from facebookarchive/RakNet
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFCM2HostTest.cpp
More file actions
169 lines (151 loc) · 5.11 KB
/
FCM2HostTest.cpp
File metadata and controls
169 lines (151 loc) · 5.11 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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
/*
* Copyright (c) 2014, Oculus VR, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*
*/
#include <cstdio>
#include <cstring>
#include <stdlib.h>
#include "GetTime.h"
#include "RakPeerInterface.h"
#include "MessageIdentifiers.h"
#include "RakNetTypes.h"
#include "RakSleep.h"
#include "FullyConnectedMesh2.h"
#include "ConnectionGraph2.h"
#include <assert.h>
#include "SocketLayer.h"
#include "Kbhit.h"
#include "PacketLogger.h"
#include "BitStream.h"
using namespace RakNet;
RakNet::RakPeerInterface *rakPeer;
int main()
{
FullyConnectedMesh2 fcm2;
ConnectionGraph2 cg2;
rakPeer=RakNet::RakPeerInterface::GetInstance();
rakPeer->AttachPlugin(&fcm2);
rakPeer->AttachPlugin(&cg2);
fcm2.SetAutoparticipateConnections(true);
RakNet::SocketDescriptor sd;
sd.socketFamily=AF_INET; // Only IPV4 supports broadcast on 255.255.255.255
sd.port=60000;
while (IRNS2_Berkley::IsPortInUse(sd.port, sd.hostAddress, sd.socketFamily, SOCK_DGRAM)==true)
sd.port++;
StartupResult sr = rakPeer->Startup(8,&sd,1);
RakAssert(sr==RAKNET_STARTED);
rakPeer->SetMaximumIncomingConnections(8);
rakPeer->SetTimeoutTime(1000,RakNet::UNASSIGNED_SYSTEM_ADDRESS);
printf("Our guid is %s\n", rakPeer->GetGuidFromSystemAddress(RakNet::UNASSIGNED_SYSTEM_ADDRESS).ToString());
printf("Started on %s\n", rakPeer->GetMyBoundAddress().ToString(true));
BitStream contextBs;
contextBs.Write(RakString("Our guid is %s\n", rakPeer->GetGuidFromSystemAddress(RakNet::UNASSIGNED_SYSTEM_ADDRESS).ToString()));
fcm2.SetMyContext(&contextBs);
// PacketLogger packetLogger;
// rakPeer->AttachPlugin(&packetLogger);
// packetLogger.SetLogDirectMessages(false);
bool quit=false;
RakNet::Packet *packet;
char ch;
while (!quit)
{
for (packet = rakPeer->Receive(); packet; rakPeer->DeallocatePacket(packet), packet = rakPeer->Receive())
{
switch (packet->data[0])
{
case ID_DISCONNECTION_NOTIFICATION:
// Connection lost normally
printf("ID_DISCONNECTION_NOTIFICATION\n");
break;
case ID_NEW_INCOMING_CONNECTION:
// Somebody connected. We have their IP now
printf("ID_NEW_INCOMING_CONNECTION from %s. guid=%s.\n", packet->systemAddress.ToString(true), packet->guid.ToString());
break;
case ID_CONNECTION_REQUEST_ACCEPTED:
// Somebody connected. We have their IP now
printf("ID_CONNECTION_REQUEST_ACCEPTED from %s. guid=%s.\n", packet->systemAddress.ToString(true), packet->guid.ToString());
break;
case ID_CONNECTION_LOST:
// Couldn't deliver a reliable packet - i.e. the other system was abnormally
// terminated
printf("ID_CONNECTION_LOST\n");
break;
case ID_ADVERTISE_SYSTEM:
if (packet->guid!=rakPeer->GetMyGUID())
rakPeer->Connect(packet->systemAddress.ToString(false), packet->systemAddress.GetPort(),0,0);
break;
case ID_FCM2_NEW_HOST:
{
if (packet->guid==rakPeer->GetMyGUID())
printf("Got new host (ourselves)");
else
printf("Got new host %s, GUID=%s", packet->systemAddress.ToString(true), packet->guid.ToString());
RakNet::BitStream bs(packet->data,packet->length,false);
bs.IgnoreBytes(1);
RakNetGUID oldHost;
bs.Read(oldHost);
// If oldHost is different then the current host, then we lost connection to the host
if (oldHost!=UNASSIGNED_RAKNET_GUID)
printf(". Oldhost Guid=%s\n", oldHost.ToString());
else
printf(". (First reported host)\n");
}
break;
// case ID_REMOTE_NEW_INCOMING_CONNECTION:
// {
// uint32_t count;
// RakNet::BitStream bsIn(packet->data, packet->length,false);
// bsIn.IgnoreBytes(1);
// bsIn.Read(count);
// SystemAddress sa;
// RakNetGUID guid;
// printf("ID_REMOTE_NEW_INCOMING_CONNECTION from %s\n", packet->systemAddress.ToString(true));
// for (uint32_t i=0; i < count; i++)
// {
// bsIn.Read(sa);
// bsIn.Read(guid);
//
// printf("%s ", sa.ToString(true));
// }
// printf("\n");
// }
}
}
if (kbhit())
{
ch=getch();
if (ch==' ')
{
DataStructures::List<RakNetGUID> participantList;
fcm2.GetParticipantList(participantList);
printf("%i participants\n", participantList.Size());
for (int i=0; i < participantList.Size(); i++)
{
BitStream userContext;
fcm2.GetParticipantContext(participantList[i], &userContext);
RakString str;
userContext.Read(str);
printf("%i. %s: %s", i+1, participantList[i].ToString(), str.C_String());
}
}
if (ch=='q' || ch=='Q')
{
printf("Quitting.\n");
quit=true;
}
}
RakSleep(30);
for (int i=0; i < 32; i++)
{
if (rakPeer->GetInternalID(RakNet::UNASSIGNED_SYSTEM_ADDRESS,0).GetPort()!=60000+i)
rakPeer->AdvertiseSystem("255.255.255.255", 60000+i, 0,0,0);
}
}
RakNet::RakPeerInterface::DestroyInstance(rakPeer);
return 0;
}