Skip to content

Commit 5f27b1c

Browse files
authored
Support auto-heartbeat on Windows, callback
In addition to already existing support on POSIX, callback.
1 parent 0a2830e commit 5f27b1c

31 files changed

+780
-635
lines changed

.pubnub.yml

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,13 @@
11
name: c-core
22
schema: 1
3-
version: 2.12.0
3+
version: 2.12.1
44
scm: github.com/pubnub/c-core
55
changelog:
6+
- version: v2.12.1
7+
date: Nov 22, 2019
8+
changes:
9+
- type: enhancement
10+
text: Add support for automatic sending of heartbeat messages on Windows, callback.
611
- version: v2.12.0
712
date: Nov 15, 2019
813
changes:

core/pb_sleep_ms.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
/* -*- c-file-style:"stroustrup"; indent-tabs-mode: nil -*- */
2+
#if defined _WIN32
3+
void pb_sleep_ms(DWORD dwMilliseconds);
4+
#else
5+
void pb_sleep_ms(long milliseconds);
6+
#endif

core/pbauto_heartbeat.h

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
/* -*- c-file-style:"stroustrup"; indent-tabs-mode: nil -*- */
2+
#if !defined INC_PBAUTO_HEARTBEAT
3+
#define INC_PBAUTO_HEARTBEAT
4+
5+
#if PUBNUB_USE_AUTO_HEARTBEAT
6+
#if defined _WIN32
7+
typedef DWORD pubnub_thread_t;
8+
typedef FILETIME pubnub_timespec_t;
9+
typedef void pubnub_watcher_t;
10+
#define thread_handle_field() HANDLE thread_handle;
11+
#define UNIT_IN_MILLI 1000
12+
#else
13+
typedef pthread_t pubnub_thread_t;
14+
typedef struct timespec pubnub_timespec_t;
15+
typedef void* pubnub_watcher_t;
16+
#define thread_handle_field()
17+
#endif
18+
19+
20+
/** Maximum number of auto heartbeat thumpers that can be used at one time */
21+
#define PUBNUB_MAX_HEARTBEAT_THUMPERS 16
22+
#define UNASSIGNED PUBNUB_MAX_HEARTBEAT_THUMPERS
23+
24+
struct pubnub_heartbeat_data {
25+
pubnub_t* pb;
26+
pubnub_t* heartbeat_pb;
27+
size_t period_sec;
28+
};
29+
30+
struct HeartbeatWatcherData {
31+
struct pubnub_heartbeat_data heartbeat_data[PUBNUB_MAX_HEARTBEAT_THUMPERS] pubnub_guarded_by(mutw);
32+
unsigned thumpers_in_use pubnub_guarded_by(mutw);
33+
/** Times left for each of the thumper timers in progress */
34+
size_t heartbeat_timers[PUBNUB_MAX_HEARTBEAT_THUMPERS] pubnub_guarded_by(timerlock);
35+
/** Array of thumper indexes whos auto heartbeat timers are active and running */
36+
unsigned timer_index_array[PUBNUB_MAX_HEARTBEAT_THUMPERS] pubnub_guarded_by(timerlock);
37+
/** Number of active thumper timers */
38+
unsigned active_timers pubnub_guarded_by(timerlock);
39+
bool stop_heartbeat_watcher_thread pubnub_guarded_by(stoplock);
40+
pubnub_mutex_t mutw;
41+
pubnub_mutex_t timerlock;
42+
pubnub_mutex_t stoplock;
43+
pubnub_thread_t thread_id;
44+
thread_handle_field()
45+
};
46+
47+
/** Pubnub context fields for saving subscribed channels and channel groups
48+
*/
49+
#define M_channelInfo() \
50+
struct { \
51+
char* channel; \
52+
char* channel_group; \
53+
} channelInfo;
54+
55+
/** Pubnub context fields for heartbeat info used by the module for keeping presence.
56+
*/
57+
#define M_heartbeatInfo() unsigned thumperIndex;
58+
59+
/** Reads channel and channel groups saved(subscribed on)
60+
*/
61+
void pbauto_heartbeat_read_channelInfo(pubnub_t const* pb,
62+
char const** channel,
63+
char const** channel_group);
64+
65+
/** Manipulates thumpers in use */
66+
pubnub_watcher_t pbauto_heartbeat_watcher_thread(void* arg);
67+
68+
/** Initializes and starts auto heartbeat watcher thread. Different on different platforms */
69+
int pbauto_heartbeat_init(struct HeartbeatWatcherData* m_watcher);
70+
71+
/** Gives notice to auto heartbeat module that subscribe, or heartbeat transaction has begun */
72+
void pbauto_heartbeat_transaction_ongoing(pubnub_t const* pb);
73+
74+
/** Starts auto heartbeat timer, if auto heartbeat is enabled, when subscribe transaction
75+
is finished.
76+
*/
77+
void pbauto_heartbeat_start_timer(pubnub_t const* pb);
78+
79+
/** Releases allocated strings for subscribed channels and channel groups
80+
*/
81+
void pbauto_heartbeat_free_channelInfo(pubnub_t* pb);
82+
83+
/** Preparess channels and channel groups, to be used in request url.
84+
*/
85+
enum pubnub_res pbauto_heartbeat_prepare_channels_and_ch_groups(pubnub_t* pb,
86+
char const** channel,
87+
char const** channel_group);
88+
89+
/** Stops auto heartbeat thread */
90+
void pbauto_heartbeat_stop(void);
91+
92+
#else
93+
#define M_channelInfo()
94+
#define M_heartbeatInfo()
95+
#define pbauto_heartbeat_read_channelInfo(pb, channel, channel_group)
96+
#define pbauto_heartbeat_watcher_thread(arg)
97+
#define pbauto_heartbeat_init(m_watcher)
98+
#define pbauto_heartbeat_transaction_ongoing(pb)
99+
#define pbauto_heartbeat_start_timer(pb)
100+
#define pbauto_heartbeat_free_channelInfo(pb)
101+
#define pbauto_heartbeat_prepare_channels_and_ch_groups(pb, addr_channel, addr_channel_group) PNR_OK
102+
#define pbauto_heartbeat_stop()
103+
#endif /* PUBNUB_USE_AUTO_HEARTBEAT */
104+
105+
#endif /* !defined INC_PBAUTO_HEARTBEAT */
106+

0 commit comments

Comments
 (0)