Created
January 24, 2011 23:23
-
-
Save piscisaureus/794202 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| #define HAVE_SYS_SELECT_H 1 | |
| #define HAVE_SELECT 1 | |
| #define HAVE_CEIL 1 |
This file contains hidden or 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 <stdio.h> | |
| #include <unistd.h> | |
| #include <windows.h> | |
| #include <winsock2.h> | |
| #include "ev.h" | |
| // Where to connect to | |
| #define HOST INADDR_LOOPBACK | |
| #define PORT 80 | |
| void callback(EV_P_ ev_io *w, int revents); | |
| int make_connection(); | |
| int fd; | |
| int limit = 100; | |
| int main() { | |
| // Init winsock | |
| WSADATA ws_info; | |
| WORD version = MAKEWORD(2, 2); | |
| if (WSAStartup(version, &ws_info)) { | |
| printf("Winsock initialization failed\n"); | |
| exit(1); | |
| } | |
| // Init libev | |
| struct ev_loop *loop = ev_default_loop(0); | |
| // Make a connection | |
| fd = make_connection(); | |
| // Observe writability | |
| ev_io *watcher = (ev_io *)malloc(sizeof(ev_io)); | |
| ev_io_init(watcher, callback, fd, EV_WRITE); | |
| ev_io_start(loop, watcher); | |
| ev_run(loop, 0); | |
| } | |
| void callback(EV_P_ ev_io *old_watcher, int revents) { | |
| // Stop the old watcher | |
| ev_io_stop(EV_DEFAULT_ old_watcher); | |
| free(old_watcher); | |
| // Close the fd | |
| fprintf(stderr, "FD %d now writable\n", fd); | |
| fprintf(stderr, "Closing FD %d\n", fd); | |
| close(fd); | |
| // Here ev_fd_closed would be appropriate | |
| // ev_fd_closed(EV_DEFAULT_ fd); | |
| // Now try to make another connection | |
| fd = make_connection(); | |
| // Observe writability of new socket | |
| ev_io *watcher = (ev_io *)malloc(sizeof(ev_io)); | |
| ev_io_init(watcher, callback, fd, EV_WRITE); | |
| ev_io_start(EV_DEFAULT_ watcher); | |
| if (!--limit) | |
| exit(0); | |
| } | |
| int make_connection() { | |
| SOCKET dummy, sock; | |
| SOCKADDR_IN addr; | |
| int fd, err, addr_len = sizeof (addr); | |
| unsigned long arg = 1; | |
| // This dummy is created to make it less likely that socket() returns the | |
| // same HANDLE value twice | |
| if ((dummy = socket(AF_INET, SOCK_STREAM, IPPROTO_IP)) == INVALID_SOCKET) | |
| goto error; | |
| if ((sock = socket(AF_INET, SOCK_STREAM, IPPROTO_IP)) == INVALID_SOCKET) | |
| goto error; | |
| if (ioctlsocket(sock, FIONBIO, &arg) == SOCKET_ERROR) | |
| goto error; | |
| fprintf(stderr, "Connecting\n"); | |
| memset((void*)&addr, 0, addr_len); | |
| addr.sin_family = AF_INET; | |
| addr.sin_addr.s_addr = htonl(HOST); | |
| addr.sin_port = htons(PORT); | |
| if (connect(sock, (SOCKADDR*)&addr, addr_len) == SOCKET_ERROR | |
| && WSAGetLastError() != WSAEWOULDBLOCK) | |
| goto error; | |
| fd = _open_osfhandle(sock, 0); | |
| if (fd < 0) | |
| goto error; | |
| fprintf(stderr, "FD is %d, underlying handle is 0x%x\n", fd, | |
| (unsigned int)sock); | |
| return fd; | |
| error: | |
| err = WSAGetLastError(); | |
| fprintf(stderr, "Error: %d\n", err); | |
| exit(1); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment