-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.h
544 lines (506 loc) · 18.6 KB
/
server.h
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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
# ifndef SERVER_H
# define SERVER_H
#include <iomanip>
#include <fstream>
#include <string>
using namespace std;
typedef unsigned char *byte_pointer;
# define PORT 8080 // the port that the server listens for new connections on
fd_set server_readfds; // global set of file descriptors for the reading server thread to watch
pthread_mutex_t server_readfd_lock; // lock for server_readfds since it's global and accessed by multiple threads
vector<int> server_fd_list; // list of currently active file descriptors
bool stopped = 0;
pthread_mutex_t stop_lock;
void* start_server_listen(void* arg);
void* start_server_read(void* arg);
int server_listen(void);
int server_read (void);
int arrayCheck(char* toCheck, int arraySize);
void stop_server_listener(int serverFd, int newSocket);
void stop_server_reader(void);
struct serverMessage{
long positionInFile; //what position the data should be placed in
int bytesToUse; //the number of bytes in the data that are "real" data - is typically 1024 unless a file
//has less than 1024 bytes of data left to send.
long fileSize; //file size in bytes
char data[1024]; //actual data we are returning
char hasFile; //Updated if haveFile in the client message is 1 - has value 1 if we have file, 0 if not
char outOfRange; //1 if the portion requested is out of range, 0 otherwise
}serverMessage;
struct clientMessage{
char fileName[128]; //name of the file we are requesting
long portionToReturn; //the portion of the file we want returned - an integer corresponding to the Nth kilobyte
//has to be a long to work correctly with seekg in readFile
char haveFile; //A boolean value that tells the server we are asking if they have the file in fileName
//when actually requesting file data this is 0, when asking if a server has a file is 1
}clientMessage;
/* -----------------------------------------------------------------------------
* void showBytes(byte_pointer start, size_t len)
* A function used primarily for debugging that prints out len bytes starting
* from the provided start pointer. Prints bytes in hexadecimal.
* Parameters:
* - byte_pointer start: a pointer to the start of the chunk of memory we want
* to print
* - size_t len: the number of bytes to print
* Returns: nothing
* ---------------------------------------------------------------------------*/
void showBytes(byte_pointer start, size_t len) {
int i;
for (i=0; i<len; i++)
printf(" %.2x", start[i]);
printf("\n");
}
/* -----------------------------------------------------------------------------
* int arrayCheck(char* toCheck, int arraySize)
* A function that checks whether a given array is all 0's, or if it contains
* some other information. Called on serverMessages that are about to be sent
* to the client as an error check to ensure that something hasn't gone wrong
* when building the message (as we should never send a message that is ALL 0's)
* Parameters:
* - char* toCheck: the array to check
* - int arraySize: the size of the array to check
* Returns: 1 if the array is not all 0's, 0 if it is
* ---------------------------------------------------------------------------*/
int arrayCheck(char* toCheck, int arraySize) {
int sum=0;
for (int i = 0; i<arraySize; i++){
sum = sum+toCheck[i];
}
if(sum != 0){
return 1;
}
return 0;
}
/* -----------------------------------------------------------------------------
* void* start_server_listen (void* arg)
* Function for the server listener thread to start in. Calls server_listen()
* where the server sets up and starts listening for new connections.
* Parameters:
* - void* arg: a void pointer required by pthread_create(). Not used.
* Returns: nothing
* ---------------------------------------------------------------------------*/
void* start_server_listen(void* arg) {
server_listen();
pthread_exit(NULL);
}
/* -----------------------------------------------------------------------------
* void* start_server_read (void* arg)
* Function for the server reader thread to start in. Calls server_read()
* where the server calls select() in a loop to read new messages coming in
* from clients.
* Parameters:
* - void* arg: a void pointer required by pthread_create(). Not used.
* Returns: nothing
* ---------------------------------------------------------------------------*/
void* start_server_read(void* arg) {
server_read();
pthread_exit(NULL);
}
/* -----------------------------------------------------------------------------
* void stop_server_listener (void)
* Called when a fatal error occurs in the server listern thread. Frees
* resources and sets the stopped variable to 1 to let other threads know they
* should exit. Does NOT return or exit the thread; the caller is responsible
* for doing that after calling stop_server_listener();
* Parameters: none
* Returns: none
* ---------------------------------------------------------------------------*/
void stop_server_listener(int serverFd, int newSocket)
{
if (pthread_mutex_lock(&stop_lock) == -1)
{
perror("pthread_mutex_lock");
}
stopped = 1;
if (serverFd > 2)
{
close(serverFd);
}
if (newSocket > 2)
{
close(newSocket);
}
if (pthread_mutex_unlock(&stop_lock) == -1)
{
perror("pthread_mutex_lock");
}
}
/* -----------------------------------------------------------------------------
* void stop_server_reader (void)
* Called when a fatal error occurs in the server reader thread. Frees
* resources and sets the stopped variable to 1 to let other threads know they
* should exit. Does NOT return or exit the thread; the caller is responsible
* for doing that after calling stop_server_reader();
* Parameters: none
* Returns: none
* ---------------------------------------------------------------------------*/
void stop_server_reader(void)
{
if (pthread_mutex_lock(&stop_lock) == -1)
{
perror("pthread_mutex_lock");
}
stopped = 1;
if (pthread_mutex_unlock(&stop_lock) == -1)
{
perror("pthread_mutex_lock");
}
// close the server's sockets with other clients
if (pthread_mutex_lock(&server_readfd_lock) == -1)
{
perror("pthread_mutex_lock");
}
for (int i = 0; i < server_fd_list.size(); i++)
{
close(server_fd_list[i]);
}
if (pthread_mutex_unlock(&server_readfd_lock) == -1)
{
perror("pthread_mutex_unlock");
}
}
/* -----------------------------------------------------------------------------
* char* readFile(struct clientMessage* toRetrieve)
* A function that, given a clientMessage, opens and reads the requested chunk
* of the requested file, constructs a serverMessage, and returns that message
* to the calling function.
* Parameters:
* - struct clientMessage* toRetrieve: a clientMessage received from a client
* with information about what part of what file is being requested
* Returns: a serverMessage, cast to a char pointer, containing the message to
* send back to the client.
* ---------------------------------------------------------------------------*/
char* readFile(struct clientMessage* toRetrieve) {
std::ifstream inFile;
char toSend[1024];
size_t startLocation;
size_t length;
long size = 0;
struct serverMessage* toReturn;
toReturn = (struct serverMessage*)malloc(sizeof(struct serverMessage));
memset(toReturn, 0, sizeof(struct serverMessage));
inFile.open(toRetrieve->fileName);
// if we have the file but can't open it, set the parameters of the message
// correctly and tell the client
if (!inFile && toRetrieve->haveFile == 1){
toReturn->positionInFile = toRetrieve->portionToReturn;
toReturn->bytesToUse = length;
toReturn->fileSize = (long)size;
toReturn->hasFile = 0;
toReturn->outOfRange = 0;
return((char*)toReturn);
}else if(!inFile){
printf("Unable to open file\n");
return NULL;
}
//get the total file size and set the position to the byte we have to read
inFile.seekg(0, inFile.end);
size = inFile.tellg();
inFile.seekg(toRetrieve->portionToReturn*1024 < size ? toRetrieve->portionToReturn*1024 : size, ios::beg);
//if there are less than 1024 bytes left in the file to read
if (size - (toRetrieve->portionToReturn*1024) <= 0){
length = 0;
toReturn->outOfRange = 1;
}else if (size-(toRetrieve->portionToReturn*1024) > sizeof(toSend)){
length = sizeof(toSend);
toReturn->outOfRange = 0;
}else{
length = size-(toRetrieve->portionToReturn*1024); //if not, just send the rest of the file
toReturn->outOfRange = 0;
}
printf("Sending %i bytes\n", (int)length);
inFile.read(toSend, length);
memcpy(toReturn->data, toSend, length);
toReturn->positionInFile = toRetrieve->portionToReturn;
toReturn->bytesToUse = length;
toReturn->fileSize = (long)size;
toReturn->hasFile = 1;
return((char*)toReturn);
}
/* -----------------------------------------------------------------------------
* int server_listen (void)
* The funtion that the server listener thread runs in. Sets up the socket to
* listen for new connections and then calls accept() in a loop to accept all
* new client connections. When a client connects, a file descriptor is assigned
* to the connection and added to the global fd_set server_readfds that the reader
* thread uses to receive client messages.
* Parameters: none
* Returns: An integer indicating whether the function exited due to an error
* or not.
* ---------------------------------------------------------------------------*/
int server_listen(void) {
int serverFd, newSocket;
int opt = 1;
struct sockaddr_in address;
int addrlen = sizeof(address);
struct timeval timeout;
// create the listening file descriptor
serverFd = socket(AF_INET, SOCK_STREAM, 0);
if (serverFd == -1)
{
stop_server_listener(serverFd, newSocket);
return 1;
}
address.sin_family = AF_INET;
address.sin_addr.s_addr = INADDR_ANY;
address.sin_port = htons(PORT);
timeout.tv_sec = 0;
timeout.tv_usec = 50000;
if (setsockopt(serverFd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt)) < 0)
{
perror("setsockopt");
stop_server_listener(serverFd, newSocket);
return 1;
}
// attach socket to the port
if (::bind(serverFd, (struct sockaddr *)&address, sizeof(address)) == -1) {
perror("bind");
stop_server_listener(serverFd, newSocket);
return 1;
}
// start listening for client connections
if (listen(serverFd, 5) == -1)
{
perror("listen");
stop_server_listener(serverFd, newSocket);
return 1;
}
// loop, listening for new conections
while (1)
{
// ----------------------------------------------------------------------
// check if the user has requested to quit the program; if they have,
// end this thread.
if (pthread_mutex_lock(&stop_lock) == -1)
{
perror("pthread_mutex_lock");
stop_server_listener(serverFd, newSocket);
return 1;
}
if (stopped == 1)
{
close(serverFd);
close(newSocket);
if (pthread_mutex_unlock(&stop_lock) == -1)
{
perror("pthread_mutex_lock");
}
return 0;
}
if (pthread_mutex_unlock(&stop_lock) == -1)
{
perror("pthread_mutex_lock");
stop_server_listener(serverFd, newSocket);
return 1;
}
// ----------------------------------------------------------------------
// set the socket to nonblocking
// necessary so that we don't hang on the accept call until we get a connection
// continuously looping without hanging allows us to check if we should shut down
long flags;
if ((flags = fcntl(serverFd, F_GETFL, NULL)) < 0)
{
perror("fcntl");
stop_server_listener(serverFd, newSocket);
return 1;
}
flags = flags | O_NONBLOCK;
if (fcntl(serverFd, F_SETFL, flags) < 0)
{
perror("fcntl");
stop_server_listener(serverFd, newSocket);
return 1;
}
newSocket = accept(serverFd, (struct sockaddr *)&address, (socklen_t*)&addrlen);
if (newSocket == -1)
{
if (errno != EWOULDBLOCK & errno != EAGAIN)
{
perror("accept");
stop_server_listener(serverFd, newSocket);
return 1;
}
}
else {
// ----------------------------------------------------------------------
// get the lock and add it to the set of file descriptors to listen to
if (pthread_mutex_lock(&server_readfd_lock) == -1)
{
perror("pthread_mutex_lock");
stop_server_listener(serverFd, newSocket);
return 1;
}
FD_SET(newSocket, &server_readfds); // put the file descriptor in the set
//readfd_count++;
server_fd_list.push_back(newSocket);
if (pthread_mutex_unlock(&server_readfd_lock) == -1)
{
perror("pthread_mutex_unlock");
stop_server_listener(serverFd, newSocket);
return 1;
}
}
// set back to nonblocking
flags = flags & ~O_NONBLOCK;
if (fcntl(serverFd, F_SETFL, flags) < 0)
{
perror("fcntl");
stop_server_listener(serverFd, newSocket);
return 1;
}
}
return 0;
}
/* -----------------------------------------------------------------------------
* int server_read (void)
* Function that the server reader thread runs. In a loop, calls select() on the
* fd_set server_readfds of client connection file descriptors that is populated by the
* listener function. If it receives a request from a client, determines what
* information the client wants and sends back the correct information.
* Parameters: none
* Returns: An integer that indicates whether the function exited due to
* an error.
* ---------------------------------------------------------------------------*/
int server_read (void) {
int ready;
// initialize the other two FD sets that select() needs; they'll be empty
fd_set writefds;
fd_set exceptfds;
// copy of server_readfds to pass to select(); this is necessary because select()
// changes the fd_sets it gets IN PLACE and we need to remember what was
// in the set
fd_set server_readfds_copy;
// timeout interval for the select() call. if it doesn't get anything, we
// want it to timeout so that the listener thread can update the set of
// file descriptors that it uses if any new connections have come in
struct timeval timeout;
timeout.tv_sec = 0;
timeout.tv_usec = 100000; // 100 milliseconds; WE MAY WANT TO CHANGE THIS
while (1) {
// ----------------------------------------------------------------------
// check if the user has requested to quit the program; if they have,
// end this thread.
if (pthread_mutex_lock(&stop_lock) == -1)
{
perror("pthread_mutex_lock");
stop_server_reader();
return 1;
}
if (stopped == 1)
{
if (pthread_mutex_unlock(&stop_lock) == -1)
{
perror("pthread_mutex_lock");
}
// ----------------------------------------------------------------------
// close the server's sockets with other clients
if (pthread_mutex_lock(&server_readfd_lock) == -1)
{
perror("pthread_mutex_lock");
}
for (int i = 0; i < server_fd_list.size(); i++)
{
close(server_fd_list[i]);
}
if (pthread_mutex_unlock(&server_readfd_lock) == -1)
{
perror("pthread_mutex_unlock");
}
// ----------------------------------------------------------------------
return 0;
}
if (pthread_mutex_unlock(&stop_lock) == -1)
{
perror("pthread_mutex_lock");
stop_server_reader();
return 1;
}
// ----------------------------------------------------------------------
// clear out the file descriptor sets to ensure they are empty
FD_ZERO(&server_readfds_copy);
FD_ZERO(&writefds);
FD_ZERO(&exceptfds);
// ----------------------------------------------------------------------
// get the lock so that we can make a copy of server_readfds to pass to select
if (pthread_mutex_lock(&server_readfd_lock) == -1)
{
perror("pthread_mutex_lock");
stop_server_reader();
return 1;
}
server_readfds_copy = server_readfds;
int nfds = 0;
// determine what nfds should be; it is supposed to be the largest FD in the set + 1
for (int i = 0; i < server_fd_list.size(); i++)
{
if (server_fd_list[i] > nfds)
{
nfds = server_fd_list[i];
}
}
nfds++;
if (pthread_mutex_unlock(&server_readfd_lock) == -1)
{
perror("pthread_mutex_unlock");
stop_server_reader();
return 1;
}
// ----------------------------------------------------------------------
// call select on the copy of server_readfds
ready = select(nfds, &server_readfds_copy, &writefds, &exceptfds, &timeout);
if (ready == -1) {
perror("select");
stop_server_reader();
return 1;
}
else if (ready != 0) {
// iterate through all of the file descriptors that we may have
// gotten data from and determine which ones actually sent us something
// ----------------------------------------------------------------------
// we need the lock here because we access server_fd_list, which is shared with
// the listener thread
if (pthread_mutex_lock(&server_readfd_lock) == -1)
{
perror("pthread_mutex_lock");
stop_server_reader();
return 1;
}
for (int i = 0; i < server_fd_list.size(); i++)
{
if (FD_ISSET(server_fd_list[i], &server_readfds_copy) != 0) {
size_t requestSize = sizeof(clientMessage);
char* buffer = new char[requestSize];
memset(buffer, 0, sizeof(clientMessage));
int valRead = read(server_fd_list[i], buffer, sizeof(clientMessage)); // weird stuff is happening here
if (valRead > 0){
if (arrayCheck(buffer, (int)sizeof(clientMessage))){
struct clientMessage* toAccept = (struct clientMessage*)buffer;
char* toReturn = readFile(toAccept);
send(server_fd_list[i], toReturn, sizeof(serverMessage), 0);
}else{
printf("Null message recieved\n");
}
free(buffer);
}
else{
// if we get here, the other side disconnected, so close this fd and remove it from our lists
close(server_fd_list[i]);
FD_CLR(server_fd_list[i], &server_readfds);
server_fd_list.erase(server_fd_list.begin() + i);
free(buffer);
//perror("read");
}
}
}
// release the lock
if (pthread_mutex_unlock(&server_readfd_lock) == -1)
{
perror("pthread_mutex_unlock");
stop_server_reader();
return 1;
}
// ----------------------------------------------------------------------
}
}
}
# endif /* SERVER_H */