Server.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  1. #include "Server.h"
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <sys/socket.h>
  5. #include <unistd.h>
  6. #include <string.h>
  7. #include <netinet/in.h>
  8. #include <arpa/inet.h>
  9. #include <sys/types.h>
  10. #include "Stream.h"
  11. #include "SocketUtils.h"
  12. void serverInitDefaults(Server* s)
  13. {
  14. s->maxClients = -1;
  15. s->port = -1;
  16. s->threads = NULL;
  17. s->clientSockets = NULL;
  18. s->connectSocket = -1;
  19. s->hAmount = -1;
  20. s->hIndex = -1;
  21. s->handlers = NULL;
  22. }
  23. int serverInit(Server* s, int maxClients, short port)
  24. {
  25. s->maxClients = maxClients;
  26. s->port = port;
  27. s->hAmount = 0;
  28. s->hIndex = 0;
  29. // initialize storage for clients
  30. s->threads = malloc(sizeof(pthread_t) * maxClients);
  31. s->clientSockets = malloc(sizeof(int) * maxClients);
  32. for(int i = 0; i < maxClients; i++)
  33. {
  34. s->threads[i] = -1;
  35. s->clientSockets[i] = -1;
  36. }
  37. // create the socket for clients to connect
  38. s->connectSocket = socket(AF_INET, SOCK_STREAM, 0);
  39. if(s->connectSocket == -1)
  40. {
  41. perror("Cannot create socket");
  42. serverRemove(s);
  43. return -1;
  44. }
  45. // prevents clients from blocking the port if the server exits
  46. struct linger sl;
  47. sl.l_onoff = 1;
  48. sl.l_linger = 0;
  49. if(setsockopt(s->connectSocket, SOL_SOCKET, SO_LINGER, &sl, sizeof(struct linger)) == -1)
  50. {
  51. perror("Cannot set non lingering");
  52. serverRemove(s);
  53. return -1;
  54. }
  55. // specifies data of the port and binds it
  56. struct sockaddr_in connectSocketData;
  57. memset(&connectSocketData, 0, sizeof(struct sockaddr_in));
  58. connectSocketData.sin_family = AF_INET;
  59. connectSocketData.sin_addr.s_addr = INADDR_ANY;
  60. connectSocketData.sin_port = htons(port);
  61. if(bind(s->connectSocket, (struct sockaddr*) &connectSocketData, sizeof(struct sockaddr_in)) != 0)
  62. {
  63. perror("Cannot bind socket");
  64. serverRemove(s);
  65. return -1;
  66. }
  67. // mark this socket as handler for connection requests
  68. if(listen(s->connectSocket, 5) != 0)
  69. {
  70. perror("Cannot start listening");
  71. serverRemove(s);
  72. return -1;
  73. }
  74. return 0;
  75. }
  76. void serverRemove(Server* s)
  77. {
  78. s->port = -1;
  79. if(s->threads != NULL)
  80. {
  81. for(int i = 0; i < s->maxClients; i++)
  82. {
  83. if(s->threads[i] != -1)
  84. {
  85. printf("%d thread\n", i);
  86. pthread_cancel(s->threads[i]);
  87. pthread_join(s->threads[i], NULL);
  88. }
  89. }
  90. free(s->threads);
  91. s->threads = NULL;
  92. }
  93. if(s->clientSockets != NULL)
  94. {
  95. for(int i = 0; i < s->maxClients; i++)
  96. {
  97. if(s->clientSockets[i] != -1)
  98. {
  99. if(close(s->clientSockets[i]) == -1)
  100. {
  101. printf("%d", i);
  102. perror(" cannot close client socket");
  103. }
  104. else
  105. {
  106. printf("%d closed\n", i);
  107. }
  108. }
  109. }
  110. free(s->clientSockets);
  111. s->clientSockets = NULL;
  112. }
  113. s->maxClients = -1;
  114. if(s->connectSocket != -1)
  115. {
  116. if(close(s->connectSocket) != 0)
  117. {
  118. perror("Cannot close server socket");
  119. }
  120. else
  121. {
  122. printf("connection listener closed\n");
  123. }
  124. s->connectSocket = -1;
  125. }
  126. s->hAmount = -1;
  127. s->hIndex = -1;
  128. if(s->handlers != NULL)
  129. {
  130. free(s->handlers);
  131. s->handlers = NULL;
  132. }
  133. }
  134. void* clientHandler(void* data)
  135. {
  136. int id = ((ThreadData*) data)->id;
  137. Server* s = ((ThreadData*) data)->server;
  138. Stream in;
  139. while(1)
  140. {
  141. streamInit(&in, 1024);
  142. int size = receiveAll(s->clientSockets[id], &in);
  143. if(size > 0)
  144. {
  145. char package;
  146. if(streamGetChar(&in, &package) == -1)
  147. {
  148. printf("Invalid package %d from %d\n", (int) package, id);
  149. }
  150. else
  151. {
  152. if(package >= 0 && package < s->hIndex)
  153. {
  154. printf("Received package %d from %d\n", (int) package, id);
  155. s->handlers[(int) package](&in);
  156. }
  157. else
  158. {
  159. printf("Invalid package %d from %d\n", (int) package, id);
  160. }
  161. }
  162. }
  163. else if(size == 0)
  164. {
  165. printf("Client %d closed remote socket\n", id);
  166. streamRemove(&in);
  167. break;
  168. }
  169. else
  170. {
  171. perror("recv error");
  172. streamRemove(&in);
  173. break;
  174. }
  175. streamRemove(&in);
  176. }
  177. if(close(s->clientSockets[id]) != 0)
  178. {
  179. printf("%d", id);
  180. perror(" cannot close client socket");
  181. }
  182. else
  183. {
  184. printf("%d closed\n", id);
  185. }
  186. s->clientSockets[id] = -1;
  187. return NULL;
  188. }
  189. int serverSend(int clientSocket, Stream* out)
  190. {
  191. if(sendAll(clientSocket, out) == -1)
  192. {
  193. perror("Cannot send data");
  194. return -1;
  195. }
  196. return 0;
  197. }
  198. void serverWaitForConnection(Server* s)
  199. {
  200. socklen_t addrlen = sizeof(struct sockaddr_in);
  201. while(1)
  202. {
  203. printf("Waiting for connections...\n");
  204. struct sockaddr_in clientSocketData;
  205. int clientSocket = accept(s->connectSocket, (struct sockaddr*) &clientSocketData, &addrlen);
  206. if(clientSocket >= 0)
  207. {
  208. printf("Client connected from %s:%d...\n", inet_ntoa(clientSocketData.sin_addr), (int) ntohs(clientSocketData.sin_port));
  209. int i = 0;
  210. while(1)
  211. {
  212. if(s->clientSockets[i] == -1)
  213. {
  214. if(s->threads[i] != -1)
  215. {
  216. pthread_cancel(s->threads[i]);
  217. pthread_join(s->threads[i], NULL);
  218. }
  219. ThreadData data;
  220. data.id = i;
  221. data.server = s;
  222. if(pthread_create(&s->threads[i], NULL, clientHandler, (void*) &data) != 0)
  223. {
  224. perror("Cannot create thread");
  225. Stream out;
  226. streamInit(&out, 16);
  227. streamWriteChar(&out, -1);
  228. streamWriteChars(&out, "Cannot create thread\n");
  229. serverSend(clientSocket, &out);
  230. streamRemove(&out);
  231. close(clientSocket);
  232. }
  233. else
  234. {
  235. s->clientSockets[i] = clientSocket;
  236. Stream out;
  237. streamInit(&out, 64);
  238. streamWriteChar(&out, 1);
  239. streamWriteChars(&out, "Welcome to the server, please enter your command:\n");
  240. if(serverSend(clientSocket, &out) == -1)
  241. {
  242. perror("Cannot send welcome message");
  243. close(clientSocket);
  244. s->clientSockets[i] = -1;
  245. }
  246. streamRemove(&out);
  247. }
  248. break;
  249. }
  250. i++;
  251. if(i >= s->maxClients)
  252. {
  253. printf("max clients reached\n");
  254. Stream out;
  255. streamInit(&out, 16);
  256. streamWriteChar(&out, -1);
  257. streamWriteChars(&out, "the server is full\n");
  258. serverSend(clientSocket, &out);
  259. streamRemove(&out);
  260. close(clientSocket);
  261. break;
  262. }
  263. }
  264. }
  265. }
  266. }
  267. void serverRegisterHandler(Server* s, StreamFunction f)
  268. {
  269. if(s->hAmount == s->hIndex)
  270. {
  271. if(s->hAmount == 0)
  272. {
  273. s->handlers = malloc(sizeof(StreamFunction));
  274. s->hAmount = 1;
  275. }
  276. else
  277. {
  278. s->hAmount *= 2;
  279. StreamFunction* functions = malloc(sizeof(StreamFunction) * s->hAmount);
  280. for(int i = 0; i < s->hIndex; i++)
  281. {
  282. functions[i] = s->handlers[i];
  283. }
  284. free(s->handlers);
  285. s->handlers = functions;
  286. }
  287. }
  288. s->handlers[s->hIndex] = f;
  289. s->hIndex++;
  290. }