Created
December 13, 2013 04:30
-
-
Save fur-q/7939783 to your computer and use it in GitHub Desktop.
libircclient and a proper event loop (specifically ae)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdarg.h> | |
#include <stdio.h> | |
#include <string.h> | |
#include <sys/select.h> | |
#include "libircclient.h" | |
#include "ae.h" | |
typedef struct irc_fds_t { | |
fd_set rfd, wfd; | |
} irc_fds_t; | |
void irc_on_connect(irc_session_t* session, const char* event, const char* origin, const char** params, unsigned int count) { | |
irc_cmd_join(session, "#piss", 0); | |
} | |
void irc_process_ae_read(aeEventLoop *evt, int fd, void *user, int mask) { | |
irc_fds_t* fds = (irc_fds_t*)user; | |
FD_SET(fd, &(fds->rfd)); | |
aeDeleteFileEvent(evt, fd, AE_READABLE); | |
} | |
void irc_process_ae_write(aeEventLoop *evt, int fd, void *user, int mask) { | |
irc_fds_t* fds = (irc_fds_t*)user; | |
FD_SET(fd, &(fds->wfd)); | |
aeDeleteFileEvent(evt, fd, AE_WRITABLE); | |
} | |
void irc_add_ae_events(irc_session_t* irc, aeEventLoop *evt, irc_fds_t* fds) { | |
int i, max; | |
fd_set wfd, rfd; | |
FD_ZERO(&wfd); | |
FD_ZERO(&rfd); | |
irc_add_select_descriptors(irc, &rfd, &wfd, &max); | |
for (i = 0; i <= max; i++) { | |
if (FD_ISSET(i, &rfd) && !(aeGetFileEvents(evt, i) == AE_READABLE)) | |
aeCreateFileEvent(evt, i, AE_READABLE, irc_process_ae_read, (void*) fds); | |
if (FD_ISSET(i, &wfd) && !(aeGetFileEvents(evt, i) == AE_WRITABLE)) | |
aeCreateFileEvent(evt, i, AE_WRITABLE, irc_process_ae_write, (void*) fds); | |
} | |
} | |
int main(int argc, char** argv) { | |
irc_callbacks_t cbx; | |
irc_session_t* irc; | |
irc_fds_t fds; | |
aeEventLoop *evt; | |
int running = 1; | |
evt = aeCreateEventLoop(1024); | |
memset(&cbx, 0, sizeof(cbx)); | |
cbx.event_connect = irc_on_connect; | |
irc = irc_create_session(&cbx); | |
if (irc_connect(irc, "localhost", 6667, 0, "flexo", "flexo", "flexo")) { | |
printf("Connection failed\n"); | |
return 1; | |
} | |
while (running) { | |
FD_ZERO(&(fds.rfd)); | |
FD_ZERO(&(fds.wfd)); | |
irc_add_ae_events(irc, evt, &fds); | |
aeProcessEvents(evt, AE_ALL_EVENTS); | |
irc_process_select_descriptors(irc, &(fds.rfd), &(fds.wfd)); | |
} | |
aeDeleteEventLoop(evt); | |
irc_destroy_session(irc); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment