src/Network/Socket.hh
author terom
Wed, 17 Dec 2008 00:40:22 +0000
changeset 381 9b35bc329d23
parent 380 d193dd1d8a7e
child 382 190f81d30624
permissions -rw-r--r--
separate sockaddr stuff out of NetworkAddress... now called NetworkEndpoint
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"
381
9b35bc329d23 separate sockaddr stuff out of NetworkAddress... now called NetworkEndpoint
terom
parents: 380
diff changeset
     9
#include "SockAddr.hh"
380
d193dd1d8a7e new NetworkReactor, fix everything to actually work
terom
parents: 378
diff changeset
    10
#include "Reactor.hh"
185
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    11
378
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
    12
/*
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
    13
 * Platform-specific includes
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
    14
 */
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
    15
#ifndef WIN32
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
    16
    // linux
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
    17
    #include <sys/types.h>
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
    18
    #include <sys/socket.h>
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
    19
    #include <unistd.h>
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
    20
    #include <fcntl.h>
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
    21
    
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
    22
    #define closesocket close
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
    23
#else
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
    24
    #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
    25
#endif
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
    26
284
27ce69fd1e06 work on Network doxygen docs
terom
parents: 186
diff changeset
    27
/**
378
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
    28
 * We use ClanLib's Socket API, but with our own extensions...
284
27ce69fd1e06 work on Network doxygen docs
terom
parents: 186
diff changeset
    29
 */
378
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
    30
class NetworkSocket {
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
    31
    private:
380
d193dd1d8a7e new NetworkReactor, fix everything to actually work
terom
parents: 378
diff changeset
    32
        /**
d193dd1d8a7e new NetworkReactor, fix everything to actually work
terom
parents: 378
diff changeset
    33
         * Socket family/type/protocol
d193dd1d8a7e new NetworkReactor, fix everything to actually work
terom
parents: 378
diff changeset
    34
         */
d193dd1d8a7e new NetworkReactor, fix everything to actually work
terom
parents: 378
diff changeset
    35
        struct socket_type {
d193dd1d8a7e new NetworkReactor, fix everything to actually work
terom
parents: 378
diff changeset
    36
            /** Socket domain */
d193dd1d8a7e new NetworkReactor, fix everything to actually work
terom
parents: 378
diff changeset
    37
            int family;
d193dd1d8a7e new NetworkReactor, fix everything to actually work
terom
parents: 378
diff changeset
    38
d193dd1d8a7e new NetworkReactor, fix everything to actually work
terom
parents: 378
diff changeset
    39
            /** Socket type */
d193dd1d8a7e new NetworkReactor, fix everything to actually work
terom
parents: 378
diff changeset
    40
            int socktype;
d193dd1d8a7e new NetworkReactor, fix everything to actually work
terom
parents: 378
diff changeset
    41
d193dd1d8a7e new NetworkReactor, fix everything to actually work
terom
parents: 378
diff changeset
    42
            /** Socket protocol */
d193dd1d8a7e new NetworkReactor, fix everything to actually work
terom
parents: 378
diff changeset
    43
            int protocol;
d193dd1d8a7e new NetworkReactor, fix everything to actually work
terom
parents: 378
diff changeset
    44
            
d193dd1d8a7e new NetworkReactor, fix everything to actually work
terom
parents: 378
diff changeset
    45
            /** Simple constructor */
d193dd1d8a7e new NetworkReactor, fix everything to actually work
terom
parents: 378
diff changeset
    46
            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
    47
        };
d193dd1d8a7e new NetworkReactor, fix everything to actually work
terom
parents: 378
diff changeset
    48
d193dd1d8a7e new NetworkReactor, fix everything to actually work
terom
parents: 378
diff changeset
    49
        /** 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
    50
        socket_type sock_type;
d193dd1d8a7e new NetworkReactor, fix everything to actually work
terom
parents: 378
diff changeset
    51
378
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
    52
        /** The file descriptor */
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
    53
        int fd;
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
    54
380
d193dd1d8a7e new NetworkReactor, fix everything to actually work
terom
parents: 378
diff changeset
    55
        /** Our current type, intialized via constructor, but updated by lazy_socket */
d193dd1d8a7e new NetworkReactor, fix everything to actually work
terom
parents: 378
diff changeset
    56
        socket_type type;
378
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
        /** 
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
    59
         * 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
    60
         */
380
d193dd1d8a7e new NetworkReactor, fix everything to actually work
terom
parents: 378
diff changeset
    61
        bool bound : 1;
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
        /**
d193dd1d8a7e new NetworkReactor, fix everything to actually work
terom
parents: 378
diff changeset
    64
         * Registered to reactor?
d193dd1d8a7e new NetworkReactor, fix everything to actually work
terom
parents: 378
diff changeset
    65
         */
d193dd1d8a7e new NetworkReactor, fix everything to actually work
terom
parents: 378
diff changeset
    66
        bool registered : 1;
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
        /**
d193dd1d8a7e new NetworkReactor, fix everything to actually work
terom
parents: 378
diff changeset
    69
         * Do we want to know about recv()s?
d193dd1d8a7e new NetworkReactor, fix everything to actually work
terom
parents: 378
diff changeset
    70
         */
d193dd1d8a7e new NetworkReactor, fix everything to actually work
terom
parents: 378
diff changeset
    71
        bool want_read : 1;
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
        /**
d193dd1d8a7e new NetworkReactor, fix everything to actually work
terom
parents: 378
diff changeset
    74
         * Is the write buffer full?
d193dd1d8a7e new NetworkReactor, fix everything to actually work
terom
parents: 378
diff changeset
    75
         */
d193dd1d8a7e new NetworkReactor, fix everything to actually work
terom
parents: 378
diff changeset
    76
        bool want_write : 1;
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
        /**
d193dd1d8a7e new NetworkReactor, fix everything to actually work
terom
parents: 378
diff changeset
    79
         * The reactor that we use, defaults to NetworkReactor::current
d193dd1d8a7e new NetworkReactor, fix everything to actually work
terom
parents: 378
diff changeset
    80
         */
d193dd1d8a7e new NetworkReactor, fix everything to actually work
terom
parents: 378
diff changeset
    81
        NetworkReactor *reactor;
378
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
        /**
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
    84
         * Read/write signals
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
    85
         */
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
    86
        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
    87
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
    88
    public:
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
    89
        /**
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
    90
         * 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
    91
         * 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
    92
         */
380
d193dd1d8a7e new NetworkReactor, fix everything to actually work
terom
parents: 378
diff changeset
    93
        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
    94
        
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
    95
        /**
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
    96
         * 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
    97
         */
380
d193dd1d8a7e new NetworkReactor, fix everything to actually work
terom
parents: 378
diff changeset
    98
        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
    99
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
   100
        /**
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
   101
         * 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
   102
         */
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
   103
        ~NetworkSocket (void);
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
   104
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
   105
    private:
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
   106
        // XXX: nocopy
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
   107
        
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
   108
        /**
380
d193dd1d8a7e new NetworkReactor, fix everything to actually work
terom
parents: 378
diff changeset
   109
         * Reset bound+poll
d193dd1d8a7e new NetworkReactor, fix everything to actually work
terom
parents: 378
diff changeset
   110
         */ 
d193dd1d8a7e new NetworkReactor, fix everything to actually work
terom
parents: 378
diff changeset
   111
        void reset (void);
d193dd1d8a7e new NetworkReactor, fix everything to actually work
terom
parents: 378
diff changeset
   112
d193dd1d8a7e new NetworkReactor, fix everything to actually work
terom
parents: 378
diff changeset
   113
        /**
378
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
   114
         * 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
   115
         */
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
   116
        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
   117
        
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
   118
        /**
380
d193dd1d8a7e new NetworkReactor, fix everything to actually work
terom
parents: 378
diff changeset
   119
         * 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
   120
         */
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
   121
        void force_close (void);
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
   122
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
   123
    public:
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
   124
        /**
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
   125
         * 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
   126
         */
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
   127
        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
   128
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
   129
        /**
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
   130
         * 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
   131
         */ 
381
9b35bc329d23 separate sockaddr stuff out of NetworkAddress... now called NetworkEndpoint
terom
parents: 380
diff changeset
   132
        void bind (const NetworkEndpoint &addr);
378
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
        /**
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
   135
         * 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
   136
         */
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
   137
        void listen (int backlog);
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
        /**
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
   140
         * Get local address
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
   141
         */
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
   142
        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
   143
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
   144
        /**
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
   145
         * Get remote address
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
   146
         */
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
   147
        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
   148
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
   149
        /**
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
   150
         * 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
   151
         */
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
   152
        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
   153
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
   154
        /**
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
   155
         * 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
   156
         */
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
   157
        NetworkSocket* accept (NetworkAddress *src);
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
        /**
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
   160
         * Establish a new connection
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
   161
         */
381
9b35bc329d23 separate sockaddr stuff out of NetworkAddress... now called NetworkEndpoint
terom
parents: 380
diff changeset
   162
        void connect (const NetworkEndpoint &addr);
378
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
        /**
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
   165
         * 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
   166
         *
381
9b35bc329d23 separate sockaddr stuff out of NetworkAddress... now called NetworkEndpoint
terom
parents: 380
diff changeset
   167
         * @param buf bytes to send
9b35bc329d23 separate sockaddr stuff out of NetworkAddress... now called NetworkEndpoint
terom
parents: 380
diff changeset
   168
         * @param size how many bytes to try and send
9b35bc329d23 separate sockaddr stuff out of NetworkAddress... now called NetworkEndpoint
terom
parents: 380
diff changeset
   169
         * @param dest optional specific destination address
378
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
   170
         * @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
   171
         * @throw NetworkSocketError on error
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
   172
         */
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
   173
        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
   174
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
   175
        /**
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
   176
         * 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
   177
         *
381
9b35bc329d23 separate sockaddr stuff out of NetworkAddress... now called NetworkEndpoint
terom
parents: 380
diff changeset
   178
         * @param buf where to recv into
9b35bc329d23 separate sockaddr stuff out of NetworkAddress... now called NetworkEndpoint
terom
parents: 380
diff changeset
   179
         * @param size how many bytes to try and receive
9b35bc329d23 separate sockaddr stuff out of NetworkAddress... now called NetworkEndpoint
terom
parents: 380
diff changeset
   180
         * @param src optionally store source address
378
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
   181
         * @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
   182
         * @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
   183
         * @throw NetworkSocketError on error
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
        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
   186
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
   187
        /**
380
d193dd1d8a7e new NetworkReactor, fix everything to actually work
terom
parents: 378
diff changeset
   188
         * 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
   189
         */
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
   190
        void close (void);
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
   191
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
         * 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
   194
         */
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
   195
        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
   196
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
   197
        /**
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
   198
         * 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
   199
         */
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
   200
        CL_Signal_v0& sig_write (void) { return _sig_write; }
380
d193dd1d8a7e new NetworkReactor, fix everything to actually work
terom
parents: 378
diff changeset
   201
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
         * Register to NetworkReactor unless already registered
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
        void register_poll (void);
d193dd1d8a7e new NetworkReactor, fix everything to actually work
terom
parents: 378
diff changeset
   206
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
         * Trigger sig_read() once socket is ready for recv?
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
        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
   211
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
         * Trigger sig_write() once socket is ready for send?
d193dd1d8a7e new NetworkReactor, fix everything to actually work
terom
parents: 378
diff changeset
   214
         */
d193dd1d8a7e new NetworkReactor, fix everything to actually work
terom
parents: 378
diff changeset
   215
        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
   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
         * What events this socket is interested in.
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
        NetworkPollMask get_poll (void) { 
d193dd1d8a7e new NetworkReactor, fix everything to actually work
terom
parents: 378
diff changeset
   221
            return (want_read ? POLL_READ : 0) | (want_write ? POLL_WRITE : 0); 
d193dd1d8a7e new NetworkReactor, fix everything to actually work
terom
parents: 378
diff changeset
   222
        }
d193dd1d8a7e new NetworkReactor, fix everything to actually work
terom
parents: 378
diff changeset
   223
d193dd1d8a7e new NetworkReactor, fix everything to actually work
terom
parents: 378
diff changeset
   224
        /**
d193dd1d8a7e new NetworkReactor, fix everything to actually work
terom
parents: 378
diff changeset
   225
         * Notify of events
d193dd1d8a7e new NetworkReactor, fix everything to actually work
terom
parents: 378
diff changeset
   226
         */
d193dd1d8a7e new NetworkReactor, fix everything to actually work
terom
parents: 378
diff changeset
   227
        void notify (NetworkPollMask mask) { 
d193dd1d8a7e new NetworkReactor, fix everything to actually work
terom
parents: 378
diff changeset
   228
            if (mask & POLL_READ) _sig_read();
d193dd1d8a7e new NetworkReactor, fix everything to actually work
terom
parents: 378
diff changeset
   229
            if (mask & POLL_WRITE) _sig_write();
d193dd1d8a7e new NetworkReactor, fix everything to actually work
terom
parents: 378
diff changeset
   230
        }
378
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
   231
};
185
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   232
284
27ce69fd1e06 work on Network doxygen docs
terom
parents: 186
diff changeset
   233
/**
27ce69fd1e06 work on Network doxygen docs
terom
parents: 186
diff changeset
   234
 * Base class for expcetions thrown by socket methods
27ce69fd1e06 work on Network doxygen docs
terom
parents: 186
diff changeset
   235
 */
185
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   236
class NetworkSocketError : public Error {
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   237
    protected:
378
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
   238
        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
   239
    
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
   240
    public:
185
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   241
        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
   242
};
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   243
284
27ce69fd1e06 work on Network doxygen docs
terom
parents: 186
diff changeset
   244
/**
27ce69fd1e06 work on Network doxygen docs
terom
parents: 186
diff changeset
   245
 * Errno-enabled exception, most common type of NetworkSocketError
27ce69fd1e06 work on Network doxygen docs
terom
parents: 186
diff changeset
   246
 */
380
d193dd1d8a7e new NetworkReactor, fix everything to actually work
terom
parents: 378
diff changeset
   247
class NetworkSocketErrno : public NetworkSocketError {
185
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   248
    public:
380
d193dd1d8a7e new NetworkReactor, fix everything to actually work
terom
parents: 378
diff changeset
   249
        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
   250
};
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   251
284
27ce69fd1e06 work on Network doxygen docs
terom
parents: 186
diff changeset
   252
/**
27ce69fd1e06 work on Network doxygen docs
terom
parents: 186
diff changeset
   253
 * Recv returned EOF
27ce69fd1e06 work on Network doxygen docs
terom
parents: 186
diff changeset
   254
 */
185
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   255
class NetworkSocketEOFError : public NetworkSocketError {
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   256
    public:
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   257
        NetworkSocketEOFError (const NetworkSocket &socket, const char *op) :
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   258
            NetworkSocketError(socket, op, "EOF") { }
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   259
};
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   260
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   261
#endif /* NETWORK_SOCKET_HH */