25 #include <event2/thread.h>
31 struct event_base *loop;
34 struct event *keepActive;
61 evthread_use_windows_threads();
63 evthread_use_pthreads();
68 natsLibevent_ProcessEvent(evutil_socket_t fd,
short event,
void *arg)
70 natsLibeventEvents *nle = (natsLibeventEvents*) arg;
80 keepAliveCb(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;
172 natsLibeventEvents *nle = (natsLibeventEvents*) userData;
176 res = event_add(nle->read, NULL);
178 res = event_del_noblock(nle->read);
194 natsLibeventEvents *nle = (natsLibeventEvents*) userData;
198 res = event_add(nle->write, NULL);
200 res = event_del_noblock(nle->write);
216 natsLibeventEvents *nle = (natsLibeventEvents*) userData;
218 if (nle->read != NULL)
219 event_free(nle->read);
220 if (nle->write != NULL)
221 event_free(nle->write);
222 if (nle->keepActive != NULL)
224 event_active(nle->keepActive, 0, 0);
225 event_free(nle->keepActive);
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:192
natsStatus natsLibevent_Read(void *userData, bool add)
Start or stop polling on READ events.
Definition: libevent.h:170
natsStatus natsLibevent_Detach(void *userData)
The connection is closed, it can be safely detached.
Definition: libevent.h:214
struct __natsConnection natsConnection
A connection to a NATS Server.
Definition: nats.h:152
int natsSock
Definition: nats.h:50
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