-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.c
221 lines (187 loc) · 4.61 KB
/
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
#include <string.h>
#include "server.h"
#include "game.h"
int main(int argc, char* argv[])
{
//for windows & the sockets
#if defined (WIN32)
WSADATA WSAData;
WSAStartup(MAKEWORD(2,2), &WSAData);
#endif
//random map generation
srand(time(NULL));
struct map map;
map.origin.x = 0;
map.origin.y = 0;
map.sizeX = 200;
map.sizeY = 200;
map.tile = malloc(map.sizeX*sizeof(struct tile*));
int i, j;
for(i=0; i<map.sizeX; i++)
{
map.tile[i] = malloc(map.sizeY*sizeof(struct tile));
for(j=0; j<map.sizeY; j++)
{
map.tile[i][j].id=rand()%NB_TILES_TYPES;//random for now
map.tile[i][j].entitie=NULL;
}
}
//flags
struct flags flags = {0,0};
//threads
pthread_t thread[CLIENT_NB];
struct threadData dataClient[CLIENT_NB];
int client;
for(client=0; client<CLIENT_NB; client++)
{
//data shared with the thread
dataClient[client].id=client;
dataClient[client].map=↦
dataClient[client].status=0;
dataClient[client].flags=&flags;
dataClient[client].sock=0;
}
//waiting for the clients connexions
clientConnexions(thread, dataClient);
//send map
char buffer[BUFFER_SIZE];
for(client=0; client<CLIENT_NB; client++)
{
sprintf(buffer, "morig%3d%3d\n", map.origin.x, map.origin.y);
send(dataClient[client].sock, buffer, 12, 0);
for(i=0; i<map.sizeX; i++)
{
for(j=0; j<map.sizeY; j++)
{
//send
}
}
}
//game logic
int play=1;
while(play)
{
printf("we play.\n");
//check for map modifications and send them to clients
if(flags.mapModified)
{
flags.mapModified=0;
//TODO: actually send modifications.
for(client=0; client<CLIENT_NB; client++)
{
//send
}
}
sleep(1);
}
//cleanup
closeConnexions(thread);
//for windows & the sockets
#if defined (WIN32)
WSACleanup();
#endif
return EXIT_SUCCESS;
}
void clientConnexions(pthread_t thread[CLIENT_NB], struct threadData threadDataArray[CLIENT_NB])
{
int client;
//the client's threads
for(client=0; client<CLIENT_NB; client++)
{
printf("thread %d\n", client);
pthread_create(&thread[client], NULL, manageClient, (void*)&threadDataArray[client]);
}
//wait until all clients are connected.
int everybodyIsConnected=0;
while(!everybodyIsConnected)
{
everybodyIsConnected=1;
for(client=0; client<CLIENT_NB; client++)
{
if(threadDataArray[client].status==0)
everybodyIsConnected=0;
}
printf("waiting for the connexions....\n");
sleep(1);
}
}
void closeConnexions(pthread_t thread[CLIENT_NB])
{
int i;
//waiting for the connexions end.
for(i=0; i<CLIENT_NB; i++)
{
void* returnValue;
pthread_join(thread[i], &returnValue);
}
}
//thread function: this function manage the clients and updates the game structures according to what it receives. It is only for reception.
void* manageClient(void* threadData)
{
//the client and servers sockets and their addresses
SOCKET sock;
SOCKADDR_IN sin;
SOCKET csock;
SOCKADDR_IN csin;
struct threadData * myData = (struct threadData *)threadData;
//getting number of the client and updating the variable.
int num = myData->id;
printf("Client %d\n", num);
//socket creation
sock = socket(AF_INET, SOCK_STREAM, 0);
if(sock == SOCKET_ERROR)
{
printf("Error during connexion of client %d: socket creation\n", num);
return NULL;
}
//preparation of the server address context
sin.sin_addr.s_addr = htonl(INADDR_ANY);
sin.sin_family = AF_INET;
sin.sin_port = htons(PORT);
//client socket's address
csin.sin_addr.s_addr = htonl(INADDR_ANY);
csin.sin_family = AF_INET;
csin.sin_port = htons(PORT);
//setting up the client socket's address
while(bind(sock, (SOCKADDR*)&sin, sizeof(sin))==SOCKET_ERROR)
{
printf("Awaiting connexion of client %d\n", num);
sleep(1);
}
//listenning and accepting connexion
if(listen(sock, 5)==SOCKET_ERROR)
{
printf("Connexion error ! (listenning) \n");
return NULL;
}
socklen_t size = sizeof(csin);
csock = accept(sock, (SOCKADDR*) &csin, &size);
if(csock == INVALID_SOCKET)
{
printf("Connexion error ! (client socket) \n");
return NULL;
}
//we are connected.
printf("Client %d connected\n", num);
myData->sock=csock;
//we are ready
myData->status=1;
//receive event and update game structures
char buffer[BUFFER_SIZE];
while(1)
{
//retrieve game event sent by client
recv(csock, buffer, 10, 0);
//TODO: have a buffer updated up to a closing character
//update games structures accordingly
if(strncmp(buffer, "click", 5)==0)
{
printf("click !!\n");
}
}
shutdown(csock, 2);
shutdown(sock, 2);
closesocket(csock);
closesocket(sock);
return NULL;
}