#include "Client.h" #include #include #include #include #define __USE_MISC #include #include #include #include #define __USE_POSIX #include #include #include "SocketUtils.h" void clientInitDefaults(Client* c) { c->port = -1; c->socket = -1; c->thread = -1; c->hAmount = -1; c->hIndex = -1; c->handlers = NULL; } static void* serverHandler(void* data) { Client* c = (Client*) data; Stream in; while(1) { streamInit(&in, 1024); int size = receiveAll(c->socket, &in); if(size > 0) { int package; if(streamGetInt(&in, &package) == -1) { printf("Invalid package %d from server\n", package); } else { if(package >= 0 && package < c->hIndex) { //printf("Received package %d from server\n", package); c->handlers[package](&in); } else { printf("Invalid package %d from server\n", package); } } } else if(size == 0) { printf("Server closed remote socket\n"); streamRemove(&in); break; } else { streamRemove(&in); break; } streamRemove(&in); } __pid_t pid = getpid(); kill(pid, SIGINT); return NULL; } int clientInit(Client* c, char* ip, short port) { c->port = port; c->hAmount = 0; c->hIndex = 0; c->socket = socket(AF_INET, SOCK_STREAM, 0); if(c->socket == -1) { perror("Cannot create socket"); clientRemove(c); return -1; } struct sockaddr_in socketData; memset(&socketData, 0, sizeof(struct sockaddr_in)); socketData.sin_family = AF_INET; socketData.sin_port = htons(port); if(inet_aton(ip, &socketData.sin_addr) == 0) { printf("'%s' is not a valid ip\n", ip); clientRemove(c); return -1; } if(connect(c->socket, (struct sockaddr*) &socketData, sizeof(struct sockaddr_in)) == 0) { printf("Connection with server (%s) established\n", inet_ntoa(socketData.sin_addr)); Stream in; streamInit(&in, 1024); if(clientReceive(c, &in) == -1) { printf("Server did not respond"); streamRemove(&in); clientRemove(c); return -1; } else { char answer; if(streamGetChar(&in, &answer) == -1) { printf("Server sent an invalid response"); streamRemove(&in); clientRemove(c); return -1; } else if(answer > 0) { printf("Successfully received server response\n"); char welcome[100]; streamGetChars(&in, welcome, 100); printf("%s\n", welcome); if(pthread_create(&c->thread, NULL, serverHandler, (void*) c) != 0) { perror("Cannot create thread"); streamRemove(&in); clientRemove(c); return -1; } } else { char error[100]; streamGetChars(&in, error, 100); printf("%s\n", error); streamRemove(&in); clientRemove(c); return -1; } } streamRemove(&in); } else { perror("No server is available"); clientRemove(c); return -1; } return 0; } int clientReceive(Client* c, Stream* in) { int size = receiveAll(c->socket, in); if(size > 0) { return 0; } return -1; } void clientRemove(Client* c) { c->port = -1; if(c->socket != -1) { if(close(c->socket) != 0) { perror("Cannot close client socket"); } else { printf("socket closed\n"); } } if(c->thread != -1) { pthread_cancel(c->thread); pthread_join(c->thread, NULL); printf("thread cancel\n"); c->thread = -1; } c->hAmount = -1; c->hIndex = -1; if(c->handlers != NULL) { free(c->handlers); c->handlers = NULL; } } int clientSendData(Client* c, Stream* out) { int bytes = sendAll(c->socket, out); if(bytes == -1) { return -1; } //printf("%d Bytes from %d sent\n", bytes, s->index); return 0; } void clientRegisterHandler(Client* c, ClientStreamFunction f) { if(c->hAmount == c->hIndex) { if(c->hAmount == 0) { c->handlers = malloc(sizeof(ClientStreamFunction)); c->hAmount = 1; } else { c->hAmount *= 2; ClientStreamFunction* functions = malloc(sizeof(ClientStreamFunction) * c->hAmount); for(int i = 0; i < c->hIndex; i++) { functions[i] = c->handlers[i]; } free(c->handlers); c->handlers = functions; } } c->handlers[c->hIndex] = f; c->hIndex++; }