123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237 |
- #include "Client.h"
- #include <sys/socket.h>
- #include <stdio.h>
- #include <sys/socket.h>
- #include <netinet/in.h>
- #define __USE_MISC
- #include <arpa/inet.h>
- #include <string.h>
- #include <unistd.h>
- #include <stdlib.h>
- #define __USE_POSIX
- #include <sys/types.h>
- #include <signal.h>
- #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++;
- }
|