src/Network/Socket.hh
author Tero Marttila <terom@fixme.fi>
Thu, 22 Jan 2009 02:38:33 +0200
branchnew_graphics
changeset 418 194bc810a570
parent 400 d64bf28c4340
permissions -rw-r--r--
add --log-level option, improve Config/Logger documentation, fix NETWORK_EANBLED typos in Engine
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
418
194bc810a570 add --log-level option, improve Config/Logger documentation, fix NETWORK_EANBLED typos in Engine
Tero Marttila <terom@fixme.fi>
parents: 400
diff changeset
    18
#include <ClanLib/signals.h>
194bc810a570 add --log-level option, improve Config/Logger documentation, fix NETWORK_EANBLED typos in Engine
Tero Marttila <terom@fixme.fi>
parents: 400
diff changeset
    19
284
27ce69fd1e06 work on Network doxygen docs
terom
parents: 186
diff changeset
    20
/**
399
c7295b72731a documentation work on Network
Tero Marttila <terom@fixme.fi>
parents: 382
diff changeset
    21
 * 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
    22
 * 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
    23
 *
c7295b72731a documentation work on Network
Tero Marttila <terom@fixme.fi>
parents: 382
diff changeset
    24
 * 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
    25
 * 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
    26
 * 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
    27
 * 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
    28
 * connect()/bind().
c7295b72731a documentation work on Network
Tero Marttila <terom@fixme.fi>
parents: 382
diff changeset
    29
 *
c7295b72731a documentation work on Network
Tero Marttila <terom@fixme.fi>
parents: 382
diff changeset
    30
 * 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
    31
 * 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
    32
 * 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
    33
 *
c7295b72731a documentation work on Network
Tero Marttila <terom@fixme.fi>
parents: 382
diff changeset
    34
 * 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
    35
 * 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
    36
 * 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
    37
 *
c7295b72731a documentation work on Network
Tero Marttila <terom@fixme.fi>
parents: 382
diff changeset
    38
 * 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
    39
 * 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
    40
 * 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
    41
 * for the syscalls - the number of bytes send/received.
c7295b72731a documentation work on Network
Tero Marttila <terom@fixme.fi>
parents: 382
diff changeset
    42
 *
c7295b72731a documentation work on Network
Tero Marttila <terom@fixme.fi>
parents: 382
diff changeset
    43
 * 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
    44
 * 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
    45
 * 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
    46
 * 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
    47
 * 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
    48
 * 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
    49
 */
378
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
    50
class NetworkSocket {
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
    51
    private:
380
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
         * Socket family/type/protocol
d193dd1d8a7e new NetworkReactor, fix everything to actually work
terom
parents: 378
diff changeset
    54
         */
d193dd1d8a7e new NetworkReactor, fix everything to actually work
terom
parents: 378
diff changeset
    55
        struct socket_type {
d193dd1d8a7e new NetworkReactor, fix everything to actually work
terom
parents: 378
diff changeset
    56
            /** Socket domain */
d193dd1d8a7e new NetworkReactor, fix everything to actually work
terom
parents: 378
diff changeset
    57
            int family;
d193dd1d8a7e new NetworkReactor, fix everything to actually work
terom
parents: 378
diff changeset
    58
d193dd1d8a7e new NetworkReactor, fix everything to actually work
terom
parents: 378
diff changeset
    59
            /** Socket type */
d193dd1d8a7e new NetworkReactor, fix everything to actually work
terom
parents: 378
diff changeset
    60
            int socktype;
d193dd1d8a7e new NetworkReactor, fix everything to actually work
terom
parents: 378
diff changeset
    61
d193dd1d8a7e new NetworkReactor, fix everything to actually work
terom
parents: 378
diff changeset
    62
            /** Socket protocol */
d193dd1d8a7e new NetworkReactor, fix everything to actually work
terom
parents: 378
diff changeset
    63
            int protocol;
d193dd1d8a7e new NetworkReactor, fix everything to actually work
terom
parents: 378
diff changeset
    64
            
d193dd1d8a7e new NetworkReactor, fix everything to actually work
terom
parents: 378
diff changeset
    65
            /** Simple constructor */
d193dd1d8a7e new NetworkReactor, fix everything to actually work
terom
parents: 378
diff changeset
    66
            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
    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
        /** 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
    70
        socket_type sock_type;
d193dd1d8a7e new NetworkReactor, fix everything to actually work
terom
parents: 378
diff changeset
    71
378
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
    72
        /** The file descriptor */
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
    73
        int fd;
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
    74
399
c7295b72731a documentation work on Network
Tero Marttila <terom@fixme.fi>
parents: 382
diff changeset
    75
        /** 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
    76
        socket_type type;
378
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
        /** 
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
    79
         * 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
    80
         */
380
d193dd1d8a7e new NetworkReactor, fix everything to actually work
terom
parents: 378
diff changeset
    81
        bool bound : 1;
d193dd1d8a7e new NetworkReactor, fix everything to actually work
terom
parents: 378
diff changeset
    82
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
         * Registered to reactor?
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
        bool registered : 1;
d193dd1d8a7e new NetworkReactor, fix everything to actually work
terom
parents: 378
diff changeset
    87
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
         * Do we want to know about recv()s?
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
        bool want_read : 1;
d193dd1d8a7e new NetworkReactor, fix everything to actually work
terom
parents: 378
diff changeset
    92
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
         * Is the write buffer full?
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
        bool want_write : 1;
d193dd1d8a7e new NetworkReactor, fix everything to actually work
terom
parents: 378
diff changeset
    97
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
         * The reactor that we use, defaults to NetworkReactor::current
d193dd1d8a7e new NetworkReactor, fix everything to actually work
terom
parents: 378
diff changeset
   100
         */
d193dd1d8a7e new NetworkReactor, fix everything to actually work
terom
parents: 378
diff changeset
   101
        NetworkReactor *reactor;
378
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
         * Read/write signals
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
        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
   107
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
   108
    public:
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
   109
        /**
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
   110
         * 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
   111
         * 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
   112
         */
380
d193dd1d8a7e new NetworkReactor, fix everything to actually work
terom
parents: 378
diff changeset
   113
        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
   114
        
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
         * 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
   117
         */
380
d193dd1d8a7e new NetworkReactor, fix everything to actually work
terom
parents: 378
diff changeset
   118
        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
   119
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
         * 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
   122
         */
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
   123
        ~NetworkSocket (void);
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
    private:
399
c7295b72731a documentation work on Network
Tero Marttila <terom@fixme.fi>
parents: 382
diff changeset
   126
        // nocopy
c7295b72731a documentation work on Network
Tero Marttila <terom@fixme.fi>
parents: 382
diff changeset
   127
        NetworkSocket (const NetworkSocket &copy);
c7295b72731a documentation work on Network
Tero Marttila <terom@fixme.fi>
parents: 382
diff changeset
   128
        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
   129
        
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
   130
        /**
380
d193dd1d8a7e new NetworkReactor, fix everything to actually work
terom
parents: 378
diff changeset
   131
         * Reset bound+poll
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
        void reset (void);
d193dd1d8a7e new NetworkReactor, fix everything to actually work
terom
parents: 378
diff changeset
   134
d193dd1d8a7e new NetworkReactor, fix everything to actually work
terom
parents: 378
diff changeset
   135
        /**
378
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
   136
         * 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
   137
         */
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
   138
        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
   139
        
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
   140
        /**
380
d193dd1d8a7e new NetworkReactor, fix everything to actually work
terom
parents: 378
diff changeset
   141
         * 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
   142
         */
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
   143
        void force_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
    public:
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
         * 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
   148
         */
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
   149
        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
   150
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
   151
        /**
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
   152
         * 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
   153
         */ 
381
9b35bc329d23 separate sockaddr stuff out of NetworkAddress... now called NetworkEndpoint
terom
parents: 380
diff changeset
   154
        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
   155
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
   156
        /**
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
   157
         * 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
   158
         */
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
   159
        void listen (int backlog);
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
   160
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
   161
        /**
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
   162
         * 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
   163
         *
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
   164
         * 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
   165
         */
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
   166
        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
   167
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
   168
        /**
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
   169
         * 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
   170
         *
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
   171
         * 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
   172
         */
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
   173
        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
   174
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
   175
        /**
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
   176
         * 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
   177
         * connects.
378
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
        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
   180
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
   181
        /**
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
   182
         * 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
   183
         * 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
   184
         *
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
   185
         * 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
   186
         */
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
   187
        NetworkSocket* accept (NetworkAddress *src);
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
   188
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
   189
        /**
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
   190
         * 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
   191
         *
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
   192
         * 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
   193
         */
381
9b35bc329d23 separate sockaddr stuff out of NetworkAddress... now called NetworkEndpoint
terom
parents: 380
diff changeset
   194
        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
   195
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
         * 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
   198
         *
381
9b35bc329d23 separate sockaddr stuff out of NetworkAddress... now called NetworkEndpoint
terom
parents: 380
diff changeset
   199
         * @param buf bytes to send
9b35bc329d23 separate sockaddr stuff out of NetworkAddress... now called NetworkEndpoint
terom
parents: 380
diff changeset
   200
         * @param size how many bytes to try and send
9b35bc329d23 separate sockaddr stuff out of NetworkAddress... now called NetworkEndpoint
terom
parents: 380
diff changeset
   201
         * @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
   202
         * @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
   203
         * @throw NetworkSocketError on error
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
        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
   206
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
   207
        /**
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
   208
         * 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
   209
         *
381
9b35bc329d23 separate sockaddr stuff out of NetworkAddress... now called NetworkEndpoint
terom
parents: 380
diff changeset
   210
         * @param buf where to recv into
9b35bc329d23 separate sockaddr stuff out of NetworkAddress... now called NetworkEndpoint
terom
parents: 380
diff changeset
   211
         * @param size how many bytes to try and receive
9b35bc329d23 separate sockaddr stuff out of NetworkAddress... now called NetworkEndpoint
terom
parents: 380
diff changeset
   212
         * @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
   213
         * @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
   214
         * @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
   215
         * @throw NetworkSocketError on error
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
        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
   218
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
   219
        /**
380
d193dd1d8a7e new NetworkReactor, fix everything to actually work
terom
parents: 378
diff changeset
   220
         * 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
   221
         */
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
   222
        void close (void);
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
   223
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
         * 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
   226
         */
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
   227
        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
   228
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
         * 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
   231
         */
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
   232
        CL_Signal_v0& sig_write (void) { return _sig_write; }
380
d193dd1d8a7e new NetworkReactor, fix everything to actually work
terom
parents: 378
diff changeset
   233
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
         * Register to NetworkReactor unless already registered
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
        void register_poll (void);
d193dd1d8a7e new NetworkReactor, fix everything to actually work
terom
parents: 378
diff changeset
   238
d193dd1d8a7e new NetworkReactor, fix everything to actually work
terom
parents: 378
diff changeset
   239
        /**
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
   240
         * Trigger sig_read() once socket is ready for recv()
380
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
        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
   243
d193dd1d8a7e new NetworkReactor, fix everything to actually work
terom
parents: 378
diff changeset
   244
        /**
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
   245
         * Trigger sig_write() once socket is ready for send()
380
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
        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
   248
d193dd1d8a7e new NetworkReactor, fix everything to actually work
terom
parents: 378
diff changeset
   249
        /**
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
   250
         * What events this socket is interested in (called by NetworkReactor)
380
d193dd1d8a7e new NetworkReactor, fix everything to actually work
terom
parents: 378
diff changeset
   251
         */
d193dd1d8a7e new NetworkReactor, fix everything to actually work
terom
parents: 378
diff changeset
   252
        NetworkPollMask get_poll (void) { 
d193dd1d8a7e new NetworkReactor, fix everything to actually work
terom
parents: 378
diff changeset
   253
            return (want_read ? POLL_READ : 0) | (want_write ? POLL_WRITE : 0); 
d193dd1d8a7e new NetworkReactor, fix everything to actually work
terom
parents: 378
diff changeset
   254
        }
d193dd1d8a7e new NetworkReactor, fix everything to actually work
terom
parents: 378
diff changeset
   255
d193dd1d8a7e new NetworkReactor, fix everything to actually work
terom
parents: 378
diff changeset
   256
        /**
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
   257
         * Notify of events (called by NetworkReactor)
380
d193dd1d8a7e new NetworkReactor, fix everything to actually work
terom
parents: 378
diff changeset
   258
         */
d193dd1d8a7e new NetworkReactor, fix everything to actually work
terom
parents: 378
diff changeset
   259
        void notify (NetworkPollMask mask) { 
d193dd1d8a7e new NetworkReactor, fix everything to actually work
terom
parents: 378
diff changeset
   260
            if (mask & POLL_READ) _sig_read();
d193dd1d8a7e new NetworkReactor, fix everything to actually work
terom
parents: 378
diff changeset
   261
            if (mask & POLL_WRITE) _sig_write();
d193dd1d8a7e new NetworkReactor, fix everything to actually work
terom
parents: 378
diff changeset
   262
        }
378
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
   263
};
185
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   264
284
27ce69fd1e06 work on Network doxygen docs
terom
parents: 186
diff changeset
   265
/**
27ce69fd1e06 work on Network doxygen docs
terom
parents: 186
diff changeset
   266
 * Base class for expcetions thrown by socket methods
27ce69fd1e06 work on Network doxygen docs
terom
parents: 186
diff changeset
   267
 */
185
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   268
class NetworkSocketError : public Error {
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   269
    protected:
378
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
   270
        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
   271
    
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 284
diff changeset
   272
    public:
185
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   273
        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
   274
};
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   275
284
27ce69fd1e06 work on Network doxygen docs
terom
parents: 186
diff changeset
   276
/**
27ce69fd1e06 work on Network doxygen docs
terom
parents: 186
diff changeset
   277
 * Errno-enabled exception, most common type of NetworkSocketError
27ce69fd1e06 work on Network doxygen docs
terom
parents: 186
diff changeset
   278
 */
380
d193dd1d8a7e new NetworkReactor, fix everything to actually work
terom
parents: 378
diff changeset
   279
class NetworkSocketErrno : public NetworkSocketError {
185
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   280
    public:
380
d193dd1d8a7e new NetworkReactor, fix everything to actually work
terom
parents: 378
diff changeset
   281
        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
   282
};
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   283
284
27ce69fd1e06 work on Network doxygen docs
terom
parents: 186
diff changeset
   284
/**
27ce69fd1e06 work on Network doxygen docs
terom
parents: 186
diff changeset
   285
 * Recv returned EOF
27ce69fd1e06 work on Network doxygen docs
terom
parents: 186
diff changeset
   286
 */
185
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   287
class NetworkSocketEOFError : public NetworkSocketError {
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   288
    public:
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   289
        NetworkSocketEOFError (const NetworkSocket &socket, const char *op) :
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   290
            NetworkSocketError(socket, op, "EOF") { }
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   291
};
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   292
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   293
#endif /* NETWORK_SOCKET_HH */