xwords/xwords4/relay/lstnrmgr.h
ehouse 25c4368231 Start adding ability to listen on multiple sockets and to dynamically
change the set listened on.  There's still some debugging to do but
nothing that worked before is broken.  Also begin to accept unique
prefixes (e.g. g for get) for commands and attributes on the control
port.  Note that relay-related code in comms seems broken now, but is
without this checkin.
2007-12-01 15:00:30 +00:00

73 lines
1.9 KiB
C

/* -*-mode: C; fill-column: 78; c-basic-offset: 4; -*- */
/*
* Copyright 2007 by Eric House (xwords@eehouse.org). All rights reserved.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#ifndef _LSTNRMGR_H_
#define _LSTNRMGR_H_
#include <string>
#include <vector>
#include <map>
#include "xwrelay_priv.h"
using namespace std;
class ListenerMgr {
public:
void RemoveAll();
/* void RemoveListener( int listener ); */
bool AddListener( int port );
void SetAll( const vector<int>* iv ); /* replace current set with this new one */
void AddToFDSet( fd_set* rfds );
int GetHighest();
bool PortInUse( int port );
private:
void removeSocket( int sock );
void removePort( int port );
bool addOne( int listener );
bool portInUse( int port );
map<int,int> m_socks_to_ports;
pthread_mutex_t m_mutex;
friend class ListenersIter;
};
class ListenersIter {
public:
ListenersIter(ListenerMgr* lm, bool fds) {
m_fds = fds;
m_lm = lm;
pthread_mutex_lock( &m_lm->m_mutex );
m_iter = lm->m_socks_to_ports.begin();
}
~ListenersIter() {
pthread_mutex_unlock( &m_lm->m_mutex );
}
int next();
private:
bool m_fds;
map<int,int>::const_iterator m_iter;
ListenerMgr* m_lm;
};
#endif