Use gettimeofday instead of clock_gettime and use poll instead of epoll for portability.
This commit is contained in:
parent
9785b99959
commit
4e192493ab
|
@ -27,11 +27,11 @@
|
||||||
#include <netdb.h>
|
#include <netdb.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
#include <fcntl.h>
|
#include <fcntl.h>
|
||||||
#include <sys/epoll.h>
|
|
||||||
#include <netinet/in.h>
|
#include <netinet/in.h>
|
||||||
#include <netinet/tcp.h>
|
#include <netinet/tcp.h>
|
||||||
#include <signal.h>
|
#include <signal.h>
|
||||||
#include <getopt.h>
|
#include <getopt.h>
|
||||||
|
#include <poll.h>
|
||||||
|
|
||||||
#include <cassert>
|
#include <cassert>
|
||||||
#include <cstdio>
|
#include <cstdio>
|
||||||
|
@ -159,11 +159,10 @@ int communicate(const std::string& host, uint16_t port,
|
||||||
}
|
}
|
||||||
make_non_block(fd);
|
make_non_block(fd);
|
||||||
Spdylay sc(fd, ssl, callbacks);
|
Spdylay sc(fd, ssl, callbacks);
|
||||||
int epollfd = epoll_create(1);
|
|
||||||
if(epollfd == -1) {
|
nfds_t npollfds = 1;
|
||||||
perror("epoll_create");
|
pollfd pollfds[1];
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
for(int i = 0, n = reqvec.size(); i < n; ++i) {
|
for(int i = 0, n = reqvec.size(); i < n; ++i) {
|
||||||
uri::UriStruct& us = reqvec[i].us;
|
uri::UriStruct& us = reqvec[i].us;
|
||||||
std::string path = us.dir+us.file+us.query;
|
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);
|
assert(r == 0);
|
||||||
path2req[path] = &reqvec[i];
|
path2req[path] = &reqvec[i];
|
||||||
}
|
}
|
||||||
ctl_epollev(epollfd, EPOLL_CTL_ADD, &sc);
|
pollfds[0].fd = fd;
|
||||||
static const size_t MAX_EVENTS = 1;
|
ctl_poll(pollfds, &sc);
|
||||||
epoll_event events[MAX_EVENTS];
|
|
||||||
bool ok = true;
|
bool ok = true;
|
||||||
while(sc.want_read() || sc.want_write()) {
|
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) {
|
if(nfds == -1) {
|
||||||
perror("epoll_wait");
|
perror("poll");
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
for(int n = 0; n < nfds; ++n) {
|
if(((pollfds[0].revents & POLLIN) && sc.recv() != 0) ||
|
||||||
if(((events[n].events & EPOLLIN) && sc.recv() != 0) ||
|
((pollfds[0].revents & POLLOUT) && sc.send() != 0)) {
|
||||||
((events[n].events & EPOLLOUT) && sc.send() != 0)) {
|
|
||||||
ok = false;
|
ok = false;
|
||||||
std::cout << "Fatal" << std::endl;
|
std::cout << "Fatal" << std::endl;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
if((events[n].events & EPOLLHUP) || (events[n].events & EPOLLERR)) {
|
if((pollfds[0].revents & POLLHUP) || (pollfds[0].revents & POLLERR)) {
|
||||||
std::cout << "HUP" << std::endl;
|
std::cout << "HUP" << std::endl;
|
||||||
ok = false;
|
ok = false;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
|
||||||
if(!ok) {
|
if(!ok) {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
ctl_epollev(epollfd, EPOLL_CTL_MOD, &sc);
|
ctl_poll(pollfds, &sc);
|
||||||
}
|
}
|
||||||
|
|
||||||
SSL_shutdown(ssl);
|
SSL_shutdown(ssl);
|
||||||
|
|
|
@ -27,7 +27,6 @@
|
||||||
#include <netdb.h>
|
#include <netdb.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
#include <fcntl.h>
|
#include <fcntl.h>
|
||||||
#include <sys/epoll.h>
|
|
||||||
#include <netinet/in.h>
|
#include <netinet/in.h>
|
||||||
#include <netinet/tcp.h>
|
#include <netinet/tcp.h>
|
||||||
|
|
||||||
|
@ -227,9 +226,9 @@ void print_nv(char **nv)
|
||||||
|
|
||||||
void print_timer()
|
void print_timer()
|
||||||
{
|
{
|
||||||
timespec ts;
|
timeval tv;
|
||||||
get_timer(&ts);
|
get_timer(&tv);
|
||||||
printf("[%3ld.%03ld]", ts.tv_sec, ts.tv_nsec/1000000);
|
printf("[%3ld.%03ld]", tv.tv_sec, tv.tv_usec/1000);
|
||||||
}
|
}
|
||||||
|
|
||||||
void print_frame(spdylay_frame_type type, spdylay_frame *frame)
|
void print_frame(spdylay_frame_type type, spdylay_frame *frame)
|
||||||
|
@ -291,19 +290,14 @@ void on_ctrl_send_callback
|
||||||
fflush(stdout);
|
fflush(stdout);
|
||||||
}
|
}
|
||||||
|
|
||||||
void ctl_epollev(int epollfd, int op, Spdylay *sc)
|
void ctl_poll(pollfd *pollfd, Spdylay *sc)
|
||||||
{
|
{
|
||||||
epoll_event ev;
|
pollfd->events = 0;
|
||||||
memset(&ev, 0, sizeof(ev));
|
|
||||||
if(sc->want_read()) {
|
if(sc->want_read()) {
|
||||||
ev.events |= EPOLLIN;
|
pollfd->events |= POLLIN;
|
||||||
}
|
}
|
||||||
if(sc->want_write()) {
|
if(sc->want_write()) {
|
||||||
ev.events |= EPOLLOUT;
|
pollfd->events |= POLLOUT;
|
||||||
}
|
|
||||||
if(epoll_ctl(epollfd, op, sc->fd(), &ev) == -1) {
|
|
||||||
perror("epoll_ctl");
|
|
||||||
exit(EXIT_FAILURE);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -357,22 +351,22 @@ int ssl_handshake(SSL *ssl, int fd)
|
||||||
}
|
}
|
||||||
|
|
||||||
namespace {
|
namespace {
|
||||||
timespec basets;
|
timeval base_tv;
|
||||||
} // namespace
|
} // namespace
|
||||||
|
|
||||||
void reset_timer()
|
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);
|
gettimeofday(tv, 0);
|
||||||
ts->tv_nsec -= basets.tv_nsec;
|
tv->tv_usec -= base_tv.tv_usec;
|
||||||
ts->tv_sec -= basets.tv_sec;
|
tv->tv_sec -= base_tv.tv_sec;
|
||||||
if(ts->tv_nsec < 0) {
|
if(tv->tv_usec < 0) {
|
||||||
ts->tv_nsec += 1000000000;
|
tv->tv_usec += 1000000;
|
||||||
--ts->tv_sec;
|
--tv->tv_sec;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -27,7 +27,8 @@
|
||||||
|
|
||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
#include <cstdlib>
|
#include <cstdlib>
|
||||||
#include <time.h>
|
#include <sys/time.h>
|
||||||
|
#include <poll.h>
|
||||||
|
|
||||||
#include <openssl/ssl.h>
|
#include <openssl/ssl.h>
|
||||||
#include <openssl/err.h>
|
#include <openssl/err.h>
|
||||||
|
@ -83,7 +84,7 @@ void on_ctrl_send_callback
|
||||||
(spdylay_session *session, spdylay_frame_type type, spdylay_frame *frame,
|
(spdylay_session *session, spdylay_frame_type type, spdylay_frame *frame,
|
||||||
void *user_data);
|
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,
|
int select_next_proto_cb(SSL* ssl,
|
||||||
unsigned char **out, unsigned char *outlen,
|
unsigned char **out, unsigned char *outlen,
|
||||||
|
@ -96,7 +97,7 @@ int ssl_handshake(SSL *ssl, int fd);
|
||||||
|
|
||||||
void reset_timer();
|
void reset_timer();
|
||||||
|
|
||||||
void get_timer(timespec *ts);
|
void get_timer(timeval *tv);
|
||||||
|
|
||||||
} // namespace spdylay
|
} // namespace spdylay
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue