From ff7b29f5ee599c152e1cca738532a7ea358fe047 Mon Sep 17 00:00:00 2001 From: Uilian Ries Date: Sun, 22 Apr 2018 20:02:21 -0300 Subject: [PATCH] #25 Unix client example - Add an example for unix socket usage - Update API documentation Signed-off-by: Uilian Ries --- doc/api.md | 8 ++++--- example/unixclient.c | 51 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 56 insertions(+), 3 deletions(-) create mode 100644 example/unixclient.c diff --git a/doc/api.md b/doc/api.md index 1cdca55..c5f22bd 100644 --- a/doc/api.md +++ b/doc/api.md @@ -29,14 +29,14 @@ Returns the version of the library as a string. Returns the current time in seconds. This time should only be used for comparisons, as no specific epoch is guaranteed. -#### int dyad\_getStreamCount(void) +#### int dyad\_getStreamCount(void) Returns the number of currently active streams. #### void dyad\_setUpdateTimeout(double seconds) Sets the maximum number of seconds the `dyad_update()` function can block for. If `seconds` is `0` then `dyad_update()` will not block. -#### void dyad\_setTickInterval(double seconds) +#### void dyad\_setTickInterval(double seconds) Sets the interval in seconds that the `DYAD_EVENT_TICK` event is emited to all streams; the default is 1 second. @@ -63,6 +63,9 @@ Performs the same task as `dyad_listen()` but provides additional options: #### int dyad\_connect(dyad\_Stream \*stream, const char \*host, int port) Connects the `stream` to the remote `host`. +#### int dyad\_unix\_connect(dyad\_Stream \*stream, const char \*path) +Connects the `stream` to `unix` domain socket. + #### void dyad\_addListener(dyad\_Stream \*stream, int event, dyad\_Callback callback, void \*udata) Adds a listener for the `event` to the `stream`. When the event occurs the `callback` is called and the event's udata field is set to `udata`. If several @@ -251,4 +254,3 @@ function should be of the following form: ```c void func(const char *message); ``` - diff --git a/example/unixclient.c b/example/unixclient.c new file mode 100644 index 0000000..039ae51 --- /dev/null +++ b/example/unixclient.c @@ -0,0 +1,51 @@ +#include +#include +#include +#include "dyad.h" + +/* An Unix server: Connects and send message to a server */ + +static const char data_to_be_sent [] = {"Dyad.c is an asynchronous networking library"}; + +static void onConnect(dyad_Event *e) { + printf("connected: %s\n", e->msg); +} + +static void onData(dyad_Event *e) { + printf("%s", e->data); +} + +static void onError(dyad_Event *e) { + fprintf(stderr, "%s", e->data); + exit(EXIT_FAILURE); +} + +int main(int argc, char* argv[]) { + dyad_Stream *s; + int bytes_sent; + const int size = strlen(data_to_be_sent); + + if (argc != 2) { + fprintf(stderr, "usage: %s \n", argv[0]); + return EXIT_FAILURE; + } + + dyad_init(); + + s = dyad_newStream(); + dyad_addListener(s, DYAD_EVENT_CONNECT, onConnect, NULL); + dyad_addListener(s, DYAD_EVENT_DATA, onData, NULL); + dyad_addListener(s, DYAD_EVENT_ERROR, onError, NULL); + if (dyad_unix_connect(s, argv[1]) == -1) { + return EXIT_FAILURE; + } + + dyad_write(s, data_to_be_sent, size); + while (bytes_sent < size) { + bytes_sent = dyad_getBytesSent(s); + dyad_update(); + } + + dyad_shutdown(); + return EXIT_SUCCESS; +}