swap out single byte socket communication with 256 byte buffer

This commit is contained in:
Lexi Quinn 2023-07-25 02:01:07 +10:00
parent fd8bbadb1e
commit 1bb5864a29
2 changed files with 64 additions and 75 deletions

View File

@ -13,7 +13,6 @@ int main(int argc, char *argv[]) {
struct hostent *server; struct hostent *server;
char buffer[256]; char buffer[256];
char commandcode;
if (argc < 2) { if (argc < 2) {
fprintf(stderr,"usage %s command\n", argv[0]); fprintf(stderr,"usage %s command\n", argv[0]);
@ -49,86 +48,80 @@ int main(int argc, char *argv[]) {
} }
if (!strcmp(argv[1], "time")) { if (!strcmp(argv[1], "time")) {
commandcode = 1; strcpy(buffer, "current_time");
} else if (!strcmp(argv[1], "start")) { } else if (!strcmp(argv[1], "start")) {
commandcode = 2; strcpy(buffer, "start");
} else if (!strcmp(argv[1], "stop")) { } else if (!strcmp(argv[1], "stop")) {
commandcode = 3; strcpy(buffer, "stop");
} else if (!strcmp(argv[1], "kill")) { } else if (!strcmp(argv[1], "kill")) {
commandcode = 4; strcpy(buffer, "kill");
} else if (!strcmp(argv[1], "split")) { } else if (!strcmp(argv[1], "split")) {
commandcode = 5; strcpy(buffer, "split");
} else if (!strcmp(argv[1], "skip")) { } else if (!strcmp(argv[1], "skip")) {
commandcode = 6; strcpy(buffer, "skip");
} else if (!strcmp(argv[1], "pause")) { } else if (!strcmp(argv[1], "pause")) {
commandcode = 7; strcpy(buffer, "pause");
} else if (!strcmp(argv[1], "resume")) { } else if (!strcmp(argv[1], "resume")) {
commandcode = 8; strcpy(buffer, "resume");
} else if (!strcmp(argv[1], "undo")) { } else if (!strcmp(argv[1], "undo")) {
commandcode = 9; strcpy(buffer, "undo");
} else if (!strcmp(argv[1], "redo")) { } else if (!strcmp(argv[1], "redo")) {
commandcode = 10; strcpy(buffer, "redo");
} else if (!strcmp(argv[1], "foreground")) { } else if (!strcmp(argv[1], "foreground")) {
commandcode = 11; strcpy(buffer, "Foreground-Color");
} else if (!strcmp(argv[1], "background")) { } else if (!strcmp(argv[1], "background")) {
commandcode = 12; strcpy(buffer, "Background-Color");
} else if (!strcmp(argv[1], "save")) { } else if (!strcmp(argv[1], "save")) {
commandcode = 13; strcpy(buffer, "save");
} else if (!strcmp(argv[1], "runs")) { } else if (!strcmp(argv[1], "runs")) {
commandcode = 14; strcpy(buffer, "run_count");
} else if (!strcmp(argv[1], "segments")) { } else if (!strcmp(argv[1], "segments")) {
commandcode = 15; strcpy(buffer, "segment_count");
} else if (!strcmp(argv[1], "start-split-stop")) { } else if (!strcmp(argv[1], "start-split-stop")) {
commandcode = 16; strcpy(buffer, "start-split-stop");
} else if (!strcmp(argv[1], "pause-resume")) { } else if (!strcmp(argv[1], "pause-resume")) {
commandcode = 17; strcpy(buffer, "pause-resume");
} else if (!strcmp(argv[1], "start-stop")) { } else if (!strcmp(argv[1], "start-stop")) {
commandcode = 18; strcpy(buffer, "start-stop");
} else if (!strcmp(argv[1], "start-split")) { } else if (!strcmp(argv[1], "start-split")) {
commandcode = 19; strcpy(buffer, "start-split");
} else if (!strcmp(argv[1], "split-stop")) { } else if (!strcmp(argv[1], "split-stop")) {
commandcode = 20; strcpy(buffer, "split-stop");
} else if (!strcmp(argv[1], "undo-redo")) { } else if (!strcmp(argv[1], "undo-redo")) {
commandcode = 21; strcpy(buffer, "undo-redo");
} else { } else {
perror("No valid command given"); perror("No valid command given");
exit(1); exit(1);
} }
/* Send message to the server */ /* Send message to the server */
n = write(sockfd, &commandcode, 1); n = write(sockfd, &buffer, 256);
if (n < 0) { if (n < 0) {
perror("ERROR writing to socket"); perror("ERROR writing to socket");
exit(1); exit(1);
} }
/* Now read server response */ /* Now read server response */
//bzero(buffer,256); bzero(buffer,256);
n = read(sockfd, &buffer, 256);
//read an int response //read an int response
if (commandcode < 11 || commandcode == 14 || commandcode == 15) { if (!strcmp(argv[1], "time") || !strcmp(argv[1], "runs") || !strcmp(argv[1], "segments")) {
int x = -1; int x = -1;
n = read(sockfd, &x, sizeof(int)); x = *(int*)&buffer;
if (n < 0) { if (n < 0) {
perror("ERROR reading from socket"); perror("ERROR reading from socket");
exit(1); exit(1);
} }
if (x != -1) if (x != -1)
printf("%d\n",x); printf("%d\n",x);
} }
//read a string response //read a string response
else { else {
bzero(buffer,256);
n = read(sockfd, &buffer, 255);
if (n < 0) { if (n < 0) {
perror("ERROR reading from socket"); perror("ERROR reading from socket");
exit(1); exit(1);
} }
if (buffer != NULL) if (buffer != NULL)
printf("%s", buffer); printf("%s", buffer);
} }

View File

@ -72,12 +72,12 @@ void timespecToRFC3339(struct timespec t, char buf[]);
void loadFiles(); void loadFiles();
void add_segment(char *sname, char *lname, char *desc); void add_segment(char *sname, char *lname, char *desc);
void addFile(char *path); void addFile(char *path);
void sendTime(int sock);
void sendValue(int sock, char* name); void sendValue(int sock, char* name);
void sendInt(int sock, int value); void sendInt(int sock, int value);
void doprocessing (int sock); void doprocessing (int sock);
void addPauseTime(); void addPauseTime();
void subtractPauseTime(); void subtractPauseTime();
int current_ms();
//basic timer commands //basic timer commands
void start(); void start();
@ -490,9 +490,8 @@ void addFile(char *path)
loadFiles(); loadFiles();
} }
void sendTime(int sock) int current_ms()
{ {
int n, x;
if (timerActive) if (timerActive)
clock_gettime(CLOCK_REALTIME, &finish); clock_gettime(CLOCK_REALTIME, &finish);
if (paused) { if (paused) {
@ -500,9 +499,14 @@ void sendTime(int sock)
} else { } else {
sub_timespec(run[0].time, finish, &delta); sub_timespec(run[0].time, finish, &delta);
} }
x = timespecToMS(delta) - pausedTime; return timespecToMS(delta) - pausedTime;
n = write(sock, &x, sizeof(int)); }
void sendInt(int sock, int value)
{
char buffer[256];
strncpy(buffer, (char*)&value, sizeof(int));
int n = write(sock, &buffer, 256);
if (n < 0) { if (n < 0) {
perror("ERROR writing to socket"); perror("ERROR writing to socket");
exit(1); exit(1);
@ -511,6 +515,7 @@ void sendTime(int sock)
void sendValue(int sock, char* name) void sendValue(int sock, char* name)
{ {
char buffer[256];
int n, x; int n, x;
bool namefound = false; bool namefound = false;
for(int i = 0; i < valuecount; i++) { for(int i = 0; i < valuecount; i++) {
@ -520,19 +525,10 @@ void sendValue(int sock, char* name)
} }
} }
if (namefound) if (namefound)
n = write(sock, values[x], strlen(values[x])); strcpy(buffer, values[x]);
else else
n = write(sock, "DATA NOT PRESENT", 17); strcpy(buffer, "DATA NOT PRESENT");
n = write(sock, &buffer, 256);
if (n < 0) {
perror("ERROR writing to socket");
exit(1);
}
}
void sendInt(int sock, int value)
{
int n = write(sock, &value, sizeof(int));
if (n < 0) { if (n < 0) {
perror("ERROR writing to socket"); perror("ERROR writing to socket");
exit(1); exit(1);
@ -542,78 +538,78 @@ void sendInt(int sock, int value)
void doprocessing (int sock) void doprocessing (int sock)
{ {
int n; int n;
char commandcode; char buffer[256];
n = read(sock,&commandcode,1); n = read(sock, &buffer, 256);
if (n < 0) { if (n < 0) {
perror("ERROR reading from socket"); perror("ERROR reading from socket");
exit(1); exit(1);
} }
if (commandcode == 1) { if (!strcmp(buffer, "current_time")) {
//printf("Recieved time command\n"); //printf("Recieved time command\n");
sendTime(sock); sendInt(sock, current_ms());
} else if (commandcode == 2) { } else if (!strcmp(buffer, "start")) {
printf("Recieved start command\n"); printf("Recieved start command\n");
start(); start();
} else if (commandcode == 3) { } else if (!strcmp(buffer, "stop")) {
printf("Recieved stop command\n"); printf("Recieved stop command\n");
stop(); stop();
} else if (commandcode == 4) { } else if (!strcmp(buffer, "kill")) {
printf("Recieved kill command\n"); printf("Recieved kill command\n");
alive = false; alive = false;
} else if (commandcode == 5) { } else if (!strcmp(buffer, "split")) {
printf("Recieved split command\n"); printf("Recieved split command\n");
split(); split();
} else if (commandcode == 6) { } else if (!strcmp(buffer, "skip")) {
printf("Recieved skip command\n"); printf("Recieved skip command\n");
skip(); skip();
} else if (commandcode == 7) { } else if (!strcmp(buffer, "pause")) {
printf("Recieved pause command\n"); printf("Recieved pause command\n");
pause_timer(); pause_timer();
} else if (commandcode == 8) { } else if (!strcmp(buffer, "resume")) {
printf("Recieved resume command\n"); printf("Recieved resume command\n");
resume(); resume();
} else if (commandcode == 9) { } else if (!strcmp(buffer, "undo")) {
printf("Recieved undo command\n"); printf("Recieved undo command\n");
undo(); undo();
} else if (commandcode == 10) { } else if (!strcmp(buffer, "redo")) {
printf("Recieved redo command\n"); printf("Recieved redo command\n");
redo(); redo();
} else if (commandcode == 11) { } else if (!strcmp(buffer, "Foreground-Color")) {
printf("Recieved request for foreground color\n"); printf("Recieved request for foreground color\n");
sendValue(sock, "Foreground-Color"); sendValue(sock, "Foreground-Color");
} else if (commandcode == 12) { } else if (!strcmp(buffer, "Background-Color")) {
printf("Recieved request for background color\n"); printf("Recieved request for background color\n");
sendValue(sock, "Background-Color"); sendValue(sock, "Background-Color");
} else if (commandcode == 13) { } else if (!strcmp(buffer, "save")) {
printf("Recieved save command\n"); printf("Recieved save command\n");
appendRunToFile(); appendRunToFile();
} else if (commandcode == 14) { } else if (!strcmp(buffer, "run_count")) {
printf("Recieved request for run count\n"); printf("Recieved request for run count\n");
sendInt(sock, run_count); sendInt(sock, run_count);
} else if (commandcode == 15) { } else if (!strcmp(buffer, "segment_count")) {
printf("Recieved request for segment count\n"); printf("Recieved request for segment count\n");
sendInt(sock, segment_count); sendInt(sock, segment_count);
} else if (commandcode == 16) { } else if (!strcmp(buffer, "start_split_stop")) {
printf("Recieved start_split_stop command\n"); printf("Recieved start_split_stop command\n");
start_split_stop(); start_split_stop();
} else if (commandcode == 17) { } else if (!strcmp(buffer, "pause_resume")) {
printf("Recieved pause_resume command\n"); printf("Recieved pause_resume command\n");
pause_resume(); pause_resume();
} else if (commandcode == 18) { } else if (!strcmp(buffer, "start-stop")) {
printf("Recieved start-stop command\n"); printf("Recieved start-stop command\n");
start_stop(); start_stop();
} else if (commandcode == 19) { } else if (!strcmp(buffer, "start-split")) {
printf("Recieved start-split command\n"); printf("Recieved start-split command\n");
start_split(); start_split();
} else if (commandcode == 20) { } else if (!strcmp(buffer, "split-stop")) {
printf("Recieved split-stop command\n"); printf("Recieved split-stop command\n");
split_stop(); split_stop();
} else if (commandcode == 21) { } else if (!strcmp(buffer, "undo-redo")) {
printf("Recieved undo-redo command\n"); printf("Recieved undo-redo command\n");
undo_redo(); undo_redo();
} else { } else {
printf("Recieved invalid command code, ignoring...\n"); printf("Recieved invalid command, ignoring...\n");
} }
} }