Public Types | Public Member Functions | Protected Member Functions | Protected Attributes
Udpraw Class Reference

#include <udpraw.h>

List of all members.

Public Types

enum  UdprawError {
  UDPRAW_NO_ERROR, UDPRAW_BIND, UDPRAW_MSGSIZE, UDPRAW_NOINIT,
  UDPRAW_NOIP, UDPRAW_NOPORT, UDPRAW_RECV, UDPRAW_SELECT,
  UDPRAW_SEND, UDPRAW_SOCKEXCEPTION, UDPRAW_UNMATCHED
}

Public Member Functions

int closeAndCleanup ()
int doSelect ()
int getError ()
std::string getErrorStr ()
std::string getErrorStr (int error)
float getSimulateLossRecv ()
float getSimulateLossSend ()
int getStatRecvd ()
int getStatSent ()
int initReceiver (int port)
void initSender (int port=0)
void initSender (const char *ip, int port=0)
void initSender (std::vector< std::string > ip_list, int port=0)
void initSender (std::vector< std::string > ip_list, std::vector< int > port_list)
int recvRaw (char *buf, int len, bool select=true)
int sendRaw (const char *buf, int len, bool select=true)
void setSendIp (const char *ip)
void setSendIp (std::vector< std::string > ip_list)
void setSendPort (int port)
void setSendPort (std::vector< int > port_list)
void setSimulateLossRecv (float probabilityofloss)
void setSimulateLossSend (float probabilityofloss)
void setStatRecvd (int bytes)
void setStatSent (int bytes)
 Udpraw ()
void writeError ()
void writeError (std::string prependMsg)
void writeError (int error)
 ~Udpraw ()

Protected Member Functions

void init ()
bool simulatePacketLoss (float prob)

Protected Attributes

bool createdRecv
bool createdSend
std::string errorPrepend
fd_set exceptionSocks
sockaddr_in fromAddr
int lastError
int numSocksReadable
fd_set readSocks
std::vector< int >::iterator recv_port_iterator
sockaddr_in recvAddr
int recvSocket
sockaddr_in sendAddr
std::vector< std::string > sendIp
std::vector< int > sendPort
int sendSocket
struct {
   float   recv
   float   send
simulateLoss
int statRecvd
int statSent
fd_set writeSocks

Detailed Description

Class for sending and receiving UDP packets.

You can also get some basic stats, and simulate packet loss for testing purposes. Start by calling either initReceiver or initSender, or both.

Definition at line 39 of file udpraw.h.


Member Enumeration Documentation

Enum for storing error.

See also:
getError()
writeError()
Enumerator:
UDPRAW_NO_ERROR 

Default value, no error.

UDPRAW_BIND 

Error binding.

UDPRAW_MSGSIZE 

Buffer not big enough for message, some data lost.

UDPRAW_NOINIT 

Socket not initialised.

UDPRAW_NOIP 

No IP address specified.

UDPRAW_NOPORT 

No port specified.

UDPRAW_RECV 

Error receiving (not message size).

UDPRAW_SELECT 

Error with select.

UDPRAW_SEND 

Error sending.

UDPRAW_SOCKEXCEPTION 

Select detected a socket exception.

UDPRAW_UNMATCHED 

The sender was initialised with multiple IP addresses and port numbers, but the number of IPs does not match the number of ports.

Definition at line 89 of file udpraw.h.


Constructor & Destructor Documentation

Constructor.

Definition at line 40 of file udpraw.cpp.

Destructor. Calls closeAndCleanup().

See also:
closeAndCleanup()

Definition at line 65 of file udpraw.cpp.


Member Function Documentation

Close sockets, WSA cleanup (on Windows). Will be sorted by destructor if not called by app.

Definition at line 524 of file udpraw.cpp.

Perform select call for read, write and exception sockets.

Called by the application so it can be done once for send and receive. Stores the number of sockets accessible.

Returns:
0 on success, -1 if there was an error with the select call, -2 if a socket exception was detected (errors also set the error number to UDPRAW_SELECT or UDPRAW_SOCKEXCEPTION accordingly)
See also:
writeError()

Definition at line 271 of file udpraw.cpp.

Get and clear the last error number.

Definition at line 70 of file udpraw.cpp.

std::string Udpraw::getErrorStr ( )

Get last error message and clear the last error number.

Definition at line 78 of file udpraw.cpp.

std::string Udpraw::getErrorStr ( int  error)

Get error message.

Parameters:
errorthe error number

Definition at line 84 of file udpraw.cpp.

Return the current probability of packet loss for receiving.

Definition at line 512 of file udpraw.cpp.

Return the current probability of packet loss for sending.

Definition at line 507 of file udpraw.cpp.

Get total bytes received.

See also:
setStatRecvd()

Definition at line 482 of file udpraw.cpp.

Get total bytes sent (if sending to > 1 ip this will count total sent).

See also:
setStatSent()

Definition at line 477 of file udpraw.cpp.

void Udpraw::init ( ) [protected]

This is called automatically from initSender/initReceiver.

Definition at line 151 of file udpraw.cpp.

int Udpraw::initReceiver ( int  port)

Initialise receiver. Returns -1 if there was an error binding (and sets error number), 0 on success.

See also:
writeError()

Definition at line 160 of file udpraw.cpp.

void Udpraw::initSender ( int  port = 0)

Initialise sender. You can init sender with or without ip and port info.

Parameters:
portthe port number, optional (can set later)
See also:
setSendIp()
setSendPort()

Definition at line 211 of file udpraw.cpp.

void Udpraw::initSender ( const char *  ip,
int  port = 0 
)

Initialise sender with one ip address.

Parameters:
ipthe ip address to send to
portthe port number, optional (can set later)
See also:
setSendPort()

Definition at line 230 of file udpraw.cpp.

void Udpraw::initSender ( std::vector< std::string >  ip_list,
int  port = 0 
)

Initialise sender with a list of IP addresses.

Parameters:
ip_lista list of IP addresses
portthe port number, optional (can set later)
See also:
setSendPort()

Definition at line 236 of file udpraw.cpp.

void Udpraw::initSender ( std::vector< std::string >  ip_list,
std::vector< int >  port_list 
)

Initialise sender with a list of IP addresses and a list of ports.

Parameters:
ip_lista list of IP addresses
port_lista list of ports

Definition at line 242 of file udpraw.cpp.

int Udpraw::recvRaw ( char *  buf,
int  len,
bool  select = true 
)

Receive a UDP packet.

Parameters:
[out]bufa pointer to a buffer to store data
lenthe length of the buffer, should be big enough for one message (if it's not we won't crash but some data will be lost and an error will be returned)
selecttrue if the application wants to go by the results of the select call (app must call doSelect() first), false if the application wants to block until next packet is received
Returns:
the number of bytes received
0 if we have no data (either socket will block and we've set select to true, or we're simulating packet loss and this one was dropped)
-1 if the buffer was detected as too small for the message (some data was lost) and sets the error number to UDPRAW_MSGSIZE
-2 for any other error (and sets error number to either UDPRAW_NOINIT or UDPRAW_RECV accordingly)
See also:
doSelect()
writeError()
setSimulateLossRecv()

Definition at line 309 of file udpraw.cpp.

int Udpraw::sendRaw ( const char *  buf,
int  len,
bool  select = true 
)

Send a UDP packet.

Parameters:
bufa pointer to data to send
lenthe length of the buffer
selecttrue if the application wants to go by the results of the select call (app must call doSelect() first), false if the application wants to block until next packet is received
Returns:
the minimum bytes sent to each destination, 0 if packet was dropped (if simulating packet loss) or -1 on error (and sets error number to either UDPRAW_NOINIT, UDPRAW_NOPORT, UDPRAW_NOIP, UDPRAW_UNMATCHED, or UDPRAW_SEND accordingly)
See also:
doSelect()
writeError()
setSimulateLossSend()

Definition at line 404 of file udpraw.cpp.

void Udpraw::setSendIp ( const char *  ip)

Set a single IP address to send to. Can have one corresponding port, or a list of ports.

Parameters:
ipan IP address
See also:
setSendPort()

Definition at line 249 of file udpraw.cpp.

void Udpraw::setSendIp ( std::vector< std::string >  ip_list)

Set a list of IP addresses to send to. Can have one corresponding port, or a list of equal size.

Parameters:
ip_lista list of IP addresses
See also:
setSendPort()

Definition at line 255 of file udpraw.cpp.

void Udpraw::setSendPort ( int  port)

Set the port to use for sending. Can have one corresponding IP, or a list of IPs.

Parameters:
portthe port number
See also:
setSendIp()

Definition at line 260 of file udpraw.cpp.

void Udpraw::setSendPort ( std::vector< int >  port_list)

Set a list of ports to use for sending. Can be multiple ports for one IP, or a list of equal length to a list of IP addresses, one port for each IP.

Parameters:
port_lista list of port numbers
See also:
setSendIp()

Definition at line 266 of file udpraw.cpp.

void Udpraw::setSimulateLossRecv ( float  probabilityofloss)

Simulate packet loss during receiving. Set to 0 to disable. Still receives but will return 0 (no bytes/socket would block).

See also:
recvRaw()

Definition at line 502 of file udpraw.cpp.

void Udpraw::setSimulateLossSend ( float  probabilityofloss)

Simulate packet loss by specifying probability of loss 0-1, 1=lose all! Set to 0 to disable.

See also:
sendRaw()

Definition at line 497 of file udpraw.cpp.

void Udpraw::setStatRecvd ( int  bytes)

Set bytes received, e.g. reset to 0.

See also:
getStatRecvd()

Definition at line 492 of file udpraw.cpp.

void Udpraw::setStatSent ( int  bytes)

Set bytes sent, e.g. reset to 0.

See also:
getStatSent()

Definition at line 487 of file udpraw.cpp.

bool Udpraw::simulatePacketLoss ( float  prob) [protected]

Returns true if packet should be dropped. False otherwise.

Definition at line 517 of file udpraw.cpp.

Write the error message for the last error to stderr and clear the last error number. This function automatically prepends "Udpraw error: " to the error message. Use writeError(std::string prependMsg) to change this.

Definition at line 132 of file udpraw.cpp.

void Udpraw::writeError ( std::string  prependMsg)

Write the error message for the last error to stderr and clear the last error number.

Parameters:
prependMsga string to prepend to the output, e.g. "Udpraw error: ", or an empty string

Definition at line 139 of file udpraw.cpp.

void Udpraw::writeError ( int  error)

Write error message to stderr.

Parameters:
errorthe error number
See also:
getError()

Definition at line 145 of file udpraw.cpp.


Member Data Documentation

bool Udpraw::createdRecv [protected]

Have we created the receive socket?

See also:
initReceiver

Definition at line 54 of file udpraw.h.

bool Udpraw::createdSend [protected]

Have we created the send socket?

See also:
initSender

Definition at line 54 of file udpraw.h.

std::string Udpraw::errorPrepend [protected]

A string to prepend to error messages.

See also:
writeError()

Definition at line 75 of file udpraw.h.

fd_set Udpraw::exceptionSocks [protected]

Sockets to watch for exceptions on.

Definition at line 59 of file udpraw.h.

sockaddr_in Udpraw::fromAddr [protected]

The address of the sender (according to what we get from recvfrom()).

Definition at line 62 of file udpraw.h.

int Udpraw::lastError [protected]

The last error that occurred.

Definition at line 74 of file udpraw.h.

int Udpraw::numSocksReadable [protected]

Number of sockets readable.

Definition at line 60 of file udpraw.h.

fd_set Udpraw::readSocks [protected]

Sockets to read from.

Definition at line 57 of file udpraw.h.

float Udpraw::recv

Definition at line 79 of file udpraw.h.

std::vector<int>::iterator Udpraw::recv_port_iterator [protected]

Port list iterator.

Definition at line 67 of file udpraw.h.

sockaddr_in Udpraw::recvAddr [protected]

Receive from receiveAddr (i.e. specify a port to read from).

Definition at line 62 of file udpraw.h.

int Udpraw::recvSocket [protected]

Socket to receive from.

Definition at line 50 of file udpraw.h.

float Udpraw::send

Definition at line 78 of file udpraw.h.

sockaddr_in Udpraw::sendAddr [protected]

Send to sendAddr.

Definition at line 62 of file udpraw.h.

std::vector<std::string> Udpraw::sendIp [protected]

One or more IPs to send to.

Definition at line 66 of file udpraw.h.

std::vector<int> Udpraw::sendPort [protected]

One or more ports to send to.

Definition at line 68 of file udpraw.h.

int Udpraw::sendSocket [protected]

Socket for sending.

Definition at line 50 of file udpraw.h.

struct { ... } Udpraw::simulateLoss [protected]

Probability of losing a packet for send/recv. 0-1, 1 = all packets lost. 0 = disable

int Udpraw::statRecvd [protected]

Total bytes received.

Definition at line 72 of file udpraw.h.

int Udpraw::statSent [protected]

Total bytes sent (that means if sending to multiple addresses, this is the total sent).

Definition at line 71 of file udpraw.h.

fd_set Udpraw::writeSocks [protected]

Sockets to write to.

Definition at line 58 of file udpraw.h.


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


tk_vrqc2011
Author(s): Martin Riedel
autogenerated on Wed Apr 24 2013 11:26:17