/src/Server.h

https://bitbucket.org/voltechs/hexen · C Header · 147 lines · 56 code · 39 blank · 52 comment · 1 complexity · 801d0aae3b9adf1ed442a462aa475f2f MD5 · raw file

  1. #ifndef __SERVER_H__
  2. #define __SERVER_H__
  3. #include <stdlib.h>
  4. #include <stdio.h>
  5. #include <string.h>
  6. #include <list>
  7. // for int conversion
  8. #include <sstream>
  9. #include <iostream>
  10. #include <unistd.h>
  11. #include "Dispatch.h"
  12. #define MAX_CONNECTIONS 10
  13. #define CORRUPT_CLIENT_THRESHOLD 10
  14. class Server {
  15. public:
  16. Server(unsigned int, unsigned int, char* const*, bool);
  17. ~Server( void );
  18. // Main run function for the server.
  19. void run();
  20. // Can be set to daemon mode so can be run in the background.
  21. bool daemon;
  22. // Print only if not in daemon mode
  23. inline void ifprintf(const char *message, ...) { if(!daemon) { va_list args; va_start(args, message); vfprintf(stdout, message, args); va_end(args); } }
  24. private:
  25. typedef struct {
  26. std::string username;
  27. // Also used as Unique Client ID
  28. uint16_t chat_socket;
  29. // UDP socket address information for NAT Punchthrough.
  30. struct sockaddr_in udp;
  31. // Indicates whether user has completed connection process
  32. bool connected;
  33. // Keeps track of the number of corrupt messages
  34. // the server recieves from the client. Too many
  35. // will result in the server dropping the client.
  36. unsigned int corrupt;
  37. } ClientConnection;
  38. struct sockaddr_in chat_socket_addr;
  39. // To listening socket to which clients initially connect.
  40. int chat_connect_socket;
  41. int file_connect_socket;
  42. // File Descriptor set for client chat connections
  43. fd_set client_set;
  44. // List of client connections
  45. std::list<ClientConnection*> clients;
  46. ClientConnection *findClientByUID( uint16_t c_id );
  47. ClientConnection *findClientByUsername( std::string c_name );
  48. // Maximum file descriptor. Used in multiplexing I/O
  49. int max_fd;
  50. unsigned int maxConnects;
  51. unsigned int chat_port;
  52. /* Deals with a clients file transfer handshaking.
  53. ***************************************************/
  54. void processFileTransferMessages( ClientConnection *from, Dispatch *d, DispatchFlag flag );
  55. /* Responsible for taking a socket and creating
  56. * a new ClientConnection.
  57. ***************************************************/
  58. void acceptConnection( int socket );
  59. /* Takes a Dispatch pointer and sends it to all
  60. * the clients in the servers client list (clients)
  61. * Will exclude the ClientConnection if specified
  62. ***************************************************/
  63. void notifyClients( Dispatch *dispatch, ClientConnection* exclude = NULL );
  64. /* Generates a dispatch containing the list of users
  65. * and sends to all connected clients
  66. ***************************************************/
  67. void notifyClientsUserList( void );
  68. /* Informs particular client of its uid and username
  69. ***************************************************/
  70. void dictateClient( ClientConnection *client );
  71. /* Checks existing users for conflicting username.
  72. * If no conflict, sets and notifies client of name change
  73. * RETURN: whether or not the requested change was accepted.
  74. ***********************************************************/
  75. bool setClientUsername( ClientConnection *client, std::string username );
  76. void handleClientMessage( ClientConnection *client, Dispatch *d );
  77. /* Completes the client connection process.
  78. * Notifies other users of new user.
  79. ***********************************************************/
  80. void completeClientConnection( ClientConnection *client, std::string username );
  81. /* Disconnects client. Closes socket, and notifies
  82. * all other clients using the most appropriate
  83. * DispatchFlag (See Dispatch.h).
  84. * DOES NOT REMOVE CLIENT FROM clients LIST!
  85. ***************************************************/
  86. void disconnectClient( ClientConnection* client );
  87. /* Cycle through all clients processing each message
  88. ***************************************************/
  89. void processCommunicationFromTCP( fd_set readfrom );
  90. /* Sets the maximum File Descriptor from the list
  91. * of clients currently connected to the server
  92. ***************************************************/
  93. void setMaxFD(void);
  94. /* Various forms of initializing the server.
  95. * If parameters are not specified, defaults are
  96. * used instead. See top of Server.h
  97. ***************************************************/
  98. void init(void);
  99. void init(unsigned int max);
  100. void init(unsigned int max, unsigned int port);
  101. void init_tcp( void );
  102. public:
  103. /* Returns a comma seperated list of users (usernames)
  104. * excluding a particular client if specified.
  105. ****************************************************/
  106. std::string listUsers( ClientConnection *exclude = NULL );
  107. };
  108. #endif