-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathDAPIProtoClient.cpp
More file actions
221 lines (186 loc) · 6.3 KB
/
DAPIProtoClient.cpp
File metadata and controls
221 lines (186 loc) · 6.3 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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
#include "DAPIProtoClient.h"
namespace DAPI
{
DAPIProtoClient::DAPIProtoClient() : mt(static_cast<unsigned int>(std::chrono::system_clock::now().time_since_epoch().count()))
{
udpbound = false;
connectionPort = 1025;
}
void DAPIProtoClient::checkForConnection()
{
if (isConnected())
return;
sf::Packet packet;
udpSocket.setBlocking(false);
if (!udpbound)
{
if (udpSocket.bind(1024, sf::IpAddress::Any) != sf::Socket::Status::Done)
return;
udpbound = true;
}
auto sender = sf::IpAddress::Any;
auto port = udpSocket.getLocalPort();
if (udpSocket.receive(packet, sender, port) != sf::Socket::Done)
return;
auto size = packet.getDataSize();
std::unique_ptr<char[]> packetContents(new char[size]);
memcpy(packetContents.get(), packet.getData(), size);
auto currentMessage = std::make_unique<dapi::message::Message>();
currentMessage->ParseFromArray(packetContents.get(), static_cast<int>(size));
if (!currentMessage->has_initbroadcast())
return;
auto reply = std::make_unique<dapi::message::Message>();
auto initResponse = reply->mutable_initresponse();
initResponse->set_port(static_cast<int>(connectionPort));
packet.clear();
size = reply->ByteSize();
std::unique_ptr<char[]> buffer(new char[size]);
reply->SerializeToArray(&buffer[0], size);
packet.append(buffer.get(), size);
udpSocket.send(packet, sender, port);
udpSocket.unbind();
udpbound = false;
tcpListener.accept(tcpSocket);
return;
}
void DAPIProtoClient::lookForServer()
{
if (isConnected())
return;
sf::Packet packet;
bool skip = false;
auto broadcastMessage = std::make_unique<dapi::message::Message>();
auto initResponse = broadcastMessage->mutable_initbroadcast();
auto size = broadcastMessage->ByteSize();
std::unique_ptr<char[]> buffer(new char[size]);
broadcastMessage->SerializeToArray(&buffer[0], size);
packet.append(buffer.get(), size);
sf::IpAddress server = sf::IpAddress::Broadcast;
unsigned short port = 1024;
udpSocket.send(packet, server, port);
server = sf::IpAddress::Any;
udpSocket.setBlocking(false);
// Sleep to give backend a chance to send the packet.
{
using namespace std::chrono_literals;
std::this_thread::sleep_for(2s);
}
if (udpSocket.receive(packet, server, port) == sf::Socket::Done)
{
size = packet.getDataSize();
std::unique_ptr<char[]> replyBuffer(new char[size]);
memcpy(replyBuffer.get(), packet.getData(), size);
auto currentMessage = std::make_unique<dapi::message::Message>();
currentMessage->ParseFromArray(replyBuffer.get(), size);
if (!currentMessage->has_initresponse())
return;
connectionPort = static_cast<unsigned short>(currentMessage->initresponse().port());
tcpSocket.connect(server, connectionPort);
if (tcpSocket.getRemoteAddress() == sf::IpAddress::None)
fprintf(stderr, "%s", "Connection failed.\n");
}
}
void DAPIProtoClient::transmitMessages()
{
// Check that we are connected to a game server.
if (!isConnected())
return;
std::unique_ptr<dapi::message::Message> currentMessage;
sf::Packet packet;
// Loop until the message queue is empty.
while (!messageQueue.empty())
{
packet.clear();
currentMessage = std::move(messageQueue.front());
messageQueue.pop_front();
auto size = currentMessage->ByteSize();
if (size > 0)
{
std::unique_ptr<char[]> buffer(new char[size]);
currentMessage->SerializeToArray(&buffer[0], size);
packet.append(buffer.get(), size);
}
if (tcpSocket.send(packet) != sf::Socket::Done)
{
// Error sending message.
fprintf(stderr, "Failed to send a Message. Disconnecting.\n");
disconnect();
}
}
// Finished with queue, send the EndOfQueue message.
currentMessage = std::make_unique<dapi::message::Message>();
currentMessage->mutable_endofqueue();
packet.clear();
auto size = currentMessage->ByteSize();
std::unique_ptr<char[]> buffer(new char[size]);
currentMessage->SerializeToArray(&buffer[0], size);
packet.append(buffer.get(), size);
if (tcpSocket.send(packet) != sf::Socket::Done)
{
// Error sending EndOfQueue
fprintf(stderr, "Failed to send end of queue message. Disconnecting.\n");
disconnect();
}
}
void DAPIProtoClient::receiveMessages()
{
// Check that we are connected to a game server or client.
if (!isConnected())
return;
std::unique_ptr<dapi::message::Message> currentMessage;
sf::Packet packet;
// Loop until the end of queue message is received.
while (true)
{
packet.clear();
currentMessage = std::make_unique<dapi::message::Message>();
if (tcpSocket.receive(packet) != sf::Socket::Done)
{
fprintf(stderr, "Failed to receive message. Disconnecting.\n");
disconnect();
return;
}
auto size = packet.getDataSize();
std::unique_ptr<char[]> packetContents(new char[size]);
memcpy(packetContents.get(), packet.getData(), size);
currentMessage->ParseFromArray(packetContents.get(), packet.getDataSize());
if (currentMessage->has_endofqueue())
return;
messageQueue.push_back(std::move(currentMessage));
}
}
void DAPIProtoClient::disconnect()
{
if (!isConnected())
return;
tcpSocket.disconnect();
}
void DAPIProtoClient::initListen()
{
tcpListener.setBlocking(true);
while (tcpListener.listen(connectionPort) != sf::Socket::Done)
connectionPort = static_cast<unsigned short>(getRandomInteger(1025, 49151));
}
void DAPIProtoClient::stopListen()
{
tcpListener.close();
}
void DAPIProtoClient::queueMessage(std::unique_ptr<dapi::message::Message> newMessage)
{
messageQueue.push_back(std::move(newMessage));
}
std::unique_ptr<dapi::message::Message> DAPIProtoClient::getNextMessage()
{
auto nextMessage = std::move(messageQueue.front());
messageQueue.pop_front();
return nextMessage;
}
bool DAPIProtoClient::isConnected() const
{
return tcpSocket.getRemoteAddress() != sf::IpAddress::None;
}
int DAPIProtoClient::messageQueueSize() const
{
return messageQueue.size();
}
}