~ubuntu-branches/debian/sid/monopd/sid

« back to all changes in this revision

Viewing changes to .pc/01_gcc43_includes.diff/src/main.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Daniel Schepler
  • Date: 2011-07-18 22:07:44 UTC
  • Revision ID: james.westby@ubuntu.com-20110718220744-bg6e0yij97p8xxno
Tags: 0.9.3-5
* Orphaning package (see #634499).
* Bump Standards-Version to 3.9.2.
* Update to 3.0 (quilt) source format.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// Copyright (c) 2001-2004 Rob Kaper <cap@capsi.com>,
 
2
//               2001 Erik Bourget <ebourg@cs.mcgill.ca>
 
3
//
 
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.
 
7
//
 
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.
 
12
//
 
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.
 
17
 
 
18
#include <signal.h>
 
19
#include <syslog.h>
 
20
#include <unistd.h>
 
21
 
 
22
#include <iostream>
 
23
#include <string>
 
24
 
 
25
#include <libcapsinetwork/listener.h>
 
26
#include <libcapsinetwork/socket.h>
 
27
 
 
28
#include "const.h"
 
29
#include "main.h"
 
30
#include "server.h"
 
31
 
 
32
MonopdListener::MonopdListener( MonopdServer *server )
 
33
 :      Listener(),
 
34
        m_server( server )
 
35
{
 
36
        if ( addListenPort( server->port() ) == -1 )
 
37
        {
 
38
                syslog( LOG_ERR, "could not bind port %d, exiting", server->port() );
 
39
                exit(1);
 
40
        }
 
41
        else
 
42
                syslog( LOG_NOTICE, "listener: port=[%d]", server->port() );
 
43
}
 
44
 
 
45
void MonopdListener::socketHandler( Socket *socket, const std::string &data )
 
46
{
 
47
        switch( socket->status() )
 
48
        {
 
49
                case Socket::New:
 
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() );
 
53
                        break;
 
54
 
 
55
                case Socket::Close:
 
56
                case Socket::Closed:
 
57
                        syslog( LOG_INFO, "disconnect: fd=[%d], ip=[%s]", socket->fd(), socket->ipAddr().c_str() );
 
58
                        m_server->closedSocket( socket );
 
59
                        break;
 
60
                        
 
61
                case Socket::Ok:
 
62
                        m_server->processInput( socket, data );
 
63
                        break;
 
64
        }
 
65
}
 
66
 
 
67
int main(int argc, char **argv)
 
68
{
 
69
        srand( (unsigned) time(0) );
 
70
        signal( SIGPIPE, SIG_IGN );
 
71
 
 
72
        openlog( "monopd", LOG_PID, LOG_DAEMON );
 
73
        syslog( LOG_NOTICE, "monopd %s started", MONOPD_VERSION_STRING );
 
74
 
 
75
        MonopdServer *server = new MonopdServer();
 
76
        if ( argc > 1 )
 
77
                server->setPort( atoi(argv[1]) );
 
78
 
 
79
        MonopdListener *listener = new MonopdListener( server );
 
80
 
 
81
        // close stdin, stdout, stderr
 
82
        // close(0); close(1); close(2);
 
83
 
 
84
        server->initMonopigatorEvent();
 
85
 
 
86
        for(;;)
 
87
        {
 
88
                // Check for network events
 
89
                listener->checkActivity();
 
90
 
 
91
                // Check for scheduled events in the timer
 
92
                int fd = server->processEvents();
 
93
                if(fd != -1)
 
94
                {
 
95
                        Socket *delSocket = listener->findSocket(fd);
 
96
                        if (delSocket && !server->findPlayer(delSocket))
 
97
                                delSocket->setStatus(Socket::Close);
 
98
                }
 
99
        }
 
100
 
 
101
        // Clean up memory
 
102
        delete server;
 
103
        delete listener;
 
104
 
 
105
        return 0;
 
106
}