src/Network/Node.hh
author Tero Marttila <terom@fixme.fi>
Fri, 16 Jan 2009 21:24:45 +0200
changeset 399 c7295b72731a
parent 286 2a8f20a53ff2
child 400 d64bf28c4340
permissions -rw-r--r--
documentation work on Network
185
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
     1
#ifndef NETWORK_NODE_HH
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
     2
#define NETWORK_NODE_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
// forward-declare
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
     5
class NetworkNode;
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
     6
286
2a8f20a53ff2 more network documentation
terom
parents: 202
diff changeset
     7
/**
2a8f20a53ff2 more network documentation
terom
parents: 202
diff changeset
     8
 * Used to differentiate between different kinds of nodes in NetworkSession::build_node
2a8f20a53ff2 more network documentation
terom
parents: 202
diff changeset
     9
 *
2a8f20a53ff2 more network documentation
terom
parents: 202
diff changeset
    10
 * @see NetworkSession::build_node
2a8f20a53ff2 more network documentation
terom
parents: 202
diff changeset
    11
 */
185
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    12
enum NetworkNodeType {
286
2a8f20a53ff2 more network documentation
terom
parents: 202
diff changeset
    13
    NETWORK_NODE_SERVER_CLIENT,     //<<< A server's client
2a8f20a53ff2 more network documentation
terom
parents: 202
diff changeset
    14
    NETWORK_NODE_CLIENT_SERVER      //<<< A client's server
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
186
0738f2949a2b move src/Network% to src/Network/%
terom
parents: 185
diff changeset
    17
#include "TCP.hh"
0738f2949a2b move src/Network% to src/Network/%
terom
parents: 185
diff changeset
    18
#include "UDP.hh"
0738f2949a2b move src/Network% to src/Network/%
terom
parents: 185
diff changeset
    19
#include "Session.hh"
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: 202
diff changeset
    21
/**
2a8f20a53ff2 more network documentation
terom
parents: 202
diff changeset
    22
 * A NetworkNode represents a remote NetworkSession connected to our NetworkSession.
2a8f20a53ff2 more network documentation
terom
parents: 202
diff changeset
    23
 *
2a8f20a53ff2 more network documentation
terom
parents: 202
diff changeset
    24
 * A NetworkNode has a tcp connection, plus an udp socket to us (either NetworkSession's udp_srv or udp_client)
2a8f20a53ff2 more network documentation
terom
parents: 202
diff changeset
    25
 */
185
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    26
class NetworkNode {
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    27
    private:
286
2a8f20a53ff2 more network documentation
terom
parents: 202
diff changeset
    28
        /**
2a8f20a53ff2 more network documentation
terom
parents: 202
diff changeset
    29
         * Our local NetworkSession
2a8f20a53ff2 more network documentation
terom
parents: 202
diff changeset
    30
         */
185
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    31
        NetworkSession &session;
286
2a8f20a53ff2 more network documentation
terom
parents: 202
diff changeset
    32
2a8f20a53ff2 more network documentation
terom
parents: 202
diff changeset
    33
        /**
2a8f20a53ff2 more network documentation
terom
parents: 202
diff changeset
    34
         * The TCP connection
2a8f20a53ff2 more network documentation
terom
parents: 202
diff changeset
    35
         */
185
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    36
        NetworkTCPTransport *tcp;
286
2a8f20a53ff2 more network documentation
terom
parents: 202
diff changeset
    37
2a8f20a53ff2 more network documentation
terom
parents: 202
diff changeset
    38
        /**
2a8f20a53ff2 more network documentation
terom
parents: 202
diff changeset
    39
         * Our NetworkSession's UDP socket that we should use to send UDP packets to this node
2a8f20a53ff2 more network documentation
terom
parents: 202
diff changeset
    40
         */
185
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    41
        NetworkUDP *udp;
286
2a8f20a53ff2 more network documentation
terom
parents: 202
diff changeset
    42
2a8f20a53ff2 more network documentation
terom
parents: 202
diff changeset
    43
        /**
2a8f20a53ff2 more network documentation
terom
parents: 202
diff changeset
    44
         * This address that the node connected from using TCP, used to associate received UDP packets with this node
2a8f20a53ff2 more network documentation
terom
parents: 202
diff changeset
    45
         */
185
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    46
        const NetworkAddress address;
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    47
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    48
        CL_SlotContainer slots;
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    49
    
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    50
    public:
286
2a8f20a53ff2 more network documentation
terom
parents: 202
diff changeset
    51
        /**
2a8f20a53ff2 more network documentation
terom
parents: 202
diff changeset
    52
         * Construct a new NetworkNode from the given tcp/udp sockets and address
2a8f20a53ff2 more network documentation
terom
parents: 202
diff changeset
    53
         *
2a8f20a53ff2 more network documentation
terom
parents: 202
diff changeset
    54
         * @param session our NetworkSession
2a8f20a53ff2 more network documentation
terom
parents: 202
diff changeset
    55
         * @param tcp this node's TCP socket
2a8f20a53ff2 more network documentation
terom
parents: 202
diff changeset
    56
         * @param udp the UDP socket to use for outgoing packets
2a8f20a53ff2 more network documentation
terom
parents: 202
diff changeset
    57
         * @param address the remote address
2a8f20a53ff2 more network documentation
terom
parents: 202
diff changeset
    58
         */
185
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    59
        NetworkNode (NetworkSession &session, NetworkTCPTransport *tcp, NetworkUDP *udp, const NetworkAddress &address);
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    60
        
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    61
    private:
286
2a8f20a53ff2 more network documentation
terom
parents: 202
diff changeset
    62
        /**
2a8f20a53ff2 more network documentation
terom
parents: 202
diff changeset
    63
         * Node should not be copied
2a8f20a53ff2 more network documentation
terom
parents: 202
diff changeset
    64
         */
185
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    65
        NetworkNode (const NetworkNode &copy);
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    66
        ~NetworkNode (void);
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    67
        NetworkNode& operator= (const NetworkNode &copy);
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    68
        
286
2a8f20a53ff2 more network documentation
terom
parents: 202
diff changeset
    69
        /**
2a8f20a53ff2 more network documentation
terom
parents: 202
diff changeset
    70
         * Our TCP socket indicates disconnect, tell NetworkSession and trigger sig_disconnect
2a8f20a53ff2 more network documentation
terom
parents: 202
diff changeset
    71
         */
185
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    72
        void on_disconnect (void);
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    73
         
286
2a8f20a53ff2 more network documentation
terom
parents: 202
diff changeset
    74
        /**
2a8f20a53ff2 more network documentation
terom
parents: 202
diff changeset
    75
         * Our disconnect signal
2a8f20a53ff2 more network documentation
terom
parents: 202
diff changeset
    76
         */
185
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    77
        CL_Signal_v0 _sig_disconnected;
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    78
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    79
    public:
286
2a8f20a53ff2 more network documentation
terom
parents: 202
diff changeset
    80
        /**
2a8f20a53ff2 more network documentation
terom
parents: 202
diff changeset
    81
         * Write to appropriate NetworkSession header to the given packet. Can be used to prepare packets for use with
2a8f20a53ff2 more network documentation
terom
parents: 202
diff changeset
    82
         * send_raw. The size of the header is equal to NETWORK_SESSION_HEADER_SIZE
2a8f20a53ff2 more network documentation
terom
parents: 202
diff changeset
    83
         *
2a8f20a53ff2 more network documentation
terom
parents: 202
diff changeset
    84
         * @param pkt the packet to write the header to
2a8f20a53ff2 more network documentation
terom
parents: 202
diff changeset
    85
         * @param channel_id the NetworkChannelID to use
2a8f20a53ff2 more network documentation
terom
parents: 202
diff changeset
    86
         *
2a8f20a53ff2 more network documentation
terom
parents: 202
diff changeset
    87
         * @see send_raw
2a8f20a53ff2 more network documentation
terom
parents: 202
diff changeset
    88
         * @see NETWORK_SESSION_HEADER_SIZE
2a8f20a53ff2 more network documentation
terom
parents: 202
diff changeset
    89
         */
202
b3f5d766391e support sending of raw packets
terom
parents: 200
diff changeset
    90
        void write_packet_header (NetworkPacketOutput &pkt, NetworkChannelID channel_id);
286
2a8f20a53ff2 more network documentation
terom
parents: 202
diff changeset
    91
        
2a8f20a53ff2 more network documentation
terom
parents: 202
diff changeset
    92
        /**
2a8f20a53ff2 more network documentation
terom
parents: 202
diff changeset
    93
         * Send a raw packet prepared using write_packet_header. This does not need to copy the packet data around.
2a8f20a53ff2 more network documentation
terom
parents: 202
diff changeset
    94
         *
2a8f20a53ff2 more network documentation
terom
parents: 202
diff changeset
    95
         * @param pkt the NetworkPacket prepared using write_packet_header
2a8f20a53ff2 more network documentation
terom
parents: 202
diff changeset
    96
         * @param reliable Whether to use TCP or UDP
2a8f20a53ff2 more network documentation
terom
parents: 202
diff changeset
    97
         *
2a8f20a53ff2 more network documentation
terom
parents: 202
diff changeset
    98
         * @see write_packet_header
2a8f20a53ff2 more network documentation
terom
parents: 202
diff changeset
    99
         */
2a8f20a53ff2 more network documentation
terom
parents: 202
diff changeset
   100
        void send_raw (const NetworkPacketBuffer &pkt, bool reliable = true);
202
b3f5d766391e support sending of raw packets
terom
parents: 200
diff changeset
   101
286
2a8f20a53ff2 more network documentation
terom
parents: 202
diff changeset
   102
        /**
2a8f20a53ff2 more network documentation
terom
parents: 202
diff changeset
   103
         * Send the given packet to this node on the given channel.
2a8f20a53ff2 more network documentation
terom
parents: 202
diff changeset
   104
         *
2a8f20a53ff2 more network documentation
terom
parents: 202
diff changeset
   105
         * Note that this constructs a new *NetworkPacket* containing our header and the given packet, so there
2a8f20a53ff2 more network documentation
terom
parents: 202
diff changeset
   106
         * given packet must be small enough to fit.
2a8f20a53ff2 more network documentation
terom
parents: 202
diff changeset
   107
         *
2a8f20a53ff2 more network documentation
terom
parents: 202
diff changeset
   108
         * @param channel_id the NetworkChannelID to use
2a8f20a53ff2 more network documentation
terom
parents: 202
diff changeset
   109
         * @param pkt the NetworkPacket to send on the given channel
2a8f20a53ff2 more network documentation
terom
parents: 202
diff changeset
   110
         * @param reliable Whether to use TCP or UDP
2a8f20a53ff2 more network documentation
terom
parents: 202
diff changeset
   111
         */
200
2dbf40661580 better NetworkBuffer/Packet stuff + some additional Physics+Network stuff + random fixes
terom
parents: 186
diff changeset
   112
        void send (NetworkChannelID channel_id, const NetworkPacketBuffer &pkt, bool reliable = true);
286
2a8f20a53ff2 more network documentation
terom
parents: 202
diff changeset
   113
        
2a8f20a53ff2 more network documentation
terom
parents: 202
diff changeset
   114
        /**
2a8f20a53ff2 more network documentation
terom
parents: 202
diff changeset
   115
         * Get this node's remote address (both TCP and UDP).
2a8f20a53ff2 more network documentation
terom
parents: 202
diff changeset
   116
         *
2a8f20a53ff2 more network documentation
terom
parents: 202
diff changeset
   117
         * @return NetworkAddress the remote address
2a8f20a53ff2 more network documentation
terom
parents: 202
diff changeset
   118
         */
185
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   119
        const NetworkAddress& getRemoteAddress (void);
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   120
        
286
2a8f20a53ff2 more network documentation
terom
parents: 202
diff changeset
   121
        /**
2a8f20a53ff2 more network documentation
terom
parents: 202
diff changeset
   122
         * This node's TCP connection was lost, and the node has been removed from the NetworkSession's nodes list.
2a8f20a53ff2 more network documentation
terom
parents: 202
diff changeset
   123
         *
2a8f20a53ff2 more network documentation
terom
parents: 202
diff changeset
   124
         * Once this completes, the node will be destructed
2a8f20a53ff2 more network documentation
terom
parents: 202
diff changeset
   125
         */
185
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   126
        CL_Signal_v0& sig_disconnected (void) { return _sig_disconnected; }
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   127
};
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   128
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   129
#endif /* NETWORK_NODE_HH */