25#include <event2/thread.h>
31 struct event_base *loop;
34 struct event *keepActive;
61 evthread_use_windows_threads();
63 evthread_use_pthreads();
68natsLibevent_ProcessEvent(evutil_socket_t fd,
short event,
void *arg)
70 natsLibeventEvents *nle = (natsLibeventEvents*) arg;
80keepAliveCb(evutil_socket_t fd,
short flags,
void * arg)
100 struct event_base *libeventLoop = (
struct event_base*) loop;
101 natsLibeventEvents *nle = (natsLibeventEvents*) (*userData);
107 nle = (natsLibeventEvents*) calloc(1,
sizeof(natsLibeventEvents));
112 nle->loop = libeventLoop;
114 nle->keepActive = event_new(nle->loop, -1, EV_PERSIST, keepAliveCb, NULL);
115 if (nle->keepActive == NULL)
120 struct timeval timeout;
122 timeout.tv_sec = 100000;
125 if (event_add(nle->keepActive, &timeout) != 0)
131 if (nle->read != NULL)
133 event_free(nle->read);
136 if (nle->write != NULL)
138 event_free(nle->write);
145 nle->read = event_new(nle->loop, socket, EV_READ|EV_PERSIST,
146 natsLibevent_ProcessEvent, (
void*) nle);
149 nle->write = event_new(nle->loop, socket, EV_WRITE|EV_PERSIST,
150 natsLibevent_ProcessEvent, (
void*) nle);
154 *userData = (
void*) nle;
162_closeCb(evutil_socket_t fd,
short event,
void *arg)
183 natsLibeventEvents *nle = (natsLibeventEvents*) userData;
187 res = event_add(nle->read, NULL);
190 int socket = event_get_fd(nle->read);
191 res = event_del_noblock(nle->read);
196 res = event_base_once(nle->loop, socket, EV_TIMEOUT, _closeCb, (
void*) nle, NULL);
214 natsLibeventEvents *nle = (natsLibeventEvents*) userData;
218 res = event_add(nle->write, NULL);
220 res = event_del_noblock(nle->write);
236 natsLibeventEvents *nle = (natsLibeventEvents*) userData;
238 if (nle->read != NULL)
239 event_free(nle->read);
240 if (nle->write != NULL)
241 event_free(nle->write);
242 if (nle->keepActive != NULL)
244 event_active(nle->keepActive, 0, 0);
245 event_free(nle->keepActive);
NATS_EXTERN void natsConnection_ProcessCloseEvent(natsSock *socket)
Process a socket close event when using external event loop.
NATS_EXTERN void natsConnection_ProcessReadEvent(natsConnection *nc)
Process a read event when using external event loop.
NATS_EXTERN void natsConnection_ProcessWriteEvent(natsConnection *nc)
Process a write event when using external event loop.
void natsLibevent_Init(void)
Initialize the adapter.
Definition libevent.h:58
natsStatus natsLibevent_Attach(void **userData, void *loop, natsConnection *nc, natsSock socket)
Attach a connection to the given event loop.
Definition libevent.h:98
natsStatus natsLibevent_Write(void *userData, bool add)
Start or stop polling on WRITE events.
Definition libevent.h:212
natsStatus natsLibevent_Read(void *userData, bool add)
Start or stop polling on READ events.
Definition libevent.h:181
natsStatus natsLibevent_Detach(void *userData)
The connection is closed, it can be safely detached.
Definition libevent.h:234
struct __natsConnection natsConnection
A connection to a NATS Server.
Definition nats.h:160
int natsSock
Definition nats.h:58
natsStatus
Status returned by most of the APIs.
Definition status.h:50
@ NATS_ERR
Generic error.
Definition status.h:53
@ NATS_NO_MEMORY
Definition status.h:102
@ NATS_OK
Success.
Definition status.h:51