CSIS 440 chat client/server project.

SircServer.h 4.0KB

    #ifndef __SIRC_SERVER__ #define __SIRC_SERVER__ /* Author: Matt Kava Asmt: CSIS 440, Spring 2010, Assignment 6 :: IRC-Like Client/Server Description: Sirc Server class header file */ #include "User.h" #include "constants.h" #include <string> #include <sys/time.h> // for Select const int QUEUE_LENGTH = 16; class SircServer { private: /** Private Members **/ fd_set _activeFDset; // the active set of file descriptors for SELECT int _numberFD; // the number of file descriptors to step through for SELECT User* _users[MAX_USER_COUNT]; // the golden User List int _wkSocket; // the well-known Socket unsigned short _wkPort; // The well-known port to bind/listen on int _connectedUsersCount; // The number of currently connected users // Constants int _maxConnectedUsersCount; // maximum number of users that can be connected at any given time int _maxNickLength; // maximum size of a user's nickname int _bufferSize; // the buffer size for all communication done by the server std::string _EOL; // the End-of-Line characters to attach to the end of every outbound message int _maxMessageLength; // the maximum message size (a message being what the server relays from a client to all other clients) bool _readyToRun; // This is set to true when the server is ready to operate. Set only through Startup()! /** Private Methods **/ // HandleUser(User*) :: Handles a user for either verifying Nick, disconnecting, or relaying a message void HandleUser(User*); // AcceptNew(int) :: Accept a new User from the provided sockfd. Error checks itself as it adds. Full server issues are handled here. void AcceptNew(int); // AddUser(User*) :: Adds a User* to the user list void AddUser(User*); // RemoveUser(User*) :: Erases the User*'s record in the user list void RemoveUser(User*); // BroadcastMessage(string) :: Sends a message to all connected clients void BroadcastMessage(std::string message); // FormatMessage(string) :: Formats a message for sending out to clients. Returns the formatted message. std::string FormatMessage(std::string); // CompareAllNicks(string) :: Compares the provided nick against all nicknames currently in use. // If a nick is in use, returns false. If available, returns true. bool CompareAllNicks(std::string); public: /** Public Members **/ // Users() :: Returns the user list container (an array) for usage and manipulation User** Users(); // Users(int) :: Searches the user list and returns a User* to the user with the sockfd requested // If there is not a user with that socketfd, it will return NULL. User* Users(int socketfd); void WkPort(unsigned short); unsigned short WkPort(); int ConnectedUsersCount(); int MaxConnectedUsersCount(); int MaxNickLength(); int BufferSize(); std::string EOL(); int MaxMessageLength(); bool ReadyToRun(); /** Public Methods **/ // Constructor() :: Initializes a default version of the server using a set of defined constants SircServer(); // Constructor(unsigned short, int) :: Initializes a default version of the server using a set of defined constants, but with a specified Well-Known Port and Max User Count SircServer(unsigned short wkport, int maxConnectedUsers=MAX_USER_COUNT); // Deconstructor ~SircServer(); // Purify(string) :: purifies the provided string and returns the purified message std::string Purify(std::string, bool=false); // Startup() :: Sets up the well-known socket (socket, bind, listen) so it is ready to accept connections // This will return error values (all errors are >0), if no errors.. returns 0 int Startup(); // Shutdown() :: Shuts down the server. Sends a shutdown message to all connected clients and disconnects all of them. int Shutdown(); // Run() :: Handles the select(...) operations of the server. Checks if the server is ready to operate before running. // This can be exited and returned to as the active file descriptor list used in the select(...) is stored in the class // and not just the method itself. void Run(); }; #endif