websocket test: formatting/indentation

master
Jessie Murray 3 years ago
parent e97056f4cf
commit 8141c00ab7
No known key found for this signature in database
GPG Key ID: E7E4D57EDDA744C5

@ -17,9 +17,7 @@
#include <event.h>
#include <http_parser.h>
#define DEBUG_LOGS 0
struct host_info {
struct host_info{
char *host;
short port;
};
@ -88,7 +86,7 @@ hex_dump(struct worker_thread *wt, char *p, size_t sz) {
letters[i] = isprint(cur[i]) ? cur[i] : '.';
}
for (int i = limit; i < 16; i++) { /* pad on last line */
wt->debug(" ");
wt->debug(" "); /* 3 spaces for "%02x " */
}
wt->debug(" %.*s\n", limit, letters);
}
@ -98,6 +96,10 @@ void
evbuffer_debug_dump(struct worker_thread *wt, struct evbuffer *buffer) {
size_t sz = evbuffer_get_length(buffer);
char *data = malloc(sz);
if (!data) {
fprintf(stderr, "failed to allocate %ld bytes\n", sz);
return;
}
evbuffer_remove(buffer, data, sz);
hex_dump(wt, data, sz);
evbuffer_prepend(buffer, data, sz);
@ -106,6 +108,7 @@ evbuffer_debug_dump(struct worker_thread *wt, struct evbuffer *buffer) {
static void
wait_for_possible_read(struct worker_thread *wt);
static void
wait_for_possible_write(struct worker_thread *wt);
@ -114,9 +117,7 @@ ws_enqueue_frame(struct worker_thread *wt);
void
process_message(struct worker_thread *wt, size_t sz) {
// printf("process_message\n");
if(wt->msg_received && wt->msg_received % 1000 == 0) {
if (wt->msg_received && wt->msg_received % 1000 == 0) {
printf("thread %d: %8d messages left (got %9d bytes so far).\n",
wt->id,
wt->msg_target - wt->msg_received, wt->byte_count);
@ -125,7 +126,7 @@ process_message(struct worker_thread *wt, size_t sz) {
/* decrement read count, and stop receiving when we reach zero. */
wt->msg_received++;
if(wt->msg_received == wt->msg_target) {
if (wt->msg_received == wt->msg_target) {
wt->debug("%s: thread %d has received all %d messages it expected\n",
__func__, wt->id, wt->msg_received);
event_base_loopexit(wt->base, NULL);
@ -141,11 +142,7 @@ websocket_can_write(int fd, short event, void *ptr) {
struct worker_thread *wt = ptr;
wt->debug("%s (wt=%p, fd=%d)\n", __func__, wt, fd);
if(event != EV_WRITE) {
return;
}
switch (wt->state)
{
switch (wt->state) {
case WS_INITIAL: { /* still sending initial HTTP request */
ret = evbuffer_write(wt->wbuffer, fd);
wt->debug("evbuffer_write returned %d\n", ret);
@ -186,20 +183,17 @@ websocket_can_read(int fd, short event, void *ptr) {
struct worker_thread *wt = ptr;
wt->debug("%s (wt=%p)\n", __func__, wt);
if(event != EV_READ) {
return;
}
/* read message */
ret = evbuffer_read(wt->rbuffer, fd, 65536);
wt->debug("evbuffer_read() returned %d; wt->state=%d. wt->rbuffer:\n", ret, wt->state);
evbuffer_debug_dump(wt, wt->rbuffer);
if (ret == 0) {
wt->debug("We didn't read anything from the socket...\n");
event_base_loopexit(wt->base, NULL);
return;
}
while(1) {
while (1) {
switch (wt->state) {
case WS_SENT_HANDSHAKE: { /* waiting for handshake response */
size_t avail_sz = evbuffer_get_length(wt->rbuffer);
@ -278,9 +272,7 @@ wait_for_possible_write(struct worker_thread *wt) {
static int
ws_on_headers_complete(http_parser *p) {
struct worker_thread *wt = p->data;
wt->debug("%s (wt=%p)\n", __func__, wt);
// TODO
return 0;
}
@ -294,7 +286,7 @@ ws_enqueue_frame_for_command(struct worker_thread *wt, char *cmd, size_t sz) {
len |= (1 << 7); /* set masking bit ON */
for (size_t i = 0; i < sz; i++) {
cmd[i] = (cmd[i] ^ mask[i%4]) & 0xff;
cmd[i] = (cmd[i] ^ mask[i % 4]) & 0xff;
}
/* 0x81 = 10000001b: FIN bit (only one message in the frame), text frame */
evbuffer_add(wt->wbuffer, "\x81", 1);
@ -325,8 +317,8 @@ ws_on_message_complete(http_parser *p) {
static void
ws_on_timeout(evutil_socket_t fd, short event, void *arg) {
struct worker_thread *wt = arg;
(void) fd;
(void) event;
(void)fd;
(void)event;
fprintf(stderr, "Time has run out! (thread %d)\n", wt->id);
event_base_loopbreak(wt->base); /* break out of event loop */
@ -341,8 +333,7 @@ worker_main(void *ptr) {
"Upgrade: WebSocket\r\n"
"Origin: http://%s:%d\r\n"
"Sec-WebSocket-Key: webdis-websocket-test-key\r\n"
"\r\n"
;
"\r\n";
struct worker_thread *wt = ptr;
@ -359,8 +350,8 @@ worker_main(void *ptr) {
memset(&(addr.sin_addr), 0, sizeof(addr.sin_addr));
addr.sin_addr.s_addr = inet_addr(wt->hi->host);
ret = connect(fd, (struct sockaddr*)&addr, sizeof(struct sockaddr));
if(ret != 0) {
ret = connect(fd, (struct sockaddr *)&addr, sizeof(struct sockaddr));
if (ret != 0) {
fprintf(stderr, "connect: ret=%d: %s\n", ret, strerror(errno));
return NULL;
}
@ -400,8 +391,7 @@ worker_main(void *ptr) {
return NULL;
}
void
usage(const char* argv0, char *host_default, short port_default,
void usage(const char *argv0, char *host_default, short port_default,
int thread_count_default, int messages_default) {
printf("Usage: %s [options]\n"
@ -447,20 +437,19 @@ main(int argc, char *argv[]) {
{"messages", required_argument, NULL, 'n'},
{"max-time", required_argument, NULL, 't'},
{"verbose", no_argument, NULL, 'v'},
{0, 0, 0, 0}
};
{0, 0, 0, 0}};
while ((opt = getopt_long(argc, argv, "h:p:c:n:t:vs", long_options, NULL)) != -1) {
switch (opt) {
case 'h':
colon = strchr(optarg, ':');
if(!colon) {
if (!colon) {
size_t sz = strlen(optarg);
hi.host = calloc(1 + sz, 1);
strncpy(hi.host, optarg, sz);
} else {
hi.host = calloc(1+colon-optarg, 1);
strncpy(hi.host, optarg, colon-optarg);
hi.port = (short)atol(colon+1);
hi.host = calloc(1 + colon - optarg, 1);
strncpy(hi.host, optarg, colon - optarg);
hi.port = (short)atol(colon + 1);
}
break;
@ -527,13 +516,13 @@ main(int argc, char *argv[]) {
float mili0 = t0.tv_sec * 1000 + t0.tv_nsec / 1000000;
float mili1 = t1.tv_sec * 1000 + t1.tv_nsec / 1000000;
if(total != 0) {
double kb_per_sec = ((double)total_bytes / (double)(mili1-mili0)) / 1.024;
if (total != 0) {
double kb_per_sec = ((double)total_bytes / (double)(mili1 - mili0)) / 1.024;
printf("Read %ld messages (%ld bytes) in %0.2f sec: %0.2f msg/sec (%0.2f KB/sec)\n",
total,
total_bytes,
(mili1-mili0)/1000.0,
1000*((double)total)/(mili1-mili0),
(mili1 - mili0) / 1000.0,
1000 * ((double)total) / (mili1 - mili0),
kb_per_sec);
return (total == thread_count * msg_target ? EXIT_SUCCESS : EXIT_FAILURE);
} else {

Loading…
Cancel
Save