PageRenderTime 41ms CodeModel.GetById 17ms app.highlight 18ms RepoModel.GetById 1ms app.codeStats 0ms

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