game_server.h 2.9 KB

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