Server return value

This commit is contained in:
Adrien Bourmault 2021-07-09 20:42:44 +02:00
parent 5ec6e86f6e
commit 8ae1f2f65f
No known key found for this signature in database
GPG Key ID: 6EB408FE0ACEC664
3 changed files with 12 additions and 1 deletions

View File

@ -123,6 +123,7 @@ struct {
struct {
pthread_t id;
bool pleaseStop;
int returnValue;
int sockfd;
} typedef Server_t;

View File

@ -28,6 +28,7 @@ int main(int argc, char **argv)
{
time_t t;
Server_t *server0;
int returnValue = 0;
// Go!
printLog("Starting gem-graph-server...\n");
@ -48,9 +49,10 @@ int main(int argc, char **argv)
ServerWait(server0);
// Exiting
returnValue |= server0->returnValue;
ServerDestroy(server0);
ModelSystemDestroy();
free(server0);
return 0;
return returnValue;
}

View File

@ -130,12 +130,14 @@ static void *serverMain(void *server)
// Get args
args = (Server_t*) server;
args->returnValue = 0;
printLog("Server #%lu online\n", args->id);
// Create socket
args->sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (args->sockfd == -1) {
printLog("Socket creation failed!\n");
args->returnValue = 1;
goto serverExiting;
}
@ -143,12 +145,14 @@ static void *serverMain(void *server)
flags = fcntl(args->sockfd, F_GETFL);
if (flags == -1) {
printLog("Socket parameters getting failed!\n");
args->returnValue = 1;
goto serverExiting;
}
// Change socket flags to non-blocking
if (fcntl(args->sockfd, F_SETFL, flags | O_NONBLOCK) < 0) {
printLog("Socket non-blocking setting failed!\n");
args->returnValue = 1;
goto serverExiting;
}
@ -163,12 +167,14 @@ static void *serverMain(void *server)
// Binding newly created socket
if ((bind(args->sockfd, (struct sockaddr*)&servaddr, sizeof(servaddr))) == -1) {
printLog("Socket bind failed!\n");
args->returnValue = 1;
goto serverExiting;
}
// Now server is ready to listen and verification
if (listen(args->sockfd, MAX_CONNECTION) == -1) {
printLog("Socket listening failed!\n");
args->returnValue = 1;
goto serverExiting;
}
@ -177,6 +183,7 @@ static void *serverMain(void *server)
if (getsockname(args->sockfd, (struct sockaddr *) &servaddr, &socklen)
== -1) {
printLog("Could not get sock name!\n");
args->returnValue = 1;
goto serverExiting;
}
@ -217,6 +224,7 @@ static void *serverMain(void *server)
(void*)&serverSlots[serverSlotIndex]);
if(threadStatus != 0) {
printLog("Error from pthread: %d\n", threadStatus);
args->returnValue = 1;
goto serverExiting;
}