mirror of
https://github.com/warmcat/libwebsockets.git
synced 2025-03-09 00:00:04 +01:00
minimal: client: hello_world: polish a bit more
This is the ur-template for the new examples, try to make it as good an exemplar as possible
This commit is contained in:
parent
0993543ac8
commit
c1e6906fc8
3 changed files with 18 additions and 20 deletions
|
@ -11,8 +11,6 @@ require_lws_config(LWS_WITH_SYS_FAULT_INJECTION 1 has_fault_injection)
|
|||
require_lws_config(LWS_WITH_SECURE_STREAMS_PROXY_API 1 has_ss_proxy)
|
||||
require_lws_config(LWS_WITH_SYS_STATE 1 has_sys_state)
|
||||
|
||||
CHECK_C_SOURCE_COMPILES("#include <libwebsockets.h>\nint main(void) {\ni#if defined(LWS_WITH_SECURE_STREAMS_PROXY_API)\n return 0;\n #else\n fail\n #endif\n return 0;\n}\n" HAS_LWS_WITH_SECURE_STREAMS_PROXY_API)
|
||||
|
||||
if (requirements)
|
||||
|
||||
add_executable(lws-minimal-ss-hello_world
|
||||
|
@ -33,8 +31,7 @@ if (requirements)
|
|||
|
||||
### --- this section related to also building example with SSPC / Proxy --->
|
||||
|
||||
if (HAS_LWS_WITH_SECURE_STREAMS_PROXY_API OR has_ss_proxy OR
|
||||
LWS_WITH_SECURE_STREAMS_PROXY_API)
|
||||
if (has_ss_proxy OR LWS_WITH_SECURE_STREAMS_PROXY_API)
|
||||
add_compile_options(-DLWS_SS_USE_SSPC)
|
||||
|
||||
add_executable(${PROJECT_NAME}-client
|
||||
|
|
|
@ -26,18 +26,17 @@ static lws_ss_state_return_t
|
|||
hello_world_rx(void *userobj, const uint8_t *in, size_t len, int flags)
|
||||
{
|
||||
hello_world_t *g = (hello_world_t *)userobj;
|
||||
struct lws_ss_handle *h = lws_ss_from_user(g);
|
||||
|
||||
lwsl_ss_user(lws_ss_from_user(g), "RX %zu, flags 0x%x", len,
|
||||
(unsigned int)flags);
|
||||
lwsl_ss_user(h, "RX %zu, flags 0x%x", len, (unsigned int)flags);
|
||||
|
||||
if (len) { /* log the first 16 and last 16 bytes of the chunk */
|
||||
lwsl_hexdump_notice(in, len >= 16 ? 16 : len);
|
||||
lwsl_hexdump_ss_info(h, in, len >= 16 ? 16 : len);
|
||||
if (len >= 16)
|
||||
lwsl_hexdump_notice(in + len - 16, 16);
|
||||
lwsl_hexdump_ss_info(h, in + len - 16, 16);
|
||||
}
|
||||
|
||||
if ((flags & LWSSS_FLAG_EOM) == LWSSS_FLAG_EOM)
|
||||
/* We received the whole message */
|
||||
if ((flags & LWSSS_FLAG_EOM) == LWSSS_FLAG_EOM) /* had whole message */
|
||||
test_result &= ~2;
|
||||
|
||||
return LWSSSSRET_OK;
|
||||
|
|
|
@ -6,23 +6,24 @@
|
|||
* This file is made available under the Creative Commons CC0 1.0
|
||||
* Universal Public Domain Dedication.
|
||||
*
|
||||
* Demonstrates the simplest example using the LWS high-level SS apis.
|
||||
* Demonstrates the simplest example using the LWS high-level SS apis, doing
|
||||
* an h1 GET from warmcat.com.
|
||||
*
|
||||
* - main.c: (this file) boilerplate to create the lws_context
|
||||
* and event loop
|
||||
* - hello_world-ss.c: the secure stream user code
|
||||
* - example-policy.json: the example policy
|
||||
*
|
||||
* Configure lws with -DCMAKE_BUILD_TYPE=DEBUG to build verbose logs, enable at
|
||||
* runtime by giving -d 1039 or -d 1151 on this example commandline.
|
||||
*/
|
||||
|
||||
#include <libwebsockets.h>
|
||||
#include <signal.h>
|
||||
|
||||
/* b0: clr when peer ACKed request, b1: clr when recieved whole response */
|
||||
int test_result = 3;
|
||||
|
||||
int test_result = 3; /* b0: clr when peer ACKed request, b1: clr when rx done */
|
||||
extern const lws_ss_info_t ssi_hello_world_t; /* from hello_world-ss.c */
|
||||
|
||||
static struct lws_context *cx; /* so the SIGINT handler below can access it */
|
||||
static struct lws_context *cx; /* so the SIGINT handler below can access it */
|
||||
|
||||
static void
|
||||
sigint_handler(int sig)
|
||||
|
@ -39,13 +40,14 @@ main(int argc, const char **argv)
|
|||
lws_cmdline_option_handle_builtin(argc, argv, &info);
|
||||
signal(SIGINT, sigint_handler);
|
||||
|
||||
lwsl_user("LWS hello_world example [-d<verb>]\n");
|
||||
|
||||
if (!(cx = lws_create_context(&info))) {
|
||||
cx = lws_create_context(&info);
|
||||
if (!cx) {
|
||||
lwsl_err("lws init failed\n");
|
||||
return 1;
|
||||
}
|
||||
|
||||
lwsl_cx_user(cx, "LWS hello_world example [-d<verb>]\n");
|
||||
|
||||
if (lws_ss_create(cx, 0, &ssi_hello_world_t, NULL, NULL, NULL, NULL)) {
|
||||
lwsl_cx_err(cx, "failed to create get secure stream");
|
||||
lws_context_destroy(cx);
|
||||
|
@ -54,7 +56,7 @@ main(int argc, const char **argv)
|
|||
|
||||
lws_context_default_loop_run_destroy(cx);
|
||||
|
||||
/* process ret 0 if actual is as expected (0, or--expected-exit 123) */
|
||||
/* process ret 0 if result is as expected (0, or --expected-exit 123) */
|
||||
|
||||
return lws_cmdline_passfail(argc, argv, test_result);
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue