SDRAngel  4.11.5
Developer docs for <a href="https://github.com/f4exb/sdrangel">SDRangel<\a>, an Open Source Qt5 / OpenGL 3.0+ SDR and signal analyzer frontend to various hardware.
udpsinkutil.h
Go to the documentation of this file.
1 // Copyright (C) 2015 F4EXB //
3 // written by Edouard Griffiths //
4 // //
5 // This program is free software; you can redistribute it and/or modify //
6 // it under the terms of the GNU General Public License as published by //
7 // the Free Software Foundation as version 3 of the License, or //
8 // (at your option) any later version. //
9 // //
10 // This program is distributed in the hope that it will be useful, //
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of //
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the //
13 // GNU General Public License V3 for more details. //
14 // //
15 // You should have received a copy of the GNU General Public License //
16 // along with this program. If not, see <http://www.gnu.org/licenses/>. //
18 
19 #ifndef INCLUDE_UTIL_UDPSINK_H_
20 #define INCLUDE_UTIL_UDPSINK_H_
21 
22 #include <stdint.h>
23 #include <QObject>
24 #include <QUdpSocket>
25 #include <QHostAddress>
26 
27 #include <cassert>
28 
29 template<typename T>
31 {
32 public:
33  UDPSinkUtil(QObject *parent, unsigned int udpSize) :
34  m_udpSize(udpSize),
35  m_udpSamples(udpSize/sizeof(T)),
36  m_address(QHostAddress::LocalHost),
37  m_port(9999),
39  {
40  assert(m_udpSamples > 0);
42  m_socket = new QUdpSocket(parent);
43  }
44 
45  UDPSinkUtil(QObject *parent, unsigned int udpSize, unsigned int port) :
46  m_udpSize(udpSize),
47  m_udpSamples(udpSize/sizeof(T)),
48  m_address(QHostAddress::LocalHost),
49  m_port(port),
51  {
52  assert(m_udpSamples > 0);
54  m_socket = new QUdpSocket(parent);
55  }
56 
57  UDPSinkUtil (QObject *parent, unsigned int udpSize, QHostAddress& address, unsigned int port) :
58  m_udpSize(udpSize),
59  m_udpSamples(udpSize/sizeof(T)),
60  m_address(address),
61  m_port(port),
63  {
64  assert(m_udpSamples > 0);
66  m_socket = new QUdpSocket(parent);
67  }
68 
70  {
71  delete[] m_sampleBuffer;
72  delete m_socket;
73  }
74 
75  void moveToThread(QThread *thread)
76  {
77  m_socket->moveToThread(thread);
78  }
79 
80  void setAddress(QString& address) { m_address.setAddress(address); }
81  void setPort(unsigned int port) { m_port = port; }
82 
83  void setDestination(const QString& address, int port)
84  {
85  m_address.setAddress(const_cast<QString&>(address));
86  m_port = port;
87  }
88 
92  void write(T sample)
93  {
95  {
98  }
99  else
100  {
101  m_socket->writeDatagram((const char*)&m_sampleBuffer[0], (qint64 ) m_udpSize, m_address, m_port);
102  m_sampleBuffer[0] = sample;
104  }
105  }
106 
110  void write(T *samples, int nbSamples)
111  {
112  int samplesIndex = 0;
113 
114  if (m_sampleBufferIndex + nbSamples > m_udpSamples) // fill remainder of buffer and send it
115  {
116  memcpy(&m_sampleBuffer[m_sampleBufferIndex], &samples[samplesIndex], (m_udpSamples - m_sampleBufferIndex)*sizeof(T)); // fill remainder of buffer
117  m_socket->writeDatagram((const char*)&m_sampleBuffer[0], (qint64 ) m_udpSize, m_address, m_port); // send buffer
118  samplesIndex += (m_udpSamples - m_sampleBufferIndex);
119  nbSamples -= (m_udpSamples - m_sampleBufferIndex);
120  m_sampleBufferIndex = 0;
121  }
122 
123  while (nbSamples > m_udpSamples) // send directly from input without buffering
124  {
125  m_socket->writeDatagram((const char*)&samples[samplesIndex], (qint64 ) m_udpSize, m_address, m_port);
126  samplesIndex += m_udpSamples;
127  nbSamples -= m_udpSamples;
128  }
129 
130  memcpy(&m_sampleBuffer[m_sampleBufferIndex], &samples[samplesIndex], nbSamples*sizeof(T)); // copy remainder of input to buffer
131  }
132 
133 private:
136  QHostAddress m_address;
137  unsigned int m_port;
138  QUdpSocket *m_socket;
141 };
142 
143 
144 
145 #endif /* INCLUDE_UTIL_UDPSINK_H_ */
void setDestination(const QString &address, int port)
Definition: udpsinkutil.h:83
void write(T *samples, int nbSamples)
Definition: udpsinkutil.h:110
UDPSinkUtil(QObject *parent, unsigned int udpSize, unsigned int port)
Definition: udpsinkutil.h:45
void setPort(unsigned int port)
Definition: udpsinkutil.h:81
void setAddress(QString &address)
Definition: udpsinkutil.h:80
void write(T sample)
Definition: udpsinkutil.h:92
QHostAddress m_address
Definition: udpsinkutil.h:136
void moveToThread(QThread *thread)
Definition: udpsinkutil.h:75
unsigned int m_port
Definition: udpsinkutil.h:137
int m_udpSamples
Definition: udpsinkutil.h:135
UDPSinkUtil(QObject *parent, unsigned int udpSize)
Definition: udpsinkutil.h:33
T * m_sampleBuffer
Definition: udpsinkutil.h:139
int m_sampleBufferIndex
Definition: udpsinkutil.h:139
QUdpSocket * m_socket
Definition: udpsinkutil.h:138
UDPSinkUtil(QObject *parent, unsigned int udpSize, QHostAddress &address, unsigned int port)
Definition: udpsinkutil.h:57