1
// Copyright (c) 2001-2004 Rob Kaper <cap@capsi.com>,
2
// 2001 Erik Bourget <ebourg@cs.mcgill.ca>
4
// This program is free software; you can redistribute it and/or
5
// modify it under the terms of the GNU General Public License
6
// version 2 as published by the Free Software Foundation.
8
// This program is distributed in the hope that it will be useful,
9
// but WITHOUT ANY WARRANTY; without even the implied warranty of
10
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11
// General Public License for more details.
13
// You should have received a copy of the GNU General Public License
14
// along with this program; see the file COPYING. If not, write to
15
// the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
16
// Boston, MA 02111-1307, USA.
25
#include <libcapsinetwork/listener.h>
26
#include <libcapsinetwork/socket.h>
32
MonopdListener::MonopdListener( MonopdServer *server )
36
if ( addListenPort( server->port() ) == -1 )
38
syslog( LOG_ERR, "could not bind port %d, exiting", server->port() );
42
syslog( LOG_NOTICE, "listener: port=[%d]", server->port() );
45
void MonopdListener::socketHandler( Socket *socket, const std::string &data )
47
switch( socket->status() )
50
syslog( LOG_INFO, "connection: fd=[%d], ip=[%s]", socket->fd(), socket->ipAddr().c_str() );
51
socket->ioWrite( std::string("<monopd><server version=\"") + MONOPD_VERSION_STRING + "\"/></monopd>\n" );
52
m_server->initSocketTimeoutEvent( socket->fd() );
57
syslog( LOG_INFO, "disconnect: fd=[%d], ip=[%s]", socket->fd(), socket->ipAddr().c_str() );
58
m_server->closedSocket( socket );
62
m_server->processInput( socket, data );
67
int main(int argc, char **argv)
69
srand( (unsigned) time(0) );
70
signal( SIGPIPE, SIG_IGN );
72
openlog( "monopd", LOG_PID, LOG_DAEMON );
73
syslog( LOG_NOTICE, "monopd %s started", MONOPD_VERSION_STRING );
75
MonopdServer *server = new MonopdServer();
77
server->setPort( atoi(argv[1]) );
79
MonopdListener *listener = new MonopdListener( server );
81
// close stdin, stdout, stderr
82
// close(0); close(1); close(2);
84
server->initMonopigatorEvent();
88
// Check for network events
89
listener->checkActivity();
91
// Check for scheduled events in the timer
92
int fd = server->processEvents();
95
Socket *delSocket = listener->findSocket(fd);
96
if (delSocket && !server->findPlayer(delSocket))
97
delSocket->setStatus(Socket::Close);