[add] nng_demo

This commit is contained in:
dianjixz
2024-08-15 12:09:45 +08:00
parent e8a0c81b40
commit ec7880a33d
8 changed files with 123 additions and 0 deletions
+6
View File
@@ -0,0 +1,6 @@
dist
build
.config.mk
.flash.conf.json
+7
View File
@@ -0,0 +1,7 @@
# [libhv](https://github.com/ithewei/libhv)
Like `libevent, libev, and libuv`,
`libhv` provides event-loop with non-blocking IO and timer,
but simpler api and richer protocols.
[README](https://github.com/ithewei/libhv/blob/master/README.md)
+4
View File
@@ -0,0 +1,4 @@
from pathlib import Path
import os
with open(str(Path(os.getcwd())/'..'/'..'/'tools'/'scons'/'project.py')) as f:
exec(f.read())
+3
View File
@@ -0,0 +1,3 @@
# CONFIG_TOOLCHAIN_PATH="/opt/gcc-linaro-7.5.0-2019.12-x86_64_arm-linux-gnueabihf/bin"
# CONFIG_TOOLCHAIN_PREFIX="arm-linux-gnueabihf-"
CONFIG_NNG_ENABLED=y
View File
+45
View File
@@ -0,0 +1,45 @@
# project_root/src/SConscript
import os
# Import the environment from the SConstruct file
Import('env')
with open(env['PROJECT_TOOL_S']) as f:
exec(f.read())
SRCS = []
INCLUDE = [ADir('include'), ADir('.')]
PRIVATE_INCLUDE = []
REQUIREMENTS = ['nng', 'pthread']
STATIC_LIB = []
DYNAMIC_LIB = []
DEFINITIONS = []
DEFINITIONS_PRIVATE = []
LDFLAGS = []
LINK_SEARCH_PATH = []
INCLUDE += [ADir('../../../github_source/nng/src')]
LDFLAGS += ['-Wl,-rpath=./']
all_src_files = Glob('src/*.c*')
for src in all_src_files:
SRCS = [src]
if str(src).endswith('.cpp'):
target_name = os.path.basename(str(src)[:-4])
elif str(src).endswith('.c'):
target_name = os.path.basename(str(src)[:-2])
env['COMPONENTS'].append({'target':target_name,
'SRCS':SRCS,
'INCLUDE':INCLUDE,
'PRIVATE_INCLUDE':PRIVATE_INCLUDE,
'REQUIREMENTS':REQUIREMENTS,
'STATIC_LIB':STATIC_LIB,
'DYNAMIC_LIB':DYNAMIC_LIB,
'DEFINITIONS':DEFINITIONS,
'DEFINITIONS_PRIVATE':DEFINITIONS_PRIVATE,
'LDFLAGS':LDFLAGS,
'LINK_SEARCH_PATH':LINK_SEARCH_PATH,
'REGISTER':'project'
})
@@ -0,0 +1,58 @@
#include <nng/nng.h>
#include <nng/protocol/reqrep0/req.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// #include <testing/nuts.h>
int main(int argc, char *argv[])
{
nng_socket rep;
nng_socket req;
nng_msg * m;
uint32_t req_id;
nng_rep0_open_raw(&rep);
nng_req0_open_raw(&req);
nng_socket_set_ms(req, NNG_OPT_RECVTIMEO, 100);
nng_socket_set_ms(req, NNG_OPT_SENDTIMEO, 1000);
nng_socket_set_ms(rep, NNG_OPT_SENDTIMEO, 1000);
nng_socket_set_ms(rep, NNG_OPT_SENDTIMEO, 1000);
nuts_marry(req, rep);
nng_msg_alloc(&m, 0);
nng_msg_append_u32(m, 0x80000000);
nng_sendmsg(req, m, 0);
printf("nng_msg_header_len:%ld\n", nng_msg_header_len(m));
nng_recvmsg(rep, &m, 0);
// // The message will have a header that contains the 32-bit pipe ID,
// // followed by the 32-bit request ID. We will discard the request
// // ID before sending it out.
printf("nng_msg_header_len:%ld\n", nng_msg_header_len(m));
nng_msg_header_chop_u32(m, &req_id);
nng_sendmsg(rep, m, 0);
nng_recvmsg(req, &m, 0), NNG_ETIMEDOUT;
nng_close(req);
nng_close(rep);
return 0;
}