Cleaned commands

This commit is contained in:
Adrien Bourmault 2021-07-08 17:26:13 +02:00
parent 8e6f99ecb7
commit 3d56a4ec61
No known key found for this signature in database
GPG Key ID: 6EB408FE0ACEC664
2 changed files with 12 additions and 22 deletions

View File

@ -36,8 +36,7 @@ char *CmdHelp(char **argv, Server_t *args)
char *CmdModel(char **argv, Server_t *args) char *CmdModel(char **argv, Server_t *args)
{ {
char *buf; char *buf;
char temp[255]; int id, eid;
int tempsize, id, eid;
Model_t *newModel; Model_t *newModel;
buf = (char *) calloc(LINE_NUMBER, LINE_LENGTH * sizeof(char)); buf = (char *) calloc(LINE_NUMBER, LINE_LENGTH * sizeof(char));
@ -66,12 +65,8 @@ char *CmdModel(char **argv, Server_t *args)
strcpy(newModel->name, argv[2] + 5); strcpy(newModel->name, argv[2] + 5);
strcat(buf, "Model "); sprintf(buf + strlen(buf), "Model %s created with id %d\n",
tempsize = sprintf(temp, "%s", newModel->name); newModel->name, newModel->id);
strcat(buf, temp);
strcat(buf, " created with id");
tempsize = sprintf(temp, " %d\n", newModel->id);
strcat(buf, temp);
goto CmdModelEnd; goto CmdModelEnd;
} else { } else {
@ -96,18 +91,12 @@ char *CmdModel(char **argv, Server_t *args)
id = (int) strtol(argv[2] + 3, NULL, 10); id = (int) strtol(argv[2] + 3, NULL, 10);
if (id == 0 || (eid = ModelLoad(id)) <= 0) { if (id == 0 || (eid = ModelLoad(id)) <= 0) {
strcat(buf, "Failed to load model id"); sprintf(buf + strlen(buf), "Failed to load model id %d\n", id);
tempsize = sprintf(temp, " %d\n", id);
strcat(buf, temp);
goto CmdModelEnd; goto CmdModelEnd;
} }
strcat(buf, "Model id"); sprintf(buf + strlen(buf), "Model id %d loaded with eid %d\n",
tempsize = sprintf(temp, " %d ", id); //XXX id, eid); //XXX
strcat(buf, temp);
strcat(buf, "loaded with eid ");
tempsize = sprintf(temp, " %d\n", eid);
strcat(buf, temp);
goto CmdModelEnd; goto CmdModelEnd;
loadEnd: loadEnd:
// invalid use // invalid use

View File

@ -113,7 +113,7 @@ static void *serverMain(void *server)
sockfd = socket(AF_INET, SOCK_STREAM, 0); sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd == -1) { if (sockfd == -1) {
printLog("Socket creation failed!\n"); printLog("Socket creation failed!\n");
return NULL; goto serverExiting;
} }
// Prepare binding structure // Prepare binding structure
@ -127,13 +127,13 @@ static void *serverMain(void *server)
// Binding newly created socket // Binding newly created socket
if ((bind(sockfd, (struct sockaddr*)&servaddr, sizeof(servaddr))) != 0) { if ((bind(sockfd, (struct sockaddr*)&servaddr, sizeof(servaddr))) != 0) {
printLog("Socket bind failed!\n"); printLog("Socket bind failed!\n");
return NULL; goto serverEarlyExiting;
} }
// Now server is ready to listen and verification // Now server is ready to listen and verification
if ((listen(sockfd, 5)) != 0) { if ((listen(sockfd, 5)) != 0) {
printLog("Socket listening failed!\n"); printLog("Socket listening failed!\n");
return NULL; goto serverExiting;
} }
printLog("Server listening...\n"); printLog("Server listening...\n");
@ -143,17 +143,18 @@ static void *serverMain(void *server)
connfd = accept(sockfd, (struct sockaddr*)&cli, &len); connfd = accept(sockfd, (struct sockaddr*)&cli, &len);
if (connfd < 0) { if (connfd < 0) {
printLog("Server acccept failed!\n"); printLog("Server acccept failed!\n");
return NULL; goto serverExiting;
} }
printLog("Client accepted\n"); printLog("Client accepted\n");
// Function for chatting between client and server // Function for chatting between client and server
serverCommunicationInstance(args, connfd); serverCommunicationInstance(args, connfd);
serverExiting:
// After communication ended close the socket // After communication ended close the socket
close(sockfd); close(sockfd);
serverEarlyExiting:
printLog("Server #%lu offline\n", *args->id); printLog("Server #%lu offline\n", *args->id);
return NULL; return NULL;
} }