src/Network/Socket.hh
author terom
Mon, 15 Dec 2008 23:56:42 +0000
changeset 378 5589abf5e61b
parent 284 27ce69fd1e06
child 380 d193dd1d8a7e
permissions -rw-r--r--
break the network code. Too late to set up a branch for this now
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
186
0738f2949a2b move src/Network% to src/Network/%
terom
parents: 185
diff changeset
     4
#include "../Error.hh"
378
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
     5
#include "Address.hh"
185
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
     6
378
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
     7
/*
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
     8
 * Platform-specific includes
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
     9
 */
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
    10
#ifndef WIN32
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
    11
    // linux
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
    12
    #include <sys/types.h>
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
    13
    #include <sys/socket.h>
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
    14
    #include <unistd.h>
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
    15
    #include <fcntl.h>
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
    16
    
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
    17
    #define closesocket close
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
    18
#else
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
    19
    #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
    20
#endif
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
    21
185
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    22
#include <cerrno>
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    23
#include <cstring>
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    24
284
27ce69fd1e06 work on Network doxygen docs
terom
parents: 186
diff changeset
    25
/**
378
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
    26
 * We use ClanLib's Socket API, but with our own extensions...
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
class NetworkSocket {
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
    29
    private:
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
    30
        /** The file descriptor */
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
    31
        int fd;
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
    32
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
    33
        /** Socket domain */
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
    34
        int family;
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
    35
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
    36
        /** Socket type */
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
    37
        int socktype;
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
    38
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
    39
        /** Socket protocol */
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
    40
        int protocol;
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
    41
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
    42
        /** 
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
    43
         * 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
    44
         */
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
    45
        bool bound;
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
    46
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
    47
        /**
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
    48
         * Read/write signals
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
    49
         */
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
    50
        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
    51
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
    52
    public:
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
    53
        /**
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
    54
         * Construct a socket of the specific type. Family and protocol can be left as NULL, but type should usually
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
    55
         * be specified.
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
        NetworkSocket (int family, int socktype, int protocol = 0);
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
        /**
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
    60
         * 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
    61
         */
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
    62
        NetworkSocket (int fd);
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
    63
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
    64
        /**
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
    65
         * 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
    66
         */
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
    67
        ~NetworkSocket (void);
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
    68
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
    69
    private:
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
    70
        // XXX: nocopy
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
    71
        
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
    72
        /**
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
    73
         * 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
    74
         */
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
    75
        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
    76
        
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
    77
        /**
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
    78
         * Close, ignoring errors
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
    79
         */
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
    80
        void force_close (void);
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
    public:
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
         * 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
    85
         */
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
    86
        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
    87
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
         * 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
    90
         */ 
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
    91
        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
    92
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
         * 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
    95
         */
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
    96
        void listen (int backlog);
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
    97
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
         * Get local address
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
        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
   102
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
         * Get remote address
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
   105
         */
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
   106
        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
   107
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
   108
        /**
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
   109
         * 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
   110
         */
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
   111
        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
   112
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
   113
        /**
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
   114
         * 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
   115
         */
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
   116
        NetworkSocket* accept (NetworkAddress *src);
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
        /**
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
   119
         * Establish a new connection
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 connect (const NetworkAddress &addr);
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
        /**
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
   124
         * 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
   125
         *
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
   126
         * @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
   127
         * @throw NetworkSocketError on error
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
        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
   130
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
   131
        /**
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
   132
         * 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
   133
         *
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
   134
         * @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
   135
         * @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
   136
         * @throw NetworkSocketError on error
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
        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
   139
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
         * Close the socket
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
        void close (void);
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
        /**
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
   146
         * 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
   147
         */
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
   148
        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
   149
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
         * 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
   152
         */
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
   153
        CL_Signal_v0& sig_write (void) { return _sig_write; }
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
   154
};
185
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   155
284
27ce69fd1e06 work on Network doxygen docs
terom
parents: 186
diff changeset
   156
/**
27ce69fd1e06 work on Network doxygen docs
terom
parents: 186
diff changeset
   157
 * Base class for expcetions thrown by socket methods
27ce69fd1e06 work on Network doxygen docs
terom
parents: 186
diff changeset
   158
 */
185
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   159
class NetworkSocketError : public Error {
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   160
    protected:
378
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
   161
        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
   162
    
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
   163
    public:
185
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   164
        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
   165
};
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   166
284
27ce69fd1e06 work on Network doxygen docs
terom
parents: 186
diff changeset
   167
/**
27ce69fd1e06 work on Network doxygen docs
terom
parents: 186
diff changeset
   168
 * Errno-enabled exception, most common type of NetworkSocketError
27ce69fd1e06 work on Network doxygen docs
terom
parents: 186
diff changeset
   169
 */
185
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   170
class NetworkSocketOSError : public NetworkSocketError {
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   171
    public:
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   172
        NetworkSocketOSError (const NetworkSocket &socket, const char *op) :
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   173
            NetworkSocketError(socket, op, strerror(errno)) { }
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   174
};
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   175
284
27ce69fd1e06 work on Network doxygen docs
terom
parents: 186
diff changeset
   176
/**
27ce69fd1e06 work on Network doxygen docs
terom
parents: 186
diff changeset
   177
 * Recv returned EOF
27ce69fd1e06 work on Network doxygen docs
terom
parents: 186
diff changeset
   178
 */
185
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   179
class NetworkSocketEOFError : public NetworkSocketError {
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   180
    public:
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   181
        NetworkSocketEOFError (const NetworkSocket &socket, const char *op) :
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   182
            NetworkSocketError(socket, op, "EOF") { }
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   183
};
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   184
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   185
#endif /* NETWORK_SOCKET_HH */