SockIt
Public Member Functions | Protected Member Functions | Private Member Functions | Private Attributes | Friends

UdpServer Class Reference

#include <UdpServer.h>

Inheritance diagram for UdpServer:
Udp Server NetworkObject

List of all members.

Public Member Functions

 UdpServer (int port, boost::asio::io_service &io_service)
 UdpServer (int port, boost::asio::io_service &io_service, map< string, string > options)
 ~UdpServer ()
virtual void start_listening ()
virtual void shutdown ()
virtual int get_port ()

Protected Member Functions

virtual void fire_error_event (const string &message)
virtual void fire_data_event (const string data, boost::shared_ptr< udp::socket > socket, boost::shared_ptr< udp::endpoint > endpoint)
virtual void close ()

Private Member Functions

 UdpServer (const UdpServer &other)
void initialize (void)
virtual void listen (void)

Private Attributes

boost::shared_ptr< udp::socket > socket
bool listening

Friends

class UdpEvent

Detailed Description

This class represents a UDP server, which inherits basic UDP handling functionality from Udp, and defines additional functionality to bind to a port and accept incoming connections.

Definition at line 25 of file UdpServer.h.


Constructor & Destructor Documentation

UdpServer::UdpServer ( int  port,
boost::asio::io_service &  io_service 
)

A constructor that creates a UDP server using IPv4. There are two constructors to satisfy Boost's requirement that sockets are initialized are the initialization list, and to provide the option for using either IPv4 and IPv6. This initializes a UDP server but does not start it listening for incoming connections.

Parameters:
portThe port on which this UDP server should listen
io_serviceThe I/O service to use for asynchronous I/O requests

Definition at line 11 of file UdpServer.cpp.

References initialize().

    : Udp("SERVER", port, io_service), socket(new udp::socket(io_service))
{
    initialize();
}
UdpServer::UdpServer ( int  port,
boost::asio::io_service &  io_service,
map< string, string >  options 
)

A constructor that creates a UDP server using IPv6. There are two constructors to satisfy Boost's requirement that sockets are initialized are the initialization list, and to provide the option for using either IPv4 and IPv6. This initializes a UDP server but does not start it listening for incoming connections.

Parameters:
portThe port on which this UDP server should listen
io_serviceThe I/O service to use for asynchronous I/O requests
optionsA map of additional options to configure this UDP server

Definition at line 18 of file UdpServer.cpp.

References initialize(), and Udp::parse_args().

    : Udp("SERVER", port, io_service), socket(new udp::socket(io_service))
{
    parse_args(options);

    initialize();
}
UdpServer::~UdpServer ( )

Deconstructs a UDP server, immediately closing the incoming socket and pending operations.

Definition at line 119 of file UdpServer.cpp.

References close().

{
    close();
}
UdpServer::UdpServer ( const UdpServer other) [private]

Disallows copying a UDP server.


Member Function Documentation

void UdpServer::close ( ) [protected, virtual]

Immediately closes the incoming socket & acceptor, and stops all pending operations.

Implements Udp.

Definition at line 47 of file UdpServer.cpp.

References socket.

Referenced by shutdown(), and ~UdpServer().

{
    /*
    if (socket->is_open())
    {
        if(multicast && multicast_group)
        {
            socket->set_option(
                    boost::asio::ip::multicast::leave_group(
                        boost::asio::ip::address::from_string(*multicast_group)));
        }

        socket->close();
    }
    */
    if(socket->is_open())
        socket->close();
}
void UdpServer::fire_data_event ( const string  data,
boost::shared_ptr< udp::socket >  socket,
boost::shared_ptr< udp::endpoint >  endpoint 
) [protected, virtual]

Helper to fire data event to javascript.

Parameters:
dataThe data received
socketThe socket on which to reply to this data
endpointThe connected endpoint to the remote host

Implements Udp.

Definition at line 213 of file UdpServer.cpp.

{
    fire_data(boost::make_shared<UdpEvent>(this, socket, endpoint, data));
}
void UdpServer::fire_error_event ( const string &  message) [protected, virtual]

Helper to fire an error event to javascript.

Parameters:
messageThe error message

Implements Udp.

Definition at line 207 of file UdpServer.cpp.

{
    fire_error(message);
}
int UdpServer::get_port ( void  ) [virtual]

Returns the port on which this server listens

Implements Server.

Definition at line 201 of file UdpServer.cpp.

References Udp::port.

{
    return port;
}
void UdpServer::initialize ( void  ) [private]

Helper function to initialize this UDP server's object by exposing the start_listening method to javascript as 'listen'.

Definition at line 67 of file UdpServer.cpp.

References Udp::BUFFER_SIZE, Udp::do_not_route, Logger::error(), Udp::host, listening, Udp::log_options(), Udp::multicast, Udp::multicast_ttl, Udp::port, Udp::reuse_address, socket, start_listening(), and Udp::using_ipv6.

Referenced by UdpServer().

{
    log_options();

    listening = false;

    if(using_ipv6 && *using_ipv6)
        socket->open(udp::v6());
    else
        socket->open(udp::v4());

    if(!socket->is_open())
    {
        string message("UDP server failed to initialize, could not open socket");
        Logger::error(message, port, host);
        fire_error(message);
    }

    // synchronize the buffer size of the socket with this class's buffer size
    boost::asio::socket_base::receive_buffer_size buf_size_option(BUFFER_SIZE);
    socket->set_option(buf_size_option);

    if(do_not_route)
    {
        boost::asio::socket_base::do_not_route option(*do_not_route);
        socket->set_option(option);
    }

    if(reuse_address)
    {
        boost::asio::socket_base::reuse_address option(*reuse_address);
        socket->set_option(option);
    }

    if(multicast)
    {
        /* force this to be set */
        boost::asio::socket_base::reuse_address option(true);
        socket->set_option(option);
    }

    if(multicast_ttl)
    {
        boost::asio::ip::multicast::hops option(*multicast_ttl);
        socket->set_option(option);
    }

    // register these methods so they can be invoked from the Javascript
    registerMethod("listen", make_method(this, &UdpServer::start_listening));
}
void UdpServer::listen ( void  ) [private, virtual]

Helper function to listen for new data from incoming connections.

Implements Udp.

Definition at line 190 of file UdpServer.cpp.

References Udp::host, Logger::info(), Udp::port, Udp::receive_buffer, Udp::receive_handler(), Udp::remote_endpoint, socket, and Logger::warn().

Referenced by start_listening().

{
    Logger::info("udpserver: starting to listen", port, host);

    if(remote_endpoint && remote_endpoint.get())
        socket->async_receive_from(boost::asio::buffer(receive_buffer), *remote_endpoint,
                boost::bind(&UdpServer::receive_handler, this, _1, _2, socket, remote_endpoint, host, port));
    else
        Logger::warn("remote endpoint is null", port, host);
}
void UdpServer::shutdown ( ) [virtual]

Gracefully shutdown this UDP server, waiting until all sends have completed before freeing all resources for this UDP server and shutting down any open connections. This function is exposed the javascript API.

Implements NetworkObject.

Definition at line 27 of file UdpServer.cpp.

References close(), Logger::error(), Udp::failed, Udp::host, Udp::pending_sends, Udp::port, and Udp::should_close.

{
    if(!failed)
    {
        should_close = true;

        if (pending_sends == 0) {
            fire_close();
            close();
        }
    }
    else
    {
        // Log & fire an error
        string message("Trying to shutdown a permanently failed UDP server!");
        Logger::error(message, port, host);
    }

}
void UdpServer::start_listening ( ) [virtual]

Called to start the the server listening for the first time, fires the 'open' event, and is exposed to the javascript. This should only be called once, and starts the server listening for incoming connections.

Implements Server.

Definition at line 125 of file UdpServer.cpp.

References Logger::error(), Udp::failed, Udp::host, Logger::info(), listen(), listening, Udp::multicast, Udp::multicast_group, Udp::port, socket, and Udp::using_ipv6.

Referenced by initialize().

{
    if(failed)
    {
        // Log & fire an error
        string message("Trying to start a UDP server that has permanently failed!");
        Logger::error(message, port, host);
        return;
    }

    if(listening) return;

    try
    {
        if(using_ipv6 && *using_ipv6)
            socket->bind(udp::endpoint(udp::v6(), port));
        else
            socket->bind(udp::endpoint(udp::v4(), port));
    }
    catch (boost::system::system_error &e)
    {
        // Catch this error, and fail gracefully
        string message(string("Caught error initializing UDP server: '") + e.what() + "'");
        Logger::error(message, port, host);

        // Stop this server from ever doing anything again
        failed = true;
        return;
    }

    Logger::info("bind!", port, host);

    if(multicast)
    {
        if(!multicast_group)
        {
            Logger::error("UdpServer: Multicast set, but no multicast group given", port, host);
        }
        else
        {
            // Try to join the multicast group, and fail gracefully if we can't join it
            try
            {
                socket->set_option(boost::asio::ip::multicast::join_group(
                            boost::asio::ip::address::from_string(*multicast_group)));
            }
            catch(boost::system::system_error & error)
            {
                // Catch this error, and fail gracefully
                string message(string("Caught error initializing UDP joinging multicast group: '") + error.what() + "'");
                Logger::error(message, port, host);

                failed = true;
                return;
            }
        }
    }

    listen();
    fire_open();

    listening = true;
}

Friends And Related Function Documentation

friend class UdpEvent [friend]

Reimplemented from Udp.

Definition at line 73 of file UdpServer.h.


Member Data Documentation

bool UdpServer::listening [private]

A flag to indicate this object is already listening

Definition at line 122 of file UdpServer.h.

Referenced by initialize(), and start_listening().

boost::shared_ptr<udp::socket> UdpServer::socket [private]

The socket for incoming communications to this server.

Definition at line 119 of file UdpServer.h.

Referenced by close(), initialize(), listen(), and start_listening().


The documentation for this class was generated from the following files:
 All Classes Files Functions Variables Typedefs Friends Defines