src/Network/Socket.hh
author Tero Marttila <terom@fixme.fi>
Fri, 16 Jan 2009 22:03:49 +0200
changeset 400 d64bf28c4340
parent 399 c7295b72731a
child 418 194bc810a570
permissions -rw-r--r--
more documentation tweaking, all Network/ files now have a @file comment. Fix Platform.h -> Platform.hh, and Buffer.hh + Packet.cc
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
399
c7295b72731a documentation work on Network
Tero Marttila <terom@fixme.fi>
parents: 382
diff changeset
     4
/**
c7295b72731a documentation work on Network
Tero Marttila <terom@fixme.fi>
parents: 382
diff changeset
     5
 * @file
c7295b72731a documentation work on Network
Tero Marttila <terom@fixme.fi>
parents: 382
diff changeset
     6
 *
c7295b72731a documentation work on Network
Tero Marttila <terom@fixme.fi>
parents: 382
diff changeset
     7
 * Network sockets
c7295b72731a documentation work on Network
Tero Marttila <terom@fixme.fi>
parents: 382
diff changeset
     8
 */
c7295b72731a documentation work on Network
Tero Marttila <terom@fixme.fi>
parents: 382
diff changeset
     9
380
d193dd1d8a7e new NetworkReactor, fix everything to actually work
terom
parents: 378
diff changeset
    10
// forward-declare
d193dd1d8a7e new NetworkReactor, fix everything to actually work
terom
parents: 378
diff changeset
    11
class NetworkSocket;
d193dd1d8a7e new NetworkReactor, fix everything to actually work
terom
parents: 378
diff changeset
    12
186
0738f2949a2b move src/Network% to src/Network/%
terom
parents: 185
diff changeset
    13
#include "../Error.hh"
399
c7295b72731a documentation work on Network
Tero Marttila <terom@fixme.fi>
parents: 382
diff changeset
    14
#include "Platform.hh"
378
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
    15
#include "Address.hh"
380
d193dd1d8a7e new NetworkReactor, fix everything to actually work
terom
parents: 378
diff changeset
    16
#include "Reactor.hh"
185
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    17
284
27ce69fd1e06 work on Network doxygen docs
terom
parents: 186
diff changeset
    18
/**
399
c7295b72731a documentation work on Network
Tero Marttila <terom@fixme.fi>
parents: 382
diff changeset
    19
 * This is a socket class that wraps an OS socket filedescriptor and provides the more important socket operations
c7295b72731a documentation work on Network
Tero Marttila <terom@fixme.fi>
parents: 382
diff changeset
    20
 * as methods. The implementation aims to be address-family agnostic, and should thence work with both IPv6 and IPv4.
c7295b72731a documentation work on Network
Tero Marttila <terom@fixme.fi>
parents: 382
diff changeset
    21
 *
c7295b72731a documentation work on Network
Tero Marttila <terom@fixme.fi>
parents: 382
diff changeset
    22
 * Network addresses are abstracted into the NetworkEndpoint and derived NetworkAddress classes. The bind() and
c7295b72731a documentation work on Network
Tero Marttila <terom@fixme.fi>
parents: 382
diff changeset
    23
 * connect() methods accept a NetworkEndpoint as an argument, which allows them to handle hosts with multiple
c7295b72731a documentation work on Network
Tero Marttila <terom@fixme.fi>
parents: 382
diff changeset
    24
 * addresses (such as all dual-stack IPv6/IPv4 hosts). Other methods such as accept(), recv() and send() require a
c7295b72731a documentation work on Network
Tero Marttila <terom@fixme.fi>
parents: 382
diff changeset
    25
 * NetworkAddress, which encodes a single specific address. Note how these are also useable as arguments to
c7295b72731a documentation work on Network
Tero Marttila <terom@fixme.fi>
parents: 382
diff changeset
    26
 * connect()/bind().
c7295b72731a documentation work on Network
Tero Marttila <terom@fixme.fi>
parents: 382
diff changeset
    27
 *
c7295b72731a documentation work on Network
Tero Marttila <terom@fixme.fi>
parents: 382
diff changeset
    28
 * The constructor accepts family/socktype/protocol arguments (as passed to the socket() syscall) which can be used to
c7295b72731a documentation work on Network
Tero Marttila <terom@fixme.fi>
parents: 382
diff changeset
    29
 * limit which kinds of addresses/sockets will be used. Usually, family can be specified as AF_UNSPEC and socktype as
c7295b72731a documentation work on Network
Tero Marttila <terom@fixme.fi>
parents: 382
diff changeset
    30
 * either SOCK_STREAM or SOCK_DGRAM - this will let bind()/connect() pick the best IPv6/IPv4 address for use.
c7295b72731a documentation work on Network
Tero Marttila <terom@fixme.fi>
parents: 382
diff changeset
    31
 *
c7295b72731a documentation work on Network
Tero Marttila <terom@fixme.fi>
parents: 382
diff changeset
    32
 * Note however that a call to bind()/connect() can result to multiple calls to the socket() syscall - this interaction
c7295b72731a documentation work on Network
Tero Marttila <terom@fixme.fi>
parents: 382
diff changeset
    33
 * is slightly complicated. On a succesfull bind() operation, the resulting socket will be "locked down", meaning that
c7295b72731a documentation work on Network
Tero Marttila <terom@fixme.fi>
parents: 382
diff changeset
    34
 * a later connect() operation will use the same local socket - this restricts the remote address families that are valid.
c7295b72731a documentation work on Network
Tero Marttila <terom@fixme.fi>
parents: 382
diff changeset
    35
 *
c7295b72731a documentation work on Network
Tero Marttila <terom@fixme.fi>
parents: 382
diff changeset
    36
 * The behaviour of send/recv differs from the behaviour of the similarly named syscalls. On failure,
c7295b72731a documentation work on Network
Tero Marttila <terom@fixme.fi>
parents: 382
diff changeset
    37
 * NetworkSocketErrno is thrown, and on EOF, NetworkSocketEOFError. Zero is returned if the syscall returns EAGAIN - in
c7295b72731a documentation work on Network
Tero Marttila <terom@fixme.fi>
parents: 382
diff changeset
    38
 * other words - if a socket is nonblocking and the operation would block. Otherwise, the return value is the same as
c7295b72731a documentation work on Network
Tero Marttila <terom@fixme.fi>
parents: 382
diff changeset
    39
 * for the syscalls - the number of bytes send/received.
c7295b72731a documentation work on Network
Tero Marttila <terom@fixme.fi>
parents: 382
diff changeset
    40
 *
c7295b72731a documentation work on Network
Tero Marttila <terom@fixme.fi>
parents: 382
diff changeset
    41
 * NetworkSockets also support polling for non-blocking operations using NetworkReactor. A socket is associated with a
c7295b72731a documentation work on Network
Tero Marttila <terom@fixme.fi>
parents: 382
diff changeset
    42
 * specific reactor (passed to the constructor, defaults to NetworkReactor::current). This is inherited by accept()'d
c7295b72731a documentation work on Network
Tero Marttila <terom@fixme.fi>
parents: 382
diff changeset
    43
 * sockets. For read/write, NetworkSocket provides a sig_read()/sig_write() signal which will be fired if a socket is
c7295b72731a documentation work on Network
Tero Marttila <terom@fixme.fi>
parents: 382
diff changeset
    44
 * registered using set_poll_read(true)/set_poll_write(true) and the NetworkReactor::poll is run. The internal logic
c7295b72731a documentation work on Network
Tero Marttila <terom@fixme.fi>
parents: 382
diff changeset
    45
 * does not manipulate the poll states. Usually, sig_read will always be enabled (except to throttle incoming traffic),
c7295b72731a documentation work on Network
Tero Marttila <terom@fixme.fi>
parents: 382
diff changeset
    46
 * and sig_write should be enabled after send() returns zero (XXX: provide an automatic mechanism for this?).
284
27ce69fd1e06 work on Network doxygen docs
terom
parents: 186
diff changeset
    47
 */
378
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
    48
class NetworkSocket {
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
    49
    private:
380
d193dd1d8a7e new NetworkReactor, fix everything to actually work
terom
parents: 378
diff changeset
    50
        /**
d193dd1d8a7e new NetworkReactor, fix everything to actually work
terom
parents: 378
diff changeset
    51
         * Socket family/type/protocol
d193dd1d8a7e new NetworkReactor, fix everything to actually work
terom
parents: 378
diff changeset
    52
         */
d193dd1d8a7e new NetworkReactor, fix everything to actually work
terom
parents: 378
diff changeset
    53
        struct socket_type {
d193dd1d8a7e new NetworkReactor, fix everything to actually work
terom
parents: 378
diff changeset
    54
            /** Socket domain */
d193dd1d8a7e new NetworkReactor, fix everything to actually work
terom
parents: 378
diff changeset
    55
            int family;
d193dd1d8a7e new NetworkReactor, fix everything to actually work
terom
parents: 378
diff changeset
    56
d193dd1d8a7e new NetworkReactor, fix everything to actually work
terom
parents: 378
diff changeset
    57
            /** Socket type */
d193dd1d8a7e new NetworkReactor, fix everything to actually work
terom
parents: 378
diff changeset
    58
            int socktype;
d193dd1d8a7e new NetworkReactor, fix everything to actually work
terom
parents: 378
diff changeset
    59
d193dd1d8a7e new NetworkReactor, fix everything to actually work
terom
parents: 378
diff changeset
    60
            /** Socket protocol */
d193dd1d8a7e new NetworkReactor, fix everything to actually work
terom
parents: 378
diff changeset
    61
            int protocol;
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
            /** Simple constructor */
d193dd1d8a7e new NetworkReactor, fix everything to actually work
terom
parents: 378
diff changeset
    64
            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
    65
        };
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
        /** 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
    68
        socket_type sock_type;
d193dd1d8a7e new NetworkReactor, fix everything to actually work
terom
parents: 378
diff changeset
    69
378
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
    70
        /** The file descriptor */
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
    71
        int fd;
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
    72
399
c7295b72731a documentation work on Network
Tero Marttila <terom@fixme.fi>
parents: 382
diff changeset
    73
        /** Our current type as used for fd, intialized via constructor, but updated by lazy_socket */
380
d193dd1d8a7e new NetworkReactor, fix everything to actually work
terom
parents: 378
diff changeset
    74
        socket_type type;
378
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
    75
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
         * 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
    78
         */
380
d193dd1d8a7e new NetworkReactor, fix everything to actually work
terom
parents: 378
diff changeset
    79
        bool bound : 1;
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
        /**
d193dd1d8a7e new NetworkReactor, fix everything to actually work
terom
parents: 378
diff changeset
    82
         * Registered to reactor?
d193dd1d8a7e new NetworkReactor, fix everything to actually work
terom
parents: 378
diff changeset
    83
         */
d193dd1d8a7e new NetworkReactor, fix everything to actually work
terom
parents: 378
diff changeset
    84
        bool registered : 1;
d193dd1d8a7e new NetworkReactor, fix everything to actually work
terom
parents: 378
diff changeset
    85
d193dd1d8a7e new NetworkReactor, fix everything to actually work
terom
parents: 378
diff changeset
    86
        /**
d193dd1d8a7e new NetworkReactor, fix everything to actually work
terom
parents: 378
diff changeset
    87
         * Do we want to know about recv()s?
d193dd1d8a7e new NetworkReactor, fix everything to actually work
terom
parents: 378
diff changeset
    88
         */
d193dd1d8a7e new NetworkReactor, fix everything to actually work
terom
parents: 378
diff changeset
    89
        bool want_read : 1;
d193dd1d8a7e new NetworkReactor, fix everything to actually work
terom
parents: 378
diff changeset
    90
d193dd1d8a7e new NetworkReactor, fix everything to actually work
terom
parents: 378
diff changeset
    91
        /**
d193dd1d8a7e new NetworkReactor, fix everything to actually work
terom
parents: 378
diff changeset
    92
         * Is the write buffer full?
d193dd1d8a7e new NetworkReactor, fix everything to actually work
terom
parents: 378
diff changeset
    93
         */
d193dd1d8a7e new NetworkReactor, fix everything to actually work
terom
parents: 378
diff changeset
    94
        bool want_write : 1;
d193dd1d8a7e new NetworkReactor, fix everything to actually work
terom
parents: 378
diff changeset
    95
d193dd1d8a7e new NetworkReactor, fix everything to actually work
terom
parents: 378
diff changeset
    96
        /**
d193dd1d8a7e new NetworkReactor, fix everything to actually work
terom
parents: 378
diff changeset
    97
         * The reactor that we use, defaults to NetworkReactor::current
d193dd1d8a7e new NetworkReactor, fix everything to actually work
terom
parents: 378
diff changeset
    98
         */
d193dd1d8a7e new NetworkReactor, fix everything to actually work
terom
parents: 378
diff changeset
    99
        NetworkReactor *reactor;
378
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
        /**
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
   102
         * Read/write signals
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
        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
   105
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
   106
    public:
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
         * 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
   109
         * 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
   110
         */
380
d193dd1d8a7e new NetworkReactor, fix everything to actually work
terom
parents: 378
diff changeset
   111
        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
   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
         * 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
   115
         */
380
d193dd1d8a7e new NetworkReactor, fix everything to actually work
terom
parents: 378
diff changeset
   116
        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
   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
         * 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
   120
         */
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
   121
        ~NetworkSocket (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
    private:
399
c7295b72731a documentation work on Network
Tero Marttila <terom@fixme.fi>
parents: 382
diff changeset
   124
        // nocopy
c7295b72731a documentation work on Network
Tero Marttila <terom@fixme.fi>
parents: 382
diff changeset
   125
        NetworkSocket (const NetworkSocket &copy);
c7295b72731a documentation work on Network
Tero Marttila <terom@fixme.fi>
parents: 382
diff changeset
   126
        NetworkSocket &operator= (const NetworkSocket &copy);
378
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
        /**
380
d193dd1d8a7e new NetworkReactor, fix everything to actually work
terom
parents: 378
diff changeset
   129
         * Reset bound+poll
d193dd1d8a7e new NetworkReactor, fix everything to actually work
terom
parents: 378
diff changeset
   130
         */ 
d193dd1d8a7e new NetworkReactor, fix everything to actually work
terom
parents: 378
diff changeset
   131
        void reset (void);
d193dd1d8a7e new NetworkReactor, fix everything to actually work
terom
parents: 378
diff changeset
   132
d193dd1d8a7e new NetworkReactor, fix everything to actually work
terom
parents: 378
diff changeset
   133
        /**
378
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
   134
         * 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
   135
         */
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
   136
        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
   137
        
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
   138
        /**
380
d193dd1d8a7e new NetworkReactor, fix everything to actually work
terom
parents: 378
diff changeset
   139
         * 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
   140
         */
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
   141
        void force_close (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
    public:
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 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
   146
         */
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
   147
        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
   148
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
   149
        /**
400
d64bf28c4340 more documentation tweaking, all Network/ files now have a @file comment. Fix Platform.h -> Platform.hh, and Buffer.hh + Packet.cc
Tero Marttila <terom@fixme.fi>
parents: 399
diff changeset
   150
         * Bind to a local endpoint. This can be specified in hostname form, and a suitable socket will be chosen.
378
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
   151
         */ 
381
9b35bc329d23 separate sockaddr stuff out of NetworkAddress... now called NetworkEndpoint
terom
parents: 380
diff changeset
   152
        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
   153
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
   154
        /**
400
d64bf28c4340 more documentation tweaking, all Network/ files now have a @file comment. Fix Platform.h -> Platform.hh, and Buffer.hh + Packet.cc
Tero Marttila <terom@fixme.fi>
parents: 399
diff changeset
   155
         * Put socket into listen mode for accept()
378
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
        void listen (int backlog);
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
         * Get local address
400
d64bf28c4340 more documentation tweaking, all Network/ files now have a @file comment. Fix Platform.h -> Platform.hh, and Buffer.hh + Packet.cc
Tero Marttila <terom@fixme.fi>
parents: 399
diff changeset
   161
         *
d64bf28c4340 more documentation tweaking, all Network/ files now have a @file comment. Fix Platform.h -> Platform.hh, and Buffer.hh + Packet.cc
Tero Marttila <terom@fixme.fi>
parents: 399
diff changeset
   162
         * Note that this may block on a reverse DNS lookup.
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
        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
   165
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
   166
        /**
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
   167
         * Get remote address
400
d64bf28c4340 more documentation tweaking, all Network/ files now have a @file comment. Fix Platform.h -> Platform.hh, and Buffer.hh + Packet.cc
Tero Marttila <terom@fixme.fi>
parents: 399
diff changeset
   168
         *
d64bf28c4340 more documentation tweaking, all Network/ files now have a @file comment. Fix Platform.h -> Platform.hh, and Buffer.hh + Packet.cc
Tero Marttila <terom@fixme.fi>
parents: 399
diff changeset
   169
         * Note that this may block on a reverse DNS lookup.
378
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
        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
   172
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
   173
        /**
400
d64bf28c4340 more documentation tweaking, all Network/ files now have a @file comment. Fix Platform.h -> Platform.hh, and Buffer.hh + Packet.cc
Tero Marttila <terom@fixme.fi>
parents: 399
diff changeset
   174
         * Make send/recv non-blocking. The current connect() implementation does not support use of non-blocking
d64bf28c4340 more documentation tweaking, all Network/ files now have a @file comment. Fix Platform.h -> Platform.hh, and Buffer.hh + Packet.cc
Tero Marttila <terom@fixme.fi>
parents: 399
diff changeset
   175
         * connects.
378
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
   176
         */
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
   177
        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
   178
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
   179
        /**
400
d64bf28c4340 more documentation tweaking, all Network/ files now have a @file comment. Fix Platform.h -> Platform.hh, and Buffer.hh + Packet.cc
Tero Marttila <terom@fixme.fi>
parents: 399
diff changeset
   180
         * Accept an incoming connection on a listen() socket as a new socket, optionally storing the connection's
d64bf28c4340 more documentation tweaking, all Network/ files now have a @file comment. Fix Platform.h -> Platform.hh, and Buffer.hh + Packet.cc
Tero Marttila <terom@fixme.fi>
parents: 399
diff changeset
   181
         * source address.
d64bf28c4340 more documentation tweaking, all Network/ files now have a @file comment. Fix Platform.h -> Platform.hh, and Buffer.hh + Packet.cc
Tero Marttila <terom@fixme.fi>
parents: 399
diff changeset
   182
         *
d64bf28c4340 more documentation tweaking, all Network/ files now have a @file comment. Fix Platform.h -> Platform.hh, and Buffer.hh + Packet.cc
Tero Marttila <terom@fixme.fi>
parents: 399
diff changeset
   183
         * Note that this may block on a reverse DNS lookup if \a src is given.
378
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
        NetworkSocket* accept (NetworkAddress *src);
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
        /**
400
d64bf28c4340 more documentation tweaking, all Network/ files now have a @file comment. Fix Platform.h -> Platform.hh, and Buffer.hh + Packet.cc
Tero Marttila <terom@fixme.fi>
parents: 399
diff changeset
   188
         * Connect this socket to a remote endpoint, going through the resolved addresses until we find one that works.
d64bf28c4340 more documentation tweaking, all Network/ files now have a @file comment. Fix Platform.h -> Platform.hh, and Buffer.hh + Packet.cc
Tero Marttila <terom@fixme.fi>
parents: 399
diff changeset
   189
         *
d64bf28c4340 more documentation tweaking, all Network/ files now have a @file comment. Fix Platform.h -> Platform.hh, and Buffer.hh + Packet.cc
Tero Marttila <terom@fixme.fi>
parents: 399
diff changeset
   190
         * This is currently implemented in an entirely blocking fashion, DNS lookups and connect() included.
378
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
   191
         */
381
9b35bc329d23 separate sockaddr stuff out of NetworkAddress... now called NetworkEndpoint
terom
parents: 380
diff changeset
   192
        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
   193
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
         * 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
   196
         *
381
9b35bc329d23 separate sockaddr stuff out of NetworkAddress... now called NetworkEndpoint
terom
parents: 380
diff changeset
   197
         * @param buf bytes to send
9b35bc329d23 separate sockaddr stuff out of NetworkAddress... now called NetworkEndpoint
terom
parents: 380
diff changeset
   198
         * @param size how many bytes to try and send
9b35bc329d23 separate sockaddr stuff out of NetworkAddress... now called NetworkEndpoint
terom
parents: 380
diff changeset
   199
         * @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
   200
         * @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
   201
         * @throw NetworkSocketError on error
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
   202
         */
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
   203
        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
   204
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
   205
        /**
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
   206
         * 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
   207
         *
381
9b35bc329d23 separate sockaddr stuff out of NetworkAddress... now called NetworkEndpoint
terom
parents: 380
diff changeset
   208
         * @param buf where to recv into
9b35bc329d23 separate sockaddr stuff out of NetworkAddress... now called NetworkEndpoint
terom
parents: 380
diff changeset
   209
         * @param size how many bytes to try and receive
9b35bc329d23 separate sockaddr stuff out of NetworkAddress... now called NetworkEndpoint
terom
parents: 380
diff changeset
   210
         * @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
   211
         * @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
   212
         * @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
   213
         * @throw NetworkSocketError on error
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
   214
         */
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
   215
        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
   216
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
   217
        /**
380
d193dd1d8a7e new NetworkReactor, fix everything to actually work
terom
parents: 378
diff changeset
   218
         * 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
   219
         */
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
   220
        void close (void);
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
   221
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
   222
        /**
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
   223
         * 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
   224
         */
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
   225
        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
   226
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
   227
        /**
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
   228
         * 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
   229
         */
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
   230
        CL_Signal_v0& sig_write (void) { return _sig_write; }
380
d193dd1d8a7e new NetworkReactor, fix everything to actually work
terom
parents: 378
diff changeset
   231
d193dd1d8a7e new NetworkReactor, fix everything to actually work
terom
parents: 378
diff changeset
   232
        /**
d193dd1d8a7e new NetworkReactor, fix everything to actually work
terom
parents: 378
diff changeset
   233
         * Register to NetworkReactor unless already registered
d193dd1d8a7e new NetworkReactor, fix everything to actually work
terom
parents: 378
diff changeset
   234
         */
d193dd1d8a7e new NetworkReactor, fix everything to actually work
terom
parents: 378
diff changeset
   235
        void register_poll (void);
d193dd1d8a7e new NetworkReactor, fix everything to actually work
terom
parents: 378
diff changeset
   236
d193dd1d8a7e new NetworkReactor, fix everything to actually work
terom
parents: 378
diff changeset
   237
        /**
400
d64bf28c4340 more documentation tweaking, all Network/ files now have a @file comment. Fix Platform.h -> Platform.hh, and Buffer.hh + Packet.cc
Tero Marttila <terom@fixme.fi>
parents: 399
diff changeset
   238
         * Trigger sig_read() once socket is ready for recv()
380
d193dd1d8a7e new NetworkReactor, fix everything to actually work
terom
parents: 378
diff changeset
   239
         */
d193dd1d8a7e new NetworkReactor, fix everything to actually work
terom
parents: 378
diff changeset
   240
        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
   241
d193dd1d8a7e new NetworkReactor, fix everything to actually work
terom
parents: 378
diff changeset
   242
        /**
400
d64bf28c4340 more documentation tweaking, all Network/ files now have a @file comment. Fix Platform.h -> Platform.hh, and Buffer.hh + Packet.cc
Tero Marttila <terom@fixme.fi>
parents: 399
diff changeset
   243
         * Trigger sig_write() once socket is ready for send()
380
d193dd1d8a7e new NetworkReactor, fix everything to actually work
terom
parents: 378
diff changeset
   244
         */
d193dd1d8a7e new NetworkReactor, fix everything to actually work
terom
parents: 378
diff changeset
   245
        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
   246
d193dd1d8a7e new NetworkReactor, fix everything to actually work
terom
parents: 378
diff changeset
   247
        /**
400
d64bf28c4340 more documentation tweaking, all Network/ files now have a @file comment. Fix Platform.h -> Platform.hh, and Buffer.hh + Packet.cc
Tero Marttila <terom@fixme.fi>
parents: 399
diff changeset
   248
         * What events this socket is interested in (called by NetworkReactor)
380
d193dd1d8a7e new NetworkReactor, fix everything to actually work
terom
parents: 378
diff changeset
   249
         */
d193dd1d8a7e new NetworkReactor, fix everything to actually work
terom
parents: 378
diff changeset
   250
        NetworkPollMask get_poll (void) { 
d193dd1d8a7e new NetworkReactor, fix everything to actually work
terom
parents: 378
diff changeset
   251
            return (want_read ? POLL_READ : 0) | (want_write ? POLL_WRITE : 0); 
d193dd1d8a7e new NetworkReactor, fix everything to actually work
terom
parents: 378
diff changeset
   252
        }
d193dd1d8a7e new NetworkReactor, fix everything to actually work
terom
parents: 378
diff changeset
   253
d193dd1d8a7e new NetworkReactor, fix everything to actually work
terom
parents: 378
diff changeset
   254
        /**
400
d64bf28c4340 more documentation tweaking, all Network/ files now have a @file comment. Fix Platform.h -> Platform.hh, and Buffer.hh + Packet.cc
Tero Marttila <terom@fixme.fi>
parents: 399
diff changeset
   255
         * Notify of events (called by NetworkReactor)
380
d193dd1d8a7e new NetworkReactor, fix everything to actually work
terom
parents: 378
diff changeset
   256
         */
d193dd1d8a7e new NetworkReactor, fix everything to actually work
terom
parents: 378
diff changeset
   257
        void notify (NetworkPollMask mask) { 
d193dd1d8a7e new NetworkReactor, fix everything to actually work
terom
parents: 378
diff changeset
   258
            if (mask & POLL_READ) _sig_read();
d193dd1d8a7e new NetworkReactor, fix everything to actually work
terom
parents: 378
diff changeset
   259
            if (mask & POLL_WRITE) _sig_write();
d193dd1d8a7e new NetworkReactor, fix everything to actually work
terom
parents: 378
diff changeset
   260
        }
378
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
   261
};
185
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   262
284
27ce69fd1e06 work on Network doxygen docs
terom
parents: 186
diff changeset
   263
/**
27ce69fd1e06 work on Network doxygen docs
terom
parents: 186
diff changeset
   264
 * Base class for expcetions thrown by socket methods
27ce69fd1e06 work on Network doxygen docs
terom
parents: 186
diff changeset
   265
 */
185
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   266
class NetworkSocketError : public Error {
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   267
    protected:
378
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
   268
        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
   269
    
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
   270
    public:
185
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   271
        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
   272
};
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   273
284
27ce69fd1e06 work on Network doxygen docs
terom
parents: 186
diff changeset
   274
/**
27ce69fd1e06 work on Network doxygen docs
terom
parents: 186
diff changeset
   275
 * Errno-enabled exception, most common type of NetworkSocketError
27ce69fd1e06 work on Network doxygen docs
terom
parents: 186
diff changeset
   276
 */
380
d193dd1d8a7e new NetworkReactor, fix everything to actually work
terom
parents: 378
diff changeset
   277
class NetworkSocketErrno : public NetworkSocketError {
185
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   278
    public:
380
d193dd1d8a7e new NetworkReactor, fix everything to actually work
terom
parents: 378
diff changeset
   279
        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
   280
};
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   281
284
27ce69fd1e06 work on Network doxygen docs
terom
parents: 186
diff changeset
   282
/**
27ce69fd1e06 work on Network doxygen docs
terom
parents: 186
diff changeset
   283
 * Recv returned EOF
27ce69fd1e06 work on Network doxygen docs
terom
parents: 186
diff changeset
   284
 */
185
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   285
class NetworkSocketEOFError : public NetworkSocketError {
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   286
    public:
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   287
        NetworkSocketEOFError (const NetworkSocket &socket, const char *op) :
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   288
            NetworkSocketError(socket, op, "EOF") { }
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   289
};
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   290
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   291
#endif /* NETWORK_SOCKET_HH */