src/Network/Reactor.hh
author Tero Marttila <terom@fixme.fi>
Fri, 16 Jan 2009 22:03:49 +0200
changeset 400 d64bf28c4340
parent 386 2f019ecb4aa9
permissions -rw-r--r--
more documentation tweaking, all Network/ files now have a @file comment. Fix Platform.h -> Platform.hh, and Buffer.hh + Packet.cc
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
400
d64bf28c4340 more documentation tweaking, all Network/ files now have a @file comment. Fix Platform.h -> Platform.hh, and Buffer.hh + Packet.cc
Tero Marttila <terom@fixme.fi>
parents: 386
diff changeset
     4
/**
d64bf28c4340 more documentation tweaking, all Network/ files now have a @file comment. Fix Platform.h -> Platform.hh, and Buffer.hh + Packet.cc
Tero Marttila <terom@fixme.fi>
parents: 386
diff changeset
     5
 * @file
d64bf28c4340 more documentation tweaking, all Network/ files now have a @file comment. Fix Platform.h -> Platform.hh, and Buffer.hh + Packet.cc
Tero Marttila <terom@fixme.fi>
parents: 386
diff changeset
     6
 *
d64bf28c4340 more documentation tweaking, all Network/ files now have a @file comment. Fix Platform.h -> Platform.hh, and Buffer.hh + Packet.cc
Tero Marttila <terom@fixme.fi>
parents: 386
diff changeset
     7
 * A select() based reactor for NetworkSocket's
d64bf28c4340 more documentation tweaking, all Network/ files now have a @file comment. Fix Platform.h -> Platform.hh, and Buffer.hh + Packet.cc
Tero Marttila <terom@fixme.fi>
parents: 386
diff changeset
     8
 */
d64bf28c4340 more documentation tweaking, all Network/ files now have a @file comment. Fix Platform.h -> Platform.hh, and Buffer.hh + Packet.cc
Tero Marttila <terom@fixme.fi>
parents: 386
diff changeset
     9
380
d193dd1d8a7e new NetworkReactor, fix everything to actually work
terom
parents:
diff changeset
    10
// forward-declare
d193dd1d8a7e new NetworkReactor, fix everything to actually work
terom
parents:
diff changeset
    11
class NetworkReactor;
d193dd1d8a7e new NetworkReactor, fix everything to actually work
terom
parents:
diff changeset
    12
d193dd1d8a7e new NetworkReactor, fix everything to actually work
terom
parents:
diff changeset
    13
/**
400
d64bf28c4340 more documentation tweaking, all Network/ files now have a @file comment. Fix Platform.h -> Platform.hh, and Buffer.hh + Packet.cc
Tero Marttila <terom@fixme.fi>
parents: 386
diff changeset
    14
 * Event types to poll for
380
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
enum NetworkPollBit {
d193dd1d8a7e new NetworkReactor, fix everything to actually work
terom
parents:
diff changeset
    17
    POLL_READ       = 0x01,
d193dd1d8a7e new NetworkReactor, fix everything to actually work
terom
parents:
diff changeset
    18
    POLL_WRITE      = 0x02,
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
d193dd1d8a7e new NetworkReactor, fix everything to actually work
terom
parents:
diff changeset
    21
/**
d193dd1d8a7e new NetworkReactor, fix everything to actually work
terom
parents:
diff changeset
    22
 * Poll event bitmask of NetworkPollBit's
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
typedef int NetworkPollMask;
d193dd1d8a7e new NetworkReactor, fix everything to actually work
terom
parents:
diff changeset
    25
400
d64bf28c4340 more documentation tweaking, all Network/ files now have a @file comment. Fix Platform.h -> Platform.hh, and Buffer.hh + Packet.cc
Tero Marttila <terom@fixme.fi>
parents: 386
diff changeset
    26
#include "Platform.hh"
380
d193dd1d8a7e new NetworkReactor, fix everything to actually work
terom
parents:
diff changeset
    27
#include "Socket.hh"
d193dd1d8a7e new NetworkReactor, fix everything to actually work
terom
parents:
diff changeset
    28
#include "Error.hh"
d193dd1d8a7e new NetworkReactor, fix everything to actually work
terom
parents:
diff changeset
    29
d193dd1d8a7e new NetworkReactor, fix everything to actually work
terom
parents:
diff changeset
    30
#include <list>
d193dd1d8a7e new NetworkReactor, fix everything to actually work
terom
parents:
diff changeset
    31
d193dd1d8a7e new NetworkReactor, fix everything to actually work
terom
parents:
diff changeset
    32
/**
400
d64bf28c4340 more documentation tweaking, all Network/ files now have a @file comment. Fix Platform.h -> Platform.hh, and Buffer.hh + Packet.cc
Tero Marttila <terom@fixme.fi>
parents: 386
diff changeset
    33
 * A reactor maintains a list of NetworkSockets (which must register themselves to their reactor), and providers
d64bf28c4340 more documentation tweaking, all Network/ files now have a @file comment. Fix Platform.h -> Platform.hh, and Buffer.hh + Packet.cc
Tero Marttila <terom@fixme.fi>
parents: 386
diff changeset
    34
 * readyness notification of non-blocking I/O operations.
d64bf28c4340 more documentation tweaking, all Network/ files now have a @file comment. Fix Platform.h -> Platform.hh, and Buffer.hh + Packet.cc
Tero Marttila <terom@fixme.fi>
parents: 386
diff changeset
    35
 *
d64bf28c4340 more documentation tweaking, all Network/ files now have a @file comment. Fix Platform.h -> Platform.hh, and Buffer.hh + Packet.cc
Tero Marttila <terom@fixme.fi>
parents: 386
diff changeset
    36
 * NetworkSockets must call add_socket(), whereup the NetworkReactor will use NetworkSocket::get_poll() to build the
d64bf28c4340 more documentation tweaking, all Network/ files now have a @file comment. Fix Platform.h -> Platform.hh, and Buffer.hh + Packet.cc
Tero Marttila <terom@fixme.fi>
parents: 386
diff changeset
    37
 * set of sockets to notify. If activity is detected, I will call NetworkSocket::notify().
d64bf28c4340 more documentation tweaking, all Network/ files now have a @file comment. Fix Platform.h -> Platform.hh, and Buffer.hh + Packet.cc
Tero Marttila <terom@fixme.fi>
parents: 386
diff changeset
    38
 *
d64bf28c4340 more documentation tweaking, all Network/ files now have a @file comment. Fix Platform.h -> Platform.hh, and Buffer.hh + Packet.cc
Tero Marttila <terom@fixme.fi>
parents: 386
diff changeset
    39
 * The poll() method can then be used by the application main loop to do timed sleeps, waking up on socket activity and
d64bf28c4340 more documentation tweaking, all Network/ files now have a @file comment. Fix Platform.h -> Platform.hh, and Buffer.hh + Packet.cc
Tero Marttila <terom@fixme.fi>
parents: 386
diff changeset
    40
 * driving the sockets.
380
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
class NetworkReactor {
d193dd1d8a7e new NetworkReactor, fix everything to actually work
terom
parents:
diff changeset
    43
    protected:
d193dd1d8a7e new NetworkReactor, fix everything to actually work
terom
parents:
diff changeset
    44
        std::list<NetworkSocket*> sockets;
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
    public:
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
         * Construct the empty reactor
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
        NetworkReactor (void);
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
        /**
400
d64bf28c4340 more documentation tweaking, all Network/ files now have a @file comment. Fix Platform.h -> Platform.hh, and Buffer.hh + Packet.cc
Tero Marttila <terom@fixme.fi>
parents: 386
diff changeset
    53
         * The global NetworkReactor, used by default for all NetworkSockets.
380
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
        static NetworkReactor *current;
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
        /**
d193dd1d8a7e new NetworkReactor, fix everything to actually work
terom
parents:
diff changeset
    58
         * Add a NetworkSocket to our list of sockets. The desired notification states are fetched directly from the
400
d64bf28c4340 more documentation tweaking, all Network/ files now have a @file comment. Fix Platform.h -> Platform.hh, and Buffer.hh + Packet.cc
Tero Marttila <terom@fixme.fi>
parents: 386
diff changeset
    59
         * socket itself using NetworkSocket::get_poll(), and it will be notified using NetworkSocket::notify().
380
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
         * @param socket the socket to watch
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
        void add_socket (NetworkSocket *socket) { sockets.push_back(socket); }
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
        /**
d193dd1d8a7e new NetworkReactor, fix everything to actually work
terom
parents:
diff changeset
    66
         * Remove a NetworkSocket from our list of sockets.
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
         * @param socket the socket to stop watching
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
        void remove_socket (NetworkSocket *socket) { sockets.remove(socket); }
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
        /**
400
d64bf28c4340 more documentation tweaking, all Network/ files now have a @file comment. Fix Platform.h -> Platform.hh, and Buffer.hh + Packet.cc
Tero Marttila <terom@fixme.fi>
parents: 386
diff changeset
    73
         * Wait for activity on any of the sockets registered and with notification enabled, driving them using 
d64bf28c4340 more documentation tweaking, all Network/ files now have a @file comment. Fix Platform.h -> Platform.hh, and Buffer.hh + Packet.cc
Tero Marttila <terom@fixme.fi>
parents: 386
diff changeset
    74
         * NetworkSocket::notify() if select() indicates activity. This method will sleep at most \a timeout, returning
d64bf28c4340 more documentation tweaking, all Network/ files now have a @file comment. Fix Platform.h -> Platform.hh, and Buffer.hh + Packet.cc
Tero Marttila <terom@fixme.fi>
parents: 386
diff changeset
    75
         * once there was socket activity, or the timeout ran out.
d64bf28c4340 more documentation tweaking, all Network/ files now have a @file comment. Fix Platform.h -> Platform.hh, and Buffer.hh + Packet.cc
Tero Marttila <terom@fixme.fi>
parents: 386
diff changeset
    76
         *
d64bf28c4340 more documentation tweaking, all Network/ files now have a @file comment. Fix Platform.h -> Platform.hh, and Buffer.hh + Packet.cc
Tero Marttila <terom@fixme.fi>
parents: 386
diff changeset
    77
         * This is intended to be particularly efficient if the socket list is empty.
380
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
        void poll (timeval *timeout = NULL);
d193dd1d8a7e new NetworkReactor, fix everything to actually work
terom
parents:
diff changeset
    80
};
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
 * Reactor error
d193dd1d8a7e new NetworkReactor, fix everything to actually work
terom
parents:
diff changeset
    84
 */
d193dd1d8a7e new NetworkReactor, fix everything to actually work
terom
parents:
diff changeset
    85
class NetworkReactorError : public NetworkErrno {
d193dd1d8a7e new NetworkReactor, fix everything to actually work
terom
parents:
diff changeset
    86
    public:
d193dd1d8a7e new NetworkReactor, fix everything to actually work
terom
parents:
diff changeset
    87
        NetworkReactorError (std::string op) : NetworkErrno(op) { }
d193dd1d8a7e new NetworkReactor, fix everything to actually work
terom
parents:
diff changeset
    88
};
d193dd1d8a7e new NetworkReactor, fix everything to actually work
terom
parents:
diff changeset
    89
d193dd1d8a7e new NetworkReactor, fix everything to actually work
terom
parents:
diff changeset
    90
#endif