2
2
* @file oscclient.h
3
3
*
4
4
*/
5
- /* Copyright (C) 2019-2023 by Arjan van Vught mailto:info@orangepi -dmx.nl
5
+ /* Copyright (C) 2019-2024 by Arjan van Vught mailto:info@gd32 -dmx.org
6
6
*
7
7
* Permission is hereby granted, free of charge, to any person obtaining a copy
8
8
* of this software and associated documentation files (the "Software"), to deal
29
29
#include < cstdint>
30
30
#include < cassert>
31
31
32
+ #include " osc.h"
33
+ #include " oscsimplesend.h"
32
34
#include " oscclientled.h"
33
35
36
+ #include " hardware.h"
37
+ #include " network.h"
38
+ #include " display.h"
39
+
40
+ #include " debug.h"
41
+
34
42
namespace oscclient {
35
43
static constexpr auto STORE = 944 ; // /< Configuration store in bytes
36
44
namespace defaults {
@@ -54,15 +62,92 @@ static constexpr uint32_t LED = oscclient::max::LED_COUNT * oscclient::max::LED_
54
62
55
63
class OscClient {
56
64
public:
57
- OscClient ();
58
- ~OscClient () {
59
- Stop ();
65
+ OscClient () :
66
+ m_nPortOutgoing (oscclient::defaults::PORT_OUTGOING),
67
+ m_nPortIncoming (oscclient::defaults::PORT_INCOMING),
68
+ m_nPingDelayMillis (oscclient::defaults::PING_DELAY_SECONDS * 1000 )
69
+ {
70
+ DEBUG_ENTRY
71
+
72
+ DEBUG_EXIT
73
+ }
74
+
75
+ void Start () {
76
+ DEBUG_ENTRY
77
+
78
+ assert (m_nHandle == -1 );
79
+ m_nHandle = Network::Get ()->Begin (m_nPortIncoming);
80
+ assert (m_nHandle != -1 );
81
+
82
+ DEBUG_EXIT
83
+ }
84
+
85
+ void Stop () {
86
+ DEBUG_ENTRY
87
+
88
+ assert (m_nHandle != -1 );
89
+ Network::Get ()->End (m_nPortIncoming);
90
+ m_nHandle = -1 ;
91
+
92
+ DEBUG_EXIT
60
93
}
61
94
62
- void Start ();
63
- void Stop ();
95
+ void Run () {
96
+ if (!m_bPingDisable) {
97
+ m_nCurrenMillis = Hardware::Get ()->Millis ();
98
+
99
+ if ((m_nCurrenMillis - m_nPreviousMillis) >= m_nPingDelayMillis) {
100
+ OscSimpleSend MsgSend (m_nHandle, m_nServerIP, m_nPortOutgoing, " /ping" , nullptr );
101
+ m_bPingSent = true ;
102
+ m_nPreviousMillis = m_nCurrenMillis;
103
+ m_nPingTimeMillis = m_nCurrenMillis;
104
+ DEBUG_PUTS (" Ping sent" );
105
+ }
106
+ }
107
+
108
+ uint32_t nRemoteIp;
109
+ uint16_t nRemotePort;
64
110
65
- void Run ();
111
+ const auto nBytesReceived = Network::Get ()->RecvFrom (m_nHandle, reinterpret_cast <const void **>(&m_pBuffer), &nRemoteIp, &nRemotePort);
112
+
113
+ if (__builtin_expect ((nBytesReceived == 0 ), 1 )) {
114
+ if (m_bPingSent && ((m_nCurrenMillis - m_nPingTimeMillis) >= 1000 )) {
115
+ if (m_bPongReceived) {
116
+ m_bPongReceived = false ;
117
+ Display::Get ()->TextStatus (" No /Pong" );
118
+ DEBUG_PUTS (" No /Pong" );
119
+ }
120
+ }
121
+ return ;
122
+ }
123
+
124
+ if (nRemoteIp != m_nServerIP) {
125
+ DEBUG_PRINTF (" Data not received from server " IPSTR , IP2STR (nRemoteIp));
126
+ return ;
127
+ }
128
+
129
+ if ((m_pOscClientLed != nullptr ) && (HandleLedMessage (nBytesReceived))) {
130
+ DEBUG_EXIT
131
+ return ;
132
+ }
133
+
134
+
135
+ if (!m_bPingDisable) {
136
+ if (!osc::is_match (m_pBuffer, " /pong" )) {
137
+ DEBUG_PUTS (m_pBuffer);
138
+ return ;
139
+ }
140
+
141
+
142
+ if (!m_bPongReceived) {
143
+ Display::Get ()->TextStatus (" Ping-Pong" );
144
+ DEBUG_PUTS (" Ping-Pong" );
145
+ }
146
+
147
+ m_bPongReceived = true ;
148
+ m_bPingSent = false ;
149
+ }
150
+ }
66
151
67
152
void Send (const char *pPath);
68
153
void SendCmd (uint32_t nCmd);
@@ -125,23 +210,23 @@ class OscClient {
125
210
bool HandleLedMessage (const uint16_t nBytesReceived);
126
211
127
212
private:
128
- uint32_t m_nServerIP { 0 };
129
213
uint16_t m_nPortOutgoing;
130
214
uint16_t m_nPortIncoming;
131
- int32_t m_nHandle { -1 };
132
- bool m_bPingDisable { false };
133
215
uint32_t m_nPingDelayMillis;
134
- bool m_bPingSent { false };
135
- bool m_bPongReceived { false };
136
- char *m_pBuffer { nullptr };
216
+ uint32_t m_nServerIP { 0 };
217
+ int32_t m_nHandle { -1 };
137
218
uint32_t m_nCurrenMillis { 0 };
138
219
uint32_t m_nPreviousMillis { 0 };
139
220
uint32_t m_nPingTimeMillis { 0 };
221
+ const char *m_pBuffer { nullptr };
222
+ bool m_bPingDisable { false };
223
+ bool m_bPingSent { false };
224
+ bool m_bPongReceived { false };
140
225
141
226
OscClientLed *m_pOscClientLed { nullptr };
142
227
143
- static char m_pCmds[oscclient::buffer::size::CMD];
144
- static char m_pLeds[oscclient::buffer::size::LED];
228
+ static inline char m_pCmds[oscclient::buffer::size::CMD];
229
+ static inline char m_pLeds[oscclient::buffer::size::LED];
145
230
};
146
231
147
232
#endif /* OSCCLIENT_H_ */
0 commit comments