summaryrefslogtreecommitdiff
path: root/c/sources/client.c
blob: 672e3e8550248504e501d44cfe9293d76abb4563 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
#define EZIPC_IMPL
#include "ezipc.h"

#include "common.h"


int main(void)
{
    printf("EZIPC C Client\n");


    printf("Connecting...");
    fflush(stdout);

    ezi_conn* conn = ezi_connect(EZIPC_TEST_PATH);
    assert(conn);

    printf(" DONE!\n");

    msg msg_txt  = { .type = MSG_TEXT };
    msg msg_exit = { .type = MSG_EXIT };

    bool running = true;
    while(running)
    {
        char in[1024] = {0};
        printf("send> ");
        scanf("%[^\n]%*c", in);

        if(strcmp(in, "exit") != 0)
        {
            strcpy((char*)msg_txt.data, in);
            if(!ezi_send(conn, &msg_txt, sizeof(msg_txt)))
            {
                printf("Send error, resetting...\n");

                ezi_disconnect(conn);
                conn = ezi_connect(EZIPC_TEST_PATH);
                assert(conn);

                continue;
            }
        }
        else
        {
            if(!ezi_send(conn, &msg_exit, sizeof(msg_exit)))
            {
                ezi_disconnect(conn);
                exit(1);
            }

            break;
        }

        msg rmsg = {0};
        size_t rsz = sizeof(rmsg);
        if(!ezi_recv(conn, &rmsg, &rsz))
        {
            printf("Recv error, resetting...\n");

            ezi_disconnect(conn);
            conn = ezi_connect(EZIPC_TEST_PATH);
            assert(conn);

            continue;
        }

        switch(rmsg.type)
        {
            case MSG_OK:
            {
                printf("acknowledged!\n");

            } break;

            case MSG_EXIT:
            {
                running = false;
                printf("told to exit...\n");

            } break;

            case MSG_TEXT:
            {
                printf("received: '%s'\n", (char*)rmsg.data);

            } break;

            default: exit(1);
        }
    }

    ezi_disconnect(conn);
    return 0;
}