src/Network/Socket.hh
author terom
Tue, 16 Dec 2008 23:21:26 +0000
changeset 380 d193dd1d8a7e
parent 378 5589abf5e61b
child 381 9b35bc329d23
permissions -rw-r--r--
new NetworkReactor, fix everything to actually work
185
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
     1
#ifndef NETWORK_SOCKET_HH
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
     2
#define NETWORK_SOCKET_HH
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
     3
380
d193dd1d8a7e new NetworkReactor, fix everything to actually work
terom
parents: 378
diff changeset
     4
// forward-declare
d193dd1d8a7e new NetworkReactor, fix everything to actually work
terom
parents: 378
diff changeset
     5
class NetworkSocket;
d193dd1d8a7e new NetworkReactor, fix everything to actually work
terom
parents: 378
diff changeset
     6
186
0738f2949a2b move src/Network% to src/Network/%
terom
parents: 185
diff changeset
     7
#include "../Error.hh"
378
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
     8
#include "Address.hh"
380
d193dd1d8a7e new NetworkReactor, fix everything to actually work
terom
parents: 378
diff changeset
     9
#include "Reactor.hh"
185
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    10
378
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
    11
/*
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
    12
 * Platform-specific includes
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
    13
 */
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
    14
#ifndef WIN32
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
    15
    // linux
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
    16
    #include <sys/types.h>
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
    17
    #include <sys/socket.h>
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
    18
    #include <unistd.h>
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
    19
    #include <fcntl.h>
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
    20
    
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
    21
    #define closesocket close
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
    22
#else
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
    23
    #error "This network code won't compile on win32 :)"
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
    24
#endif
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
    25
284
27ce69fd1e06 work on Network doxygen docs
terom
parents: 186
diff changeset
    26
/**
378
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
    27
 * We use ClanLib's Socket API, but with our own extensions...
284
27ce69fd1e06 work on Network doxygen docs
terom
parents: 186
diff changeset
    28
 */
378
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
    29
class NetworkSocket {
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
    30
    private:
380
d193dd1d8a7e new NetworkReactor, fix everything to actually work
terom
parents: 378
diff changeset
    31
        /**
d193dd1d8a7e new NetworkReactor, fix everything to actually work
terom
parents: 378
diff changeset
    32
         * Socket family/type/protocol
d193dd1d8a7e new NetworkReactor, fix everything to actually work
terom
parents: 378
diff changeset
    33
         */
d193dd1d8a7e new NetworkReactor, fix everything to actually work
terom
parents: 378
diff changeset
    34
        struct socket_type {
d193dd1d8a7e new NetworkReactor, fix everything to actually work
terom
parents: 378
diff changeset
    35
            /** Socket domain */
d193dd1d8a7e new NetworkReactor, fix everything to actually work
terom
parents: 378
diff changeset
    36
            int family;
d193dd1d8a7e new NetworkReactor, fix everything to actually work
terom
parents: 378
diff changeset
    37
d193dd1d8a7e new NetworkReactor, fix everything to actually work
terom
parents: 378
diff changeset
    38
            /** Socket type */
d193dd1d8a7e new NetworkReactor, fix everything to actually work
terom
parents: 378
diff changeset
    39
            int socktype;
d193dd1d8a7e new NetworkReactor, fix everything to actually work
terom
parents: 378
diff changeset
    40
d193dd1d8a7e new NetworkReactor, fix everything to actually work
terom
parents: 378
diff changeset
    41
            /** Socket protocol */
d193dd1d8a7e new NetworkReactor, fix everything to actually work
terom
parents: 378
diff changeset
    42
            int protocol;
d193dd1d8a7e new NetworkReactor, fix everything to actually work
terom
parents: 378
diff changeset
    43
            
d193dd1d8a7e new NetworkReactor, fix everything to actually work
terom
parents: 378
diff changeset
    44
            /** Simple constructor */
d193dd1d8a7e new NetworkReactor, fix everything to actually work
terom
parents: 378
diff changeset
    45
            socket_type (int family = 0, int socktype = 0, int protocol = 0) : family(family), socktype(socktype), protocol(protocol) { }
d193dd1d8a7e new NetworkReactor, fix everything to actually work
terom
parents: 378
diff changeset
    46
        };
d193dd1d8a7e new NetworkReactor, fix everything to actually work
terom
parents: 378
diff changeset
    47
d193dd1d8a7e new NetworkReactor, fix everything to actually work
terom
parents: 378
diff changeset
    48
        /** These are nonzero if given via the constructor, used to filter out unwanted addrinfos */
d193dd1d8a7e new NetworkReactor, fix everything to actually work
terom
parents: 378
diff changeset
    49
        socket_type sock_type;
d193dd1d8a7e new NetworkReactor, fix everything to actually work
terom
parents: 378
diff changeset
    50
378
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
    51
        /** The file descriptor */
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
    52
        int fd;
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
    53
380
d193dd1d8a7e new NetworkReactor, fix everything to actually work
terom
parents: 378
diff changeset
    54
        /** Our current type, intialized via constructor, but updated by lazy_socket */
d193dd1d8a7e new NetworkReactor, fix everything to actually work
terom
parents: 378
diff changeset
    55
        socket_type type;
378
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
    56
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
    57
        /** 
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
    58
         * Has the socket been explicitly bind()'d? If so, force ourselves to use this socket in connect().
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
    59
         */
380
d193dd1d8a7e new NetworkReactor, fix everything to actually work
terom
parents: 378
diff changeset
    60
        bool bound : 1;
d193dd1d8a7e new NetworkReactor, fix everything to actually work
terom
parents: 378
diff changeset
    61
d193dd1d8a7e new NetworkReactor, fix everything to actually work
terom
parents: 378
diff changeset
    62
        /**
d193dd1d8a7e new NetworkReactor, fix everything to actually work
terom
parents: 378
diff changeset
    63
         * Registered to reactor?
d193dd1d8a7e new NetworkReactor, fix everything to actually work
terom
parents: 378
diff changeset
    64
         */
d193dd1d8a7e new NetworkReactor, fix everything to actually work
terom
parents: 378
diff changeset
    65
        bool registered : 1;
d193dd1d8a7e new NetworkReactor, fix everything to actually work
terom
parents: 378
diff changeset
    66
d193dd1d8a7e new NetworkReactor, fix everything to actually work
terom
parents: 378
diff changeset
    67
        /**
d193dd1d8a7e new NetworkReactor, fix everything to actually work
terom
parents: 378
diff changeset
    68
         * Do we want to know about recv()s?
d193dd1d8a7e new NetworkReactor, fix everything to actually work
terom
parents: 378
diff changeset
    69
         */
d193dd1d8a7e new NetworkReactor, fix everything to actually work
terom
parents: 378
diff changeset
    70
        bool want_read : 1;
d193dd1d8a7e new NetworkReactor, fix everything to actually work
terom
parents: 378
diff changeset
    71
d193dd1d8a7e new NetworkReactor, fix everything to actually work
terom
parents: 378
diff changeset
    72
        /**
d193dd1d8a7e new NetworkReactor, fix everything to actually work
terom
parents: 378
diff changeset
    73
         * Is the write buffer full?
d193dd1d8a7e new NetworkReactor, fix everything to actually work
terom
parents: 378
diff changeset
    74
         */
d193dd1d8a7e new NetworkReactor, fix everything to actually work
terom
parents: 378
diff changeset
    75
        bool want_write : 1;
d193dd1d8a7e new NetworkReactor, fix everything to actually work
terom
parents: 378
diff changeset
    76
d193dd1d8a7e new NetworkReactor, fix everything to actually work
terom
parents: 378
diff changeset
    77
        /**
d193dd1d8a7e new NetworkReactor, fix everything to actually work
terom
parents: 378
diff changeset
    78
         * The reactor that we use, defaults to NetworkReactor::current
d193dd1d8a7e new NetworkReactor, fix everything to actually work
terom
parents: 378
diff changeset
    79
         */
d193dd1d8a7e new NetworkReactor, fix everything to actually work
terom
parents: 378
diff changeset
    80
        NetworkReactor *reactor;
378
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
    81
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
    82
        /**
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
    83
         * Read/write signals
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
    84
         */
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
    85
        CL_Signal_v0 _sig_read, _sig_write;
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
    86
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
    87
    public:
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
    88
        /**
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
    89
         * Construct a socket of the specific type. Family and protocol can be left as NULL, but type should usually
380
d193dd1d8a7e new NetworkReactor, fix everything to actually work
terom
parents: 378
diff changeset
    90
         * be specified. The given reactor is used for polling, defaults to NetworkReactor::current
378
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
    91
         */
380
d193dd1d8a7e new NetworkReactor, fix everything to actually work
terom
parents: 378
diff changeset
    92
        NetworkSocket (int family, int socktype, int protocol = 0, NetworkReactor *reactor = NULL);
378
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
    93
        
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
    94
        /**
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
    95
         * Create a socket from the given pre-existing fd
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
    96
         */
380
d193dd1d8a7e new NetworkReactor, fix everything to actually work
terom
parents: 378
diff changeset
    97
        NetworkSocket (int fd, socket_type type, NetworkReactor *reactor = NULL);
378
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
    98
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
    99
        /**
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
   100
         * Force-close the socket if it's still open
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
   101
         */
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
   102
        ~NetworkSocket (void);
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
   103
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
   104
    private:
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
   105
        // XXX: nocopy
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
   106
        
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
   107
        /**
380
d193dd1d8a7e new NetworkReactor, fix everything to actually work
terom
parents: 378
diff changeset
   108
         * Reset bound+poll
d193dd1d8a7e new NetworkReactor, fix everything to actually work
terom
parents: 378
diff changeset
   109
         */ 
d193dd1d8a7e new NetworkReactor, fix everything to actually work
terom
parents: 378
diff changeset
   110
        void reset (void);
d193dd1d8a7e new NetworkReactor, fix everything to actually work
terom
parents: 378
diff changeset
   111
d193dd1d8a7e new NetworkReactor, fix everything to actually work
terom
parents: 378
diff changeset
   112
        /**
378
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
   113
         * Create a new socket of the given type, unless we already have one
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
   114
         */
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
   115
        void lazy_socket (int family, int type, int protocol);
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
   116
        
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
   117
        /**
380
d193dd1d8a7e new NetworkReactor, fix everything to actually work
terom
parents: 378
diff changeset
   118
         * Close and reset, ignoring errors
378
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
   119
         */
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
   120
        void force_close (void);
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
   121
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
   122
    public:
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
   123
        /**
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
   124
         * Get the socket fd... promise not to break it
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
   125
         */
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
   126
        int get_socket (void) const { return fd; }
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
   127
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
   128
        /**
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
   129
         * Bind to a specific local address
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
   130
         */ 
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
   131
        void bind (const NetworkAddress &addr);
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
   132
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
   133
        /**
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
   134
         * Put socket into listen mode
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
   135
         */
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
   136
        void listen (int backlog);
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
   137
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
   138
        /**
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
   139
         * Get local address
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
   140
         */
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
   141
        NetworkAddress get_local_address (void); 
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
   142
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
   143
        /**
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
   144
         * Get remote address
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
   145
         */
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
   146
        NetworkAddress get_remote_address (void); 
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
   147
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
   148
        /**
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
   149
         * Make send/recv/connect non-blocking
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
   150
         */
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
   151
        void set_nonblocking (bool nonblocking);
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
   152
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
   153
        /**
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
   154
         * Accept a new connection, optionally giving the connection's source address
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
   155
         */
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
   156
        NetworkSocket* accept (NetworkAddress *src);
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
   157
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
   158
        /**
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
   159
         * Establish a new connection
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
   160
         */
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
   161
        void connect (const NetworkAddress &addr);
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
   162
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
   163
        /**
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
   164
         * Send, optionally using the specific destination
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
   165
         *
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
   166
         * @return number of bytes sent, zero if busy
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
   167
         * @throw NetworkSocketError on error
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
   168
         */
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
   169
        size_t send (const char *buf, size_t size, const NetworkAddress *dest = NULL);
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
   170
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
   171
        /**
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
   172
         * Recv, optionally storing the source in src
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
   173
         *
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
   174
         * @return number of bytes received, zero if none available
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
   175
         * @throw NetworkSocketEOFError if the connection was closed
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
   176
         * @throw NetworkSocketError on error
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
   177
         */
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
   178
        size_t recv (char *buf, size_t size, NetworkAddress *src = NULL);
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
   179
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
   180
        /**
380
d193dd1d8a7e new NetworkReactor, fix everything to actually work
terom
parents: 378
diff changeset
   181
         * Close and reset the socket
378
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
   182
         */
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
   183
        void close (void);
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
   184
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
   185
        /**
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
   186
         * Triggered when socket becomes readable
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
   187
         */
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
   188
        CL_Signal_v0& sig_read (void) { return _sig_read; }
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
   189
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
   190
        /**
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
   191
         * Triggered when socket becomes writeable after a send that returned zero
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
   192
         */
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
   193
        CL_Signal_v0& sig_write (void) { return _sig_write; }
380
d193dd1d8a7e new NetworkReactor, fix everything to actually work
terom
parents: 378
diff changeset
   194
d193dd1d8a7e new NetworkReactor, fix everything to actually work
terom
parents: 378
diff changeset
   195
        /**
d193dd1d8a7e new NetworkReactor, fix everything to actually work
terom
parents: 378
diff changeset
   196
         * Register to NetworkReactor unless already registered
d193dd1d8a7e new NetworkReactor, fix everything to actually work
terom
parents: 378
diff changeset
   197
         */
d193dd1d8a7e new NetworkReactor, fix everything to actually work
terom
parents: 378
diff changeset
   198
        void register_poll (void);
d193dd1d8a7e new NetworkReactor, fix everything to actually work
terom
parents: 378
diff changeset
   199
d193dd1d8a7e new NetworkReactor, fix everything to actually work
terom
parents: 378
diff changeset
   200
        /**
d193dd1d8a7e new NetworkReactor, fix everything to actually work
terom
parents: 378
diff changeset
   201
         * Trigger sig_read() once socket is ready for recv?
d193dd1d8a7e new NetworkReactor, fix everything to actually work
terom
parents: 378
diff changeset
   202
         */
d193dd1d8a7e new NetworkReactor, fix everything to actually work
terom
parents: 378
diff changeset
   203
        void set_poll_read (bool want_read) { this->want_read = want_read; if (!registered) register_poll(); }
d193dd1d8a7e new NetworkReactor, fix everything to actually work
terom
parents: 378
diff changeset
   204
d193dd1d8a7e new NetworkReactor, fix everything to actually work
terom
parents: 378
diff changeset
   205
        /**
d193dd1d8a7e new NetworkReactor, fix everything to actually work
terom
parents: 378
diff changeset
   206
         * Trigger sig_write() once socket is ready for send?
d193dd1d8a7e new NetworkReactor, fix everything to actually work
terom
parents: 378
diff changeset
   207
         */
d193dd1d8a7e new NetworkReactor, fix everything to actually work
terom
parents: 378
diff changeset
   208
        void set_poll_write (bool want_write) { this->want_write = want_write; if (!registered) register_poll(); }
d193dd1d8a7e new NetworkReactor, fix everything to actually work
terom
parents: 378
diff changeset
   209
d193dd1d8a7e new NetworkReactor, fix everything to actually work
terom
parents: 378
diff changeset
   210
        /**
d193dd1d8a7e new NetworkReactor, fix everything to actually work
terom
parents: 378
diff changeset
   211
         * What events this socket is interested in.
d193dd1d8a7e new NetworkReactor, fix everything to actually work
terom
parents: 378
diff changeset
   212
         */
d193dd1d8a7e new NetworkReactor, fix everything to actually work
terom
parents: 378
diff changeset
   213
        NetworkPollMask get_poll (void) { 
d193dd1d8a7e new NetworkReactor, fix everything to actually work
terom
parents: 378
diff changeset
   214
            return (want_read ? POLL_READ : 0) | (want_write ? POLL_WRITE : 0); 
d193dd1d8a7e new NetworkReactor, fix everything to actually work
terom
parents: 378
diff changeset
   215
        }
d193dd1d8a7e new NetworkReactor, fix everything to actually work
terom
parents: 378
diff changeset
   216
d193dd1d8a7e new NetworkReactor, fix everything to actually work
terom
parents: 378
diff changeset
   217
        /**
d193dd1d8a7e new NetworkReactor, fix everything to actually work
terom
parents: 378
diff changeset
   218
         * Notify of events
d193dd1d8a7e new NetworkReactor, fix everything to actually work
terom
parents: 378
diff changeset
   219
         */
d193dd1d8a7e new NetworkReactor, fix everything to actually work
terom
parents: 378
diff changeset
   220
        void notify (NetworkPollMask mask) { 
d193dd1d8a7e new NetworkReactor, fix everything to actually work
terom
parents: 378
diff changeset
   221
            if (mask & POLL_READ) _sig_read();
d193dd1d8a7e new NetworkReactor, fix everything to actually work
terom
parents: 378
diff changeset
   222
            if (mask & POLL_WRITE) _sig_write();
d193dd1d8a7e new NetworkReactor, fix everything to actually work
terom
parents: 378
diff changeset
   223
        }
378
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
   224
};
185
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   225
284
27ce69fd1e06 work on Network doxygen docs
terom
parents: 186
diff changeset
   226
/**
27ce69fd1e06 work on Network doxygen docs
terom
parents: 186
diff changeset
   227
 * Base class for expcetions thrown by socket methods
27ce69fd1e06 work on Network doxygen docs
terom
parents: 186
diff changeset
   228
 */
185
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   229
class NetworkSocketError : public Error {
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   230
    protected:
378
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
   231
        static std::string build_str (const NetworkSocket &socket, const char *op, const char *err);
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
   232
    
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
   233
    public:
185
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   234
        NetworkSocketError (const NetworkSocket &socket, const char *op, const char *err);
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   235
};
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   236
284
27ce69fd1e06 work on Network doxygen docs
terom
parents: 186
diff changeset
   237
/**
27ce69fd1e06 work on Network doxygen docs
terom
parents: 186
diff changeset
   238
 * Errno-enabled exception, most common type of NetworkSocketError
27ce69fd1e06 work on Network doxygen docs
terom
parents: 186
diff changeset
   239
 */
380
d193dd1d8a7e new NetworkReactor, fix everything to actually work
terom
parents: 378
diff changeset
   240
class NetworkSocketErrno : public NetworkSocketError {
185
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   241
    public:
380
d193dd1d8a7e new NetworkReactor, fix everything to actually work
terom
parents: 378
diff changeset
   242
        NetworkSocketErrno (const NetworkSocket &socket, const char *op);
185
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   243
};
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   244
284
27ce69fd1e06 work on Network doxygen docs
terom
parents: 186
diff changeset
   245
/**
27ce69fd1e06 work on Network doxygen docs
terom
parents: 186
diff changeset
   246
 * Recv returned EOF
27ce69fd1e06 work on Network doxygen docs
terom
parents: 186
diff changeset
   247
 */
185
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   248
class NetworkSocketEOFError : public NetworkSocketError {
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   249
    public:
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   250
        NetworkSocketEOFError (const NetworkSocket &socket, const char *op) :
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   251
            NetworkSocketError(socket, op, "EOF") { }
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   252
};
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   253
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   254
#endif /* NETWORK_SOCKET_HH */