game_server.h 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. #ifndef GAME_SERVER_H
  2. #define GAME_SERVER_H
  3. #include <time.h>
  4. #include <stdio.h>
  5. #include <string.h>
  6. #include <stdlib.h>
  7. #include <unistd.h>
  8. #include <arpa/inet.h>
  9. #include <sys/select.h>
  10. #include <signal.h>
  11. #include <sys/time.h>
  12. #include <errno.h>
  13. #ifndef PORT
  14. #define PORT 7418
  15. #endif
  16. #define NAME 0 //reflects 2 states of client connection: 0 - identification, 1 - identified, can enter commands
  17. #define COMMAND 1
  18. #define DELIM " \n"
  19. #define TIMER_TICK 120
  20. #define MAX_NAME 10 // Max playername length
  21. #define INPUT_ARG_MAX_NUM 10
  22. #define BUFFER_SIZE 1024
  23. //Node for the linked list which stores currently connected clients
  24. typedef struct client {
  25. int fd;
  26. int state; // either NAME or COMMAND
  27. char name[MAX_NAME];
  28. char buf[BUFFER_SIZE]; // each client has its own buffer
  29. int inbuf; // and a personal pointer to the current place in buffer
  30. struct client *next;
  31. } Client;
  32. //Node for linked list which stores all players ever played - these are never removed
  33. typedef struct player {
  34. char name[MAX_NAME];
  35. int max_score;
  36. int total_games;
  37. int total_score;
  38. struct player *next;
  39. } Player;
  40. //=====================================
  41. // These are implemented in players.c
  42. //=====================================
  43. /*
  44. * Create a new player with the given name. Insert it at the tail of the list
  45. * of players whose head is pointed to by *player_ptr_add.
  46. *
  47. * Return:
  48. * - 0 if successful
  49. * - 1 if a player by this name already exists in this list
  50. * - 2 if the given name cannot fit in the 'name' array
  51. * - in this case truncate the name (don't forget about the null terminator),
  52. * add to the list, and return 2 to send message to the client that name was truncated.
  53. *
  54. */
  55. int create_player(const char *name, Player **player_ptr_add);
  56. /*
  57. * Return a pointer to the player with this name in
  58. * the list starting with head. Return NULL if no such player exists.
  59. *
  60. * NOTE: You'll likely need to cast a (const Player *) to a (Player *)
  61. * to satisfy the prototype without warnings.
  62. */
  63. Player *find_player(const char *name, const Player *head);
  64. //=====================================
  65. // These implemented in game_server.c
  66. //=====================================
  67. /*
  68. * Sets up server socket: bind and listen
  69. * Returns a file descriptor for listening and accepting new connections
  70. */
  71. int setup (void);
  72. void timer_handler(int sig);
  73. /* accept new connection and calls add_client */
  74. void new_connection(int listenfd) ;
  75. /* Sets up Client fields, add to the list, and ask for the name */
  76. void add_client(int fd, struct in_addr addr);
  77. /* remove client from the list */
  78. void remove_client(int fd);
  79. void sendclient(Client *p, char *msg);
  80. void receiveclient(Client *p);
  81. void interpret_message(Client *p);
  82. int parse_line(Client *p);
  83. int do_command(struct client * p, int cmd_argc, char **cmd_argv);
  84. int find_network_newline(char *buf, int inbuf);
  85. char* boardGenerator();
  86. void boardCreator(char* boardValues);
  87. #endif