Update tests
This commit is contained in:
parent
e5d9ef954d
commit
37ea7d572d
31
test/echo.c
31
test/echo.c
@ -22,6 +22,9 @@ void on_close(ol_handle* peer, ol_err err);
|
||||
void on_accept(ol_handle* server, ol_handle* new_client);
|
||||
|
||||
|
||||
ol_handle *server = NULL;
|
||||
|
||||
|
||||
void after_write(ol_req* req) {
|
||||
peer_t *peer = (peer_t*) req->data;
|
||||
try_read(peer);
|
||||
@ -58,7 +61,6 @@ void on_close(ol_handle* peer, ol_err err) {
|
||||
|
||||
void on_accept(ol_handle* server, ol_handle* new_client) {
|
||||
peer_t* p;
|
||||
int r;
|
||||
|
||||
new_client->close_cb = on_close;
|
||||
|
||||
@ -70,21 +72,29 @@ void on_accept(ol_handle* server, ol_handle* new_client) {
|
||||
ol_req_init(&p->req, NULL);
|
||||
|
||||
try_read(p);
|
||||
}
|
||||
|
||||
r = ol_write2(new_client, "Hello\n");
|
||||
if (r < 0) {
|
||||
/* error */
|
||||
assert(0);
|
||||
|
||||
void on_server_close(ol_handle* handle, ol_err err) {
|
||||
assert(handle == server);
|
||||
|
||||
if (err) {
|
||||
fprintf(stdout, "Socket error\n");
|
||||
}
|
||||
|
||||
ol_free(server);
|
||||
server = NULL;
|
||||
}
|
||||
|
||||
|
||||
int echo_start(int port) {
|
||||
ol_handle* server = ol_tcp_handle_new(on_close, NULL);
|
||||
|
||||
struct sockaddr_in addr = ol_ip4_addr("0.0.0.0", port);
|
||||
int r;
|
||||
|
||||
int r = ol_bind(server, (struct sockaddr*) &addr);
|
||||
assert(server == NULL);
|
||||
server = ol_tcp_handle_new(&on_server_close, NULL);
|
||||
|
||||
r = ol_bind(server, (struct sockaddr*) &addr);
|
||||
if (r) {
|
||||
/* TODO: Error codes */
|
||||
fprintf(stderr, "Bind error\n");
|
||||
@ -100,3 +110,8 @@ int echo_start(int port) {
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int echo_stop() {
|
||||
assert(server != NULL);
|
||||
ol_close(server);
|
||||
}
|
||||
@ -1,3 +1,4 @@
|
||||
#include "../ol.h"
|
||||
|
||||
int echo_start(int port);
|
||||
int echo_stop();
|
||||
@ -20,30 +20,31 @@ typedef struct {
|
||||
char read_buffer[BUFSIZE];
|
||||
} pinger;
|
||||
|
||||
void pinger_try_read(pinger* pinger);
|
||||
|
||||
void pinger_on_close(ol_handle* handle, ol_err err) {
|
||||
pinger* p;
|
||||
|
||||
assert(!err);
|
||||
pinger* p = handle->data;
|
||||
p = (pinger*)handle->data;
|
||||
assert(1000 == p->pongs);
|
||||
free(p);
|
||||
ol_free(handle);
|
||||
completed_pingers++;
|
||||
echo_stop();
|
||||
}
|
||||
|
||||
|
||||
void pinger_after_read(ol_req* req, size_t nread, ol_err err) {
|
||||
int i, r;
|
||||
|
||||
if (!err) {
|
||||
return;
|
||||
}
|
||||
void pinger_after_read(ol_req* req, size_t nread) {
|
||||
unsigned int i;
|
||||
int r;
|
||||
pinger* p;
|
||||
|
||||
if (nread == 0) {
|
||||
ol_close(req->handle);
|
||||
return;
|
||||
}
|
||||
|
||||
pinger *p = req->data;
|
||||
p = (pinger*)req->data;
|
||||
|
||||
/* Now we count the pings */
|
||||
for (i = 0; i < nread; i++) {
|
||||
@ -57,9 +58,12 @@ void pinger_after_read(ol_req* req, size_t nread, ol_err err) {
|
||||
assert(!r);
|
||||
} else {
|
||||
ol_close(p->handle);
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pinger_try_read(p);
|
||||
}
|
||||
|
||||
|
||||
@ -70,8 +74,9 @@ void pinger_try_read(pinger* pinger) {
|
||||
}
|
||||
|
||||
|
||||
void pinger_on_connect(ol_handle* handle, ol_err err) {
|
||||
void pinger_on_connect(ol_req *req, ol_err err) {
|
||||
int r;
|
||||
ol_handle *handle = req->handle;
|
||||
|
||||
pinger *p = calloc(sizeof(pinger), 1);
|
||||
p->handle = handle;
|
||||
@ -94,8 +99,12 @@ void pinger_on_connect(ol_handle* handle, ol_err err) {
|
||||
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);
|
||||
struct sockaddr_in client_addr = ol_ip4_addr("0.0.0.0", 0);
|
||||
struct sockaddr_in server_addr = ol_ip4_addr("127.0.0.1", port);
|
||||
|
||||
ol_bind(handle, (struct sockaddr*)&client_addr);
|
||||
ol_req_init(&connect_req, &pinger_on_connect);
|
||||
return ol_connect(handle, &connect_req, (struct sockaddr*)&server_addr);
|
||||
}
|
||||
|
||||
|
||||
|
||||
Loading…
Reference in New Issue
Block a user