src/Network/Reactor.hh
author terom
Mon, 22 Dec 2008 02:37:28 +0000
changeset 386 2f019ecb4aa9
parent 380 d193dd1d8a7e
child 400 d64bf28c4340
permissions -rw-r--r--
amend r387
380
d193dd1d8a7e new NetworkReactor, fix everything to actually work
terom
parents:
diff changeset
     1
#ifndef NETWORK_REACTOR_HH
d193dd1d8a7e new NetworkReactor, fix everything to actually work
terom
parents:
diff changeset
     2
#define NETWORK_REACTOR_HH
d193dd1d8a7e new NetworkReactor, fix everything to actually work
terom
parents:
diff changeset
     3
d193dd1d8a7e new NetworkReactor, fix everything to actually work
terom
parents:
diff changeset
     4
// forward-declare
d193dd1d8a7e new NetworkReactor, fix everything to actually work
terom
parents:
diff changeset
     5
class NetworkReactor;
d193dd1d8a7e new NetworkReactor, fix everything to actually work
terom
parents:
diff changeset
     6
d193dd1d8a7e new NetworkReactor, fix everything to actually work
terom
parents:
diff changeset
     7
/**
d193dd1d8a7e new NetworkReactor, fix everything to actually work
terom
parents:
diff changeset
     8
 * Events to poll for
d193dd1d8a7e new NetworkReactor, fix everything to actually work
terom
parents:
diff changeset
     9
 */
d193dd1d8a7e new NetworkReactor, fix everything to actually work
terom
parents:
diff changeset
    10
enum NetworkPollBit {
d193dd1d8a7e new NetworkReactor, fix everything to actually work
terom
parents:
diff changeset
    11
    POLL_READ       = 0x01,
d193dd1d8a7e new NetworkReactor, fix everything to actually work
terom
parents:
diff changeset
    12
    POLL_WRITE      = 0x02,
d193dd1d8a7e new NetworkReactor, fix everything to actually work
terom
parents:
diff changeset
    13
};
d193dd1d8a7e new NetworkReactor, fix everything to actually work
terom
parents:
diff changeset
    14
d193dd1d8a7e new NetworkReactor, fix everything to actually work
terom
parents:
diff changeset
    15
/**
d193dd1d8a7e new NetworkReactor, fix everything to actually work
terom
parents:
diff changeset
    16
 * Poll event bitmask of NetworkPollBit's
d193dd1d8a7e new NetworkReactor, fix everything to actually work
terom
parents:
diff changeset
    17
 */
d193dd1d8a7e new NetworkReactor, fix everything to actually work
terom
parents:
diff changeset
    18
typedef int NetworkPollMask;
d193dd1d8a7e new NetworkReactor, fix everything to actually work
terom
parents:
diff changeset
    19
d193dd1d8a7e new NetworkReactor, fix everything to actually work
terom
parents:
diff changeset
    20
#include "Socket.hh"
d193dd1d8a7e new NetworkReactor, fix everything to actually work
terom
parents:
diff changeset
    21
#include "Error.hh"
d193dd1d8a7e new NetworkReactor, fix everything to actually work
terom
parents:
diff changeset
    22
d193dd1d8a7e new NetworkReactor, fix everything to actually work
terom
parents:
diff changeset
    23
/*
d193dd1d8a7e new NetworkReactor, fix everything to actually work
terom
parents:
diff changeset
    24
 * Platform-specific includes
d193dd1d8a7e new NetworkReactor, fix everything to actually work
terom
parents:
diff changeset
    25
 */
d193dd1d8a7e new NetworkReactor, fix everything to actually work
terom
parents:
diff changeset
    26
#ifndef WIN32
d193dd1d8a7e new NetworkReactor, fix everything to actually work
terom
parents:
diff changeset
    27
    // linux
d193dd1d8a7e new NetworkReactor, fix everything to actually work
terom
parents:
diff changeset
    28
    #include <sys/select.h>
386
2f019ecb4aa9 amend r387
terom
parents: 380
diff changeset
    29
    #include <errno.h>
380
d193dd1d8a7e new NetworkReactor, fix everything to actually work
terom
parents:
diff changeset
    30
#else
d193dd1d8a7e new NetworkReactor, fix everything to actually work
terom
parents:
diff changeset
    31
    #error "This network code won't compile on win32 :)"
d193dd1d8a7e new NetworkReactor, fix everything to actually work
terom
parents:
diff changeset
    32
#endif
d193dd1d8a7e new NetworkReactor, fix everything to actually work
terom
parents:
diff changeset
    33
d193dd1d8a7e new NetworkReactor, fix everything to actually work
terom
parents:
diff changeset
    34
#include <list>
d193dd1d8a7e new NetworkReactor, fix everything to actually work
terom
parents:
diff changeset
    35
d193dd1d8a7e new NetworkReactor, fix everything to actually work
terom
parents:
diff changeset
    36
/**
d193dd1d8a7e new NetworkReactor, fix everything to actually work
terom
parents:
diff changeset
    37
 * A Reactor manages a set of NetworkSockets, providing readyness notification and a poll method
d193dd1d8a7e new NetworkReactor, fix everything to actually work
terom
parents:
diff changeset
    38
 */
d193dd1d8a7e new NetworkReactor, fix everything to actually work
terom
parents:
diff changeset
    39
class NetworkReactor {
d193dd1d8a7e new NetworkReactor, fix everything to actually work
terom
parents:
diff changeset
    40
    protected:
d193dd1d8a7e new NetworkReactor, fix everything to actually work
terom
parents:
diff changeset
    41
        std::list<NetworkSocket*> sockets;
d193dd1d8a7e new NetworkReactor, fix everything to actually work
terom
parents:
diff changeset
    42
d193dd1d8a7e new NetworkReactor, fix everything to actually work
terom
parents:
diff changeset
    43
    public:
d193dd1d8a7e new NetworkReactor, fix everything to actually work
terom
parents:
diff changeset
    44
        /**
d193dd1d8a7e new NetworkReactor, fix everything to actually work
terom
parents:
diff changeset
    45
         * Construct the empty reactor
d193dd1d8a7e new NetworkReactor, fix everything to actually work
terom
parents:
diff changeset
    46
         */
d193dd1d8a7e new NetworkReactor, fix everything to actually work
terom
parents:
diff changeset
    47
        NetworkReactor (void);
d193dd1d8a7e new NetworkReactor, fix everything to actually work
terom
parents:
diff changeset
    48
d193dd1d8a7e new NetworkReactor, fix everything to actually work
terom
parents:
diff changeset
    49
        /**
d193dd1d8a7e new NetworkReactor, fix everything to actually work
terom
parents:
diff changeset
    50
         * Our static global reactor
d193dd1d8a7e new NetworkReactor, fix everything to actually work
terom
parents:
diff changeset
    51
         */
d193dd1d8a7e new NetworkReactor, fix everything to actually work
terom
parents:
diff changeset
    52
        static NetworkReactor *current;
d193dd1d8a7e new NetworkReactor, fix everything to actually work
terom
parents:
diff changeset
    53
d193dd1d8a7e new NetworkReactor, fix everything to actually work
terom
parents:
diff changeset
    54
        /**
d193dd1d8a7e new NetworkReactor, fix everything to actually work
terom
parents:
diff changeset
    55
         * Add a NetworkSocket to our list of sockets. The desired notification states are fetched directly from the
d193dd1d8a7e new NetworkReactor, fix everything to actually work
terom
parents:
diff changeset
    56
         * socket itself.
d193dd1d8a7e new NetworkReactor, fix everything to actually work
terom
parents:
diff changeset
    57
         *
d193dd1d8a7e new NetworkReactor, fix everything to actually work
terom
parents:
diff changeset
    58
         * @param socket the socket to watch
d193dd1d8a7e new NetworkReactor, fix everything to actually work
terom
parents:
diff changeset
    59
         */
d193dd1d8a7e new NetworkReactor, fix everything to actually work
terom
parents:
diff changeset
    60
        void add_socket (NetworkSocket *socket) { sockets.push_back(socket); }
d193dd1d8a7e new NetworkReactor, fix everything to actually work
terom
parents:
diff changeset
    61
d193dd1d8a7e new NetworkReactor, fix everything to actually work
terom
parents:
diff changeset
    62
        /**
d193dd1d8a7e new NetworkReactor, fix everything to actually work
terom
parents:
diff changeset
    63
         * Remove a NetworkSocket from our list of sockets.
d193dd1d8a7e new NetworkReactor, fix everything to actually work
terom
parents:
diff changeset
    64
         *
d193dd1d8a7e new NetworkReactor, fix everything to actually work
terom
parents:
diff changeset
    65
         * @param socket the socket to stop watching
d193dd1d8a7e new NetworkReactor, fix everything to actually work
terom
parents:
diff changeset
    66
         */
d193dd1d8a7e new NetworkReactor, fix everything to actually work
terom
parents:
diff changeset
    67
        void remove_socket (NetworkSocket *socket) { sockets.remove(socket); }
d193dd1d8a7e new NetworkReactor, fix everything to actually work
terom
parents:
diff changeset
    68
d193dd1d8a7e new NetworkReactor, fix everything to actually work
terom
parents:
diff changeset
    69
        /**
d193dd1d8a7e new NetworkReactor, fix everything to actually work
terom
parents:
diff changeset
    70
         * Poll our sockets and drive any I/O, optionally sleeping for the given timeout. This is efficient if our
d193dd1d8a7e new NetworkReactor, fix everything to actually work
terom
parents:
diff changeset
    71
         * sockets list is empty.
d193dd1d8a7e new NetworkReactor, fix everything to actually work
terom
parents:
diff changeset
    72
         */
d193dd1d8a7e new NetworkReactor, fix everything to actually work
terom
parents:
diff changeset
    73
        void poll (timeval *timeout = NULL);
d193dd1d8a7e new NetworkReactor, fix everything to actually work
terom
parents:
diff changeset
    74
};
d193dd1d8a7e new NetworkReactor, fix everything to actually work
terom
parents:
diff changeset
    75
d193dd1d8a7e new NetworkReactor, fix everything to actually work
terom
parents:
diff changeset
    76
/**
d193dd1d8a7e new NetworkReactor, fix everything to actually work
terom
parents:
diff changeset
    77
 * Reactor error
d193dd1d8a7e new NetworkReactor, fix everything to actually work
terom
parents:
diff changeset
    78
 */
d193dd1d8a7e new NetworkReactor, fix everything to actually work
terom
parents:
diff changeset
    79
class NetworkReactorError : public NetworkErrno {
d193dd1d8a7e new NetworkReactor, fix everything to actually work
terom
parents:
diff changeset
    80
    public:
d193dd1d8a7e new NetworkReactor, fix everything to actually work
terom
parents:
diff changeset
    81
        NetworkReactorError (std::string op) : NetworkErrno(op) { }
d193dd1d8a7e new NetworkReactor, fix everything to actually work
terom
parents:
diff changeset
    82
};
d193dd1d8a7e new NetworkReactor, fix everything to actually work
terom
parents:
diff changeset
    83
d193dd1d8a7e new NetworkReactor, fix everything to actually work
terom
parents:
diff changeset
    84
#endif