/* core/netserver.h This file is part of the Osirion project and is distributed under the terms of the GNU General Public License version 2 */ #ifndef __INCLUDED_CORE_NETSERVER_H__ #define __INCLUDED_CORE_NETSERVER_H__ #ifndef _WIN32 #include #else #include #endif #include #include #include "core/net.h" #include "core/netclient.h" #include "core/player.h" namespace core { /// Network server class NetServer { public: typedef std::list Clients; NetServer(std::string const host, unsigned int const port); ~NetServer(); /*----- inspectors ------------------------------------------------ */ /// Returns true if the TCP server has a valid file descriptor inline bool valid() const { return netserver_fd != -1; } /// Returns true if the TCP server has an invalid file descriptor inline bool invalid() const { return netserver_fd == -1; } /// Returns the error state of the TCP server inline bool error() const { return netserver_error; } /// Return the socket file descriptor inline int fd() const { return netserver_fd; } /*----- mutators -------------------------------------------------- */ /// receive data from clients void receive(); /// transmit data to clients void transmit(); /// broadcast a message void broadcast_message(const char *channel, std::string const & message, Player *ignore_player=0); /// send a message to a single client void send_message(NetClient *client, const char *channel, std::string const & message); /// disconnect a single client void send_disconnect(NetClient *client); /// broadcast a server frame message void broadcast_frame(float timestamp, float previoustimestamp); /// broadcast a delete entity event void broadcast_entity_delete(Entity *entity); /// broadcast a create entity event void broadcast_entity_create(Entity *entity); /// send a create entity event to a single player void send_entity_create(NetClient *client, Entity *entity); /// send a zone update event to a single player void send_zone_update(NetClient *client, Zone *zone); /// broadcast a update entity event void broadcast_entity_update(Entity *entity); /// broadcast updated player information, if necessary void broadcast_player_update(); /// send an update player information message to a single client void send_player_update(NetClient *client); /// find the client corresponding to a player NetClient *find_client(Player const *player); protected: /// set the error state void abort(); /// called when a new client connects NetClient *client_connect(std::string const host, int const port); /// called when a client has send the intial handshake void client_initialize(NetClient *client); /// remove terminated clients void reap(); /// parse incoming client messages void parse_incoming_message(NetClient *client, const std::string & message); private: bool netserver_error; int netserver_fd; struct sockaddr_in netserver_addr; fd_set serverset; int fdmax; char recbuf[FRAMESIZE]; Clients clients; }; } #endif // __INCLUDED_CORE_NETSERVER_H__