src/Network/Session.hh
author terom
Wed, 17 Dec 2008 00:40:22 +0000
changeset 381 9b35bc329d23
parent 286 2a8f20a53ff2
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_SESSION_HH
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
     2
#define NETWORK_SESSION_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 <map>
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
     5
#include <stdint.h>
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
     6
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
     7
// forward-declare
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
     8
class NetworkSession;
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
     9
286
2a8f20a53ff2 more network documentation
terom
parents: 203
diff changeset
    10
/**
2a8f20a53ff2 more network documentation
terom
parents: 203
diff changeset
    11
 * A NetworkSession puts each packet onto a specific channel, which can the be used to run multiple different modules
2a8f20a53ff2 more network documentation
terom
parents: 203
diff changeset
    12
 * on top of a single session.
2a8f20a53ff2 more network documentation
terom
parents: 203
diff changeset
    13
 *
2a8f20a53ff2 more network documentation
terom
parents: 203
diff changeset
    14
 * NetworkChannelID zero is reserved for internal NetworkSession use
185
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    15
 */
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    16
typedef uint16_t NetworkChannelID;
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    17
286
2a8f20a53ff2 more network documentation
terom
parents: 203
diff changeset
    18
/**
2a8f20a53ff2 more network documentation
terom
parents: 203
diff changeset
    19
 * Size of a NetworkSession's packet header:
2a8f20a53ff2 more network documentation
terom
parents: 203
diff changeset
    20
 *  uint16      channel_id
203
3ec7ab40755f send initial terrain data to clients using a new NETCHAN_TERRAIN_ARRAY channel using raw BigNetworkPacket
terom
parents: 202
diff changeset
    21
 */
3ec7ab40755f send initial terrain data to clients using a new NETCHAN_TERRAIN_ARRAY channel using raw BigNetworkPacket
terom
parents: 202
diff changeset
    22
const size_t NETWORK_SESSION_HEADER_SIZE = sizeof(uint16_t);
3ec7ab40755f send initial terrain data to clients using a new NETCHAN_TERRAIN_ARRAY channel using raw BigNetworkPacket
terom
parents: 202
diff changeset
    23
186
0738f2949a2b move src/Network% to src/Network/%
terom
parents: 185
diff changeset
    24
#include "TCP.hh"
0738f2949a2b move src/Network% to src/Network/%
terom
parents: 185
diff changeset
    25
#include "UDP.hh"
0738f2949a2b move src/Network% to src/Network/%
terom
parents: 185
diff changeset
    26
#include "Node.hh"
185
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    27
286
2a8f20a53ff2 more network documentation
terom
parents: 203
diff changeset
    28
/**
2a8f20a53ff2 more network documentation
terom
parents: 203
diff changeset
    29
 * A NetworkSession provides TCP/UDP Server and Client functionality, representing remote NetworkSessions with
2a8f20a53ff2 more network documentation
terom
parents: 203
diff changeset
    30
 * NetworkNodes. A NetworkSession can then communicate with its NetworkNodes using TCP or UDP NetworkPackets.
2a8f20a53ff2 more network documentation
terom
parents: 203
diff changeset
    31
 */
185
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    32
class NetworkSession {
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    33
    friend class NetworkNode;
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    34
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    35
    private:
286
2a8f20a53ff2 more network documentation
terom
parents: 203
diff changeset
    36
        /**
2a8f20a53ff2 more network documentation
terom
parents: 203
diff changeset
    37
         * The application's magic ID
2a8f20a53ff2 more network documentation
terom
parents: 203
diff changeset
    38
         */
185
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    39
        uint64_t magic;
286
2a8f20a53ff2 more network documentation
terom
parents: 203
diff changeset
    40
2a8f20a53ff2 more network documentation
terom
parents: 203
diff changeset
    41
        /**
2a8f20a53ff2 more network documentation
terom
parents: 203
diff changeset
    42
         * Our TCP server, if we're in listen() mode
2a8f20a53ff2 more network documentation
terom
parents: 203
diff changeset
    43
         */
185
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    44
        NetworkTCPServer *tcp_srv;
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    45
286
2a8f20a53ff2 more network documentation
terom
parents: 203
diff changeset
    46
        /**
2a8f20a53ff2 more network documentation
terom
parents: 203
diff changeset
    47
         * Our UDP server, if we're in listen() mode
2a8f20a53ff2 more network documentation
terom
parents: 203
diff changeset
    48
         */
2a8f20a53ff2 more network documentation
terom
parents: 203
diff changeset
    49
        NetworkUDP *udp_srv;
2a8f20a53ff2 more network documentation
terom
parents: 203
diff changeset
    50
2a8f20a53ff2 more network documentation
terom
parents: 203
diff changeset
    51
        /**
2a8f20a53ff2 more network documentation
terom
parents: 203
diff changeset
    52
         * Our UDP client, if we're in connect() mode
2a8f20a53ff2 more network documentation
terom
parents: 203
diff changeset
    53
         */
2a8f20a53ff2 more network documentation
terom
parents: 203
diff changeset
    54
        NetworkUDP *udp_client;
2a8f20a53ff2 more network documentation
terom
parents: 203
diff changeset
    55
        
185
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    56
        CL_SlotContainer slots;
286
2a8f20a53ff2 more network documentation
terom
parents: 203
diff changeset
    57
        
2a8f20a53ff2 more network documentation
terom
parents: 203
diff changeset
    58
        /**
2a8f20a53ff2 more network documentation
terom
parents: 203
diff changeset
    59
         * A map of NetworkAddress -> NetworkNode, manipulated when TCP connections are established/broken down,
2a8f20a53ff2 more network documentation
terom
parents: 203
diff changeset
    60
         * and used to map UDP packets to their NetworkNode
2a8f20a53ff2 more network documentation
terom
parents: 203
diff changeset
    61
         */
2a8f20a53ff2 more network documentation
terom
parents: 203
diff changeset
    62
        std::map<NetworkAddress, NetworkNode*> nodes;
185
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    63
286
2a8f20a53ff2 more network documentation
terom
parents: 203
diff changeset
    64
        /**
2a8f20a53ff2 more network documentation
terom
parents: 203
diff changeset
    65
         * A map of NetworkChannelID -> signal, used to signal our users when we receieve packets
2a8f20a53ff2 more network documentation
terom
parents: 203
diff changeset
    66
         */
200
2dbf40661580 better NetworkBuffer/Packet stuff + some additional Physics+Network stuff + random fixes
terom
parents: 186
diff changeset
    67
        std::map<NetworkChannelID, CL_Signal_v2<NetworkPacketInput&, NetworkNode *> > _map_sig_chan_message;
185
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    68
    
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    69
    public:
286
2a8f20a53ff2 more network documentation
terom
parents: 203
diff changeset
    70
        /**
2a8f20a53ff2 more network documentation
terom
parents: 203
diff changeset
    71
         * Construct an idle NetworkSession using the given application magic, which should be unique to tell different
2a8f20a53ff2 more network documentation
terom
parents: 203
diff changeset
    72
         * applications apart from each other.
2a8f20a53ff2 more network documentation
terom
parents: 203
diff changeset
    73
         *
2a8f20a53ff2 more network documentation
terom
parents: 203
diff changeset
    74
         * @param magic unique application magic
2a8f20a53ff2 more network documentation
terom
parents: 203
diff changeset
    75
         */
185
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    76
        NetworkSession (uint64_t magic);
286
2a8f20a53ff2 more network documentation
terom
parents: 203
diff changeset
    77
        
2a8f20a53ff2 more network documentation
terom
parents: 203
diff changeset
    78
        /**
2a8f20a53ff2 more network documentation
terom
parents: 203
diff changeset
    79
         * Have the NetworkSession enter server mode, listening on the given address using both TCP and UDP
2a8f20a53ff2 more network documentation
terom
parents: 203
diff changeset
    80
         *
2a8f20a53ff2 more network documentation
terom
parents: 203
diff changeset
    81
         * @param addr local address to listen on
2a8f20a53ff2 more network documentation
terom
parents: 203
diff changeset
    82
         */
381
9b35bc329d23 separate sockaddr stuff out of NetworkAddress... now called NetworkEndpoint
terom
parents: 286
diff changeset
    83
        void listen (const NetworkEndpoint &addr);
185
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    84
286
2a8f20a53ff2 more network documentation
terom
parents: 203
diff changeset
    85
        /**
2a8f20a53ff2 more network documentation
terom
parents: 203
diff changeset
    86
         * Have the NetworkSession enter client mode, establishing a TCP connection to the server, and then allocating
2a8f20a53ff2 more network documentation
terom
parents: 203
diff changeset
    87
         * an UDP socket on the same local address as the TCP connection.
2a8f20a53ff2 more network documentation
terom
parents: 203
diff changeset
    88
         *
2a8f20a53ff2 more network documentation
terom
parents: 203
diff changeset
    89
         * @param addr remote address to connect to
2a8f20a53ff2 more network documentation
terom
parents: 203
diff changeset
    90
         */
381
9b35bc329d23 separate sockaddr stuff out of NetworkAddress... now called NetworkEndpoint
terom
parents: 286
diff changeset
    91
        NetworkNode* connect (const NetworkEndpoint &addr);
185
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    92
    
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    93
    protected:
286
2a8f20a53ff2 more network documentation
terom
parents: 203
diff changeset
    94
        /**
2a8f20a53ff2 more network documentation
terom
parents: 203
diff changeset
    95
         * Used to build a new NetworkNode by connect/on_tcp_client. Can be used to override what kind of NetworkNodes
2a8f20a53ff2 more network documentation
terom
parents: 203
diff changeset
    96
         * get created. Type tells what kind of node this is.
2a8f20a53ff2 more network documentation
terom
parents: 203
diff changeset
    97
         *
2a8f20a53ff2 more network documentation
terom
parents: 203
diff changeset
    98
         * @param tcp the TCP transport for this node
2a8f20a53ff2 more network documentation
terom
parents: 203
diff changeset
    99
         * @param udp the UDP socket to use for this node
2a8f20a53ff2 more network documentation
terom
parents: 203
diff changeset
   100
         * @param addr the remote address
2a8f20a53ff2 more network documentation
terom
parents: 203
diff changeset
   101
         * @param type the type of node
2a8f20a53ff2 more network documentation
terom
parents: 203
diff changeset
   102
         * @see NetworkNodeType
2a8f20a53ff2 more network documentation
terom
parents: 203
diff changeset
   103
         */
185
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   104
        virtual NetworkNode *build_node (NetworkTCPTransport *tcp, NetworkUDP *udp, const NetworkAddress &addr, enum NetworkNodeType type);
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   105
        
286
2a8f20a53ff2 more network documentation
terom
parents: 203
diff changeset
   106
        /**
2a8f20a53ff2 more network documentation
terom
parents: 203
diff changeset
   107
         * A NetworkNode's TCP connection has failed. Removes the node from our nodes-map (using node->getRemoteAddress)
2a8f20a53ff2 more network documentation
terom
parents: 203
diff changeset
   108
         *
2a8f20a53ff2 more network documentation
terom
parents: 203
diff changeset
   109
         * @param node the node that has disconnected
2a8f20a53ff2 more network documentation
terom
parents: 203
diff changeset
   110
         */
185
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   111
        void handle_disconnect (NetworkNode *node);
286
2a8f20a53ff2 more network documentation
terom
parents: 203
diff changeset
   112
2a8f20a53ff2 more network documentation
terom
parents: 203
diff changeset
   113
        /**
2a8f20a53ff2 more network documentation
terom
parents: 203
diff changeset
   114
         * We have received a NetworkPacket from the given node (either TCP or UDP, we don't know)
2a8f20a53ff2 more network documentation
terom
parents: 203
diff changeset
   115
         *
2a8f20a53ff2 more network documentation
terom
parents: 203
diff changeset
   116
         * @param pkt the NetworkPacket that we received
2a8f20a53ff2 more network documentation
terom
parents: 203
diff changeset
   117
         * @param node the node that sent it
2a8f20a53ff2 more network documentation
terom
parents: 203
diff changeset
   118
         */
200
2dbf40661580 better NetworkBuffer/Packet stuff + some additional Physics+Network stuff + random fixes
terom
parents: 186
diff changeset
   119
        void handle_message (NetworkPacketInput &pkt, NetworkNode *node);
185
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
    private:
286
2a8f20a53ff2 more network documentation
terom
parents: 203
diff changeset
   122
        /**
2a8f20a53ff2 more network documentation
terom
parents: 203
diff changeset
   123
         * Our tcp_srv has accept()'d a new client.
2a8f20a53ff2 more network documentation
terom
parents: 203
diff changeset
   124
         *
2a8f20a53ff2 more network documentation
terom
parents: 203
diff changeset
   125
         * Create a NetworkNode using build_node and udp_srv, add it to our node-map, and trigger sig_node_connected
2a8f20a53ff2 more network documentation
terom
parents: 203
diff changeset
   126
         */
185
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   127
        void on_tcp_client (NetworkTCPTransport *client);
286
2a8f20a53ff2 more network documentation
terom
parents: 203
diff changeset
   128
2a8f20a53ff2 more network documentation
terom
parents: 203
diff changeset
   129
        /**
2a8f20a53ff2 more network documentation
terom
parents: 203
diff changeset
   130
         * Our udp_srv has recv()'d a NetworkPacket.
2a8f20a53ff2 more network documentation
terom
parents: 203
diff changeset
   131
         *
2a8f20a53ff2 more network documentation
terom
parents: 203
diff changeset
   132
         * Map it to a NetworkNode using our node-map and call handle_message
2a8f20a53ff2 more network documentation
terom
parents: 203
diff changeset
   133
         */
200
2dbf40661580 better NetworkBuffer/Packet stuff + some additional Physics+Network stuff + random fixes
terom
parents: 186
diff changeset
   134
        void on_udp_packet (NetworkPacketInput &pkt, const NetworkAddress &addr);
286
2a8f20a53ff2 more network documentation
terom
parents: 203
diff changeset
   135
        
2a8f20a53ff2 more network documentation
terom
parents: 203
diff changeset
   136
        /**
2a8f20a53ff2 more network documentation
terom
parents: 203
diff changeset
   137
         * New-client signal
2a8f20a53ff2 more network documentation
terom
parents: 203
diff changeset
   138
         */
185
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   139
        CL_Signal_v1<NetworkNode*> _sig_node_connected;
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   140
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   141
    public:
286
2a8f20a53ff2 more network documentation
terom
parents: 203
diff changeset
   142
        /**
2a8f20a53ff2 more network documentation
terom
parents: 203
diff changeset
   143
         * Send the given NetworkPacket to all our nodes using the given NetworkChannelID, using TCP if reliable, UDP otherwise.
2a8f20a53ff2 more network documentation
terom
parents: 203
diff changeset
   144
         *
2a8f20a53ff2 more network documentation
terom
parents: 203
diff changeset
   145
         * @param channel_id the NetworkChannelID to use
2a8f20a53ff2 more network documentation
terom
parents: 203
diff changeset
   146
         * @param pkt the NetworkPacket to send
2a8f20a53ff2 more network documentation
terom
parents: 203
diff changeset
   147
         * @param reliable Whether to use TCP or UDP
2a8f20a53ff2 more network documentation
terom
parents: 203
diff changeset
   148
         */
200
2dbf40661580 better NetworkBuffer/Packet stuff + some additional Physics+Network stuff + random fixes
terom
parents: 186
diff changeset
   149
        void send_all (NetworkChannelID channel_id, const NetworkPacketBuffer &pkt, bool reliable = true);
286
2a8f20a53ff2 more network documentation
terom
parents: 203
diff changeset
   150
2a8f20a53ff2 more network documentation
terom
parents: 203
diff changeset
   151
        /**
2a8f20a53ff2 more network documentation
terom
parents: 203
diff changeset
   152
         * Like send_all, but do not send the packet to the specified node. If node is NULL, this behaves like
2a8f20a53ff2 more network documentation
terom
parents: 203
diff changeset
   153
         * send_all.
2a8f20a53ff2 more network documentation
terom
parents: 203
diff changeset
   154
         *
2a8f20a53ff2 more network documentation
terom
parents: 203
diff changeset
   155
         * @see send_all
2a8f20a53ff2 more network documentation
terom
parents: 203
diff changeset
   156
         */
200
2dbf40661580 better NetworkBuffer/Packet stuff + some additional Physics+Network stuff + random fixes
terom
parents: 186
diff changeset
   157
        void send_all_except (NetworkChannelID channel_id, const NetworkPacketBuffer &pkt, const NetworkNode *node, bool reliable = true);
185
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   158
286
2a8f20a53ff2 more network documentation
terom
parents: 203
diff changeset
   159
        /**
2a8f20a53ff2 more network documentation
terom
parents: 203
diff changeset
   160
         * A new node has connected to us
2a8f20a53ff2 more network documentation
terom
parents: 203
diff changeset
   161
         */
2a8f20a53ff2 more network documentation
terom
parents: 203
diff changeset
   162
        CL_Signal_v1<NetworkNode*>& sig_node_connected (void) { return _sig_node_connected; }
202
b3f5d766391e support sending of raw packets
terom
parents: 200
diff changeset
   163
286
2a8f20a53ff2 more network documentation
terom
parents: 203
diff changeset
   164
        /**
2a8f20a53ff2 more network documentation
terom
parents: 203
diff changeset
   165
         * We have received a NetworkPacket from a NetworkNode on the given NetworkChannelID
2a8f20a53ff2 more network documentation
terom
parents: 203
diff changeset
   166
         */
200
2dbf40661580 better NetworkBuffer/Packet stuff + some additional Physics+Network stuff + random fixes
terom
parents: 186
diff changeset
   167
        CL_Signal_v2<NetworkPacketInput&, NetworkNode *>& sig_chan_message (NetworkChannelID cid) { return _map_sig_chan_message[cid]; }
185
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   168
};
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   169
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   170
#endif /* NETWORK_SESSION_HH */