#include <udpraw.h>
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 |
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.
enum Udpraw::UdprawError |
Enum for storing error.
Udpraw::Udpraw | ( | ) |
Constructor.
Definition at line 40 of file udpraw.cpp.
Udpraw::~Udpraw | ( | ) |
Destructor. Calls closeAndCleanup().
Definition at line 65 of file udpraw.cpp.
int Udpraw::closeAndCleanup | ( | ) |
Close sockets, WSA cleanup (on Windows). Will be sorted by destructor if not called by app.
Definition at line 524 of file udpraw.cpp.
int Udpraw::doSelect | ( | ) |
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.
Definition at line 271 of file udpraw.cpp.
int Udpraw::getError | ( | ) |
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 | ) |
float Udpraw::getSimulateLossRecv | ( | ) |
Return the current probability of packet loss for receiving.
Definition at line 512 of file udpraw.cpp.
float Udpraw::getSimulateLossSend | ( | ) |
Return the current probability of packet loss for sending.
Definition at line 507 of file udpraw.cpp.
int Udpraw::getStatRecvd | ( | ) |
int Udpraw::getStatSent | ( | ) |
Get total bytes sent (if sending to > 1 ip this will count total sent).
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.
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.
port | the port number, optional (can set later) |
Definition at line 211 of file udpraw.cpp.
void Udpraw::initSender | ( | const char * | ip, |
int | port = 0 |
||
) |
Initialise sender with one ip address.
ip | the ip address to send to |
port | the port number, optional (can set later) |
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.
ip_list | a list of IP addresses |
port | the port number, optional (can set later) |
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.
ip_list | a list of IP addresses |
port_list | a 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.
[out] | buf | a pointer to a buffer to store data |
len | the 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) | |
select | true 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 |
Definition at line 309 of file udpraw.cpp.
int Udpraw::sendRaw | ( | const char * | buf, |
int | len, | ||
bool | select = true |
||
) |
Send a UDP packet.
buf | a pointer to data to send |
len | the length of the buffer |
select | true 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 |
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.
ip | an IP address |
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.
ip_list | a list of IP addresses |
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.
port | the port number |
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.
port_list | a list of port numbers |
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).
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.
Definition at line 497 of file udpraw.cpp.
void Udpraw::setStatRecvd | ( | int | bytes | ) |
Set bytes received, e.g. reset to 0.
Definition at line 492 of file udpraw.cpp.
void Udpraw::setStatSent | ( | int | bytes | ) |
bool Udpraw::simulatePacketLoss | ( | float | prob | ) | [protected] |
Returns true if packet should be dropped. False otherwise.
Definition at line 517 of file udpraw.cpp.
void Udpraw::writeError | ( | ) |
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.
prependMsg | a 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.
error | the error number |
Definition at line 145 of file udpraw.cpp.
bool Udpraw::createdRecv [protected] |
bool Udpraw::createdSend [protected] |
std::string Udpraw::errorPrepend [protected] |
A string to prepend to error messages.
fd_set Udpraw::exceptionSocks [protected] |
sockaddr_in Udpraw::fromAddr [protected] |
int Udpraw::lastError [protected] |
int Udpraw::numSocksReadable [protected] |
fd_set Udpraw::readSocks [protected] |
float Udpraw::recv |
std::vector<int>::iterator Udpraw::recv_port_iterator [protected] |
sockaddr_in Udpraw::recvAddr [protected] |
int Udpraw::recvSocket [protected] |
float Udpraw::send |
sockaddr_in Udpraw::sendAddr [protected] |
std::vector<std::string> Udpraw::sendIp [protected] |
std::vector<int> Udpraw::sendPort [protected] |
int Udpraw::sendSocket [protected] |
struct { ... } Udpraw::simulateLoss [protected] |
Probability of losing a packet for send/recv. 0-1, 1 = all packets lost. 0 = disable
int Udpraw::statRecvd [protected] |
int Udpraw::statSent [protected] |
fd_set Udpraw::writeSocks [protected] |