• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * lws-minimal-ws-client-ping
3  *
4  * Written in 2010-2020 by Andy Green <andy@warmcat.com>
5  *
6  * This file is made available under the Creative Commons CC0 1.0
7  * Universal Public Domain Dedication.
8  *
9  * This demonstrates keeping a ws connection validated by the lws validity
10  * timer stuff without having to do anything in the code.  Use debug logging
11  * -d1039 to see lws doing the pings / pongs in the background.
12  */
13 
14 #include <libwebsockets.h>
15 #include <string.h>
16 #include <signal.h>
17 #include <pthread.h>
18 
19 static struct lws_context *context;
20 static struct lws *client_wsi;
21 static int interrupted, port = 443, ssl_connection = LCCSCF_USE_SSL;
22 static const char *server_address = "libwebsockets.org", *pro = "lws-mirror-protocol";
23 static lws_sorted_usec_list_t sul;
24 
25 static const lws_retry_bo_t retry = {
26 	.secs_since_valid_ping = 3,
27 	.secs_since_valid_hangup = 10,
28 };
29 
30 static void
connect_cb(lws_sorted_usec_list_t * _sul)31 connect_cb(lws_sorted_usec_list_t *_sul)
32 {
33 	struct lws_client_connect_info i;
34 
35 	lwsl_notice("%s: connecting\n", __func__);
36 
37 	memset(&i, 0, sizeof(i));
38 
39 	i.context = context;
40 	i.port = port;
41 	i.address = server_address;
42 	i.path = "/";
43 	i.host = i.address;
44 	i.origin = i.address;
45 	i.ssl_connection = ssl_connection;
46 	i.protocol = pro;
47 	i.alpn = "h2;http/1.1";
48 	i.local_protocol_name = "lws-ping-test";
49 	i.pwsi = &client_wsi;
50 	i.retry_and_idle_policy = &retry;
51 
52 	if (!lws_client_connect_via_info(&i))
53 		lws_sul_schedule(context, 0, _sul, connect_cb, 5 * LWS_USEC_PER_SEC);
54 }
55 
56 static int
callback_minimal_pingtest(struct lws * wsi,enum lws_callback_reasons reason,void * user,void * in,size_t len)57 callback_minimal_pingtest(struct lws *wsi, enum lws_callback_reasons reason,
58 			 void *user, void *in, size_t len)
59 {
60 
61 	switch (reason) {
62 
63 	case LWS_CALLBACK_CLIENT_CONNECTION_ERROR:
64 		lwsl_err("CLIENT_CONNECTION_ERROR: %s\n",
65 			 in ? (char *)in : "(null)");
66 		lws_sul_schedule(context, 0, &sul, connect_cb, 5 * LWS_USEC_PER_SEC);
67 		break;
68 
69 	case LWS_CALLBACK_CLIENT_ESTABLISHED:
70 		lwsl_user("%s: established\n", __func__);
71 		break;
72 
73 	default:
74 		break;
75 	}
76 
77 	return lws_callback_http_dummy(wsi, reason, user, in, len);
78 }
79 
80 static const struct lws_protocols protocols[] = {
81 	{
82 		"lws-ping-test",
83 		callback_minimal_pingtest,
84 		0,
85 		0,
86 	},
87 	{ NULL, NULL, 0, 0 }
88 };
89 
90 static void
sigint_handler(int sig)91 sigint_handler(int sig)
92 {
93 	interrupted = 1;
94 }
95 
main(int argc,const char ** argv)96 int main(int argc, const char **argv)
97 {
98 	struct lws_context_creation_info info;
99 	const char *p;
100 	int n = 0, logs = LLL_USER | LLL_ERR | LLL_WARN | LLL_NOTICE
101 			/* for LLL_ verbosity above NOTICE to be built into lws,
102 			 * lws must have been configured and built with
103 			 * -DCMAKE_BUILD_TYPE=DEBUG instead of =RELEASE */
104 			/* | LLL_INFO */ /* | LLL_PARSER */ /* | LLL_HEADER */
105 			/* | LLL_EXT */ /* | LLL_CLIENT */ /* | LLL_LATENCY */
106 			/* | LLL_DEBUG */;
107 
108 	signal(SIGINT, sigint_handler);
109 
110 	if ((p = lws_cmdline_option(argc, argv, "-d")))
111 		logs = atoi(p);
112 
113 	lws_set_log_level(logs, NULL);
114 	lwsl_user("LWS minimal ws client PING\n");
115 
116 	memset(&info, 0, sizeof info); /* otherwise uninitialized garbage */
117 	info.options = LWS_SERVER_OPTION_DO_SSL_GLOBAL_INIT;
118 	info.port = CONTEXT_PORT_NO_LISTEN; /* we do not run any server */
119 	info.protocols = protocols;
120 #if defined(LWS_WITH_MBEDTLS)
121 	/*
122 	 * OpenSSL uses the system trust store.  mbedTLS has to be told which
123 	 * CA to trust explicitly.
124 	 */
125 	info.client_ssl_ca_filepath = "./libwebsockets.org.cer";
126 #endif
127 
128 	if ((p = lws_cmdline_option(argc, argv, "--protocol")))
129 		pro = p;
130 
131 	if ((p = lws_cmdline_option(argc, argv, "--server"))) {
132 		server_address = p;
133 		pro = "lws-minimal";
134 		ssl_connection |= LCCSCF_ALLOW_SELFSIGNED;
135 	}
136 
137 	if ((p = lws_cmdline_option(argc, argv, "--port")))
138 		port = atoi(p);
139 
140 	info.fd_limit_per_thread = 1 + 1 + 1;
141 
142 	context = lws_create_context(&info);
143 	if (!context) {
144 		lwsl_err("lws init failed\n");
145 		return 1;
146 	}
147 
148 	lws_sul_schedule(context, 0, &sul, connect_cb, 100);
149 
150 	while (n >= 0 && !interrupted)
151 		n = lws_service(context, 0);
152 
153 	lws_context_destroy(context);
154 	lwsl_user("Completed\n");
155 
156 	return 0;
157 }
158