src/Network/Session.hh
author Tero Marttila <terom@fixme.fi>
Fri, 16 Jan 2009 22:03:49 +0200
changeset 400 d64bf28c4340
parent 381 9b35bc329d23
child 431 c6d7272a164b
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_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
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: 381
diff changeset
     4
/**
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: 381
diff changeset
     5
 * @file
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: 381
diff changeset
     6
 *
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: 381
diff changeset
     7
 * Support for a groups of clients/servers with associated TCP/UDP sockets
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: 381
diff changeset
     8
 */
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: 381
diff changeset
     9
185
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    10
#include <map>
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    11
#include <stdint.h>
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    12
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    13
// forward-declare
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    14
class NetworkSession;
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    15
286
2a8f20a53ff2 more network documentation
terom
parents: 203
diff changeset
    16
/**
2a8f20a53ff2 more network documentation
terom
parents: 203
diff changeset
    17
 * 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
    18
 * on top of a single session.
2a8f20a53ff2 more network documentation
terom
parents: 203
diff changeset
    19
 *
2a8f20a53ff2 more network documentation
terom
parents: 203
diff changeset
    20
 * 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
    21
 */
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    22
typedef uint16_t NetworkChannelID;
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    23
286
2a8f20a53ff2 more network documentation
terom
parents: 203
diff changeset
    24
/**
2a8f20a53ff2 more network documentation
terom
parents: 203
diff changeset
    25
 * Size of a NetworkSession's packet header:
2a8f20a53ff2 more network documentation
terom
parents: 203
diff changeset
    26
 *  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
    27
 */
3ec7ab40755f send initial terrain data to clients using a new NETCHAN_TERRAIN_ARRAY channel using raw BigNetworkPacket
terom
parents: 202
diff changeset
    28
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
    29
186
0738f2949a2b move src/Network% to src/Network/%
terom
parents: 185
diff changeset
    30
#include "TCP.hh"
0738f2949a2b move src/Network% to src/Network/%
terom
parents: 185
diff changeset
    31
#include "UDP.hh"
0738f2949a2b move src/Network% to src/Network/%
terom
parents: 185
diff changeset
    32
#include "Node.hh"
185
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    33
286
2a8f20a53ff2 more network documentation
terom
parents: 203
diff changeset
    34
/**
2a8f20a53ff2 more network documentation
terom
parents: 203
diff changeset
    35
 * A NetworkSession provides TCP/UDP Server and Client functionality, representing remote NetworkSessions with
2a8f20a53ff2 more network documentation
terom
parents: 203
diff changeset
    36
 * NetworkNodes. A NetworkSession can then communicate with its NetworkNodes using TCP or UDP NetworkPackets.
2a8f20a53ff2 more network documentation
terom
parents: 203
diff changeset
    37
 */
185
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    38
class NetworkSession {
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    39
    friend class NetworkNode;
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
    private:
286
2a8f20a53ff2 more network documentation
terom
parents: 203
diff changeset
    42
        /**
2a8f20a53ff2 more network documentation
terom
parents: 203
diff changeset
    43
         * The application's magic ID
2a8f20a53ff2 more network documentation
terom
parents: 203
diff changeset
    44
         */
185
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    45
        uint64_t magic;
286
2a8f20a53ff2 more network documentation
terom
parents: 203
diff changeset
    46
2a8f20a53ff2 more network documentation
terom
parents: 203
diff changeset
    47
        /**
2a8f20a53ff2 more network documentation
terom
parents: 203
diff changeset
    48
         * Our TCP server, if we're in listen() mode
2a8f20a53ff2 more network documentation
terom
parents: 203
diff changeset
    49
         */
185
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    50
        NetworkTCPServer *tcp_srv;
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    51
286
2a8f20a53ff2 more network documentation
terom
parents: 203
diff changeset
    52
        /**
2a8f20a53ff2 more network documentation
terom
parents: 203
diff changeset
    53
         * Our UDP server, if we're in listen() mode
2a8f20a53ff2 more network documentation
terom
parents: 203
diff changeset
    54
         */
2a8f20a53ff2 more network documentation
terom
parents: 203
diff changeset
    55
        NetworkUDP *udp_srv;
2a8f20a53ff2 more network documentation
terom
parents: 203
diff changeset
    56
2a8f20a53ff2 more network documentation
terom
parents: 203
diff changeset
    57
        /**
2a8f20a53ff2 more network documentation
terom
parents: 203
diff changeset
    58
         * Our UDP client, if we're in connect() mode
2a8f20a53ff2 more network documentation
terom
parents: 203
diff changeset
    59
         */
2a8f20a53ff2 more network documentation
terom
parents: 203
diff changeset
    60
        NetworkUDP *udp_client;
2a8f20a53ff2 more network documentation
terom
parents: 203
diff changeset
    61
        
185
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    62
        CL_SlotContainer slots;
286
2a8f20a53ff2 more network documentation
terom
parents: 203
diff changeset
    63
        
2a8f20a53ff2 more network documentation
terom
parents: 203
diff changeset
    64
        /**
2a8f20a53ff2 more network documentation
terom
parents: 203
diff changeset
    65
         * A map of NetworkAddress -> NetworkNode, manipulated when TCP connections are established/broken down,
2a8f20a53ff2 more network documentation
terom
parents: 203
diff changeset
    66
         * and used to map UDP packets to their NetworkNode
2a8f20a53ff2 more network documentation
terom
parents: 203
diff changeset
    67
         */
2a8f20a53ff2 more network documentation
terom
parents: 203
diff changeset
    68
        std::map<NetworkAddress, NetworkNode*> nodes;
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: 203
diff changeset
    70
        /**
2a8f20a53ff2 more network documentation
terom
parents: 203
diff changeset
    71
         * A map of NetworkChannelID -> signal, used to signal our users when we receieve packets
2a8f20a53ff2 more network documentation
terom
parents: 203
diff changeset
    72
         */
200
2dbf40661580 better NetworkBuffer/Packet stuff + some additional Physics+Network stuff + random fixes
terom
parents: 186
diff changeset
    73
        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
    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: 203
diff changeset
    76
        /**
2a8f20a53ff2 more network documentation
terom
parents: 203
diff changeset
    77
         * 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
    78
         * applications apart from each other.
2a8f20a53ff2 more network documentation
terom
parents: 203
diff changeset
    79
         *
2a8f20a53ff2 more network documentation
terom
parents: 203
diff changeset
    80
         * @param magic unique application magic
2a8f20a53ff2 more network documentation
terom
parents: 203
diff changeset
    81
         */
185
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    82
        NetworkSession (uint64_t magic);
286
2a8f20a53ff2 more network documentation
terom
parents: 203
diff changeset
    83
        
2a8f20a53ff2 more network documentation
terom
parents: 203
diff changeset
    84
        /**
2a8f20a53ff2 more network documentation
terom
parents: 203
diff changeset
    85
         * 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
    86
         *
2a8f20a53ff2 more network documentation
terom
parents: 203
diff changeset
    87
         * @param addr local address to listen on
2a8f20a53ff2 more network documentation
terom
parents: 203
diff changeset
    88
         */
381
9b35bc329d23 separate sockaddr stuff out of NetworkAddress... now called NetworkEndpoint
terom
parents: 286
diff changeset
    89
        void listen (const NetworkEndpoint &addr);
185
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    90
286
2a8f20a53ff2 more network documentation
terom
parents: 203
diff changeset
    91
        /**
2a8f20a53ff2 more network documentation
terom
parents: 203
diff changeset
    92
         * 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
    93
         * an UDP socket on the same local address as the TCP connection.
2a8f20a53ff2 more network documentation
terom
parents: 203
diff changeset
    94
         *
2a8f20a53ff2 more network documentation
terom
parents: 203
diff changeset
    95
         * @param addr remote address to connect to
2a8f20a53ff2 more network documentation
terom
parents: 203
diff changeset
    96
         */
381
9b35bc329d23 separate sockaddr stuff out of NetworkAddress... now called NetworkEndpoint
terom
parents: 286
diff changeset
    97
        NetworkNode* connect (const NetworkEndpoint &addr);
185
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    98
    
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    99
    protected:
286
2a8f20a53ff2 more network documentation
terom
parents: 203
diff changeset
   100
        /**
2a8f20a53ff2 more network documentation
terom
parents: 203
diff changeset
   101
         * 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
   102
         * get created. Type tells what kind of node this is.
2a8f20a53ff2 more network documentation
terom
parents: 203
diff changeset
   103
         *
2a8f20a53ff2 more network documentation
terom
parents: 203
diff changeset
   104
         * @param tcp the TCP transport for this node
2a8f20a53ff2 more network documentation
terom
parents: 203
diff changeset
   105
         * @param udp the UDP socket to use for this node
2a8f20a53ff2 more network documentation
terom
parents: 203
diff changeset
   106
         * @param addr the remote address
2a8f20a53ff2 more network documentation
terom
parents: 203
diff changeset
   107
         * @param type the type of node
2a8f20a53ff2 more network documentation
terom
parents: 203
diff changeset
   108
         * @see NetworkNodeType
2a8f20a53ff2 more network documentation
terom
parents: 203
diff changeset
   109
         */
185
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   110
        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
   111
        
286
2a8f20a53ff2 more network documentation
terom
parents: 203
diff changeset
   112
        /**
2a8f20a53ff2 more network documentation
terom
parents: 203
diff changeset
   113
         * 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
   114
         *
2a8f20a53ff2 more network documentation
terom
parents: 203
diff changeset
   115
         * @param node the node that has disconnected
2a8f20a53ff2 more network documentation
terom
parents: 203
diff changeset
   116
         */
185
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   117
        void handle_disconnect (NetworkNode *node);
286
2a8f20a53ff2 more network documentation
terom
parents: 203
diff changeset
   118
2a8f20a53ff2 more network documentation
terom
parents: 203
diff changeset
   119
        /**
2a8f20a53ff2 more network documentation
terom
parents: 203
diff changeset
   120
         * 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
   121
         *
2a8f20a53ff2 more network documentation
terom
parents: 203
diff changeset
   122
         * @param pkt the NetworkPacket that we received
2a8f20a53ff2 more network documentation
terom
parents: 203
diff changeset
   123
         * @param node the node that sent it
2a8f20a53ff2 more network documentation
terom
parents: 203
diff changeset
   124
         */
200
2dbf40661580 better NetworkBuffer/Packet stuff + some additional Physics+Network stuff + random fixes
terom
parents: 186
diff changeset
   125
        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
   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: 203
diff changeset
   128
        /**
2a8f20a53ff2 more network documentation
terom
parents: 203
diff changeset
   129
         * Our tcp_srv has accept()'d a new client.
2a8f20a53ff2 more network documentation
terom
parents: 203
diff changeset
   130
         *
2a8f20a53ff2 more network documentation
terom
parents: 203
diff changeset
   131
         * 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
   132
         */
185
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   133
        void on_tcp_client (NetworkTCPTransport *client);
286
2a8f20a53ff2 more network documentation
terom
parents: 203
diff changeset
   134
2a8f20a53ff2 more network documentation
terom
parents: 203
diff changeset
   135
        /**
2a8f20a53ff2 more network documentation
terom
parents: 203
diff changeset
   136
         * Our udp_srv has recv()'d a NetworkPacket.
2a8f20a53ff2 more network documentation
terom
parents: 203
diff changeset
   137
         *
2a8f20a53ff2 more network documentation
terom
parents: 203
diff changeset
   138
         * Map it to a NetworkNode using our node-map and call handle_message
2a8f20a53ff2 more network documentation
terom
parents: 203
diff changeset
   139
         */
200
2dbf40661580 better NetworkBuffer/Packet stuff + some additional Physics+Network stuff + random fixes
terom
parents: 186
diff changeset
   140
        void on_udp_packet (NetworkPacketInput &pkt, const NetworkAddress &addr);
286
2a8f20a53ff2 more network documentation
terom
parents: 203
diff changeset
   141
        
2a8f20a53ff2 more network documentation
terom
parents: 203
diff changeset
   142
        /**
2a8f20a53ff2 more network documentation
terom
parents: 203
diff changeset
   143
         * New-client signal
2a8f20a53ff2 more network documentation
terom
parents: 203
diff changeset
   144
         */
185
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   145
        CL_Signal_v1<NetworkNode*> _sig_node_connected;
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   146
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   147
    public:
286
2a8f20a53ff2 more network documentation
terom
parents: 203
diff changeset
   148
        /**
2a8f20a53ff2 more network documentation
terom
parents: 203
diff changeset
   149
         * 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
   150
         *
2a8f20a53ff2 more network documentation
terom
parents: 203
diff changeset
   151
         * @param channel_id the NetworkChannelID to use
2a8f20a53ff2 more network documentation
terom
parents: 203
diff changeset
   152
         * @param pkt the NetworkPacket to send
2a8f20a53ff2 more network documentation
terom
parents: 203
diff changeset
   153
         * @param reliable Whether to use TCP or UDP
2a8f20a53ff2 more network documentation
terom
parents: 203
diff changeset
   154
         */
200
2dbf40661580 better NetworkBuffer/Packet stuff + some additional Physics+Network stuff + random fixes
terom
parents: 186
diff changeset
   155
        void send_all (NetworkChannelID channel_id, const NetworkPacketBuffer &pkt, bool reliable = true);
286
2a8f20a53ff2 more network documentation
terom
parents: 203
diff changeset
   156
2a8f20a53ff2 more network documentation
terom
parents: 203
diff changeset
   157
        /**
2a8f20a53ff2 more network documentation
terom
parents: 203
diff changeset
   158
         * 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
   159
         * send_all.
2a8f20a53ff2 more network documentation
terom
parents: 203
diff changeset
   160
         *
2a8f20a53ff2 more network documentation
terom
parents: 203
diff changeset
   161
         * @see send_all
2a8f20a53ff2 more network documentation
terom
parents: 203
diff changeset
   162
         */
200
2dbf40661580 better NetworkBuffer/Packet stuff + some additional Physics+Network stuff + random fixes
terom
parents: 186
diff changeset
   163
        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
   164
286
2a8f20a53ff2 more network documentation
terom
parents: 203
diff changeset
   165
        /**
2a8f20a53ff2 more network documentation
terom
parents: 203
diff changeset
   166
         * A new node has connected to us
2a8f20a53ff2 more network documentation
terom
parents: 203
diff changeset
   167
         */
2a8f20a53ff2 more network documentation
terom
parents: 203
diff changeset
   168
        CL_Signal_v1<NetworkNode*>& sig_node_connected (void) { return _sig_node_connected; }
202
b3f5d766391e support sending of raw packets
terom
parents: 200
diff changeset
   169
286
2a8f20a53ff2 more network documentation
terom
parents: 203
diff changeset
   170
        /**
2a8f20a53ff2 more network documentation
terom
parents: 203
diff changeset
   171
         * We have received a NetworkPacket from a NetworkNode on the given NetworkChannelID
2a8f20a53ff2 more network documentation
terom
parents: 203
diff changeset
   172
         */
200
2dbf40661580 better NetworkBuffer/Packet stuff + some additional Physics+Network stuff + random fixes
terom
parents: 186
diff changeset
   173
        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
   174
};
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   175
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   176
#endif /* NETWORK_SESSION_HH */