src/Network/TCP.hh
author terom
Wed, 17 Dec 2008 00:40:22 +0000
changeset 381 9b35bc329d23
parent 380 d193dd1d8a7e
child 400 d64bf28c4340
permissions -rw-r--r--
separate sockaddr stuff out of NetworkAddress... now called NetworkEndpoint
185
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
     1
#ifndef NETWORK_TCP_HH
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
     2
#define NETWORK_TCP_HH
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
     3
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
     4
#include <ClanLib/core.h>
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
     5
186
0738f2949a2b move src/Network% to src/Network/%
terom
parents: 185
diff changeset
     6
#include "Socket.hh"
0738f2949a2b move src/Network% to src/Network/%
terom
parents: 185
diff changeset
     7
#include "Address.hh"
0738f2949a2b move src/Network% to src/Network/%
terom
parents: 185
diff changeset
     8
#include "Packet.hh"
200
2dbf40661580 better NetworkBuffer/Packet stuff + some additional Physics+Network stuff + random fixes
terom
parents: 186
diff changeset
     9
#include "Buffer.hh"
185
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    10
286
2a8f20a53ff2 more network documentation
terom
parents: 200
diff changeset
    11
// @{
2a8f20a53ff2 more network documentation
terom
parents: 200
diff changeset
    12
/**
2a8f20a53ff2 more network documentation
terom
parents: 200
diff changeset
    13
 * Initial input/output buffer size given to NetworkTCPTransport::in/out constructor
2a8f20a53ff2 more network documentation
terom
parents: 200
diff changeset
    14
 *
2a8f20a53ff2 more network documentation
terom
parents: 200
diff changeset
    15
 * @see NetworkBufferBase
2a8f20a53ff2 more network documentation
terom
parents: 200
diff changeset
    16
 */
185
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    17
const size_t NETWORK_TCP_INITIAL_IN_BUF = 4096;
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    18
const size_t NETWORK_TCP_INITIAL_OUT_BUF = 0;
286
2a8f20a53ff2 more network documentation
terom
parents: 200
diff changeset
    19
// @}
185
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    20
286
2a8f20a53ff2 more network documentation
terom
parents: 200
diff changeset
    21
/**
2a8f20a53ff2 more network documentation
terom
parents: 200
diff changeset
    22
 * A connected TCP socket, so either a client or a server-accept socket
2a8f20a53ff2 more network documentation
terom
parents: 200
diff changeset
    23
 */
185
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    24
class NetworkTCPTransport {
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    25
    protected:
286
2a8f20a53ff2 more network documentation
terom
parents: 200
diff changeset
    26
        /**
2a8f20a53ff2 more network documentation
terom
parents: 200
diff changeset
    27
         * The SOCK_STREAM socket
2a8f20a53ff2 more network documentation
terom
parents: 200
diff changeset
    28
         */
378
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 286
diff changeset
    29
        NetworkSocket *socket;
286
2a8f20a53ff2 more network documentation
terom
parents: 200
diff changeset
    30
        
2a8f20a53ff2 more network documentation
terom
parents: 200
diff changeset
    31
        /**
2a8f20a53ff2 more network documentation
terom
parents: 200
diff changeset
    32
         * Our input buffer associated with socket
2a8f20a53ff2 more network documentation
terom
parents: 200
diff changeset
    33
         */
2a8f20a53ff2 more network documentation
terom
parents: 200
diff changeset
    34
        NetworkBufferInput in;
185
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    35
286
2a8f20a53ff2 more network documentation
terom
parents: 200
diff changeset
    36
        /**
2a8f20a53ff2 more network documentation
terom
parents: 200
diff changeset
    37
         * Our output buffer associated with socket
2a8f20a53ff2 more network documentation
terom
parents: 200
diff changeset
    38
         */
200
2dbf40661580 better NetworkBuffer/Packet stuff + some additional Physics+Network stuff + random fixes
terom
parents: 186
diff changeset
    39
        NetworkBufferOutput out;
185
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    40
    
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    41
        CL_SlotContainer slots; 
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    42
    
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    43
    public:
286
2a8f20a53ff2 more network documentation
terom
parents: 200
diff changeset
    44
        /**
2a8f20a53ff2 more network documentation
terom
parents: 200
diff changeset
    45
         * Construct this using the given socket
2a8f20a53ff2 more network documentation
terom
parents: 200
diff changeset
    46
         */
378
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 286
diff changeset
    47
        NetworkTCPTransport (NetworkSocket *socket);
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 286
diff changeset
    48
        
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 286
diff changeset
    49
        /**
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 286
diff changeset
    50
         * Deletes the socket
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 286
diff changeset
    51
         */
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 286
diff changeset
    52
        ~NetworkTCPTransport (void);
185
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    53
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    54
    private:
286
2a8f20a53ff2 more network documentation
terom
parents: 200
diff changeset
    55
        /**
2a8f20a53ff2 more network documentation
terom
parents: 200
diff changeset
    56
         * Triggered when the socket is read for recv
2a8f20a53ff2 more network documentation
terom
parents: 200
diff changeset
    57
         */
185
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    58
        void on_read (void);
286
2a8f20a53ff2 more network documentation
terom
parents: 200
diff changeset
    59
2a8f20a53ff2 more network documentation
terom
parents: 200
diff changeset
    60
        /**
2a8f20a53ff2 more network documentation
terom
parents: 200
diff changeset
    61
         * Triggered when the socket is read for send
2a8f20a53ff2 more network documentation
terom
parents: 200
diff changeset
    62
         */
185
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    63
        void on_write (void);
286
2a8f20a53ff2 more network documentation
terom
parents: 200
diff changeset
    64
        
2a8f20a53ff2 more network documentation
terom
parents: 200
diff changeset
    65
        /**
2a8f20a53ff2 more network documentation
terom
parents: 200
diff changeset
    66
         * The packet-read signal
2a8f20a53ff2 more network documentation
terom
parents: 200
diff changeset
    67
         */
2a8f20a53ff2 more network documentation
terom
parents: 200
diff changeset
    68
        CL_Signal_v1<NetworkPacketInput&> _sig_packet;
185
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    69
286
2a8f20a53ff2 more network documentation
terom
parents: 200
diff changeset
    70
        /**
2a8f20a53ff2 more network documentation
terom
parents: 200
diff changeset
    71
         * The disconnected-signal
2a8f20a53ff2 more network documentation
terom
parents: 200
diff changeset
    72
         */
185
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    73
        CL_Signal_v0 _sig_disconnect;
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    74
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    75
    public:
286
2a8f20a53ff2 more network documentation
terom
parents: 200
diff changeset
    76
        /**
378
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 286
diff changeset
    77
         * Get this TCP socket's local address
286
2a8f20a53ff2 more network documentation
terom
parents: 200
diff changeset
    78
         */
380
d193dd1d8a7e new NetworkReactor, fix everything to actually work
terom
parents: 378
diff changeset
    79
        NetworkAddress get_local_address (void) { return socket->get_local_address(); }
286
2a8f20a53ff2 more network documentation
terom
parents: 200
diff changeset
    80
2a8f20a53ff2 more network documentation
terom
parents: 200
diff changeset
    81
        /**
2a8f20a53ff2 more network documentation
terom
parents: 200
diff changeset
    82
         * Get this TCP socket's remote address
2a8f20a53ff2 more network documentation
terom
parents: 200
diff changeset
    83
         */
380
d193dd1d8a7e new NetworkReactor, fix everything to actually work
terom
parents: 378
diff changeset
    84
        NetworkAddress get_remote_address (void) { return socket->get_remote_address(); }
286
2a8f20a53ff2 more network documentation
terom
parents: 200
diff changeset
    85
        
2a8f20a53ff2 more network documentation
terom
parents: 200
diff changeset
    86
        /**
2a8f20a53ff2 more network documentation
terom
parents: 200
diff changeset
    87
         * Write the given packet to this socket output, buffering the data if need be
2a8f20a53ff2 more network documentation
terom
parents: 200
diff changeset
    88
         *
2a8f20a53ff2 more network documentation
terom
parents: 200
diff changeset
    89
         * @param packet the packet to send
2a8f20a53ff2 more network documentation
terom
parents: 200
diff changeset
    90
         */
200
2dbf40661580 better NetworkBuffer/Packet stuff + some additional Physics+Network stuff + random fixes
terom
parents: 186
diff changeset
    91
        void write_packet (const NetworkPacketBuffer &packet);
380
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
         * Handle disconnect, disable socket
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
        void handle_disconnect (void);
185
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    97
        
286
2a8f20a53ff2 more network documentation
terom
parents: 200
diff changeset
    98
        /**
2a8f20a53ff2 more network documentation
terom
parents: 200
diff changeset
    99
         * A full packet has been received from the remote end
2a8f20a53ff2 more network documentation
terom
parents: 200
diff changeset
   100
         */
200
2dbf40661580 better NetworkBuffer/Packet stuff + some additional Physics+Network stuff + random fixes
terom
parents: 186
diff changeset
   101
        CL_Signal_v1<NetworkPacketInput&>& sig_packet (void) { return _sig_packet; }
286
2a8f20a53ff2 more network documentation
terom
parents: 200
diff changeset
   102
2a8f20a53ff2 more network documentation
terom
parents: 200
diff changeset
   103
        /**
2a8f20a53ff2 more network documentation
terom
parents: 200
diff changeset
   104
         * The connection has been lost (no specific error code given)
2a8f20a53ff2 more network documentation
terom
parents: 200
diff changeset
   105
         */
185
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   106
        CL_Signal_v0& sig_disconnect (void) { return _sig_disconnect; }
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   107
};
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   108
286
2a8f20a53ff2 more network documentation
terom
parents: 200
diff changeset
   109
/**
2a8f20a53ff2 more network documentation
terom
parents: 200
diff changeset
   110
 * A TCP server, listening on the given address, creating NetworkTCPTransports for clients that connect
2a8f20a53ff2 more network documentation
terom
parents: 200
diff changeset
   111
 */
185
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   112
class NetworkTCPServer {
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   113
    private:
286
2a8f20a53ff2 more network documentation
terom
parents: 200
diff changeset
   114
        /**
2a8f20a53ff2 more network documentation
terom
parents: 200
diff changeset
   115
         * The listen() socket
2a8f20a53ff2 more network documentation
terom
parents: 200
diff changeset
   116
         */
185
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   117
        NetworkSocket socket;
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   118
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   119
        CL_SlotContainer slots; 
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   120
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   121
    public:
286
2a8f20a53ff2 more network documentation
terom
parents: 200
diff changeset
   122
        /**
2a8f20a53ff2 more network documentation
terom
parents: 200
diff changeset
   123
         * Listen on the specific address.
2a8f20a53ff2 more network documentation
terom
parents: 200
diff changeset
   124
         */
381
9b35bc329d23 separate sockaddr stuff out of NetworkAddress... now called NetworkEndpoint
terom
parents: 380
diff changeset
   125
        NetworkTCPServer (const NetworkEndpoint &listen_addr);
185
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   126
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   127
    private:
286
2a8f20a53ff2 more network documentation
terom
parents: 200
diff changeset
   128
        /**
2a8f20a53ff2 more network documentation
terom
parents: 200
diff changeset
   129
         * The socket is read for read()
2a8f20a53ff2 more network documentation
terom
parents: 200
diff changeset
   130
         */
185
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   131
        void on_accept (void);
286
2a8f20a53ff2 more network documentation
terom
parents: 200
diff changeset
   132
        
2a8f20a53ff2 more network documentation
terom
parents: 200
diff changeset
   133
        /**
2a8f20a53ff2 more network documentation
terom
parents: 200
diff changeset
   134
         * The on_accept signal
2a8f20a53ff2 more network documentation
terom
parents: 200
diff changeset
   135
         */
185
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   136
        CL_Signal_v1<NetworkTCPTransport *> _sig_client;
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   137
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   138
    protected:
286
2a8f20a53ff2 more network documentation
terom
parents: 200
diff changeset
   139
        /**
2a8f20a53ff2 more network documentation
terom
parents: 200
diff changeset
   140
         * Called by on_accept to build the NetworkTCPTransport, can be used to override what class gets used.
2a8f20a53ff2 more network documentation
terom
parents: 200
diff changeset
   141
         *
2a8f20a53ff2 more network documentation
terom
parents: 200
diff changeset
   142
         * By default, this just constructs a new NetworkTCPTransport and returns it
2a8f20a53ff2 more network documentation
terom
parents: 200
diff changeset
   143
         *
2a8f20a53ff2 more network documentation
terom
parents: 200
diff changeset
   144
         * @param socket the socket returned by accept()
2a8f20a53ff2 more network documentation
terom
parents: 200
diff changeset
   145
         */
378
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 286
diff changeset
   146
        virtual NetworkTCPTransport* buildTransport (NetworkSocket *socket);
185
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   147
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   148
    public:
286
2a8f20a53ff2 more network documentation
terom
parents: 200
diff changeset
   149
        /**
2a8f20a53ff2 more network documentation
terom
parents: 200
diff changeset
   150
         * A new client has connected, and a NetworkTCPTransport has been connected for it
2a8f20a53ff2 more network documentation
terom
parents: 200
diff changeset
   151
         */
185
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   152
        CL_Signal_v1<NetworkTCPTransport *>& sig_client (void) { return _sig_client; }
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   153
};
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   154
286
2a8f20a53ff2 more network documentation
terom
parents: 200
diff changeset
   155
/**
2a8f20a53ff2 more network documentation
terom
parents: 200
diff changeset
   156
 * A TCP Client that connects to the given address
2a8f20a53ff2 more network documentation
terom
parents: 200
diff changeset
   157
 */ 
185
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   158
class NetworkTCPClient : public NetworkTCPTransport {
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   159
    public:
286
2a8f20a53ff2 more network documentation
terom
parents: 200
diff changeset
   160
        /**
2a8f20a53ff2 more network documentation
terom
parents: 200
diff changeset
   161
         * Create a NetworkTCPTransport, and then connect our socket to the given address.
2a8f20a53ff2 more network documentation
terom
parents: 200
diff changeset
   162
         *
2a8f20a53ff2 more network documentation
terom
parents: 200
diff changeset
   163
         * @param connect_addr the address to connect to
2a8f20a53ff2 more network documentation
terom
parents: 200
diff changeset
   164
         */
381
9b35bc329d23 separate sockaddr stuff out of NetworkAddress... now called NetworkEndpoint
terom
parents: 380
diff changeset
   165
        NetworkTCPClient (const NetworkEndpoint &connect_addr);
185
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   166
};
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   167
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   168
#endif /* NETWORK_TCP_HH */