terom@185: #ifndef NETWORK_NODE_HH terom@185: #define NETWORK_NODE_HH terom@185: terom@400: /** terom@400: * @file terom@400: * terom@400: * Remote NetworkSessions terom@400: */ terom@400: terom@185: // forward-declare terom@185: class NetworkNode; terom@185: terom@286: /** terom@286: * Used to differentiate between different kinds of nodes in NetworkSession::build_node terom@286: * terom@286: * @see NetworkSession::build_node terom@286: */ terom@185: enum NetworkNodeType { terom@286: NETWORK_NODE_SERVER_CLIENT, //<<< A server's client terom@286: NETWORK_NODE_CLIENT_SERVER //<<< A client's server terom@185: }; terom@185: terom@186: #include "TCP.hh" terom@186: #include "UDP.hh" terom@186: #include "Session.hh" terom@185: terom@286: /** terom@286: * A NetworkNode represents a remote NetworkSession connected to our NetworkSession. terom@286: * terom@286: * A NetworkNode has a tcp connection, plus an udp socket to us (either NetworkSession's udp_srv or udp_client) terom@286: */ terom@185: class NetworkNode { terom@185: private: terom@286: /** terom@286: * Our local NetworkSession terom@286: */ terom@185: NetworkSession &session; terom@286: terom@286: /** terom@286: * The TCP connection terom@286: */ terom@185: NetworkTCPTransport *tcp; terom@286: terom@286: /** terom@286: * Our NetworkSession's UDP socket that we should use to send UDP packets to this node terom@286: */ terom@185: NetworkUDP *udp; terom@286: terom@286: /** terom@286: * This address that the node connected from using TCP, used to associate received UDP packets with this node terom@286: */ terom@185: const NetworkAddress address; terom@185: terom@185: CL_SlotContainer slots; terom@185: terom@185: public: terom@286: /** terom@286: * Construct a new NetworkNode from the given tcp/udp sockets and address terom@286: * terom@286: * @param session our NetworkSession terom@286: * @param tcp this node's TCP socket terom@286: * @param udp the UDP socket to use for outgoing packets terom@286: * @param address the remote address terom@286: */ terom@185: NetworkNode (NetworkSession &session, NetworkTCPTransport *tcp, NetworkUDP *udp, const NetworkAddress &address); terom@185: terom@185: private: terom@286: /** terom@286: * Node should not be copied terom@286: */ terom@185: NetworkNode (const NetworkNode ©); terom@185: ~NetworkNode (void); terom@185: NetworkNode& operator= (const NetworkNode ©); terom@185: terom@286: /** terom@286: * Our TCP socket indicates disconnect, tell NetworkSession and trigger sig_disconnect terom@286: */ terom@185: void on_disconnect (void); terom@185: terom@286: /** terom@286: * Our disconnect signal terom@286: */ terom@185: CL_Signal_v0 _sig_disconnected; terom@185: terom@185: public: terom@286: /** terom@286: * Write to appropriate NetworkSession header to the given packet. Can be used to prepare packets for use with terom@286: * send_raw. The size of the header is equal to NETWORK_SESSION_HEADER_SIZE terom@286: * terom@286: * @param pkt the packet to write the header to terom@286: * @param channel_id the NetworkChannelID to use terom@286: * terom@286: * @see send_raw terom@286: * @see NETWORK_SESSION_HEADER_SIZE terom@286: */ terom@202: void write_packet_header (NetworkPacketOutput &pkt, NetworkChannelID channel_id); terom@286: terom@286: /** terom@286: * Send a raw packet prepared using write_packet_header. This does not need to copy the packet data around. terom@286: * terom@286: * @param pkt the NetworkPacket prepared using write_packet_header terom@286: * @param reliable Whether to use TCP or UDP terom@286: * terom@286: * @see write_packet_header terom@286: */ terom@286: void send_raw (const NetworkPacketBuffer &pkt, bool reliable = true); terom@202: terom@286: /** terom@286: * Send the given packet to this node on the given channel. terom@286: * terom@286: * Note that this constructs a new *NetworkPacket* containing our header and the given packet, so there terom@286: * given packet must be small enough to fit. terom@286: * terom@286: * @param channel_id the NetworkChannelID to use terom@286: * @param pkt the NetworkPacket to send on the given channel terom@286: * @param reliable Whether to use TCP or UDP terom@286: */ terom@200: void send (NetworkChannelID channel_id, const NetworkPacketBuffer &pkt, bool reliable = true); terom@286: terom@286: /** terom@286: * Get this node's remote address (both TCP and UDP). terom@286: * terom@286: * @return NetworkAddress the remote address terom@286: */ terom@185: const NetworkAddress& getRemoteAddress (void); terom@185: terom@286: /** terom@286: * This node's TCP connection was lost, and the node has been removed from the NetworkSession's nodes list. terom@286: * terom@286: * Once this completes, the node will be destructed terom@286: */ terom@185: CL_Signal_v0& sig_disconnected (void) { return _sig_disconnected; } terom@185: }; terom@185: terom@185: #endif /* NETWORK_NODE_HH */