From df17fe4f1e919060bd86194f304c01db9cd10206 Mon Sep 17 00:00:00 2001 From: ehouse Date: Wed, 20 Apr 2005 12:10:05 +0000 Subject: [PATCH] reorganize; support ctrl functions --- xwords4/relay/cref.h | 53 +++++++++++++++++++++++++++++++++++++------- 1 file changed, 45 insertions(+), 8 deletions(-) diff --git a/xwords4/relay/cref.h b/xwords4/relay/cref.h index 80301dfeb..3f4008476 100644 --- a/xwords4/relay/cref.h +++ b/xwords4/relay/cref.h @@ -25,22 +25,35 @@ class CookieRef { If the hostID is HOST_ID_SERVER, it's the server. */ void Associate( int socket, HostID srcID ); short GetHeartbeat() { return 60; } - short GetConnID() { return m_connectionID; } + CookieID GetCookieID() { return m_connectionID; } int SocketForHost( HostID dest ); void Remove( int socket ); int CountSockets() { return m_hostSockets.size(); } string Name() { return m_name; } - static CookieMapIterator GetCookieNameIterator(); + void RecordSent( int nBytes, int socket ) { + /* This really needs a lock.... */ + m_totalSent += nBytes; + } + + /* for console */ + void PrintCookieInfo( string& out ); + void PrintSocketInfo( string& out, int socket ); + + static CookieMapIterator GetCookieIterator(); static CookieRef* AddNew( string s ); + /* Nuke an existing */ + static void Delete( CookieID id ); + static void Delete( const char* name ); private: CookieRef( string s ); map m_hostSockets; - pthread_mutex_t m_mutex; + pthread_rwlock_t m_sockets_rwlock; CookieID m_connectionID; string m_name; + int m_totalSent; static CookieID ms_nextConnectionID; }; @@ -51,15 +64,39 @@ class CookieMapIterator { public: CookieMapIterator(); ~CookieMapIterator() {} - const char* Next(); + CookieID Next(); private: CookieMap::const_iterator _iter; }; -CookieRef* get_make_cookieRef( char* cookie, CookieID connID ); +CookieRef* get_make_cookieRef( const char* cookie, CookieID connID ); CookieRef* get_cookieRef( unsigned short cookieID ); -void Associate( int socket, CookieRef* cref ); -void RemoveSocketRefs( int socket ); -pthread_mutex_t* GetWriteMutexForSocket( int socket ); +CookieID CookieIdForName( const char* name ); + +class SocketStuff; +typedef map< int, SocketStuff* > SocketMap; + +class SocketsIterator { + public: + SocketsIterator( SocketMap::iterator iter ); + int Next(); + private: + SocketMap::iterator m_iter; +}; + +class SocketMgr { + public: + static void Associate( int socket, CookieRef* cref ); + static pthread_mutex_t* GetWriteMutexForSocket( int socket ); + static void RemoveSocketRefs( int socket ); + static void PrintSocketInfo( int socket, string& out ); + static SocketsIterator MakeSocketsIterator(); + + private: + static CookieRef* CookieRefForSocket( int socket ); + static SocketMap ms_SocketStuff; + static pthread_mutex_t ms_SocketStuffMutex; +}; + #endif