zhangmeng
2021-12-10 5990bac28af438a914165441b3c33a370b320d16
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
//
//  Copyright 2017 Garrett D'Amore <garrett@damore.org>
//
// 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.
//
 
#include "nng/nng.h"
#include "nng/protocol/pair1/pair.h"
 
#include <cstring>
#include <iostream>
 
#define SOCKET_ADDRESS "inproc://c++"
 
int
main(int argc, char **argv)
{
 
#if defined(NNG_HAVE_PAIR1)
 
    nng_socket s1;
    nng_socket s2;
    int        rv;
    size_t     sz;
    char       buf[8];
    (void) argc;
    (void) argv;
 
    if ((rv = nng_pair1_open(&s1)) != 0) {
        throw nng_strerror(rv);
    }
    if ((rv = nng_pair1_open(&s2)) != 0) {
        throw nng_strerror(rv);
    }
    if ((rv = nng_listen(s1, SOCKET_ADDRESS, NULL, 0)) != 0) {
        throw nng_strerror(rv);
    }
    if ((rv = nng_dial(s2, SOCKET_ADDRESS, NULL, 0)) != 0) {
        throw nng_strerror(rv);
    }
    if ((rv = nng_send(s2, (void *) "ABC", 4, 0)) != 0) {
        throw nng_strerror(rv);
    }
    sz = sizeof(buf);
    if ((rv = nng_recv(s1, buf, &sz, 0)) != 0) {
        throw nng_strerror(rv);
    }
    if ((sz != 4) || (memcmp(buf, "ABC", 4) != 0)) {
        throw "Contents did not match";
    }
    if ((rv = nng_send(s1, (void *) "DEF", 4, 0)) != 0) {
        throw nng_strerror(rv);
    }
    sz = sizeof(buf);
    if ((rv = nng_recv(s2, buf, &sz, 0)) != 0) {
        throw nng_strerror(rv);
    }
    if ((sz != 4) || (memcmp(buf, "DEF", 4) != 0)) {
        throw "Contents did not match";
    }
    if ((rv = nng_close(s1)) != 0) {
        throw nng_strerror(rv);
    }
    if ((rv = nng_close(s2)) != 0) {
        throw nng_strerror(rv);
    }
 
    std::cout << "Pass." << std::endl;
#else
    (void) argc;
    (void) argv;
    std::cout << "Skipped (protocol unconfigured)." << std::endl;
#endif
 
    return (0);
}