Skip to content

Commit 143ef60

Browse files
sveljkovveljko
authored andcommitted
Remove retries and enable setting TCP/IP port (#26)
Retries were not limited, making it possible to have a _long_ blocking call, which is not a nice thing in Arduino `loop()`. Limitting would only partially fix the problem. Removing them altogether works much better and user can retry at her own convenience. Support for setting the TCP/IP port was added, which is needed when using a TLS/SSL (network) client class. Before, we always used HTTP port, which would not work.
1 parent 0987150 commit 143ef60

File tree

3 files changed

+57
-23
lines changed

3 files changed

+57
-23
lines changed

PubNubDefs.h

Lines changed: 48 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -254,6 +254,7 @@ class PubNub {
254254
d_uuid = 0;
255255
d_auth = 0;
256256
d_last_http_status_code_class = http_scc_unknown;
257+
set_port(http_port);
257258
}
258259

259260
/**
@@ -281,6 +282,16 @@ class PubNub {
281282
http_scc_server_error = 5
282283
};
283284

285+
/**
286+
* Possible TCP/IP ports to use when connecting to Pubnub.
287+
*/
288+
enum port_for_connection {
289+
/** Connect via HTTP on its default port */
290+
http_port,
291+
/** Connect via TLS (formerly known as SSL) on its default port) */
292+
tls_port
293+
};
294+
284295
/**
285296
* Set the UUID identification of PubNub client. This is useful
286297
* e.g. for presence identification.
@@ -299,6 +310,23 @@ class PubNub {
299310
* in begin()). */
300311
void set_auth(const char* auth) { d_auth = auth; }
301312

313+
/**
314+
* Set the TCP/IP port to use when connecting to Pubnub.
315+
* Basically, only call this if your client supports SSL/TLS
316+
* as you will need to set the `tls_port`.
317+
*/
318+
void set_port(port_for_connection port) {
319+
switch (port) {
320+
case http_port:
321+
default:
322+
d_port = 80;
323+
break;
324+
case tls_port:
325+
d_port = 443;
326+
break;
327+
}
328+
}
329+
302330
/**
303331
* Publish/Send a message (assumed to be well-formed JSON) to a
304332
* given channel.
@@ -397,6 +425,9 @@ class PubNub {
397425
const char* d_uuid;
398426
const char* d_auth;
399427

428+
/// TCP/IP port to use.
429+
unsigned d_port;
430+
400431
/// The HTTP status code class of the last PubNub transaction
401432
http_status_code_class d_last_http_status_code_class;
402433

@@ -575,14 +606,12 @@ inline PubNonSubClient* PubNub::publish(const char* channel,
575606
int timeout)
576607
{
577608
PubNonSubClient& client = publish_client;
578-
unsigned long t_start;
579609
int have_param = 0;
610+
unsigned long t_start = millis();
580611

581-
retry:
582-
t_start = millis();
583612
/* connect() timeout is about 30s, much lower than our usual
584613
* timeout is. */
585-
int rslt = client.connect(d_origin, 80);
614+
int rslt = client.connect(d_origin, d_port);
586615
if (rslt != 1) {
587616
DBGprint("Connection error ");
588617
DBGprintln(rslt);
@@ -638,30 +667,30 @@ inline PubNonSubClient* PubNub::publish(const char* channel,
638667
case PubNub_BH_OK:
639668
return &client;
640669
case PubNub_BH_ERROR:
670+
DBGprintln("publish() BH_ERROR");
641671
client.stop();
642672
while (client.connected())
643673
;
644674
return 0;
645675
case PubNub_BH_TIMEOUT:
676+
DBGprintln("publish() BH_TIMEOUT");
646677
client.stop();
647678
while (client.connected())
648679
;
649-
goto retry;
680+
return 0;
650681
}
651682
}
652683

653684

654685
inline PubSubClient* PubNub::subscribe(const char* channel, int timeout)
655686
{
656687
PubSubClient& client = subscribe_client;
657-
unsigned long t_start;
658688
int have_param = 0;
689+
unsigned long t_start = millis();
659690

660-
retry:
661-
t_start = millis();
662691
/* connect() timeout is about 30s, much lower than our usual
663692
* timeout is. */
664-
if (!client.connect(d_origin, 80)) {
693+
if (!client.connect(d_origin, d_port)) {
665694
DBGprintln("Connection error");
666695
client.stop();
667696
return 0;
@@ -716,28 +745,30 @@ inline PubSubClient* PubNub::subscribe(const char* channel, int timeout)
716745
return &client;
717746

718747
case PubNub_BH_ERROR:
748+
DBGprintln("subscribe() BH_ERROR");
719749
client.stop();
720750
while (client.connected())
721751
;
722752
return 0;
723753

724754
case PubNub_BH_TIMEOUT:
755+
DBGprintln("subscribe() BH_TIMEOUT");
725756
client.stop();
757+
DBGprintln("subscribe() BH_TIMEOUT stopped");
726758
while (client.connected())
727759
;
728-
goto retry;
760+
DBGprintln("subscribe() BH_TIMEOUT disconnected");
761+
return 0;
729762
}
730763
}
731764

732765

733766
inline PubNonSubClient* PubNub::history(const char* channel, int limit, int timeout)
734767
{
735768
PubNonSubClient& client = history_client;
736-
unsigned long t_start;
769+
unsigned long t_start = millis();
737770

738-
retry:
739-
t_start = millis();
740-
if (!client.connect(d_origin, 80)) {
771+
if (!client.connect(d_origin, d_port)) {
741772
DBGprintln("Connection error");
742773
client.stop();
743774
return 0;
@@ -757,15 +788,17 @@ inline PubNonSubClient* PubNub::history(const char* channel, int limit, int time
757788
case PubNub_BH_OK:
758789
return &client;
759790
case PubNub_BH_ERROR:
791+
DBGprintln("history() BH_ERROR");
760792
client.stop();
761793
while (client.connected())
762794
;
763795
return 0;
764796
case PubNub_BH_TIMEOUT:
797+
DBGprintln("history() BH_TIMEOUT");
765798
client.stop();
766799
while (client.connected())
767800
;
768-
goto retry;
801+
return 0;
769802
}
770803
}
771804

README.md

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -323,14 +323,14 @@ that, it expects a working network. We provide examples for some HW.
323323

324324
### ESP8266
325325

326-
In previous section we already showed what to do to use ESP8266, but
327-
in most versions of ESP8266 support for Arduino, some of the (de-facto)
328-
standard library functions that we use are missing. To use our own
329-
implementation of them, `#define` a macro constant before you include
330-
`Pubnub.h`, like this:
326+
In previous section we already showed how to use ESP8266, but in some
327+
(older) versions of ESP8266 support for Arduino, some of the
328+
(de-facto) standard library functions that we use are missing. To use
329+
our own implementation of them, `#define` a macro constant before you
330+
include `PubNub.h`, like this:
331331

332332
#define PUBNUB_DEFINE_STRSPN_AND_STRNCASECMP
333-
#include <Pubnub.h>
333+
#include <PubNub.h>
334334

335335
## Notes
336336

@@ -346,6 +346,7 @@ implementation of them, `#define` a macro constant before you include
346346
most Arduino compatible boards. But, some shields/boards have SSL
347347
("Secure") clients and you may succeed in using them instead of the
348348
non-secure clients (`WiFiClientSecure` instead of `WiFiClient`).
349+
But don't forget to `PubNub.set_port(PubNub.tls_port)`.
349350

350351
* We re-resolve the origin server IP address before each request.
351352
This means some slow-down for intensive communication, but we rather
@@ -358,7 +359,7 @@ implementation of them, `#define` a macro constant before you include
358359
waste precious RAM by pre-allocating buffers that are never needed.
359360

360361
* The optional timeout parameter allows you to specify a timeout
361-
period after which the subscribe call shall be retried. Note
362+
period after which the subscribe call shall be cancelled. Note
362363
that this timeout is applied only for reading response, not for
363364
connecting or sending data; use retransmission parameters of
364365
the network library to tune this. As a rule of thumb, timeout

library.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name=Pubnub
2-
version=3.0.1
2+
version=3.1.0
33
author=Vladimir Veljkovic <[email protected]>
44
maintainer=Vladimir Veljkovic <[email protected]>
55
sentence=Pubnub SDK for Arduino.

0 commit comments

Comments
 (0)