SockIt
Public Member Functions | Private Attributes

UdpEvent Class Reference

#include <UdpEvent.h>

Inheritance diagram for UdpEvent:
Event

List of all members.

Public Member Functions

 UdpEvent (Udp *udp, boost::shared_ptr< udp::socket > socket, boost::shared_ptr< udp::endpoint > endpoint, string data)
virtual ~UdpEvent ()
virtual void send (const string &data)
virtual void send_bytes (const vector< byte > &bytes)
virtual string read () const
virtual FB::VariantList read_bytes () const
virtual string get_host ()
virtual unsigned short get_port ()

Private Attributes

string data
bool failed
string host
int port
boost::shared_ptr< udp::socket > socket
boost::shared_ptr< udp::endpoint > endpoint
Udpudp_object

Detailed Description

A UDP implementation of a event to allow javascript to respond to data on a UDP connection.

See also:
Event

Definition at line 21 of file UdpEvent.h.


Constructor & Destructor Documentation

UdpEvent::UdpEvent ( Udp udp,
boost::shared_ptr< udp::socket >  socket,
boost::shared_ptr< udp::endpoint >  endpoint,
string  data 
)

Constructs a new UdpEvent object containing the UDP connection on which to reply.

Parameters:
udpThe UDP server or client associated with this event
socketThe UDP connection on which to reply
endpointThe remote endpoint for this UDP event, from which we can find information about the remote host
dataThe data this event was fired from

Definition at line 10 of file UdpEvent.cpp.

References Logger::error(), failed, host, and port.

                                                                                                                          :
    port(-1), socket(socket), endpoint(endpoint), udp_object(udp_object), failed(false), data(data)
{
    // Check to see if any the parameters are null, and log and fail if this occurs
    if (endpoint && udp_object)
    {
        // Initialize only if it's safe
        host = boost::lexical_cast<string>(endpoint->address());
        port = endpoint->port();
    }
    else
    {
        // Fail permanently and log it
        failed = true;

        string message("UDP Event was not properly initialized, permanently failed.");
        Logger::error(message, port, host);
        fire_error(message);
    }
}
UdpEvent::~UdpEvent ( ) [virtual]

Deconstructs the UDP event object, after a single reply.

Definition at line 31 of file UdpEvent.cpp.

{
    // do not free socket, endpoint or udp here
}

Member Function Documentation

string UdpEvent::get_host ( void  ) [virtual]

Gets the hostname of the remote endpoint of the UDP connection for this UdpEvent.

Returns:
The remote hostname of the UDP connection for this UdpEvent

Implements Event.

Definition at line 93 of file UdpEvent.cpp.

References host.

{
    return host;
}
unsigned short UdpEvent::get_port ( void  ) [virtual]

Gets the port of the remote endpoint of the UDP connection for this UdpEvent.

Returns:
The remote port of the UDP connection for this UdpEvent

Implements Event.

Definition at line 98 of file UdpEvent.cpp.

References port.

{
    return port;
}
string UdpEvent::read ( ) const [virtual]

Reads the string data that belongs to this event.

Returns:
The string data received when this event was fired

Implements Event.

Definition at line 76 of file UdpEvent.cpp.

References data.

{
    return data;
}
FB::VariantList UdpEvent::read_bytes ( ) const [virtual]

Reads the byte data that belongs to this event

Returns:
The byte data received when this event was fired

Implements Event.

Definition at line 81 of file UdpEvent.cpp.

References data.

{
    FB::VariantList fb_bytes;

    for (int i = 0; i < data.size(); i++)
    {
        fb_bytes.push_back((unsigned char) (data.data())[i]);
    }

    return fb_bytes;
}
void UdpEvent::send ( const string &  data) [virtual]

Replies on the UDP connection with some data.

Parameters:
dataThe data with which to reply

Implements Event.

Definition at line 48 of file UdpEvent.cpp.

References endpoint, Logger::error(), Udp::failed, failed, host, Udp::pending_sends, port, Udp::send_handler(), socket, and udp_object.

Referenced by send_bytes().

{
    // Don't send if we've permanently failed
    if (failed)
    {
        string message("UDP event failed to send, Event already failed permanently");
        Logger::error(message, port, host);
        fire_error(message);
        return;
    }

    if (udp_object && udp_object->failed)
    {
        // Log & fire an error
        string message("UDP event failed trying to reply to a UDP object that has permanently failed!");
        Logger::error(message, port, host);
        fire_error(message);
        return;
    }

    if (socket && endpoint)
    {
        udp_object->pending_sends++;
        socket->async_send_to(boost::asio::buffer(data.data(), data.size()), *endpoint, 
                boost::bind(&Udp::send_handler, udp_object, _1, _2, data, host, endpoint->port()));
    }
}
void UdpEvent::send_bytes ( const vector< byte > &  bytes) [virtual]

Replies on the UDP connection with some data.

Parameters:
bytesThe bytes of data with which to reply

Implements Event.

Definition at line 36 of file UdpEvent.cpp.

References data, and send().

{
    string data;

    for (int i = 0; i < bytes.size(); i++)
    {
        data.push_back((unsigned char) bytes[i]);
    }

    send(data);
}

Member Data Documentation

string UdpEvent::data [private]

The data received when this event was fired

Definition at line 88 of file UdpEvent.h.

Referenced by read(), read_bytes(), send_bytes(), and UdpClient::send_bytes().

boost::shared_ptr<udp::endpoint> UdpEvent::endpoint [private]

The remote endpoint for this event

Definition at line 113 of file UdpEvent.h.

Referenced by send().

bool UdpEvent::failed [private]

A flag to prevent this event from blowing up if was initialized improperly

Definition at line 93 of file UdpEvent.h.

Referenced by send(), and UdpEvent().

string UdpEvent::host [private]

The remote hostname of the UDP connection for this UdpEvent

Definition at line 98 of file UdpEvent.h.

Referenced by get_host(), send(), and UdpEvent().

int UdpEvent::port [private]

The remote port of the UDP connection for this UdpEvent

Definition at line 103 of file UdpEvent.h.

Referenced by get_port(), send(), and UdpEvent().

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

The socket corresponding to this UDP 'connection', on which to reply

Definition at line 108 of file UdpEvent.h.

Referenced by send().

The UDP server or client associated with this event

Definition at line 118 of file UdpEvent.h.

Referenced by send().


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