forked from khalladay/InsecureSnake
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
135 lines (109 loc) · 2.85 KB
/
main.cpp
File metadata and controls
135 lines (109 loc) · 2.85 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
#include "drawing.h"
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <errno.h>
#include <stdio.h>
#include <cstring>
#include <cstdlib>
#include <thread>
#include "snake_game.h"
const int SERVER_PORT = 10000;
const int NOTIFICATION_PORT = 10001;
const char* SERVER_ADDRESS = "127.0.0.1";
const int EULA_LEN = 1024;
static char eula[EULA_LEN];
static void(*packetHandler)();
static int randomSeed;
bool shouldQuit = false;
void handleNotificationPacket();
void notificationThread_Main(int socketHandle);
void inputThread_Main();
SnakeGame* currentGame;
int main()
{
memset(eula,0,EULA_LEN);
randomSeed = 42;
packetHandler = handleNotificationPacket;
int eulaSocket;
int notificationSocket;
{
struct sockaddr_in sockAddr = {0};
sockAddr.sin_family = AF_INET;
sockAddr.sin_port = htons(SERVER_PORT);
inet_pton(AF_INET, SERVER_ADDRESS, &sockAddr.sin_addr);
eulaSocket = socket(AF_INET, SOCK_STREAM, 0);
int err = connect(eulaSocket, (struct sockaddr*)&sockAddr, sizeof(sockAddr));
if (err< 0)
{
printf("Error connecting to eula server: %s\n", strerror(errno));
exit(1);
}
}
{
struct sockaddr_in sockAddr = {0};
sockAddr.sin_family = AF_INET;
sockAddr.sin_port = htons(NOTIFICATION_PORT);
inet_pton(AF_INET, SERVER_ADDRESS, &sockAddr.sin_addr);
notificationSocket = socket(AF_INET, SOCK_STREAM, 0);
int err = connect(notificationSocket, (struct sockaddr*)&sockAddr, sizeof(sockAddr));
if (err< 0)
{
printf("Error connecting to notification server: %s\n", strerror(errno));
exit(1);
}
}
char buff[EULA_LEN*2];
recv(eulaSocket, &buff, EULA_LEN*2, 0);
strcpy(eula, buff);
//once we have eula, start notification thread, and start rendering the game (eula screen first)
std::thread notificationThread(¬ificationThread_Main, notificationSocket);
notificationThread.detach();
printf("Welcome To Snake. Press any key to continue.\n");
getc(stdin);
printf("%s\n", &eula[0]);
printf("Press any key to accept and continue.\n");
getc(stdin);
WindowSize winSize = initializeWindow();
currentGame = new SnakeGame(winSize.w,winSize.h, randomSeed);
//has to be below the initWindow call since that handles calling nodelay()
std::thread inputThread(&inputThread_Main);
inputThread.detach();
while (!shouldQuit)
{
currentGame->tick();
drawGame(currentGame);
}
shutdownDrawing();
return 0;
}
void inputThread_Main()
{
while(1)
{
char input = getc(stdin);
if (currentGame)
{
currentGame->handleInput(input);
}
if (input == ':' || currentGame->currentMode == GAMEOVER)
{
shouldQuit = true;
break;
}
}
}
void notificationThread_Main(int socketHandle)
{
while(1)
{
char buff[1];
recv(socketHandle, &buff, 1, 0);
(*packetHandler)();
break;
}
}
void handleNotificationPacket()
{
printf("Notification Received\n");
}