zhangmeng
2024-04-22 16935f4aebffdd1b6580b844391a0aa0f4f3012b
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
//
// Copyright 2018 Staysail Systems, Inc. <info@staysail.tech>
// Copyright 2021 Capitar IT Group BV <info@capitar.com>
//
// This software is supplied under the terms of the MIT License, a
// copy of which should be located in the distribution where this
// file was obtained (LICENSE.txt).  A copy of the license may also be
// found online at https://opensource.org/licenses/MIT.
//
 
//
// This is just a simple REQ/REP demonstration application.  It is derived
// from the legacy nanomsg demonstration program of the same name, written
// by Tim Dysinger, but updated for nng.  I've also updated it to pass simpler
// binary data rather than strings over the network.
//
// The program implements a simple RPC style service, which just returns
// the date in UNIX time (seconds since 1970).
//
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
 
#include <nng/nng.h>
#include <nng/protocol/reqrep0/rep.h>
#include <nng/protocol/reqrep0/req.h>
#include <nng/transport/zerotier/zerotier.h>
#include <nng/supplemental/util/platform.h>
 
#define CLIENT "client"
#define SERVER "server"
#define DATECMD 1
 
#define PUT64(ptr, u)                                        \
    do {                                                 \
        (ptr)[0] = (uint8_t)(((uint64_t)(u)) >> 56); \
        (ptr)[1] = (uint8_t)(((uint64_t)(u)) >> 48); \
        (ptr)[2] = (uint8_t)(((uint64_t)(u)) >> 40); \
        (ptr)[3] = (uint8_t)(((uint64_t)(u)) >> 32); \
        (ptr)[4] = (uint8_t)(((uint64_t)(u)) >> 24); \
        (ptr)[5] = (uint8_t)(((uint64_t)(u)) >> 16); \
        (ptr)[6] = (uint8_t)(((uint64_t)(u)) >> 8);  \
        (ptr)[7] = (uint8_t)((uint64_t)(u));         \
    } while (0)
 
#define GET64(ptr, v)                                 \
    v = (((uint64_t)((uint8_t)(ptr)[0])) << 56) + \
        (((uint64_t)((uint8_t)(ptr)[1])) << 48) + \
        (((uint64_t)((uint8_t)(ptr)[2])) << 40) + \
        (((uint64_t)((uint8_t)(ptr)[3])) << 32) + \
        (((uint64_t)((uint8_t)(ptr)[4])) << 24) + \
        (((uint64_t)((uint8_t)(ptr)[5])) << 16) + \
        (((uint64_t)((uint8_t)(ptr)[6])) << 8) +  \
        (((uint64_t)(uint8_t)(ptr)[7]))
 
void
fatal(const char *func, int rv)
{
    fprintf(stderr, "%s: %s\n", func, nng_strerror(rv));
    exit(1);
}
 
void
showdate(time_t now)
{
    struct tm *info = localtime(&now);
    printf("%s", asctime(info));
}
 
int
server(const char *url)
{
    nng_socket sock;
    nng_listener listener;
    int        rv;
    int        count = 0;
 
    if ((rv = nng_rep0_open(&sock)) != 0) {
        fatal("nng_rep0_open", rv);
    }
 
    if ((rv = nng_listener_create(&listener, sock, url)) != 0) {
        fatal("nng_listener_create", rv);
    }
 
    if (strncmp(url, "zt://", 5) == 0) {
        printf("ZeroTier transport will store its keys in current working directory.\n");
        printf("The server and client instances must run in separate directories.\n");
        nng_listener_setopt_string(listener, NNG_OPT_ZT_HOME, ".");
        nng_listener_setopt_ms(listener, NNG_OPT_RECONNMINT, 1);
        nng_listener_setopt_ms(listener, NNG_OPT_RECONNMAXT, 1000);
        nng_setopt_ms(sock, NNG_OPT_REQ_RESENDTIME, 2000);
        nng_setopt_ms(sock, NNG_OPT_RECVMAXSZ, 0);
        nng_listener_setopt_ms(listener, NNG_OPT_ZT_PING_TIME, 10000);
        nng_listener_setopt_ms(listener, NNG_OPT_ZT_CONN_TIME, 1000);
    } else {
        nng_setopt_ms(sock, NNG_OPT_REQ_RESENDTIME, 2000);
    }
    nng_listener_start(listener, 0);
 
    for (;;) {
        char *   buf = NULL;
        size_t   sz;
        uint64_t val;
        count++;
        if ((rv = nng_recv(sock, &buf, &sz, NNG_FLAG_ALLOC)) != 0) {
            fatal("nng_recv", rv);
        }
        if ((sz == sizeof(uint64_t)) &&
            ((GET64(buf, val)) == DATECMD)) {
            time_t now;
            printf("SERVER: RECEIVED DATE REQUEST\n");
            now = time(&now);
            if (count == 6) {
                printf("SERVER: SKIP SENDING REPLY\n");
                nng_free(buf, sz);
                continue;
            }
            printf("SERVER: SENDING DATE: ");
            showdate(now);
 
            // Reuse the buffer.  We know it is big enough.
            PUT64(buf, (uint64_t) now);
            rv = nng_send(sock, buf, sz, NNG_FLAG_ALLOC);
            if (rv != 0) {
                fatal("nng_send", rv);
            }
            continue;
        }
        // Unrecognized command, so toss the buffer.
        nng_free(buf, sz);
    }
}
 
int
client(const char *url)
{
    nng_socket sock;
    nng_dialer dialer;
    int        rv;
    size_t     sz;
    char *     buf = NULL;
    uint8_t    cmd[sizeof(uint64_t)];
    int        sleep = 0;
 
    PUT64(cmd, DATECMD);
 
    if ((rv = nng_req0_open(&sock)) != 0) {
        fatal("nng_socket", rv);
    }
 
    if ((rv = nng_dialer_create(&dialer, sock, url)) != 0) {
        fatal("nng_dialer_create", rv);
    }
 
    if (strncmp(url, "zt://", 5) == 0) {
        printf("ZeroTier transport will store its keys in current working directory\n");
        printf("The server and client instances must run in separate directories.\n");
        nng_dialer_setopt_string(dialer, NNG_OPT_ZT_HOME, ".");
        nng_dialer_setopt_ms(dialer, NNG_OPT_RECONNMINT, 1);
        nng_dialer_setopt_ms(dialer, NNG_OPT_RECONNMAXT, 1000);
        nng_setopt_ms(sock, NNG_OPT_REQ_RESENDTIME, 2000);
        nng_setopt_ms(sock, NNG_OPT_RECVMAXSZ, 0);
        nng_dialer_setopt_ms(dialer, NNG_OPT_ZT_PING_TIME, 10000);
        nng_dialer_setopt_ms(dialer, NNG_OPT_ZT_CONN_TIME, 1000);
    } else {
        nng_setopt_ms(sock, NNG_OPT_REQ_RESENDTIME, 2000);
    }
 
    nng_dialer_start(dialer, NNG_FLAG_NONBLOCK);
 
    while (1) {
 
        printf("CLIENT: SENDING DATE REQUEST\n");
        if ((rv = nng_send(sock, cmd, sizeof(cmd), 0)) != 0) {
            fatal("nng_send", rv);
        }
        if ((rv = nng_recv(sock, &buf, &sz, NNG_FLAG_ALLOC)) != 0) {
            fatal("nng_recv", rv);
        }
 
        if (sz == sizeof(uint64_t)) {
            uint64_t now;
            GET64(buf, now);
            printf("CLIENT: RECEIVED DATE: ");
            showdate((time_t) now);
        } else {
            printf("CLIENT: GOT WRONG SIZE!\n");
        }
        nng_msleep(sleep);
        sleep++;
        if (sleep == 4) {
            sleep = 4000;
        }
    }
 
    // This assumes that buf is ASCIIZ (zero terminated).
    nng_free(buf, sz);
    nng_close(sock);
    return (0);
}
 
int
main(const int argc, const char **argv)
{
    if ((argc > 1) && (strcmp(CLIENT, argv[1]) == 0))
        return (client(argv[2]));
 
    if ((argc > 1) && (strcmp(SERVER, argv[1]) == 0))
        return (server(argv[2]));
 
    fprintf(stderr, "Usage: reqrep %s|%s <URL> ...\n", CLIENT, SERVER);
    return (1);
}