JackTrip

Signals | Public Member Functions | Protected Member Functions | List of all members
UdpDataProtocol Class Reference

UDP implementation of DataProtocol class. More...

`#include <UdpDataProtocol.h>`

Inheritance diagram for UdpDataProtocol:
[legend]
Collaboration diagram for UdpDataProtocol:
[legend]

## Signals
void | signalWatingTooLong (int wait_msec)
| Signals when waiting every 10 milliseconds, with the total wait on wait_msec. More...
Signals inherited from DataProtocol
void | signalError (const char *error_message)
void | signalReceivedConnectionFromPeer ()

## Public Member Functions
| UdpDataProtocol (JackTrip *jacktrip, const runModeT runmode, int bind_port, int peer_port, unsigned int udp_redundancy_factor=1)
| The class constructor. More...
virtual | ~UdpDataProtocol ()
| The class destructor. More...
void | setPeerAddress (const char *peerHostOrIP) throw (std::invalid_argument)
| Set the Peer address to connect to. More...
virtual int | receivePacket (QUdpSocket &UdpSocket, char *buf, const size_t n)
| Receives a packet. It blocks until a packet is received. More...
virtual int | sendPacket (QUdpSocket &UdpSocket, const QHostAddress &PeerAddress, const char *buf, const size_t n)
| Sends a packet. More...
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. More...
void | setBindPort (int port)
| Sets the bind port number. More...
void | setPeerPort (int port)
| Sets the peer port number. More...
virtual void | run ()
| Implements the Thread Loop. To start the thread, call start() ( DO NOT CALL run() ) More...
Public Member Functions inherited from DataProtocol
| DataProtocol (JackTrip *jacktrip, const runModeT runmode, int bind_port, int peer_port)
| The class constructor. More...
virtual | ~DataProtocol ()
| The class destructor. More...
virtual void | stop ()
| Stops the execution of the Thread. More...
void | setAudioPacketSize (const size_t size_bytes)
| Sets the size of the audio part of the packets. More...
size_t | getAudioPacketSizeInBites ()
| Get the size of the audio part of the packets. More...

## Protected Member Functions
void | bindSocket (QUdpSocket &UdpSocket) throw (std::runtime_error)
| Binds the UDP socket to the available address and specified port. More...
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. More...
virtual void | 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)
| Redundancy algorythm at the receiving end. More...
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. More...
Protected Member Functions inherited from DataProtocol
runModeT | getRunMode () const
| Get the Run Mode of the object. More...

## Additional Inherited Members
Public Types inherited from DataProtocol
enum | packetHeaderTypeT { DEFAULT, JAMLINK, EMPTY }
| Enum to define packet header types. More...
enum | runModeT { SENDER, RECEIVER }
| Enum to define class modes, SENDER or RECEIVER. More...
Protected Attributes inherited from DataProtocol
volatile bool | mStopped
| Boolean stop the execution of the thread. More...
volatile bool | mHasPeerAddress
| Boolean to indicate if the RECEIVER is waiting to obtain peer address. More...
volatile bool | mHasPacketsToReceive
| Boolean that indicates if a packet was received. More...
QMutex | mMutex
JackTrip * | mJackTrip
| JackTrip mediator class. More...

## Detailed Description

UDP implementation of DataProtocol class.

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.

## Constructor & Destructor Documentation

## ◆ UdpDataProtocol()

UdpDataProtocol::UdpDataProtocol | ( | JackTrip * | jacktrip,
|  | const runModeT | runmode,
|  | int | bind_port,
|  | int | peer_port,
|  | unsigned int | udp_redundancy_factor = `1`
| ) |  |

The class constructor.

Parameters

  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::~UdpDataProtocol | ( |  | ) |
| virtual

The class destructor.

## Member Function Documentation

## ◆ bindSocket()

void UdpDataProtocol::bindSocket | ( | QUdpSocket & | UdpSocket | ) |
throw | ( | std::runtime_error
| ) |  |
| protected

Binds the UDP socket to the available address and specified port.

## ◆ getPeerAddressFromFirstPacket()

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.

Parameters

  peerHostAddress | QHostAddress to store the peer address
  port | Receiving port

## ◆ receivePacket()

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

Parameters

  buf | Buffer to store the recieved packet
  n | size of packet to receive

Returns
  number of bytes read, -1 on error

## ◆ receivePacketRedundancy()

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.

## ◆ run()

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.

Todo:
  fix this to avoid memory leaks

Implements DataProtocol.

## ◆ sendPacket()

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

Parameters

  buf | Buffer to send
  n | size of packet to receive

Returns
  number of bytes read, -1 on error

## ◆ sendPacketRedundancy()

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.

## ◆ setBindPort()

void UdpDataProtocol::setBindPort | ( | int | port | ) |
| inline

Sets the bind port number.

## ◆ setPeerAddress()

void UdpDataProtocol::setPeerAddress | ( | const char * | peerHostOrIP | ) |
throw | ( | std::invalid_argument
| ) |  |
| virtual

Set the Peer address to connect to.

Parameters

  peerHostOrIP | IPv4 number or host name

Implements DataProtocol.

## ◆ setPeerPort()

void UdpDataProtocol::setPeerPort | ( | int | port | ) |
| inline virtual

Sets the peer port number.

Implements DataProtocol.

## ◆ signalWatingTooLong

void UdpDataProtocol::signalWatingTooLong | ( | int | wait_msec | ) |
| signal

Signals when waiting every 10 milliseconds, with the total wait on wait_msec.

Parameters

  wait_msec | Total wait in milliseconds

## ◆ waitForReady()

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.

Returns
  returns true if there is data available for reading; otherwise it returns false (if an error occurred or the operation timed out)

The documentation for this class was generated from the following files:

- src/UdpDataProtocol.h
- src/release/moc_UdpDataProtocol.cpp
- src/UdpDataProtocol.cpp