Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Q
qgroundcontrol
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Valentin Platzgummer
qgroundcontrol
Commits
765dfbbd
Commit
765dfbbd
authored
Jun 27, 2015
by
dogmaphobic
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Make the fix dependent on the Qt version.
parent
ac7e80d6
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
71 additions
and
42 deletions
+71
-42
UDPLink.cc
src/comm/UDPLink.cc
+69
-41
UDPLink.h
src/comm/UDPLink.h
+2
-1
No files found.
src/comm/UDPLink.cc
View file @
765dfbbd
...
...
@@ -28,6 +28,13 @@ This file is part of the QGROUNDCONTROL project
*
*/
#include <QtGlobal>
#if QT_VERSION > 0x050401
#define UDP_BROKEN_SIGNAL 1
#else
#define UDP_BROKEN_SIGNAL 0
#endif
#include <QTimer>
#include <QList>
#include <QDebug>
...
...
@@ -100,6 +107,7 @@ UDPLink::~UDPLink()
_disconnect
();
// Tell the thread to exit
_running
=
false
;
quit
();
// Wait for it to exit
wait
();
while
(
_outQueue
.
count
()
>
0
)
{
...
...
@@ -115,13 +123,24 @@ UDPLink::~UDPLink()
void
UDPLink
::
run
()
{
if
(
_hardwareConnect
())
{
while
(
true
)
{
readBytes
();
if
(
_writeBytes
()
&&
_running
)
continue
;
if
(
!
_running
)
break
;
this
->
msleep
(
50
);
if
(
UDP_BROKEN_SIGNAL
)
{
bool
loop
=
false
;
while
(
true
)
{
//-- Anything to read?
loop
=
_socket
->
hasPendingDatagrams
();
if
(
loop
)
{
readBytes
();
}
//-- Loop right away if busy
if
((
_dequeBytes
()
||
loop
)
&&
_running
)
continue
;
if
(
!
_running
)
break
;
//-- Settle down (it gets here if there is nothing to read or write)
this
->
msleep
(
50
);
}
}
else
{
exec
();
}
}
if
(
_socket
)
{
...
...
@@ -154,51 +173,58 @@ void UDPLink::removeHost(const QString& host)
_config
->
removeHost
(
host
);
}
#define UDPLINK_DEBUG 0
void
UDPLink
::
writeBytes
(
const
char
*
data
,
qint64
size
)
{
if
(
!
_socket
)
{
return
;
}
QByteArray
*
qdata
=
new
QByteArray
(
data
,
size
);
QMutexLocker
lock
(
&
_mutex
);
_outQueue
.
enqueue
(
qdata
);
if
(
UDP_BROKEN_SIGNAL
)
{
QByteArray
*
qdata
=
new
QByteArray
(
data
,
size
);
QMutexLocker
lock
(
&
_mutex
);
_outQueue
.
enqueue
(
qdata
);
}
else
{
_sendBytes
(
data
,
size
);
}
}
bool
UDPLink
::
_
writ
eBytes
()
bool
UDPLink
::
_
dequ
eBytes
()
{
QMutexLocker
lock
(
&
_mutex
);
if
(
_outQueue
.
count
()
>
0
)
{
QByteArray
*
qdata
=
_outQueue
.
dequeue
();
lock
.
unlock
();
QStringList
goneHosts
;
// Send to all connected systems
QString
host
;
int
port
;
if
(
_config
->
firstHost
(
host
,
port
))
{
do
{
QHostAddress
currentHost
(
host
);
if
(
_socket
->
writeDatagram
(
qdata
->
data
(),
qdata
->
size
(),
currentHost
,
(
quint16
)
port
)
<
0
)
{
// This host is gone. Add to list to be removed
goneHosts
.
append
(
host
);
}
// Log the amount and time written out for future data rate calculations.
QMutexLocker
dataRateLocker
(
&
dataRateMutex
);
logDataRateToBuffer
(
outDataWriteAmounts
,
outDataWriteTimes
,
&
outDataIndex
,
qdata
->
size
(),
QDateTime
::
currentMSecsSinceEpoch
());
}
while
(
_config
->
nextHost
(
host
,
port
));
//-- Remove hosts that are no longer there
foreach
(
QString
ghost
,
goneHosts
)
{
_config
->
removeHost
(
ghost
);
}
}
_sendBytes
(
qdata
->
data
(),
qdata
->
size
());
delete
qdata
;
lock
.
relock
();
}
return
(
_outQueue
.
count
()
>
0
);
}
void
UDPLink
::
_sendBytes
(
const
char
*
data
,
qint64
size
)
{
QStringList
goneHosts
;
// Send to all connected systems
QString
host
;
int
port
;
if
(
_config
->
firstHost
(
host
,
port
))
{
do
{
QHostAddress
currentHost
(
host
);
if
(
_socket
->
writeDatagram
(
data
,
size
,
currentHost
,
(
quint16
)
port
)
<
0
)
{
// This host is gone. Add to list to be removed
goneHosts
.
append
(
host
);
}
// Log the amount and time written out for future data rate calculations.
QMutexLocker
dataRateLocker
(
&
dataRateMutex
);
logDataRateToBuffer
(
outDataWriteAmounts
,
outDataWriteTimes
,
&
outDataIndex
,
size
,
QDateTime
::
currentMSecsSinceEpoch
());
}
while
(
_config
->
nextHost
(
host
,
port
));
//-- Remove hosts that are no longer there
foreach
(
QString
ghost
,
goneHosts
)
{
_config
->
removeHost
(
ghost
);
}
}
}
/**
* @brief Read a number of bytes from the interface.
**/
...
...
@@ -235,7 +261,7 @@ void UDPLink::readBytes()
// would trigger this.
// Add host to broadcast list if not yet present, or update its port
_config
->
addHost
(
sender
.
toString
(),
(
int
)
senderPort
);
if
(
!
_running
)
if
(
UDP_BROKEN_SIGNAL
&&
!
_running
)
break
;
}
}
...
...
@@ -248,6 +274,7 @@ void UDPLink::readBytes()
bool
UDPLink
::
_disconnect
(
void
)
{
_running
=
false
;
quit
();
wait
();
if
(
_socket
)
{
// Make sure delete happen on correct thread
...
...
@@ -255,9 +282,8 @@ bool UDPLink::_disconnect(void)
_socket
=
NULL
;
emit
disconnected
();
}
// TODO When would this ever return false?
_connectState
=
false
;
return
!
_connectStat
e
;
return
tru
e
;
}
/**
...
...
@@ -267,17 +293,15 @@ bool UDPLink::_disconnect(void)
**/
bool
UDPLink
::
_connect
(
void
)
{
if
(
this
->
isRunning
())
if
(
this
->
isRunning
()
||
_running
)
{
_running
=
false
;
quit
();
wait
();
}
// TODO When would this ever return false?
bool
connected
=
true
;
// I see no reason to run this in "HighPriority"
_running
=
true
;
start
(
NormalPriority
);
return
connected
;
return
true
;
}
bool
UDPLink
::
_hardwareConnect
()
...
...
@@ -292,6 +316,10 @@ bool UDPLink::_hardwareConnect()
_connectState
=
_socket
->
bind
(
host
,
_config
->
localPort
(),
QAbstractSocket
::
ReuseAddressHint
);
if
(
_connectState
)
{
_registerZeroconf
(
_config
->
localPort
(),
kZeroconfRegistration
);
//-- Connect signal if this version of Qt is not broken
if
(
!
UDP_BROKEN_SIGNAL
)
{
QObject
::
connect
(
_socket
,
SIGNAL
(
readyRead
()),
this
,
SLOT
(
readBytes
()));
}
emit
connected
();
}
else
{
emit
communicationError
(
"UDP Link Error"
,
"Error binding UDP port"
);
...
...
src/comm/UDPLink.h
View file @
765dfbbd
...
...
@@ -219,7 +219,8 @@ private:
QMutex
_mutex
;
QQueue
<
QByteArray
*>
_outQueue
;
bool
_writeBytes
();
bool
_dequeBytes
();
void
_sendBytes
(
const
char
*
data
,
qint64
size
);
};
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment