#include <UdpDataProtocol.h>
Signals | |
void | signalWatingTooLong (int wait_msec) |
Signals when waiting every 10 milliseconds, with the total wait on wait_msec. | |
Public Member Functions | |
UdpDataProtocol (JackTrip *jacktrip, const runModeT runmode, int bind_port, int peer_port, unsigned int udp_redundancy_factor=1) | |
The class constructor. | |
virtual | ~UdpDataProtocol () |
The class destructor. | |
void | setPeerAddress (const char *peerHostOrIP) |
Set the Peer address to connect to. | |
virtual int | receivePacket (QUdpSocket &UdpSocket, char *buf, const size_t n) |
Receives a packet. It blocks until a packet is received. | |
virtual int | sendPacket (QUdpSocket &UdpSocket, const QHostAddress &PeerAddress, const char *buf, const size_t n) |
Sends a packet. | |
virtual void | getPeerAddressFromFirstPacket (QUdpSocket &UdpSocket, QHostAddress &peerHostAddress, uint16_t &port) |
Obtains the peer address from the first UDP packet received. This address is used by the SERVER mode to connect back to the client. | |
void | setBindPort (int port) |
Sets the bind port number. | |
void | setPeerPort (int port) |
Sets the peer port number. | |
virtual void | run () |
Implements the Thread Loop. To start the thread, call start() ( DO NOT CALL run() ). | |
Protected Member Functions | |
void | bindSocket (QUdpSocket &UdpSocket) |
Binds the UDP socket to the available address and specified port. | |
bool | waitForReady (QUdpSocket &UdpSocket, int timeout_msec) |
This function blocks until data is available for reading in the QUdpSocket. The function will timeout after timeout_msec microseconds. | |
virtual void | receivePacketRedundancy (QUdpSocket &UdpSocket, int8_t *full_redundant_packet, int full_redundant_packet_size, int full_packet_size, uint16_t ¤t_seq_num, uint16_t &last_seq_num, uint16_t &newer_seq_num) |
Redundancy algorythm at the receiving end. | |
virtual void | sendPacketRedundancy (QUdpSocket &UdpSocket, QHostAddress &PeerAddress, int8_t *full_redundant_packet, int full_redundant_packet_size, int full_packet_size) |
Redundancy algorythm at the sender's end. | |
Private Slots | |
void | printUdpWaitedTooLong (int wait_msec) |
Private Attributes | |
int | mBindPort |
Local Port number to Bind. | |
int | mPeerPort |
Peer Port number. | |
const runModeT | mRunMode |
Run mode, either SENDER or RECEIVER. | |
QHostAddress | mPeerAddress |
The Peer Address. | |
int8_t * | mAudioPacket |
Buffer to store Audio Packets. | |
int8_t * | mFullPacket |
Buffer to store Full Packet (audio+header). | |
unsigned int | mUdpRedundancyFactor |
Factor of redundancy. | |
Static Private Attributes | |
static QMutex | sUdpMutex |
Mutex to make thread safe the binding process. |
The class has a bind port
and a peer port
. The meaning of these depends on the runModeT. If it's a SENDER, bind port
is the source port and peer port
is the destination port for each UDP packet. If it's a RECEIVER, the bind port
destination port (for incoming packets) and the peer port
is the source port.
The SENDER and RECEIVER socket can share the same port/address pair (for compatibility with the JamLink boxes). This is achieved setting the resusable property in the socket for address and port. You have to externaly check if the port is already binded if you want to avoid re-binding to the same port.
UdpDataProtocol::UdpDataProtocol | ( | JackTrip * | jacktrip, | |
const runModeT | runmode, | |||
int | bind_port, | |||
int | peer_port, | |||
unsigned int | udp_redundancy_factor = 1 | |||
) |
The class constructor.
jacktrip | Pointer to the JackTrip class that connects all classes (mediator) | |
runmode | Sets the run mode, use either SENDER or RECEIVER | |
bind_port | Port number to bind for this socket (this is the receive or send port depending on the runmode) | |
peer_port | Peer port number (this is the receive or send port depending on the runmode) | |
udp_redundancy_factor | Number of redundant packets |
UdpDataProtocol::~UdpDataProtocol | ( | ) | [virtual] |
The class destructor.
void UdpDataProtocol::setPeerAddress | ( | const char * | peerHostOrIP | ) | [virtual] |
Set the Peer address to connect to.
peerHostOrIP | IPv4 number or host name |
Implements DataProtocol.
int UdpDataProtocol::receivePacket | ( | QUdpSocket & | UdpSocket, | |
char * | buf, | |||
const size_t | n | |||
) | [virtual] |
Receives a packet. It blocks until a packet is received.
This function makes sure we recieve a complete packet of size n
buf | Buffer to store the recieved packet | |
n | size of packet to receive |
int UdpDataProtocol::sendPacket | ( | QUdpSocket & | UdpSocket, | |
const QHostAddress & | PeerAddress, | |||
const char * | buf, | |||
const size_t | n | |||
) | [virtual] |
Sends a packet.
This function meakes sure we send a complete packet of size n
buf | Buffer to send | |
n | size of packet to receive |
void UdpDataProtocol::getPeerAddressFromFirstPacket | ( | QUdpSocket & | UdpSocket, | |
QHostAddress & | peerHostAddress, | |||
uint16_t & | port | |||
) | [virtual] |
Obtains the peer address from the first UDP packet received. This address is used by the SERVER mode to connect back to the client.
peerHostAddress | QHostAddress to store the peer address | |
port | Receiving port |
void UdpDataProtocol::setBindPort | ( | int | port | ) | [inline] |
Sets the bind port number.
void UdpDataProtocol::setPeerPort | ( | int | port | ) | [inline, virtual] |
void UdpDataProtocol::run | ( | ) | [virtual] |
Implements the Thread Loop. To start the thread, call start() ( DO NOT CALL run() ).
This function creats and binds all the socket and start the connection loop thread.
Implements DataProtocol.
void UdpDataProtocol::printUdpWaitedTooLong | ( | int | wait_msec | ) | [private, slot] |
void UdpDataProtocol::signalWatingTooLong | ( | int | wait_msec | ) | [signal] |
Signals when waiting every 10 milliseconds, with the total wait on wait_msec.
wait_msec | Total wait in milliseconds |
void UdpDataProtocol::bindSocket | ( | QUdpSocket & | UdpSocket | ) | [protected] |
Binds the UDP socket to the available address and specified port.
bool UdpDataProtocol::waitForReady | ( | QUdpSocket & | UdpSocket, | |
int | timeout_msec | |||
) | [protected] |
This function blocks until data is available for reading in the QUdpSocket. The function will timeout after timeout_msec microseconds.
This function is intended to replace QAbstractSocket::waitForReadyRead which has some problems with multithreading.
void UdpDataProtocol::receivePacketRedundancy | ( | QUdpSocket & | UdpSocket, | |
int8_t * | full_redundant_packet, | |||
int | full_redundant_packet_size, | |||
int | full_packet_size, | |||
uint16_t & | current_seq_num, | |||
uint16_t & | last_seq_num, | |||
uint16_t & | newer_seq_num | |||
) | [protected, virtual] |
Redundancy algorythm at the receiving end.
void UdpDataProtocol::sendPacketRedundancy | ( | QUdpSocket & | UdpSocket, | |
QHostAddress & | PeerAddress, | |||
int8_t * | full_redundant_packet, | |||
int | full_redundant_packet_size, | |||
int | full_packet_size | |||
) | [protected, virtual] |
Redundancy algorythm at the sender's end.
int UdpDataProtocol::mBindPort [private] |
Local Port number to Bind.
int UdpDataProtocol::mPeerPort [private] |
const runModeT UdpDataProtocol::mRunMode [private] |
QHostAddress UdpDataProtocol::mPeerAddress [private] |
The Peer Address.
int8_t* UdpDataProtocol::mAudioPacket [private] |
Buffer to store Audio Packets.
int8_t* UdpDataProtocol::mFullPacket [private] |
Buffer to store Full Packet (audio+header).
unsigned int UdpDataProtocol::mUdpRedundancyFactor [private] |
Factor of redundancy.
QMutex UdpDataProtocol::sUdpMutex [static, private] |
Mutex to make thread safe the binding process.