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