src/Network/Reactor.hh
author terom
Tue, 16 Dec 2008 23:21:26 +0000
changeset 380 d193dd1d8a7e
child 386 2f019ecb4aa9
permissions -rw-r--r--
new NetworkReactor, fix everything to actually work
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>
d193dd1d8a7e new NetworkReactor, fix everything to actually work
terom
parents:
diff changeset
    29
#else
d193dd1d8a7e new NetworkReactor, fix everything to actually work
terom
parents:
diff changeset
    30
    #error "This network code won't compile on win32 :)"
d193dd1d8a7e new NetworkReactor, fix everything to actually work
terom
parents:
diff changeset
    31
#endif
d193dd1d8a7e new NetworkReactor, fix everything to actually work
terom
parents:
diff changeset
    32
d193dd1d8a7e new NetworkReactor, fix everything to actually work
terom
parents:
diff changeset
    33
#include <list>
d193dd1d8a7e new NetworkReactor, fix everything to actually work
terom
parents:
diff changeset
    34
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
 * 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
    37
 */
d193dd1d8a7e new NetworkReactor, fix everything to actually work
terom
parents:
diff changeset
    38
class NetworkReactor {
d193dd1d8a7e new NetworkReactor, fix everything to actually work
terom
parents:
diff changeset
    39
    protected:
d193dd1d8a7e new NetworkReactor, fix everything to actually work
terom
parents:
diff changeset
    40
        std::list<NetworkSocket*> sockets;
d193dd1d8a7e new NetworkReactor, fix everything to actually work
terom
parents:
diff changeset
    41
d193dd1d8a7e new NetworkReactor, fix everything to actually work
terom
parents:
diff changeset
    42
    public:
d193dd1d8a7e new NetworkReactor, fix everything to actually work
terom
parents:
diff changeset
    43
        /**
d193dd1d8a7e new NetworkReactor, fix everything to actually work
terom
parents:
diff changeset
    44
         * Construct the empty reactor
d193dd1d8a7e new NetworkReactor, fix everything to actually work
terom
parents:
diff changeset
    45
         */
d193dd1d8a7e new NetworkReactor, fix everything to actually work
terom
parents:
diff changeset
    46
        NetworkReactor (void);
d193dd1d8a7e new NetworkReactor, fix everything to actually work
terom
parents:
diff changeset
    47
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
         * Our static global reactor
d193dd1d8a7e new NetworkReactor, fix everything to actually work
terom
parents:
diff changeset
    50
         */
d193dd1d8a7e new NetworkReactor, fix everything to actually work
terom
parents:
diff changeset
    51
        static NetworkReactor *current;
d193dd1d8a7e new NetworkReactor, fix everything to actually work
terom
parents:
diff changeset
    52
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
         * 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
    55
         * socket itself.
d193dd1d8a7e new NetworkReactor, fix everything to actually work
terom
parents:
diff changeset
    56
         *
d193dd1d8a7e new NetworkReactor, fix everything to actually work
terom
parents:
diff changeset
    57
         * @param socket the socket to watch
d193dd1d8a7e new NetworkReactor, fix everything to actually work
terom
parents:
diff changeset
    58
         */
d193dd1d8a7e new NetworkReactor, fix everything to actually work
terom
parents:
diff changeset
    59
        void add_socket (NetworkSocket *socket) { sockets.push_back(socket); }
d193dd1d8a7e new NetworkReactor, fix everything to actually work
terom
parents:
diff changeset
    60
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
         * Remove a NetworkSocket from our list of sockets.
d193dd1d8a7e new NetworkReactor, fix everything to actually work
terom
parents:
diff changeset
    63
         *
d193dd1d8a7e new NetworkReactor, fix everything to actually work
terom
parents:
diff changeset
    64
         * @param socket the socket to stop watching
d193dd1d8a7e new NetworkReactor, fix everything to actually work
terom
parents:
diff changeset
    65
         */
d193dd1d8a7e new NetworkReactor, fix everything to actually work
terom
parents:
diff changeset
    66
        void remove_socket (NetworkSocket *socket) { sockets.remove(socket); }
d193dd1d8a7e new NetworkReactor, fix everything to actually work
terom
parents:
diff changeset
    67
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
         * 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
    70
         * sockets list is empty.
d193dd1d8a7e new NetworkReactor, fix everything to actually work
terom
parents:
diff changeset
    71
         */
d193dd1d8a7e new NetworkReactor, fix everything to actually work
terom
parents:
diff changeset
    72
        void poll (timeval *timeout = NULL);
d193dd1d8a7e new NetworkReactor, fix everything to actually work
terom
parents:
diff changeset
    73
};
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
 * Reactor error
d193dd1d8a7e new NetworkReactor, fix everything to actually work
terom
parents:
diff changeset
    77
 */
d193dd1d8a7e new NetworkReactor, fix everything to actually work
terom
parents:
diff changeset
    78
class NetworkReactorError : public NetworkErrno {
d193dd1d8a7e new NetworkReactor, fix everything to actually work
terom
parents:
diff changeset
    79
    public:
d193dd1d8a7e new NetworkReactor, fix everything to actually work
terom
parents:
diff changeset
    80
        NetworkReactorError (std::string op) : NetworkErrno(op) { }
d193dd1d8a7e new NetworkReactor, fix everything to actually work
terom
parents:
diff changeset
    81
};
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
#endif