diff --git a/.gitignore b/.gitignore index f51fc83d..25639a40 100644 --- a/.gitignore +++ b/.gitignore @@ -17,8 +17,9 @@ ev/config.log ev/config.status ev/libtool ev/stamp-h1 -test/echo-server ev/autom4te.cache /ipch/ /Debug/ -/Release/ \ No newline at end of file +/Release/ + +test/echo-demo diff --git a/Makefile b/Makefile index 1d255f7f..70a88db0 100644 --- a/Makefile +++ b/Makefile @@ -1,11 +1,14 @@ -test/echo-server: test/echo-server.c ol.a - $(CC) -ansi -g -o test/echo-server test/echo-server.c ol.a -lm +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 ol.a: ol-unix.o ev/ev.o $(AR) rcs ol.a ol-unix.o ev/ev.o ol-unix.o: ol-unix.c ol.h ol-unix.h - $(CC) -ansi -g -c ol-unix.c -o ol-unix.o -lm + $(CC) -ansi -g -c ol-unix.c -o ol-unix.o + +test/echo.o: test/echo.c test/echo.h + $(CC) -ansi -g -c test/echo.c -o test/echo.o ev/ev.o: ev/config.h ev/ev.c $(MAKE) -C ev diff --git a/test/echo-demo.c b/test/echo-demo.c new file mode 100644 index 00000000..1fadb19f --- /dev/null +++ b/test/echo-demo.c @@ -0,0 +1,15 @@ +#include "../ol.h" +#include "echo.h" + +int main(int argc, char** argv) { + ol_init(); + + int r = echo_start(8000); + if (r) { + return r; + } + + ol_run(); + + return 0; +} diff --git a/test/echo-server.c b/test/echo.c similarity index 85% rename from test/echo-server.c rename to test/echo.c index 7a0ab5c2..c3be192a 100644 --- a/test/echo-server.c +++ b/test/echo.c @@ -66,7 +66,7 @@ void on_accept(ol_handle* server, ol_handle* new_client) { new_client->close_cb = on_close; - p = (peer_t*)malloc(sizeof(peer_t)); + p = (peer_t*)malloc(sizeof(peer_t)); p->handle = new_client; p->buf.base = p->read_buffer; p->buf.len = BUFSIZE; @@ -82,31 +82,25 @@ void on_accept(ol_handle* server, ol_handle* new_client) { } -int main(int argc, char** argv) { - ol_handle* server; - struct sockaddr_in addr; - int r; +int echo_start(int port) { + ol_handle* server = ol_tcp_handle_new(on_close, NULL); - ol_init(); + struct sockaddr_in addr = ol_ip4_addr("0.0.0.0", port); - server = ol_tcp_handle_new(on_close, NULL); - - addr = ol_ip4_addr("0.0.0.0", 8000); - - r = ol_bind(server, (struct sockaddr*) &addr); + int r = ol_bind(server, (struct sockaddr*) &addr); if (r) { + /* TODO: Error codes */ fprintf(stderr, "Bind error\n"); return 1; } r = ol_listen(server, 128, on_accept); if (r) { + /* TODO: Error codes */ fprintf(stderr, "Listen error\n"); return 1; } - ol_run(); - return 0; } diff --git a/test/echo.h b/test/echo.h new file mode 100644 index 00000000..81e3c29e --- /dev/null +++ b/test/echo.h @@ -0,0 +1,3 @@ +#include "../ol.h" + +int echo_start(int port);