SockIt
Public Member Functions | Private Attributes

TcpEvent Class Reference

#include <TcpEvent.h>

Inheritance diagram for TcpEvent:
Event

List of all members.

Public Member Functions

 TcpEvent (Tcp *tcp, boost::shared_ptr< tcp::socket > connection, string data)
virtual ~TcpEvent ()
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
Tcptcp_object
string host
int port
boost::shared_ptr< tcp::socket > connection

Detailed Description

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

See also:
Event

Definition at line 25 of file TcpEvent.h.


Constructor & Destructor Documentation

TcpEvent::TcpEvent ( Tcp tcp,
boost::shared_ptr< tcp::socket >  connection,
string  data 
)

Constructs a new TcpEvent object containing the TCP connection on which to reply.

Parameters:
tcpThe TCP server or client associated with this event
connectionThe TCP connection on which to reply
dataThe data received when this event was fired

Definition at line 12 of file TcpEvent.cpp.

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

                                                                                       :
    tcp_object(tcp_object), connection(connection), failed(false), data(data)
{
    // Check to see if any the parameters are null, and log and fail if this occurs
    if(tcp_object && connection)
    {
        // Initialize only if it's safe
        port = connection->remote_endpoint().port();
        host = connection->remote_endpoint().address().to_string();
    }
    else
    {
        // Fail permanently and log it
        failed = true;

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

Deconstructs the TCP event object, after a single reply.

Definition at line 33 of file TcpEvent.cpp.

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

Member Function Documentation

string TcpEvent::get_host ( void  ) [virtual]

Gets the hostname of the remote endpoint of the TCP connection for this TcpEvent.

Returns:
The remote hostname of the TCP connection for this TcpEvent

Implements Event.

Definition at line 91 of file TcpEvent.cpp.

References host.

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

Gets the port of the remote endpoint of the TCP connection for this TcpEvent.

Returns:
The remote port of the TCP connection for this TcpEvent

Implements Event.

Definition at line 96 of file TcpEvent.cpp.

References port.

{
    return port;
}
string TcpEvent::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 74 of file TcpEvent.cpp.

References data.

{
    return data;
}
FB::VariantList TcpEvent::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 79 of file TcpEvent.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 TcpEvent::send ( const string &  data) [virtual]

Replies on the TCP connection with some data.

Parameters:
dataThe data with which to reply

Implements Event.

Definition at line 50 of file TcpEvent.cpp.

References Tcp::active_jobs, connection, Logger::error(), Tcp::failed, failed, host, port, Tcp::send_handler(), and tcp_object.

Referenced by send_bytes().

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

    if(!tcp_object->failed)
    {
        tcp_object->active_jobs++;
        connection->async_send(boost::asio::buffer(data.data(), data.size()),
                boost::bind(&Tcp::send_handler, tcp_object, _1, _2, data, host, port, connection));
    }
    else
    {
        string message("TCP event failed trying to reply on a permanently failed TCP object");
        Logger::error(message, port, host);
        fire_error(message);
    }
}
void TcpEvent::send_bytes ( const vector< byte > &  bytes) [virtual]

Replies on the TCP connection with some data.

Parameters:
bytesThe bytes of data with which to reply

Implements Event.

Definition at line 38 of file TcpEvent.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

boost::shared_ptr<tcp::socket> TcpEvent::connection [private]

The TCP connection on which to reply

Definition at line 115 of file TcpEvent.h.

Referenced by send().

string TcpEvent::data [private]

The data received when this event was fired

Definition at line 90 of file TcpEvent.h.

Referenced by TcpClient::flush(), read(), read_bytes(), send_bytes(), and TcpClient::send_bytes().

bool TcpEvent::failed [private]

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

Definition at line 95 of file TcpEvent.h.

Referenced by send(), and TcpEvent().

string TcpEvent::host [private]

The remote hostname of the TCP connection for this TcpEvent

Definition at line 105 of file TcpEvent.h.

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

int TcpEvent::port [private]

The remote port of the TCP connection for this TcpEvent.

Definition at line 110 of file TcpEvent.h.

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

The TCP server or client associated with this event

Definition at line 100 of file TcpEvent.h.

Referenced by send().


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