From 4e192493abae0f533cf06c41ceea25927b85d51e Mon Sep 17 00:00:00 2001 From: Tatsuhiro Tsujikawa Date: Tue, 31 Jan 2012 22:04:51 +0900 Subject: [PATCH] Use gettimeofday instead of clock_gettime and use poll instead of epoll for portability. --- examples/spdycat.cc | 39 ++++++++++++++++++--------------------- examples/spdylay_ssl.cc | 38 ++++++++++++++++---------------------- examples/spdylay_ssl.h | 7 ++++--- 3 files changed, 38 insertions(+), 46 deletions(-) diff --git a/examples/spdycat.cc b/examples/spdycat.cc index 205e396..1c2a602 100644 --- a/examples/spdycat.cc +++ b/examples/spdycat.cc @@ -27,11 +27,11 @@ #include #include #include -#include #include #include #include #include +#include #include #include @@ -159,11 +159,10 @@ int communicate(const std::string& host, uint16_t port, } make_non_block(fd); Spdylay sc(fd, ssl, callbacks); - int epollfd = epoll_create(1); - if(epollfd == -1) { - perror("epoll_create"); - return -1; - } + + nfds_t npollfds = 1; + pollfd pollfds[1]; + for(int i = 0, n = reqvec.size(); i < n; ++i) { uri::UriStruct& us = reqvec[i].us; std::string path = us.dir+us.file+us.query; @@ -171,33 +170,31 @@ int communicate(const std::string& host, uint16_t port, assert(r == 0); path2req[path] = &reqvec[i]; } - ctl_epollev(epollfd, EPOLL_CTL_ADD, &sc); - static const size_t MAX_EVENTS = 1; - epoll_event events[MAX_EVENTS]; + pollfds[0].fd = fd; + ctl_poll(pollfds, &sc); + bool ok = true; while(sc.want_read() || sc.want_write()) { - int nfds = epoll_wait(epollfd, events, MAX_EVENTS, -1); + int nfds = poll(pollfds, npollfds, -1); if(nfds == -1) { - perror("epoll_wait"); + perror("poll"); return -1; } - for(int n = 0; n < nfds; ++n) { - if(((events[n].events & EPOLLIN) && sc.recv() != 0) || - ((events[n].events & EPOLLOUT) && sc.send() != 0)) { + if(((pollfds[0].revents & POLLIN) && sc.recv() != 0) || + ((pollfds[0].revents & POLLOUT) && sc.send() != 0)) { ok = false; std::cout << "Fatal" << std::endl; break; - } - if((events[n].events & EPOLLHUP) || (events[n].events & EPOLLERR)) { - std::cout << "HUP" << std::endl; - ok = false; - break; - } + } + if((pollfds[0].revents & POLLHUP) || (pollfds[0].revents & POLLERR)) { + std::cout << "HUP" << std::endl; + ok = false; + break; } if(!ok) { break; } - ctl_epollev(epollfd, EPOLL_CTL_MOD, &sc); + ctl_poll(pollfds, &sc); } SSL_shutdown(ssl); diff --git a/examples/spdylay_ssl.cc b/examples/spdylay_ssl.cc index 99d94b0..e14333a 100644 --- a/examples/spdylay_ssl.cc +++ b/examples/spdylay_ssl.cc @@ -27,7 +27,6 @@ #include #include #include -#include #include #include @@ -227,9 +226,9 @@ void print_nv(char **nv) void print_timer() { - timespec ts; - get_timer(&ts); - printf("[%3ld.%03ld]", ts.tv_sec, ts.tv_nsec/1000000); + timeval tv; + get_timer(&tv); + printf("[%3ld.%03ld]", tv.tv_sec, tv.tv_usec/1000); } void print_frame(spdylay_frame_type type, spdylay_frame *frame) @@ -291,19 +290,14 @@ void on_ctrl_send_callback fflush(stdout); } -void ctl_epollev(int epollfd, int op, Spdylay *sc) +void ctl_poll(pollfd *pollfd, Spdylay *sc) { - epoll_event ev; - memset(&ev, 0, sizeof(ev)); + pollfd->events = 0; if(sc->want_read()) { - ev.events |= EPOLLIN; + pollfd->events |= POLLIN; } if(sc->want_write()) { - ev.events |= EPOLLOUT; - } - if(epoll_ctl(epollfd, op, sc->fd(), &ev) == -1) { - perror("epoll_ctl"); - exit(EXIT_FAILURE); + pollfd->events |= POLLOUT; } } @@ -357,22 +351,22 @@ int ssl_handshake(SSL *ssl, int fd) } namespace { -timespec basets; +timeval base_tv; } // namespace void reset_timer() { - clock_gettime(CLOCK_MONOTONIC_RAW, &basets); + gettimeofday(&base_tv, 0); } -void get_timer(timespec* ts) +void get_timer(timeval* tv) { - clock_gettime(CLOCK_MONOTONIC_RAW, ts); - ts->tv_nsec -= basets.tv_nsec; - ts->tv_sec -= basets.tv_sec; - if(ts->tv_nsec < 0) { - ts->tv_nsec += 1000000000; - --ts->tv_sec; + gettimeofday(tv, 0); + tv->tv_usec -= base_tv.tv_usec; + tv->tv_sec -= base_tv.tv_sec; + if(tv->tv_usec < 0) { + tv->tv_usec += 1000000; + --tv->tv_sec; } } diff --git a/examples/spdylay_ssl.h b/examples/spdylay_ssl.h index 5582f96..3ec3a6e 100644 --- a/examples/spdylay_ssl.h +++ b/examples/spdylay_ssl.h @@ -27,7 +27,8 @@ #include #include -#include +#include +#include #include #include @@ -83,7 +84,7 @@ void on_ctrl_send_callback (spdylay_session *session, spdylay_frame_type type, spdylay_frame *frame, void *user_data); -void ctl_epollev(int epollfd, int op, Spdylay *sc); +void ctl_poll(pollfd *pollfd, Spdylay *sc); int select_next_proto_cb(SSL* ssl, unsigned char **out, unsigned char *outlen, @@ -96,7 +97,7 @@ int ssl_handshake(SSL *ssl, int fd); void reset_timer(); -void get_timer(timespec *ts); +void get_timer(timeval *tv); } // namespace spdylay