Skip to content

Commit

Permalink
rxi#25 Unix client example
Browse files Browse the repository at this point in the history
- Add an example for unix socket usage
- Update API documentation

Signed-off-by: Uilian Ries <[email protected]>
  • Loading branch information
uilianries committed Apr 22, 2018
1 parent 03fe293 commit ff7b29f
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 3 deletions.
8 changes: 5 additions & 3 deletions doc/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand All @@ -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
Expand Down Expand Up @@ -251,4 +254,3 @@ function should be of the following form:
```c
void func(const char *message);
```

51 changes: 51 additions & 0 deletions example/unixclient.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#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 <unix socket>\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;
}

0 comments on commit ff7b29f

Please sign in to comment.