src/Network/UDP.hh
author terom
Wed, 17 Dec 2008 00:40:22 +0000
changeset 381 9b35bc329d23
parent 286 2a8f20a53ff2
child 400 d64bf28c4340
permissions -rw-r--r--
separate sockaddr stuff out of NetworkAddress... now called NetworkEndpoint
#ifndef NETWORK_UDP_HH
#define NETWORK_UDP_HH

#include "Socket.hh"
#include "Address.hh"
#include "Packet.hh"

/**
 * A UDP socket that can send and receive NetworkPackets
 */
class NetworkUDP {
    private:
        /**
         * The socket itself
         */
        NetworkSocket socket;

        CL_SlotContainer slots;

    public:
        /**
         * Allocate an UDP socket on an ephemeral port (random port chosen by OS)
         */
        NetworkUDP (void);

        /**
         * Allocate an UDP socket on a specific local port/interface
         *
         * @param bind_addr the local address to bind to
         */
        NetworkUDP (const NetworkEndpoint &bind_addr);

    private:
        /**
         * Socket is ready for recv, read as many packets as available, calling sig_packet on each
         */
        void on_recv (void);
        
        /**
         * The signal called by on_recv
         */
        CL_Signal_v2<NetworkPacketInput&, const NetworkAddress&> _sig_packet;

    public:
        /**
         * Send the given packet on this UDP socket to the given destination address
         */
        bool sendto (const NetworkPacketBuffer &packet, const NetworkAddress &dst);
        
        /**
         * Triggered whenever a packet has been received, giving the NetworkPacketInput and the source address
         */
        CL_Signal_v2<NetworkPacketInput&, const NetworkAddress&>& sig_packet (void) { return _sig_packet; }
};

#endif /* NETWORK_UDP_HH */