/
ClockTestClientMain.cpp
80 lines (68 loc) · 2.06 KB
/
ClockTestClientMain.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
#include "SDL.h"
#include "SDL_net.h"
#include <string>
#include <vector>
#include <array>
#include <atomic>
#include <cstdio>
#include "Timer.h"
#include "Debug.h"
#include "Ignore.h"
//const std::string SERVER_IP = "127.0.0.1";
const std::string SERVER_IP = "45.79.37.63";
static constexpr unsigned int SERVER_PORT = 41499;
using namespace AM;
int main(int argc, char* argv[])
{
// SDL2 needs this signature for main, but we don't use the parameters.
ignore(argc);
ignore(argv);
if (SDL_Init(0) == -1) {
DebugInfo("SDL_Init: %s", SDLNet_GetError());
return 1;
}
if (SDLNet_Init() == -1) {
DebugInfo("SDLNet_Init: %s", SDLNet_GetError());
return 2;
}
DebugInfo("Connecting to server.");
IPaddress ip;
if (SDLNet_ResolveHost(&ip, SERVER_IP.c_str(), SERVER_PORT)) {
DebugInfo("Could not resolve host.");
return 3;
}
TCPsocket serverSocket = SDLNet_TCP_Open(&ip);
if (!serverSocket) {
DebugInfo("Could not open serverSocket.");
return 4;
}
DebugInfo("Connected.");
/* Wait for 5s to let the connection settle. */
Timer timer;
timer.updateSavedTime();
while (timer.getDeltaSeconds(false) < 5) {
SDL_Delay(1);
}
/* Send the start byte, wait for the desired time, then send the end byte. */
// Send the start byte.
std::array<Uint8, 1> sendBuf = { 5 };
int bytesSent = SDLNet_TCP_Send(serverSocket, &sendBuf, 1);
if (bytesSent < 1) {
DebugInfo("Failed to send all bytes.");
return 5;
}
// Wait for the desired time.
timer.updateSavedTime();
while (timer.getDeltaSeconds(false) < 1200) {
SDL_Delay(1);
}
// Send the end byte.
sendBuf[0] = 6;
bytesSent = SDLNet_TCP_Send(serverSocket, &sendBuf, 1);
if (bytesSent < 1) {
DebugInfo("Failed to send all bytes.");
return 5;
}
DebugInfo("Sent end byte. Time passed: ~%.8f", timer.getDeltaSeconds(true));
return 0;
}