File: netiobase.cpp

package info (click to toggle)
cutesdr 1.20-4
  • links: PTS
  • area: main
  • in suites: bookworm, bullseye, forky, sid, trixie
  • size: 2,848 kB
  • sloc: cpp: 18,902; makefile: 21; sh: 5
file content (363 lines) | stat: -rw-r--r-- 13,623 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
//////////////////////////////////////////////////////////////////////
// NetInterface.cpp: implementation of the CNetio and CUdp.
//
//  This class implements the low level UDP and TCP interfaces.
//
// History:
//	2012-12-12  Initial creation MSW
//	2013-02-05  Modified for CuteSDR. Now TCP runs in gui thread to simplify
//	2013-07-28  fixed DisconnectFromServerSlot bug
//	2015-03-26  Added  support for small MTU and UDP keepalive in case of port forwarding timeouts
//	2015-07-13  removed winsock dependency, Changed a few threading issues
/////////////////////////////////////////////////////////////////////
//==========================================================================================
// + + +   This Software is released under the "Simplified BSD License"  + + +
//Copyright 2010 Moe Wheatley. All rights reserved.
//
//Redistribution and use in source and binary forms, with or without modification, are
//permitted provided that the following conditions are met:
//
//   1. Redistributions of source code must retain the above copyright notice, this list of
//	  conditions and the following disclaimer.
//
//   2. Redistributions in binary form must reproduce the above copyright notice, this list
//	  of conditions and the following disclaimer in the documentation and/or other materials
//	  provided with the distribution.
//
//THIS SOFTWARE IS PROVIDED BY Moe Wheatley ``AS IS'' AND ANY EXPRESS OR IMPLIED
//WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
//FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL Moe Wheatley OR
//CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
//CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
//SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
//ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
//NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
//ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
//
//The views and conclusions contained in the software and documentation are those of the
//authors and should not be interpreted as representing official policies, either expressed
//or implied, of Moe Wheatley.
//=============================================================================
/*---------------------------------------------------------------------------*/
/*------------------------> I N C L U D E S <--------------------------------*/
/*---------------------------------------------------------------------------*/
#include <QtNetwork>
#include <QDebug>
#include "netiobase.h"

#define MSGSTATE_HDR1 0		//ASCP msg assembly states
#define MSGSTATE_HDR2 1
#define MSGSTATE_DATA 2

#define RCVBUF_SIZE_UDP 2097152

//*********    C U d p  I m p l e m e n t a t i o n       ***********
/////////////////////////////////////////////////////////////////////
// Constructor/Destructor
/////////////////////////////////////////////////////////////////////
CUdp::CUdp(QObject *parent) : m_pParent(parent)
{
qDebug()<<"CUdp constructor";
}

CUdp::~CUdp()
{
	CleanupThread();	//tell thread to cleanup after itself by calling ThreadExit()
qDebug()<<"CUdp destructor";
}

////////////////////////////////////////////////////////////////////////
//  called by CUdp worker thread to initialize its world
////////////////////////////////////////////////////////////////////////
void CUdp::ThreadInit()	//override called by new thread when started
{
	m_pUdpSocket = new QUdpSocket;
	m_pUdpFwdSocket = new QUdpSocket;
	connect(m_pParent, SIGNAL(StartUdp(quint32, quint32, quint16) ), this, SLOT( StartUdpSlot(quint32, quint32, quint16) ) );
	connect(m_pParent, SIGNAL(StopUdp() ), this, SLOT( StopUdpSlot() ) );
	connect(m_pParent, SIGNAL(SendUdpKeepalive() ), this, SLOT( SendUdpKeepaliveSlot() ) );

qDebug()<<"UDP Thread "<<this->thread()->currentThread();
}

/////////////////////////////////////////////////////////////////////
// Called by this worker thread to cleanup after itself
/////////////////////////////////////////////////////////////////////
void CUdp::ThreadExit()
{
	disconnect();
	if(m_pUdpSocket)
		delete m_pUdpSocket;
	if(m_pUdpFwdSocket)
		delete m_pUdpFwdSocket;
}

////////////////////////////////////////////////////////////////////////
// Binds and starts up UDP receive
////////////////////////////////////////////////////////////////////////
void CUdp::StartUdpSlot(quint32 ServerAdr, quint32 ClientAdr, quint16 ServerPort)
{
QHostAddress CIPAdr(ClientAdr);
	m_ServerIPAdr.setAddress(ServerAdr);
	m_ServerPort = ServerPort;

	m_pThread->setPriority(QThread::HighestPriority);
	if(m_pUdpSocket->bind( ServerPort ) )
	{
		//need to bump socket memory buffer size for higher speeds
		m_pUdpSocket->setSocketOption(QAbstractSocket::ReceiveBufferSizeSocketOption, RCVBUF_SIZE_UDP);
		connect(m_pUdpSocket, SIGNAL(readyRead()), this, SLOT(GotUdpData()));
		qDebug()<<"Udp Bind ok"<<CIPAdr<< ServerPort;
	}
	else
	{
		qDebug()<<"Udp Bind fail"<<CIPAdr << ServerPort<<m_pUdpSocket->errorString().toLocal8Bit();
	}
    m_pUdpFwdSocket->bind( m_FwdPort );
}

////////////////////////////////////////////////////////////////////////
// Stops and closes UDP socket
////////////////////////////////////////////////////////////////////////
void CUdp::StopUdpSlot()
{
	qDebug()<<"Stop Udp";
	if(m_pUdpSocket->isValid())
	{
		m_pUdpSocket->flush();
		m_pUdpSocket->close();
	}
}

////////////////////////////////////////////////////////////////////////
// Called via connect from UDP thread to process new rx data
////////////////////////////////////////////////////////////////////////
void CUdp::GotUdpData()
{
char pBuf[20000];
	//loop while there are still datagrams to get
	while( m_pUdpSocket->hasPendingDatagrams() )
	{
		qint64 n = m_pUdpSocket->pendingDatagramSize();
		if(n<20000)
		{
			m_UdpMutex.lock();
			m_pUdpSocket->readDatagram(pBuf, n);
			m_UdpMutex.unlock();
			((CNetio*)m_pParent)->ProcessUdpData(pBuf, n);
			if(m_UseUdpFwd)
				if( m_pUdpFwdSocket->isValid())
					m_pUdpFwdSocket->writeDatagram(pBuf, n, m_IPFwdAdr, m_FwdPort);
#if 0
static unsigned char seq=0;
if(seq!=(unsigned char)pBuf[3])
{
	seq = (unsigned char)pBuf[3];
	qDebug("%X %u",(unsigned char)pBuf[3], (unsigned int)n);
}
#endif
		}
	}
}

////////////////////////////////////////////////////////////////////////
// slot Called send UDP data keepalive
////////////////////////////////////////////////////////////////////////
void CUdp::SendUdpKeepaliveSlot()
{
char DummyData = 0x5A;
	if( m_pThread->isRunning() )
	{
		if( m_pUdpSocket->isValid())
		{
			m_UdpMutex.lock();
			m_pUdpSocket->writeDatagram(&DummyData, 1, m_ServerIPAdr, m_ServerPort);
			m_UdpMutex.unlock();
		}
	}
}

//***********   C N e t i o   I m p l e m e n t a t i o n    ********
/////////////////////////////////////////////////////////////////////
// Constructor/Destructor
/////////////////////////////////////////////////////////////////////
CNetio::CNetio()
{
qDebug()<<"CNetio constructor";
	m_pTcpClient = new QTcpSocket;
	connect(m_pTcpClient, SIGNAL(readyRead()), this, SLOT(ReadTcpData()));
	connect(m_pTcpClient, SIGNAL(stateChanged(QAbstractSocket::SocketState)),
			this, SLOT(TcpStateChanged(QAbstractSocket::SocketState)));
	m_pUdpIo = new CUdp(this);
	emit NewStatus(NOT_CONNECTED);
}

CNetio::~CNetio()
{
qDebug()<<"CNetio destructor";
	if(m_pTcpClient->state() == QAbstractSocket::ConnectedState)
	{
		m_pTcpClient->abort();
		m_pTcpClient->close();
	}
	disconnect();
	if(m_pTcpClient)
		delete m_pTcpClient;
	if(m_pUdpIo)
		delete m_pUdpIo;
}

///////////////////////////////////////////////////////////////////////////////
// sends signal to indicate network/sdr status change
///////////////////////////////////////////////////////////////////////////////
void CNetio::SendStatus(eStatus status)
{
	m_Status = status;
//qDebug()<<"Status = "<<m_Status;
	emit NewStatus( status );
}

///////////////////////////////////////////////////////////////////////////////
// Called to connect to TCP Server SDR
///////////////////////////////////////////////////////////////////////////////
void CNetio::ConnectToServer(QHostAddress IPAdr, quint16 Port)
{
	m_MsgState = MSGSTATE_HDR1;
	m_RxMsgIndex = 0;
	m_RxMsgLength = 0;
	m_ServerIPAdr = IPAdr;
	m_ServerPort = Port;
	m_pTcpClient->abort();
	m_pTcpClient->close();
	emit NewStatus(CONNECTING);
	m_pTcpClient->connectToHost(m_ServerIPAdr, m_ServerPort);
qDebug()<<"Connecting to "<<m_ServerIPAdr <<m_ServerPort;
}

///////////////////////////////////////////////////////////////////////////////
// Called to disconnect from TCP Server SDR
///////////////////////////////////////////////////////////////////////////////
void CNetio::DisconnectFromServerSlot()
{
	m_pTcpClient->abort();
	m_pTcpClient->close();
	emit StopUdp();
	emit NewStatus(NOT_CONNECTED);
}

///////////////////////////////////////////////////////////////////////////////
// Called by tcp socket when status changes
///////////////////////////////////////////////////////////////////////////////
void CNetio::TcpStateChanged(QAbstractSocket::SocketState State)
{
qDebug()<<State;
	switch(State)
	{
		case QAbstractSocket::ConnectingState:
		case QAbstractSocket::HostLookupState:
			SendStatus(CONNECTING);
			break;
		case QAbstractSocket::ConnectedState:
			emit StartUdp(m_ServerIPAdr.toIPv4Address(), m_pTcpClient->localAddress().toIPv4Address(), m_ServerPort);
			SendStatus(CONNECTED);
			break;
		case QAbstractSocket::ClosingState:
			emit StopUdp();
			SendStatus(NOT_CONNECTED);
			break;
		default:
			break;
	}
}

///////////////////////////////////////////////////////////////////////////////
// Called by tcp socket when new TCP data is available
///////////////////////////////////////////////////////////////////////////////
void CNetio::ReadTcpData()
{
quint8 pBuf[10000];
	if(m_pTcpClient->isValid())
	{
		qint64 n = m_pTcpClient->bytesAvailable();
		if(n<10000)
		{
			m_pTcpClient->read((char*)pBuf, n);
			AssembleAscpMsg(pBuf, n);
		}
	}
}


/*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
/*                  -------------------------------                          */
/*                 | A s s e m b l e A s c p M s g |                         */
/*                  -------------------------------                          */
/*  Helper function to assemble TCP data stream into ASCP formatted messages */
/*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
void CNetio::AssembleAscpMsg(quint8* Buf, int Len)
{
	for(int i=0; i<Len; i++)
	{	//process everything in Buf
		switch(m_MsgState)	//Simple state machine to get generic ASCP msg
		{
			case MSGSTATE_HDR1:		//get first header byte of ASCP msg
				m_RxAscpMsg.Buf8[0] = Buf[i];
				m_MsgState = MSGSTATE_HDR2;	//go to get second header byte state
				break;
			case MSGSTATE_HDR2:	//here for second byte of header
				m_RxAscpMsg.Buf8[1] = Buf[i];
				m_RxMsgLength = m_RxAscpMsg.Buf16[0] & 0x1FFF;
				m_RxMsgIndex = 2;
				if(2 == m_RxMsgLength)	//if msg has no parameters then we are done
				{
					m_MsgState = MSGSTATE_HDR1;	//go back to first state
					ParseAscpMsg( &m_RxAscpMsg );
				}
				else	//there are data bytes to fetch
				{
					if( 0 == m_RxMsgLength)
						m_RxMsgLength = 8192+2;	//handle special case of 8192 byte data message
					m_MsgState = MSGSTATE_DATA;	//go to data byte reading state
				}
				break;
			case MSGSTATE_DATA:	//try to read the rest of the message
				m_RxAscpMsg.Buf8[m_RxMsgIndex++] = Buf[i];
				if( m_RxMsgIndex >= m_RxMsgLength )
				{
					m_MsgState = MSGSTATE_HDR1;	//go back to first stage
					m_RxMsgIndex = 0;
					ParseAscpMsg( &m_RxAscpMsg );	//got complete msg so call virtual parse..
				}
				break;
		} //end switch statement
	}
}

/*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
/*                     -----------------------                               */
/*                    | S e n d A s c p M s g |                              */
/*                     -----------------------                               */
/*   Called send an ASCP formated message to the TCP socket                  */
/*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
void CNetio::SendAscpMsg(CAscpTxMsg* pMsg)
{
	if(m_pTcpClient->isValid())
		m_pTcpClient->write((char*)pMsg->Buf8, (qint64)pMsg->GetLength());
}

/*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
/*                     ---------------------                                 */
/*                    | S e n d U d p M s g |                                */
/*                     ---------------------                                 */
/*   Called send block to the UDP socket                                     */
/*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
void CNetio::SendUdpMsg(quint8* pBuf, int Length)
{
	m_pUdpIo->m_UdpMutex.lock();
	quint64 sent = m_UdpTxSocket.writeDatagram((char*)pBuf, (qint64)Length, m_ServerIPAdr, m_ServerPort );
	m_pUdpIo->m_UdpMutex.unlock();
	if(sent != (qint64)Length)
		qDebug()<<Length<< m_ServerIPAdr<< m_ServerPort;
}