Add ping-pong test - not yet working on unix
This commit is contained in:
parent
5304a18002
commit
42d96dc3ca
1
.gitignore
vendored
1
.gitignore
vendored
@ -23,3 +23,4 @@ ev/autom4te.cache
|
|||||||
/Release/
|
/Release/
|
||||||
|
|
||||||
test/echo-demo
|
test/echo-demo
|
||||||
|
test/test-ping-pong
|
||||||
|
|||||||
5
Makefile
5
Makefile
@ -1,6 +1,11 @@
|
|||||||
|
all: test/echo-demo test/test-ping-pong
|
||||||
|
|
||||||
test/echo-demo: test/echo-demo.c test/echo.o ol.a
|
test/echo-demo: test/echo-demo.c test/echo.o ol.a
|
||||||
$(CC) -ansi -g -o test/echo-demo test/echo-demo.c test/echo.o ol.a -lm
|
$(CC) -ansi -g -o test/echo-demo test/echo-demo.c test/echo.o ol.a -lm
|
||||||
|
|
||||||
|
test/test-ping-pong: test/test-ping-pong.c test/echo.o ol.a
|
||||||
|
$(CC) -ansi -g -o test/test-ping-pong test/test-ping-pong.c test/echo.o ol.a -lm
|
||||||
|
|
||||||
ol.a: ol-unix.o ev/ev.o
|
ol.a: ol-unix.o ev/ev.o
|
||||||
$(AR) rcs ol.a ol-unix.o ev/ev.o
|
$(AR) rcs ol.a ol-unix.o ev/ev.o
|
||||||
|
|
||||||
|
|||||||
@ -63,6 +63,7 @@ ol_handle* ol_tcp_handle_new(ol_close_cb close_cb, void* data) {
|
|||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
handle->type = OL_TCP;
|
||||||
handle->close_cb = close_cb;
|
handle->close_cb = close_cb;
|
||||||
handle->data = data;
|
handle->data = data;
|
||||||
|
|
||||||
@ -316,7 +317,6 @@ void ol_tcp_io(EV_P_ ev_io* watcher, int revents) {
|
|||||||
void ol_tcp_connect(ol_handle* handle, ol_req* req) {
|
void ol_tcp_connect(ol_handle* handle, ol_req* req) {
|
||||||
assert(handle->_.fd >= 0);
|
assert(handle->_.fd >= 0);
|
||||||
assert(req);
|
assert(req);
|
||||||
assert(req->type == OL_CONNECT);
|
|
||||||
|
|
||||||
int error;
|
int error;
|
||||||
int errorsize = sizeof(int);
|
int errorsize = sizeof(int);
|
||||||
|
|||||||
@ -8,7 +8,7 @@
|
|||||||
|
|
||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
ol_handle *handle;
|
ol_handle* handle;
|
||||||
ol_req req;
|
ol_req req;
|
||||||
ol_buf buf;
|
ol_buf buf;
|
||||||
char read_buffer[BUFSIZE];
|
char read_buffer[BUFSIZE];
|
||||||
@ -103,4 +103,3 @@ int echo_start(int port) {
|
|||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
118
test/test-ping-pong.c
Normal file
118
test/test-ping-pong.c
Normal file
@ -0,0 +1,118 @@
|
|||||||
|
#include "../ol.h"
|
||||||
|
#include "echo.h"
|
||||||
|
#include <assert.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
|
static int completed_pingers = 0;
|
||||||
|
static ol_req connect_req;
|
||||||
|
|
||||||
|
/* 64 bytes is enough for a pinger */
|
||||||
|
#define BUFSIZE 64
|
||||||
|
|
||||||
|
static char* PING = "PING\n";
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
int pongs;
|
||||||
|
int state;
|
||||||
|
ol_handle* handle;
|
||||||
|
ol_req req;
|
||||||
|
ol_buf buf;
|
||||||
|
char read_buffer[BUFSIZE];
|
||||||
|
} pinger;
|
||||||
|
|
||||||
|
|
||||||
|
void pinger_on_close(ol_handle* handle, ol_err err) {
|
||||||
|
assert(!err);
|
||||||
|
pinger* p = handle->data;
|
||||||
|
assert(1000 == p->pongs);
|
||||||
|
free(p);
|
||||||
|
ol_free(handle);
|
||||||
|
completed_pingers++;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void pinger_after_read(ol_req* req, size_t nread, ol_err err) {
|
||||||
|
int i, r;
|
||||||
|
|
||||||
|
if (!err) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (nread == 0) {
|
||||||
|
ol_close(req->handle);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
pinger *p = req->data;
|
||||||
|
|
||||||
|
/* Now we count the pings */
|
||||||
|
for (i = 0; i < nread; i++) {
|
||||||
|
assert(p->buf.base[i] == PING[p->state]);
|
||||||
|
/* 5 = strlen(PING) */
|
||||||
|
p->state = (p->state + 1) % 5;
|
||||||
|
if (p->state == 0) {
|
||||||
|
p->pongs++;
|
||||||
|
if (p->pongs < 1000) {
|
||||||
|
r = ol_write2(p->handle, PING);
|
||||||
|
assert(!r);
|
||||||
|
} else {
|
||||||
|
ol_close(p->handle);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void pinger_try_read(pinger* pinger) {
|
||||||
|
pinger->buf.len = BUFSIZE;
|
||||||
|
pinger->req.cb = pinger_after_read;
|
||||||
|
ol_read(pinger->handle, &pinger->req, &pinger->buf, 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void pinger_on_connect(ol_handle* handle, ol_err err) {
|
||||||
|
int r;
|
||||||
|
|
||||||
|
pinger *p = calloc(sizeof(pinger), 1);
|
||||||
|
p->handle = handle;
|
||||||
|
p->buf.base = p->read_buffer;
|
||||||
|
p->buf.len = BUFSIZE;
|
||||||
|
p->req.data = p;
|
||||||
|
|
||||||
|
handle->data = p;
|
||||||
|
|
||||||
|
pinger_try_read(p);
|
||||||
|
|
||||||
|
r = ol_write2(handle, PING);
|
||||||
|
if (r < 0) {
|
||||||
|
/* error */
|
||||||
|
assert(0);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
int pinger_connect(int port) {
|
||||||
|
/* Try to connec to the server and do 1000 ping-pongs. */
|
||||||
|
ol_handle* handle = ol_tcp_handle_new(pinger_on_close, NULL);
|
||||||
|
struct sockaddr_in addr = ol_ip4_addr("127.0.0.1", port);
|
||||||
|
return ol_connect(handle, &connect_req, (struct sockaddr*)&addr);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
int main(int argc, char** argv) {
|
||||||
|
ol_init();
|
||||||
|
|
||||||
|
if (echo_start(8000)) {
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (pinger_connect(8000)) {
|
||||||
|
return 2;
|
||||||
|
}
|
||||||
|
|
||||||
|
ol_run();
|
||||||
|
|
||||||
|
assert(completed_pingers == 1);
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue
Block a user