Use gettimeofday instead of clock_gettime and use poll instead of epoll for portability.

This commit is contained in:
Tatsuhiro Tsujikawa 2012-01-31 22:04:51 +09:00
parent 9785b99959
commit 4e192493ab
3 changed files with 38 additions and 46 deletions

View File

@ -27,11 +27,11 @@
#include <netdb.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/epoll.h>
#include <netinet/in.h>
#include <netinet/tcp.h>
#include <signal.h>
#include <getopt.h>
#include <poll.h>
#include <cassert>
#include <cstdio>
@ -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);

View File

@ -27,7 +27,6 @@
#include <netdb.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/epoll.h>
#include <netinet/in.h>
#include <netinet/tcp.h>
@ -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;
}
}

View File

@ -27,7 +27,8 @@
#include <stdint.h>
#include <cstdlib>
#include <time.h>
#include <sys/time.h>
#include <poll.h>
#include <openssl/ssl.h>
#include <openssl/err.h>
@ -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