-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtcp_server.c
249 lines (195 loc) · 7.12 KB
/
tcp_server.c
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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
/*******************************************************************************************
** Authors: Chad Erdwins
** Date: 12 OCT 2018
** Description: the game server
*******************************************************************************************/
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <sys/stat.h>
#include <time.h>
#include "interfaces.h"
#include "gamePlay.h"
#include "tcp_client.h"
#include "levels.h"
#include "effects.h"
#include "ai.h"
#include "menu.h"
#include "scores.h"
int main() {
/*hold return value of our socket call*/
int server_socket;
server_socket = socket(AF_INET, SOCK_STREAM, 0);
if (server_socket == -1) {
fprintf(stderr, "Error creating socket.\n");
exit(1);
}
/*Define address structure*/
struct sockaddr_in server_address;
/*specifies the connection family*/
server_address.sin_family = AF_INET;
/*specifies the listening port*/
int port = SERVER_IP_PORT;
server_address.sin_port = htons(port);
/*This is where you specify the IP Address. INADDR_ANY means any adress on the local machine*/
// const char *ipaddr = "128.193.36.41";
// const char *ipaddr = "192.168.0.18";
const char *ipaddr = SERVER_IP_ADDR;
server_address.sin_addr.s_addr = inet_addr(ipaddr);
/*binding the socket to our specified IP and port*/
if (bind(server_socket,(struct sockaddr*) &server_address, sizeof(server_address)) == -1) {
fprintf(stderr, "Error binding socket.\n");
exit(1);
}
/*use the listen function to start listening to connections*/
/*number corresponds to how many connections we want to have*/
RENEW:printf("listening on port %d\n", port);
listen(server_socket, 2);
/*holds client socket*/
int client_socket;
int client_socket2;
// END NET INIT
// BEGIN GAME INIT
int maxX, maxY, maxX2, maxY2;
int vertCtrl = 1;
// int count=0;
struct gameState *state = malloc(sizeof(struct gameState));
struct library *lib = malloc(sizeof(struct library));
struct levelData *level = malloc(sizeof(struct levelData));
initGame(state, lib, level);
/*option to get the clients IP adress here, passed in NULL bc it isn't relevant in this case*/
while ((client_socket = accept(server_socket, NULL, NULL))) {
if ((client_socket2 = accept(server_socket, NULL, NULL))) {
// only do this once, maxX inits to 1 (so will only occur on first run through)
if (state->maxX ==1) {
recv(client_socket, &maxX, sizeof(int), 0);
recv(client_socket, &maxY, sizeof(int), 0);
recv(client_socket2, &maxX2, sizeof(int), 0);
recv(client_socket2, &maxY2, sizeof(int), 0);
state->maxX = maxX;
state->maxY = maxY;
if (maxX2 < maxX) {
state->maxX = maxX2;
}
if (maxY2 < maxY) {
state->maxY = maxY2;
}
state->gndHeight = state->maxY;
state->maxY = state->maxY - state->titleSize;
// set the level max height now that you have your limits
setMaxHeight(level, state);
// init background (only needed for level 1, will fly into other backgrounds)
initOpenSpaceBG(state, lib);
// state initialzied, now send that state back to the clients
// printf("float(%lu), int(%lu), dispArrAddr(%lu), sprite(%lu), char(%lu), dispPair(%lu)\n", sizeof(float), sizeof(int), sizeof(struct dispPair *), sizeof(struct sprite), sizeof(char), sizeof(struct dispPair));
send_all(client_socket, state, lib, level);
send_all(client_socket2, state, lib, level);
if (getRand(1,100)<50) {
send_data(client_socket, &vertCtrl, sizeof(int));
vertCtrl=0;
send_data(client_socket2, &vertCtrl, sizeof(int));
}
else {
send_data(client_socket2, &vertCtrl, sizeof(int));
vertCtrl=0;
send_data(client_socket, &vertCtrl, sizeof(int));
}
}
int input1, input2;
// init timing function
struct timespec timeHold;
clock_gettime(CLOCK_MONOTONIC, &timeHold);
float tstart = timeHold.tv_sec + timeHold.tv_nsec / 1e9;
while (state->playFlag) {
// make sure both inputs are valid
recv(client_socket, &input1, sizeof(int), 0);
//printf("input1: %d\n", input1);
recv(client_socket2, &input2, sizeof(int), 0);
//printf("input2: %d\n", input2);
handleInput(input1, state, lib);
handleInput(input2, state, lib);
restrictPlaySpace(state);
// procedural level generation
procGen(state, lib, level);
// this updates the sprite actions (including firing)
updateSpriteAI(state, lib);
// update physics for all sprites
updatePhysics(state);
// handles collision detection and collision results
detectCollision(state, lib);
// calculates the current score, stores it in state
calcScore(state, level);
// this is bad and you should feel bad
printEffectServer(state);
// ding dong the player's dead!
killPlayer(state);
// BLOCK HERE
// usleep(75000);
clock_gettime(CLOCK_MONOTONIC, &timeHold);
state->timeWait = timeHold.tv_sec + timeHold.tv_nsec / 1e9 - tstart;
if (((1./FRAME_RATE)-(state->timeWait-state->time))*1e6 > 0) {
usleep(((1./FRAME_RATE)-(state->timeWait-state->time))*1e6);
}
else {
// do nothing! you're trying to catch up on frame rate!
//printf("not keeping up %i\n", count); /debugging!
//count++;
}
// generate the output game time (and time used for physics)
state->timeLast=state->time;
clock_gettime(CLOCK_MONOTONIC, &timeHold);
state->time = timeHold.tv_sec + timeHold.tv_nsec / 1e9 - tstart;
// send it!
// printf("sending!\n");
send_all(client_socket, state, lib, level);
send_all(client_socket2, state, lib, level);
// printf("sent\n");
}
if (state->deathScreen) {
while (state->deathScreen) {
// Receive death screen inputs.
recv(client_socket, &input1, sizeof(int), 0);
recv(client_socket2, &input2, sizeof(int), 0);
// Handle inputs.
if (!handleDeathScreenInput(input1, state->playerName) || !handleDeathScreenInput(input2, state->playerName)) {
// All done.
state->deathScreen = false;
}
// Send updated player name and deathScreen flag.
send_data(client_socket, state->playerName, sizeof(state->playerName));
send_data(client_socket2, state->playerName, sizeof(state->playerName));
send_data(client_socket, &state->deathScreen, sizeof(state->deathScreen));
send_data(client_socket2, &state->deathScreen, sizeof(state->deathScreen));
}
// Record new highscore.
struct highscore newHighscore;
newHighscore.score = state->score;
strcpy(newHighscore.name, state->playerName);
putScore(newHighscore);
// Send highscore file size.
struct stat st;
stat(HIGHSCORES_FILENAME, &st);
off_t highscoreFileSize = st.st_size;
send_data(client_socket, &highscoreFileSize, sizeof(off_t));
send_data(client_socket2, &highscoreFileSize, sizeof(off_t));
// Send highscore file.
send_file(client_socket, HIGHSCORES_FILENAME);
send_file(client_socket2, HIGHSCORES_FILENAME);
break;
}
}
}
//debug
//printf("Is this printing?");
printf("\nRestarting server...\n");
// clean up
freeGame(state, lib, level);
goto RENEW;
/*finished basic server app, time to close the socket*/
close(server_socket);
return 0;
}