Skip to content

Commit dd9973c

Browse files
committed
Move the test non-block code into the network callbacks, which know if the non-blocking is allowed.
1 parent 97cb820 commit dd9973c

File tree

2 files changed

+52
-70
lines changed

2 files changed

+52
-70
lines changed

examples/mqttnet.c

+52
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,10 @@ typedef struct MulticastCtx {
3232
} MulticastCtx;
3333
#endif
3434

35+
#ifndef WOLFMQTT_TEST_NONBLOCK_TIMES
36+
#define WOLFMQTT_TEST_NONBLOCK_TIMES 1
37+
#endif
38+
3539
/* Private functions */
3640

3741
/* -------------------------------------------------------------------------- */
@@ -643,11 +647,16 @@ static int NetWrite(void *context, const byte* buf, int buf_len,
643647
int timeout_ms)
644648
{
645649
SocketContext *sock = (SocketContext*)context;
650+
MQTTCtx* mqttCtx;
646651
int rc;
647652
SOERROR_T so_error = 0;
648653
#ifndef WOLFMQTT_NO_TIMEOUT
649654
struct timeval tv;
650655
#endif
656+
#if defined(WOLFMQTT_NONBLOCK) && defined(WOLFMQTT_TEST_NONBLOCK)
657+
static int testNbWriteAlt = 0;
658+
static int testSmallerWrite = 0;
659+
#endif
651660

652661
if (context == NULL || buf == NULL || buf_len <= 0) {
653662
return MQTT_CODE_ERROR_BAD_ARG;
@@ -656,6 +665,27 @@ static int NetWrite(void *context, const byte* buf, int buf_len,
656665
if (sock->fd == SOCKET_INVALID)
657666
return MQTT_CODE_ERROR_BAD_ARG;
658667

668+
mqttCtx = sock->mqttCtx;
669+
(void)mqttCtx;
670+
671+
#if defined(WOLFMQTT_NONBLOCK) && defined(WOLFMQTT_TEST_NONBLOCK)
672+
if (mqttCtx->useNonBlockMode) {
673+
if (testNbWriteAlt < WOLFMQTT_TEST_NONBLOCK_TIMES) {
674+
testNbWriteAlt++;
675+
return MQTT_CODE_CONTINUE;
676+
}
677+
testNbWriteAlt = 0;
678+
if (!testSmallerWrite) {
679+
if (buf_len > 2)
680+
buf_len /= 2;
681+
testSmallerWrite = 1;
682+
}
683+
else {
684+
testSmallerWrite = 0;
685+
}
686+
}
687+
#endif
688+
659689
#ifndef WOLFMQTT_NO_TIMEOUT
660690
/* Setup timeout */
661691
setup_timeout(&tv, timeout_ms);
@@ -706,6 +736,10 @@ static int NetRead_ex(void *context, byte* buf, int buf_len,
706736
fd_set errfds;
707737
struct timeval tv;
708738
#endif
739+
#if defined(WOLFMQTT_NONBLOCK) && defined(WOLFMQTT_TEST_NONBLOCK)
740+
static int testNbReadAlt = 0;
741+
static int testSmallerRead = 0;
742+
#endif
709743

710744
if (context == NULL || buf == NULL || buf_len <= 0) {
711745
return MQTT_CODE_ERROR_BAD_ARG;
@@ -721,6 +755,24 @@ static int NetRead_ex(void *context, byte* buf, int buf_len,
721755
mqttCtx = sock->mqttCtx;
722756
(void)mqttCtx;
723757

758+
#if defined(WOLFMQTT_NONBLOCK) && defined(WOLFMQTT_TEST_NONBLOCK)
759+
if (mqttCtx->useNonBlockMode) {
760+
if (testNbReadAlt < WOLFMQTT_TEST_NONBLOCK_TIMES) {
761+
testNbReadAlt++;
762+
return MQTT_CODE_CONTINUE;
763+
}
764+
testNbReadAlt = 0;
765+
if (!testSmallerRead) {
766+
if (buf_len > 2)
767+
buf_len /= 2;
768+
testSmallerRead = 1;
769+
}
770+
else {
771+
testSmallerRead = 0;
772+
}
773+
}
774+
#endif
775+
724776
#ifndef WOLFMQTT_NO_TIMEOUT
725777
/* Setup timeout */
726778
setup_timeout(&tv, timeout_ms);

src/mqtt_socket.c

-70
Original file line numberDiff line numberDiff line change
@@ -42,11 +42,6 @@
4242
#undef WOLFMQTT_DEBUG_SOCKET
4343
#endif
4444

45-
/* #define WOLFMQTT_TEST_NONBLOCK */
46-
#ifdef WOLFMQTT_TEST_NONBLOCK
47-
#define WOLFMQTT_TEST_NONBLOCK_TIMES 1
48-
#endif
49-
5045
/* lwip */
5146
#ifdef WOLFSSL_LWIP
5247
#undef read
@@ -64,18 +59,6 @@ int MqttSocket_TlsSocketReceive(WOLFSSL* ssl, char *buf, int sz,
6459
MqttClient *client = (MqttClient*)ptr;
6560
(void)ssl; /* Not used */
6661

67-
#if defined(WOLFMQTT_NONBLOCK) && defined(WOLFMQTT_TEST_NONBLOCK)
68-
static int testSmallerTlsRead = 0;
69-
if (!testSmallerTlsRead) {
70-
if (sz > 2)
71-
sz /= 2;
72-
testSmallerTlsRead = 1;
73-
}
74-
else {
75-
testSmallerTlsRead = 0;
76-
}
77-
#endif
78-
7962
rc = client->net->read(client->net->context, (byte*)buf, sz,
8063
client->tls.timeout_ms);
8164

@@ -99,18 +82,6 @@ int MqttSocket_TlsSocketSend(WOLFSSL* ssl, char *buf, int sz,
9982
MqttClient *client = (MqttClient*)ptr;
10083
(void)ssl; /* Not used */
10184

102-
#if defined(WOLFMQTT_NONBLOCK) && defined(WOLFMQTT_TEST_NONBLOCK)
103-
static int testSmallerTlsWrite = 0;
104-
if (!testSmallerTlsWrite) {
105-
if (sz > 2)
106-
sz /= 2;
107-
testSmallerTlsWrite = 1;
108-
}
109-
else {
110-
testSmallerTlsWrite = 0;
111-
}
112-
#endif
113-
11485
rc = client->net->write(client->net->context, (byte*)buf, sz,
11586
client->tls.timeout_ms);
11687

@@ -153,15 +124,6 @@ static int MqttSocket_WriteDo(MqttClient *client, const byte* buf, int buf_len,
153124
{
154125
int rc;
155126

156-
#if defined(WOLFMQTT_NONBLOCK) && defined(WOLFMQTT_TEST_NONBLOCK)
157-
static int testNbWriteAlt = 0;
158-
if (testNbWriteAlt < WOLFMQTT_TEST_NONBLOCK_TIMES) {
159-
testNbWriteAlt++;
160-
return MQTT_CODE_CONTINUE;
161-
}
162-
testNbWriteAlt = 0;
163-
#endif
164-
165127
#ifdef ENABLE_MQTT_TLS
166128
if (MqttClient_Flags(client,0,0) & MQTT_CLIENT_FLAG_IS_TLS) {
167129
client->tls.timeout_ms = timeout_ms;
@@ -194,18 +156,6 @@ static int MqttSocket_WriteDo(MqttClient *client, const byte* buf, int buf_len,
194156
else
195157
#endif /* ENABLE_MQTT_TLS */
196158
{
197-
#if defined(WOLFMQTT_NONBLOCK) && defined(WOLFMQTT_TEST_NONBLOCK)
198-
static int testSmallerWrite = 0;
199-
if (!testSmallerWrite) {
200-
if (buf_len > 2)
201-
buf_len /= 2;
202-
testSmallerWrite = 1;
203-
}
204-
else {
205-
testSmallerWrite = 0;
206-
}
207-
#endif
208-
209159
rc = client->net->write(client->net->context, buf, buf_len,
210160
timeout_ms);
211161
}
@@ -276,15 +226,6 @@ static int MqttSocket_ReadDo(MqttClient *client, byte* buf, int buf_len,
276226
{
277227
int rc;
278228

279-
#if defined(WOLFMQTT_NONBLOCK) && defined(WOLFMQTT_TEST_NONBLOCK)
280-
static int testNbReadAlt = 0;
281-
if (testNbReadAlt < WOLFMQTT_TEST_NONBLOCK_TIMES) {
282-
testNbReadAlt++;
283-
return MQTT_CODE_CONTINUE;
284-
}
285-
testNbReadAlt = 0;
286-
#endif
287-
288229
#ifdef ENABLE_MQTT_TLS
289230
if (MqttClient_Flags(client,0,0) & MQTT_CLIENT_FLAG_IS_TLS) {
290231
client->tls.timeout_ms = timeout_ms;
@@ -320,17 +261,6 @@ static int MqttSocket_ReadDo(MqttClient *client, byte* buf, int buf_len,
320261
else
321262
#endif /* ENABLE_MQTT_TLS */
322263
{
323-
#if defined(WOLFMQTT_NONBLOCK) && defined(WOLFMQTT_TEST_NONBLOCK)
324-
static int testSmallerRead = 0;
325-
if (!testSmallerRead) {
326-
if (buf_len > 2)
327-
buf_len /= 2;
328-
testSmallerRead = 1;
329-
}
330-
else {
331-
testSmallerRead = 0;
332-
}
333-
#endif
334264
rc = client->net->read(client->net->context, buf, buf_len, timeout_ms);
335265
}
336266

0 commit comments

Comments
 (0)