Skip to content

Commit 20909f6

Browse files
committed
A few fixes
C++ wrapper now "translates" empty channel or channel group string to "NULL" for C functions. Also, a bug in handling residual messages on errors is fixed.
1 parent 8f0e0b4 commit 20909f6

File tree

2 files changed

+31
-17
lines changed

2 files changed

+31
-17
lines changed

core/pubnub_ccore.c

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,7 @@ static int simple_parse_response(struct pbcc_context *p)
115115
{
116116
char *reply = p->http_reply;
117117
int replylen = p->http_buf_len;
118+
DEBUG_PRINTF("simple_parse_response()\n");
118119
if (replylen < 2) {
119120
return -1;
120121
}
@@ -168,6 +169,7 @@ int pbcc_parse_time_response(struct pbcc_context *p)
168169

169170
int pbcc_parse_history_response(struct pbcc_context *p)
170171
{
172+
DEBUG_PRINTF("pbcc_parse_history_response()\n");
171173
return simple_parse_response(p);
172174
}
173175

@@ -189,8 +191,7 @@ int pbcc_parse_presence_response(struct pbcc_context *p)
189191
return -1;
190192
}
191193

192-
p->chan_ofs = 0;
193-
p->chan_end = 0;
194+
p->chan_ofs = p->chan_end = 0;
194195

195196
p->msg_ofs = 0;
196197
p->msg_end = replylen;
@@ -207,8 +208,7 @@ enum pubnub_res pbcc_parse_channel_registry_response(struct pbcc_context *p)
207208
p->chan_ofs = 0;
208209
p->chan_end = p->http_buf_len;
209210

210-
p->msg_ofs = 0;
211-
p->msg_end = 0;
211+
p->msg_ofs = p->msg_end = 0;
212212

213213
/* We should probably also check that there is a key "service"
214214
with value "channel-registry". Maybe even that there is a key
@@ -424,7 +424,7 @@ enum pubnub_res pbcc_subscribe_prep(struct pbcc_context *p, const char *channel,
424424
}
425425

426426
p->http_content_len = 0;
427-
p->msg_ofs = 0;
427+
p->msg_ofs = p->msg_end = 0;
428428

429429
p->http_buf_len = snprintf(
430430
p->http_buf, sizeof(p->http_buf),
@@ -475,7 +475,7 @@ enum pubnub_res pbcc_time_prep(struct pbcc_context *pb)
475475
}
476476

477477
pb->http_content_len = 0;
478-
pb->msg_ofs = 0;
478+
pb->msg_ofs = pb->msg_end = 0;
479479

480480
pb->http_buf_len = snprintf(
481481
pb->http_buf, sizeof pb->http_buf,
@@ -503,7 +503,7 @@ enum pubnub_res pbcc_history_prep(struct pbcc_context *pb, const char *channel,
503503
}
504504

505505
pb->http_content_len = 0;
506-
pb->msg_ofs = 0;
506+
pb->msg_ofs = pb->msg_end = 0;
507507

508508
pb->http_buf_len = snprintf(
509509
pb->http_buf, sizeof pb->http_buf,
@@ -531,7 +531,7 @@ enum pubnub_res pbcc_historyv2_prep(struct pbcc_context *pb, const char *channel
531531
}
532532

533533
pb->http_content_len = 0;
534-
pb->msg_ofs = 0;
534+
pb->msg_ofs = pb->msg_end = 0;
535535

536536
pb->http_buf_len = snprintf(
537537
pb->http_buf, sizeof pb->http_buf,
@@ -561,7 +561,7 @@ enum pubnub_res pbcc_here_now_prep(struct pbcc_context *pb, const char *channel,
561561
}
562562

563563
pb->http_content_len = 0;
564-
pb->msg_ofs = 0;
564+
pb->msg_ofs = pb->msg_end = 0;
565565

566566
pb->http_buf_len = snprintf(
567567
pb->http_buf, sizeof pb->http_buf,
@@ -588,7 +588,7 @@ enum pubnub_res pbcc_where_now_prep(struct pbcc_context *pb, const char *uuid)
588588
}
589589

590590
pb->http_content_len = 0;
591-
pb->msg_ofs = 0;
591+
pb->msg_ofs = pb->msg_end = 0;
592592

593593
pb->http_buf_len = snprintf(
594594
pb->http_buf, sizeof pb->http_buf,

cpp/pubnub_common.hpp

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -263,7 +263,9 @@ namespace pubnub {
263263
/// Subscribes to @p channel and/or @p channel_group
264264
/// @see pubnub_subscribe
265265
futres subscribe(std::string const &channel, std::string const &channel_group = "") {
266-
return doit(pubnub_subscribe(d_pb, channel.c_str(), channel_group.c_str()));
266+
char const *ch = channel.empty() ? 0 : channel.c_str();
267+
char const *gr = channel_group.empty() ? 0 : channel_group.c_str();
268+
return doit(pubnub_subscribe(d_pb, ch, gr));
267269
}
268270

269271
/// Pass a vector of channels in the @p channel and a vector
@@ -276,7 +278,9 @@ namespace pubnub {
276278
/// Leaves a @p channel and/or @p channel_group
277279
/// @see pubnub_leave
278280
futres leave(std::string const &channel, std::string const &channel_group) {
279-
return doit(pubnub_leave(d_pb, channel.c_str(), channel_group.c_str()));
281+
char const *ch = channel.empty() ? 0 : channel.c_str();
282+
char const *gr = channel_group.empty() ? 0 : channel_group.c_str();
283+
return doit(pubnub_leave(d_pb, ch, gr));
280284
}
281285

282286
/// Pass a vector of channels in the @p channel and a vector
@@ -297,7 +301,9 @@ namespace pubnub {
297301
/// messages to retrieve
298302
/// @see pubnub_history
299303
futres history(std::string const &channel, std::string const &channel_group = "", unsigned count = 100) {
300-
return doit(pubnub_history(d_pb, channel.c_str(), channel_group.c_str(), count));
304+
char const *ch = channel.empty() ? 0 : channel.c_str();
305+
char const *gr = channel_group.empty() ? 0 : channel_group.c_str();
306+
return doit(pubnub_history(d_pb, ch, gr, count));
301307
}
302308

303309
/// Pass a vector of channels in the @p channel and a vector
@@ -313,7 +319,9 @@ namespace pubnub {
313319
/// a time token for each message. Uses the v2 protocol.
314320
/// @see pubnub_historyv2
315321
futres historyv2(std::string const &channel, std::string const &channel_group = "", unsigned count = 100, bool include_token = false) {
316-
return doit(pubnub_historyv2(d_pb, channel.c_str(), channel_group.c_str(), count, include_token));
322+
char const *ch = channel.empty() ? 0 : channel.c_str();
323+
char const *gr = channel_group.empty() ? 0 : channel_group.c_str();
324+
return doit(pubnub_historyv2(d_pb, ch, gr, count, include_token));
317325
}
318326

319327
/// Pass a vector of channels in the @p channel and a vector
@@ -327,7 +335,9 @@ namespace pubnub {
327335
/// UUIDs on a @p channel and/or @p channel_group
328336
/// @see pubnub_here_now
329337
futres here_now(std::string const &channel, std::string const &channel_group = "") {
330-
return doit(pubnub_here_now(d_pb, channel.c_str(), channel_group.c_str()));
338+
char const *ch = channel.empty() ? 0 : channel.c_str();
339+
char const *gr = channel_group.empty() ? 0 : channel_group.c_str();
340+
return doit(pubnub_here_now(d_pb, ch, gr));
331341
}
332342

333343
/// Pass a vector of channels in the @p channel and a vector
@@ -356,7 +366,9 @@ namespace pubnub {
356366
/// given @p channel and/or @pchannel_group of the given @p uuid
357367
/// @see pubnub_set_state
358368
futres set_state(std::string const &channel, std::string const &channel_group, std::string const &uuid, std::string const &state) {
359-
return doit(pubnub_set_state(d_pb, channel.c_str(), channel_group.c_str(), uuid.c_str(), state.c_str()));
369+
char const *ch = channel.empty() ? 0 : channel.c_str();
370+
char const *gr = channel_group.empty() ? 0 : channel_group.c_str();
371+
return doit(pubnub_set_state(d_pb, ch, gr, uuid.c_str(), state.c_str()));
360372
}
361373

362374
/// Pass a vector of channels in the @p channel and a vector
@@ -371,7 +383,9 @@ namespace pubnub {
371383
/// uuid
372384
/// @see pubnub_set_state
373385
futres state_get(std::string const &channel, std::string const &channel_group = "", std::string const &uuid = "") {
374-
return doit(pubnub_state_get(d_pb, channel.c_str(), channel_group.c_str(), uuid.empty() ? NULL : uuid.c_str()));
386+
char const *ch = channel.empty() ? 0 : channel.c_str();
387+
char const *gr = channel_group.empty() ? 0 : channel_group.c_str();
388+
return doit(pubnub_state_get(d_pb, ch, gr, uuid.empty() ? NULL : uuid.c_str()));
375389
}
376390
futres state_get(std::vector<std::string> const &channel, std::vector<std::string> const &channel_group, std::string const &uuid = "") {
377391
return state_get(join(channel, comma), join(channel_group, comma), uuid);

0 commit comments

Comments
 (0)