Project homepage Mailing List  Warmcat.com  API Docs  Github Mirror 
{"schema":"libjg2-1", "vpath":"/git/", "avatar":"/git/avatar/", "alang":"en-US,en;q\u003d0.5", "gen_ut":1606542680, "reponame":"libwebsockets", "desc":"libwebsockets lightweight C networking library", "owner": { "name": "Andy Green", "email": "andy@warmcat.com", "md5": "c50933ca2aa61e0fe2c43d46bb6b59cb" },"url":"https://libwebsockets.org/repo/libwebsockets", "f":3, "items": [ {"schema":"libjg2-1", "cid":"2d126013892c9c606afa4e5890134ac3", "oid":{ "oid": "41240965cf0359128c7c51cd35d6bd6d0bc95bb0", "alias": [ "refs/heads/master"]},"blobname": "minimal-examples/ws-client/minimal-ws-client/minimal-ws-client.c", "blob": "/*\n * lws-minimal-ws-client\n *\n * Written in 2010-2020 by Andy Green \u003candy@warmcat.com\u003e\n *\n * This file is made available under the Creative Commons CC0 1.0\n * Universal Public Domain Dedication.\n *\n * This demonstrates a ws client that connects by default to libwebsockets.org\n * dumb increment ws server.\n */\n\n#include \u003clibwebsockets.h\u003e\n#include \u003cstring.h\u003e\n#include \u003csignal.h\u003e\n\n/*\n * This represents your object that \u0022contains\u0022 the client connection and has\n * the client connection bound to it\n */\n\nstatic struct my_conn {\n\tlws_sorted_usec_list_t\tsul;\t /* schedule connection retry */\n\tstruct lws\t\t*wsi;\t /* related wsi if any */\n\tuint16_t\t\tretry_count; /* count of consequetive retries */\n} mco;\n\nstatic struct lws_context *context;\nstatic int interrupted, port \u003d 443, ssl_connection \u003d LCCSCF_USE_SSL;\nstatic const char *server_address \u003d \u0022libwebsockets.org\u0022,\n\t\t *pro \u003d \u0022dumb-increment-protocol\u0022;\n\n/*\n * The retry and backoff policy we want to use for our client connections\n */\n\nstatic const uint32_t backoff_ms[] \u003d { 1000, 2000, 3000, 4000, 5000 };\n\nstatic const lws_retry_bo_t retry \u003d {\n\t.retry_ms_table\t\t\t\u003d backoff_ms,\n\t.retry_ms_table_count\t\t\u003d LWS_ARRAY_SIZE(backoff_ms),\n\t.conceal_count\t\t\t\u003d LWS_ARRAY_SIZE(backoff_ms),\n\n\t.secs_since_valid_ping\t\t\u003d 3, /* force PINGs after secs idle */\n\t.secs_since_valid_hangup\t\u003d 10, /* hangup after secs idle */\n\n\t.jitter_percent\t\t\t\u003d 20,\n};\n\n/*\n * Scheduled sul callback that starts the connection attempt\n */\n\nstatic void\nconnect_client(lws_sorted_usec_list_t *sul)\n{\n\tstruct my_conn *mco \u003d lws_container_of(sul, struct my_conn, sul);\n\tstruct lws_client_connect_info i;\n\n\tmemset(\u0026i, 0, sizeof(i));\n\n\ti.context \u003d context;\n\ti.port \u003d port;\n\ti.address \u003d server_address;\n\ti.path \u003d \u0022/\u0022;\n\ti.host \u003d i.address;\n\ti.origin \u003d i.address;\n\ti.ssl_connection \u003d ssl_connection;\n\ti.protocol \u003d pro;\n\ti.local_protocol_name \u003d \u0022lws-minimal-client\u0022;\n\ti.pwsi \u003d \u0026mco-\u003ewsi;\n\ti.retry_and_idle_policy \u003d \u0026retry;\n\ti.userdata \u003d mco;\n\n\tif (!lws_client_connect_via_info(\u0026i))\n\t\t/*\n\t\t * Failed... schedule a retry... we can't use the _retry_wsi()\n\t\t * convenience wrapper api here because no valid wsi at this\n\t\t * point.\n\t\t */\n\t\tif (lws_retry_sul_schedule(context, 0, sul, \u0026retry,\n\t\t\t\t\t connect_client, \u0026mco-\u003eretry_count)) {\n\t\t\tlwsl_err(\u0022%s: connection attempts exhausted\u005cn\u0022, __func__);\n\t\t\tinterrupted \u003d 1;\n\t\t}\n}\n\nstatic int\ncallback_minimal(struct lws *wsi, enum lws_callback_reasons reason,\n\t\t void *user, void *in, size_t len)\n{\n\tstruct my_conn *mco \u003d (struct my_conn *)user;\n\n\tswitch (reason) {\n\n\tcase LWS_CALLBACK_CLIENT_CONNECTION_ERROR:\n\t\tlwsl_err(\u0022CLIENT_CONNECTION_ERROR: %s\u005cn\u0022,\n\t\t\t in ? (char *)in : \u0022(null)\u0022);\n\t\tgoto do_retry;\n\t\tbreak;\n\n\tcase LWS_CALLBACK_CLIENT_RECEIVE:\n\t\tlwsl_hexdump_notice(in, len);\n\t\tbreak;\n\n\tcase LWS_CALLBACK_CLIENT_ESTABLISHED:\n\t\tlwsl_user(\u0022%s: established\u005cn\u0022, __func__);\n\t\tbreak;\n\n\tcase LWS_CALLBACK_CLIENT_CLOSED:\n\t\tgoto do_retry;\n\n\tdefault:\n\t\tbreak;\n\t}\n\n\treturn lws_callback_http_dummy(wsi, reason, user, in, len);\n\ndo_retry:\n\t/*\n\t * retry the connection to keep it nailed up\n\t *\n\t * For this example, we try to conceal any problem for one set of\n\t * backoff retries and then exit the app.\n\t *\n\t * If you set retry.conceal_count to be larger than the number of\n\t * elements in the backoff table, it will never give up and keep\n\t * retrying at the last backoff delay plus the random jitter amount.\n\t */\n\tif (lws_retry_sul_schedule_retry_wsi(wsi, \u0026mco-\u003esul, connect_client,\n\t\t\t\t\t \u0026mco-\u003eretry_count)) {\n\t\tlwsl_err(\u0022%s: connection attempts exhausted\u005cn\u0022, __func__);\n\t\tinterrupted \u003d 1;\n\t}\n\n\treturn 0;\n}\n\nstatic const struct lws_protocols protocols[] \u003d {\n\t{ \u0022lws-minimal-client\u0022, callback_minimal, 0, 0, },\n\t{ NULL, NULL, 0, 0 }\n};\n\nstatic void\nsigint_handler(int sig)\n{\n\tinterrupted \u003d 1;\n}\n\nint main(int argc, const char **argv)\n{\n\tstruct lws_context_creation_info info;\n\tconst char *p;\n\tint n \u003d 0;\n\n\tsignal(SIGINT, sigint_handler);\n\tmemset(\u0026info, 0, sizeof info);\n\tlws_cmdline_option_handle_builtin(argc, argv, \u0026info);\n\n\tlwsl_user(\u0022LWS minimal ws client\u005cn\u0022);\n\n\tinfo.options \u003d LWS_SERVER_OPTION_DO_SSL_GLOBAL_INIT;\n\tinfo.port \u003d CONTEXT_PORT_NO_LISTEN; /* we do not run any server */\n\tinfo.protocols \u003d protocols;\n\n#if defined(LWS_WITH_MBEDTLS) || defined(USE_WOLFSSL)\n\t/*\n\t * OpenSSL uses the system trust store. mbedTLS has to be told which\n\t * CA to trust explicitly.\n\t */\n\tinfo.client_ssl_ca_filepath \u003d \u0022./libwebsockets.org.cer\u0022;\n#endif\n\n\tif ((p \u003d lws_cmdline_option(argc, argv, \u0022--protocol\u0022)))\n\t\tpro \u003d p;\n\n\tif ((p \u003d lws_cmdline_option(argc, argv, \u0022-s\u0022)))\n\t\tserver_address \u003d p;\n\n\tif ((p \u003d lws_cmdline_option(argc, argv, \u0022-p\u0022)))\n\t\tport \u003d atoi(p);\n\n\tif (lws_cmdline_option(argc, argv, \u0022-j\u0022))\n\t\tssl_connection |\u003d LCCSCF_ALLOW_SELFSIGNED;\n\n\tif (lws_cmdline_option(argc, argv, \u0022-k\u0022))\n\t\tssl_connection |\u003d LCCSCF_ALLOW_INSECURE;\n\n\tif (lws_cmdline_option(argc, argv, \u0022-m\u0022))\n\t\tssl_connection |\u003d LCCSCF_SKIP_SERVER_CERT_HOSTNAME_CHECK;\n\n\tif (lws_cmdline_option(argc, argv, \u0022-e\u0022))\n\t\tssl_connection |\u003d LCCSCF_ALLOW_EXPIRED;\n\n\tinfo.fd_limit_per_thread \u003d 1 + 1 + 1;\n\n\tcontext \u003d lws_create_context(\u0026info);\n\tif (!context) {\n\t\tlwsl_err(\u0022lws init failed\u005cn\u0022);\n\t\treturn 1;\n\t}\n\n\t/* schedule the first client connection attempt to happen immediately */\n\tlws_sul_schedule(context, 0, \u0026mco.sul, connect_client, 1);\n\n\twhile (n \u003e\u003d 0 \u0026\u0026 !interrupted)\n\t\tn \u003d lws_service(context, 0);\n\n\tlws_context_destroy(context);\n\tlwsl_user(\u0022Completed\u005cn\u0022);\n\n\treturn 0;\n}\n","s":{"c":1606542680,"u": 300}} ],"g": 1771,"chitpc": 0,"ehitpc": 0,"indexed":0 , "ab": 1, "si": 0, "db":0, "di":0, "sat":0, "lfc": "0000"}