mirror of
https://github.com/wg/wrk
synced 2025-01-08 06:52:55 +08:00
read all available bytes when socket ready
This commit is contained in:
parent
796f4e1226
commit
fe4c1a692b
@ -2,6 +2,7 @@
|
||||
|
||||
#include <errno.h>
|
||||
#include <unistd.h>
|
||||
#include <sys/ioctl.h>
|
||||
|
||||
#include "net.h"
|
||||
|
||||
@ -30,3 +31,9 @@ status sock_write(connection *c, char *buf, size_t len, size_t *n) {
|
||||
*n = (size_t) r;
|
||||
return OK;
|
||||
}
|
||||
|
||||
size_t sock_readable(connection *c) {
|
||||
int n, rc;
|
||||
rc = ioctl(c->fd, FIONREAD, &n);
|
||||
return rc == -1 ? 0 : n;
|
||||
}
|
||||
|
10
src/net.h
10
src/net.h
@ -13,15 +13,17 @@ typedef enum {
|
||||
} status;
|
||||
|
||||
struct sock {
|
||||
status (*connect)(connection *);
|
||||
status ( *close)(connection *);
|
||||
status ( *read)(connection *, size_t *);
|
||||
status ( *write)(connection *, char *, size_t, size_t *);
|
||||
status ( *connect)(connection *);
|
||||
status ( *close)(connection *);
|
||||
status ( *read)(connection *, size_t *);
|
||||
status ( *write)(connection *, char *, size_t, size_t *);
|
||||
size_t (*readable)(connection *);
|
||||
};
|
||||
|
||||
status sock_connect(connection *);
|
||||
status sock_close(connection *);
|
||||
status sock_read(connection *, size_t *);
|
||||
status sock_write(connection *, char *, size_t, size_t *);
|
||||
size_t sock_readable(connection *);
|
||||
|
||||
#endif /* NET_H */
|
||||
|
@ -93,3 +93,7 @@ status ssl_write(connection *c, char *buf, size_t len, size_t *n) {
|
||||
*n = (size_t) r;
|
||||
return OK;
|
||||
}
|
||||
|
||||
size_t ssl_readable(connection *c) {
|
||||
return SSL_pending(c->ssl);
|
||||
}
|
||||
|
@ -9,5 +9,6 @@ status ssl_connect(connection *);
|
||||
status ssl_close(connection *);
|
||||
status ssl_read(connection *, size_t *);
|
||||
status ssl_write(connection *, char *, size_t, size_t *);
|
||||
size_t ssl_readable(connection *);
|
||||
|
||||
#endif /* SSL_H */
|
||||
|
34
src/wrk.c
34
src/wrk.c
@ -22,10 +22,11 @@ static struct {
|
||||
} statistics;
|
||||
|
||||
static struct sock sock = {
|
||||
.connect = sock_connect,
|
||||
.close = sock_close,
|
||||
.read = sock_read,
|
||||
.write = sock_write
|
||||
.connect = sock_connect,
|
||||
.close = sock_close,
|
||||
.read = sock_read,
|
||||
.write = sock_write,
|
||||
.readable = sock_readable
|
||||
};
|
||||
|
||||
static struct http_parser_settings parser_settings = {
|
||||
@ -114,10 +115,11 @@ int main(int argc, char **argv) {
|
||||
ERR_print_errors_fp(stderr);
|
||||
exit(1);
|
||||
}
|
||||
sock.connect = ssl_connect;
|
||||
sock.close = ssl_close;
|
||||
sock.read = ssl_read;
|
||||
sock.write = ssl_write;
|
||||
sock.connect = ssl_connect;
|
||||
sock.close = ssl_close;
|
||||
sock.read = ssl_read;
|
||||
sock.write = ssl_write;
|
||||
sock.readable = ssl_readable;
|
||||
}
|
||||
|
||||
signal(SIGPIPE, SIG_IGN);
|
||||
@ -496,14 +498,16 @@ static void socket_readable(aeEventLoop *loop, int fd, void *data, int mask) {
|
||||
connection *c = data;
|
||||
size_t n;
|
||||
|
||||
switch (sock.read(c, &n)) {
|
||||
case OK: break;
|
||||
case ERROR: goto error;
|
||||
case RETRY: return;
|
||||
}
|
||||
do {
|
||||
switch (sock.read(c, &n)) {
|
||||
case OK: break;
|
||||
case ERROR: goto error;
|
||||
case RETRY: return;
|
||||
}
|
||||
|
||||
if (http_parser_execute(&c->parser, &parser_settings, c->buf, n) != n) goto error;
|
||||
c->thread->bytes += n;
|
||||
if (http_parser_execute(&c->parser, &parser_settings, c->buf, n) != n) goto error;
|
||||
c->thread->bytes += n;
|
||||
} while (n == RECVBUF && sock.readable(c) > 0);
|
||||
|
||||
return;
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user