src/Network/Node.hh
author Tero Marttila <terom@fixme.fi>
Fri, 16 Jan 2009 22:03:49 +0200
changeset 400 d64bf28c4340
parent 286 2a8f20a53ff2
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
#ifndef NETWORK_NODE_HH
#define NETWORK_NODE_HH

/**
 * @file
 *
 * Remote NetworkSessions
 */

// forward-declare
class NetworkNode;

/**
 * Used to differentiate between different kinds of nodes in NetworkSession::build_node
 *
 * @see NetworkSession::build_node
 */
enum NetworkNodeType {
    NETWORK_NODE_SERVER_CLIENT,     //<<< A server's client
    NETWORK_NODE_CLIENT_SERVER      //<<< A client's server
};

#include "TCP.hh"
#include "UDP.hh"
#include "Session.hh"

/**
 * A NetworkNode represents a remote NetworkSession connected to our NetworkSession.
 *
 * A NetworkNode has a tcp connection, plus an udp socket to us (either NetworkSession's udp_srv or udp_client)
 */
class NetworkNode {
    private:
        /**
         * Our local NetworkSession
         */
        NetworkSession &session;

        /**
         * The TCP connection
         */
        NetworkTCPTransport *tcp;

        /**
         * Our NetworkSession's UDP socket that we should use to send UDP packets to this node
         */
        NetworkUDP *udp;

        /**
         * This address that the node connected from using TCP, used to associate received UDP packets with this node
         */
        const NetworkAddress address;

        CL_SlotContainer slots;
    
    public:
        /**
         * Construct a new NetworkNode from the given tcp/udp sockets and address
         *
         * @param session our NetworkSession
         * @param tcp this node's TCP socket
         * @param udp the UDP socket to use for outgoing packets
         * @param address the remote address
         */
        NetworkNode (NetworkSession &session, NetworkTCPTransport *tcp, NetworkUDP *udp, const NetworkAddress &address);
        
    private:
        /**
         * Node should not be copied
         */
        NetworkNode (const NetworkNode &copy);
        ~NetworkNode (void);
        NetworkNode& operator= (const NetworkNode &copy);
        
        /**
         * Our TCP socket indicates disconnect, tell NetworkSession and trigger sig_disconnect
         */
        void on_disconnect (void);
         
        /**
         * Our disconnect signal
         */
        CL_Signal_v0 _sig_disconnected;

    public:
        /**
         * Write to appropriate NetworkSession header to the given packet. Can be used to prepare packets for use with
         * send_raw. The size of the header is equal to NETWORK_SESSION_HEADER_SIZE
         *
         * @param pkt the packet to write the header to
         * @param channel_id the NetworkChannelID to use
         *
         * @see send_raw
         * @see NETWORK_SESSION_HEADER_SIZE
         */
        void write_packet_header (NetworkPacketOutput &pkt, NetworkChannelID channel_id);
        
        /**
         * Send a raw packet prepared using write_packet_header. This does not need to copy the packet data around.
         *
         * @param pkt the NetworkPacket prepared using write_packet_header
         * @param reliable Whether to use TCP or UDP
         *
         * @see write_packet_header
         */
        void send_raw (const NetworkPacketBuffer &pkt, bool reliable = true);

        /**
         * Send the given packet to this node on the given channel.
         *
         * Note that this constructs a new *NetworkPacket* containing our header and the given packet, so there
         * given packet must be small enough to fit.
         *
         * @param channel_id the NetworkChannelID to use
         * @param pkt the NetworkPacket to send on the given channel
         * @param reliable Whether to use TCP or UDP
         */
        void send (NetworkChannelID channel_id, const NetworkPacketBuffer &pkt, bool reliable = true);
        
        /**
         * Get this node's remote address (both TCP and UDP).
         *
         * @return NetworkAddress the remote address
         */
        const NetworkAddress& getRemoteAddress (void);
        
        /**
         * This node's TCP connection was lost, and the node has been removed from the NetworkSession's nodes list.
         *
         * Once this completes, the node will be destructed
         */
        CL_Signal_v0& sig_disconnected (void) { return _sig_disconnected; }
};

#endif /* NETWORK_NODE_HH */