src/Network/UDP.hh
author Tero Marttila <terom@fixme.fi>
Fri, 16 Jan 2009 22:03:49 +0200
changeset 400 d64bf28c4340
parent 381 9b35bc329d23
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_UDP_HH
#define NETWORK_UDP_HH

/**
 * @file
 *
 * UDP implementation
 */

#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
         *
         * @return true if the packet was passed on to the socket API, false if the socket was busy and the packet was
         * dropped (no chance of it arriving).
         */
        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 */