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":1582972501, "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":"bd1866e53092bb6530904f7fa1d0c293", "oid":{ "oid": "0f44160e56e4e710061d21ce4200f13cb47bcb17", "alias": [ "refs/heads/master"]},"blobname": "minimal-examples/ws-client/minimal-ws-client-pmd-bulk/protocol_lws_minimal_pmd_bulk.c", "blob": "/*\n * ws protocol handler plugin for \u0022lws-minimal-pmd-bulk\u0022\n *\n * Written in 2010-2019 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 * The protocol shows how to send and receive bulk messages over a ws connection\n * that optionally may have the permessage-deflate extension negotiated on it.\n */\n\n#if !defined (LWS_PLUGIN_STATIC)\n#define LWS_DLL\n#define LWS_INTERNAL\n#include \u003clibwebsockets.h\u003e\n#endif\n\n#include \u003cstring.h\u003e\n\n/*\n * We will produce a large ws message either from this text repeated many times,\n * or from 0x40 + a 6-bit pseudorandom number\n */\n\nstatic const char * const redundant_string \u003d\n\t\u0022No one would have believed in the last years of the nineteenth \u0022\n\t\u0022century that this world was being watched keenly and closely by \u0022\n\t\u0022intelligences greater than man's and yet as mortal as his own; that as \u0022\n\t\u0022men busied themselves about their various concerns they were \u0022\n\t\u0022scrutinised and studied, perhaps almost as narrowly as a man with a \u0022\n\t\u0022microscope might scrutinise the transient creatures that swarm and \u0022\n\t\u0022multiply in a drop of water. With infinite complacency men went to \u0022\n\t\u0022and fro over this globe about their little affairs, serene in their \u0022\n\t\u0022assurance of their empire over matter. It is possible that the \u0022\n\t\u0022infusoria under the microscope do the same. No one gave a thought to \u0022\n\t\u0022the older worlds of space as sources of human danger, or thought of \u0022\n\t\u0022them only to dismiss the idea of life upon them as impossible or \u0022\n\t\u0022improbable. It is curious to recall some of the mental habits of \u0022\n\t\u0022those departed days. At most terrestrial men fancied there might be \u0022\n\t\u0022other men upon Mars, perhaps inferior to themselves and ready to \u0022\n\t\u0022welcome a missionary enterprise. Yet across the gulf of space, minds \u0022\n\t\u0022that are to our minds as ours are to those of the beasts that perish, \u0022\n\t\u0022intellects vast and cool and unsympathetic, regarded this earth with \u0022\n\t\u0022envious eyes, and slowly and surely drew their plans against us. And \u0022\n\t\u0022early in the twentieth century came the great disillusionment. \u0022\n;\n\n/* this reflects the length of the string above */\n#define REPEAT_STRING_LEN 1337\n/* this is the total size of the ws message we will send */\n#define MESSAGE_SIZE (100 * REPEAT_STRING_LEN)\n/* this is how much we will send each time the connection is writable */\n#define MESSAGE_CHUNK_SIZE (1 * 1024)\n\n/* one of these is created for each client connecting to us */\n\nstruct per_session_data__minimal_pmd_bulk {\n\tint position_tx, position_rx;\n\tuint64_t rng_rx, rng_tx;\n};\n\nstruct vhd_minimal_pmd_bulk {\n\tstruct lws_context *context;\n\tstruct lws_vhost *vhost;\n\tstruct lws *client_wsi;\n\n\tint *interrupted;\n\tint *options;\n};\n\nstatic uint64_t rng(uint64_t *r)\n{\n *r ^\u003d *r \u003c\u003c 21;\n *r ^\u003d *r \u003e\u003e 35;\n *r ^\u003d *r \u003c\u003c 4;\n\n return *r;\n}\n\nstatic int\nconnect_client(struct vhd_minimal_pmd_bulk *vhd)\n{\n\tstruct lws_client_connect_info i;\n\n\tmemset(\u0026i, 0, sizeof(i));\n\n\ti.context \u003d vhd-\u003econtext;\n\ti.port \u003d 7681;\n\ti.address \u003d \u0022localhost\u0022;\n\ti.path \u003d \u0022/\u0022;\n\ti.host \u003d i.address;\n\ti.origin \u003d i.address;\n\ti.ssl_connection \u003d 0;\n\ti.vhost \u003d vhd-\u003evhost;\n\ti.protocol \u003d \u0022lws-minimal-pmd-bulk\u0022;\n\ti.pwsi \u003d \u0026vhd-\u003eclient_wsi;\n\n\treturn !lws_client_connect_via_info(\u0026i);\n}\n\nstatic void\nschedule_callback(struct lws *wsi, int reason, int secs)\n{\n\tlws_timed_callback_vh_protocol(lws_get_vhost(wsi),\n\t\tlws_get_protocol(wsi), reason, secs);\n}\n\nstatic int\ncallback_minimal_pmd_bulk(struct lws *wsi, enum lws_callback_reasons reason,\n\t\t\t void *user, void *in, size_t len)\n{\n\tstruct per_session_data__minimal_pmd_bulk *pss \u003d\n\t\t\t(struct per_session_data__minimal_pmd_bulk *)user;\n\tstruct vhd_minimal_pmd_bulk *vhd \u003d (struct vhd_minimal_pmd_bulk *)\n\t\t\tlws_protocol_vh_priv_get(lws_get_vhost(wsi),\n\t\t\t\tlws_get_protocol(wsi));\n\tuint8_t buf[LWS_PRE + MESSAGE_CHUNK_SIZE], *start \u003d \u0026buf[LWS_PRE], *p;\n\tint n, m, flags;\n\n\tswitch (reason) {\n\n\tcase LWS_CALLBACK_PROTOCOL_INIT:\n\t\tvhd \u003d lws_protocol_vh_priv_zalloc(lws_get_vhost(wsi),\n\t\t\t\tlws_get_protocol(wsi),\n\t\t\t\tsizeof(struct vhd_minimal_pmd_bulk));\n\t\tif (!vhd)\n\t\t\treturn -1;\n\n\t\tvhd-\u003econtext \u003d lws_get_context(wsi);\n\t\tvhd-\u003evhost \u003d lws_get_vhost(wsi);\n\n\t\t/* get the pointer to \u0022interrupted\u0022 we were passed in pvo */\n\t\tvhd-\u003einterrupted \u003d (int *)lws_pvo_search(\n\t\t\t(const struct lws_protocol_vhost_options *)in,\n\t\t\t\u0022interrupted\u0022)-\u003evalue;\n\t\tvhd-\u003eoptions \u003d (int *)lws_pvo_search(\n\t\t\t(const struct lws_protocol_vhost_options *)in,\n\t\t\t\u0022options\u0022)-\u003evalue;\n\n\t\tif (connect_client(vhd))\n\t\t\tschedule_callback(wsi, LWS_CALLBACK_USER, 1);\n\t\tbreak;\n\n\tcase LWS_CALLBACK_CLIENT_ESTABLISHED:\n\t\tpss-\u003erng_tx \u003d 4;\n\t\tpss-\u003erng_rx \u003d 4;\n\t\tlws_callback_on_writable(wsi);\n\t\tbreak;\n\n\tcase LWS_CALLBACK_CLIENT_WRITEABLE:\n\n\t\t/*\n\t\t * when we connect, we will send the server a message\n\t\t */\n\n\t\tif (pss-\u003eposition_tx \u003d\u003d MESSAGE_SIZE)\n\t\t\tbreak;\n\n\t\t/* fill up one chunk's worth of message content */\n\n\t\tp \u003d start;\n\t\tn \u003d MESSAGE_CHUNK_SIZE;\n\t\tif (n \u003e MESSAGE_SIZE - pss-\u003eposition_tx)\n\t\t\tn \u003d MESSAGE_SIZE - pss-\u003eposition_tx;\n\n\t\tflags \u003d lws_write_ws_flags(LWS_WRITE_BINARY, !pss-\u003eposition_tx,\n\t\t\t\t\t pss-\u003eposition_tx + n \u003d\u003d MESSAGE_SIZE);\n\n\t\t/*\n\t\t * select between producing compressible repeated text,\n\t\t * or uncompressible PRNG output\n\t\t */\n\n\t\tif (*vhd-\u003eoptions \u0026 1) {\n\t\t\twhile (n) {\n\t\t\t\tsize_t s;\n\n\t\t\t\tm \u003d pss-\u003eposition_tx % REPEAT_STRING_LEN;\n\t\t\t\ts \u003d REPEAT_STRING_LEN - m;\n\t\t\t\tif (s \u003e (size_t)n)\n\t\t\t\t\ts \u003d n;\n\t\t\t\tmemcpy(p, \u0026redundant_string[m], s);\n\t\t\t\tpss-\u003eposition_tx +\u003d s;\n\t\t\t\tp +\u003d s;\n\t\t\t\tn -\u003d s;\n\t\t\t}\n\t\t} else {\n\t\t\tpss-\u003eposition_tx +\u003d n;\n\t\t\twhile (n--)\n\t\t\t\t*p++ \u003d rng(\u0026pss-\u003erng_tx);\n\t\t}\n\n\t\tn \u003d lws_ptr_diff(p, start);\n\t\tm \u003d lws_write(wsi, start, n, flags);\n\t\tif (m \u003c n) {\n\t\t\tlwsl_err(\u0022ERROR %d writing ws\u005cn\u0022, m);\n\t\t\treturn -1;\n\t\t}\n\t\tif (pss-\u003eposition_tx !\u003d MESSAGE_SIZE) /* if more to do... */\n\t\t\tlws_callback_on_writable(wsi);\n\t\telse\n\t\t\t/* if we sent and received everything */\n\t\t\tif (pss-\u003eposition_rx \u003d\u003d MESSAGE_SIZE)\n\t\t\t\t*vhd-\u003einterrupted \u003d 2;\n\t\tbreak;\n\n\tcase LWS_CALLBACK_CLIENT_RECEIVE:\n\n\t\t/*\n\t\t * When we connect, the server will send us a message too\n\t\t */\n\n\t\tlwsl_user(\u0022LWS_CALLBACK_CLIENT_RECEIVE: %4d (rpp %5d, last %d)\u005cn\u0022,\n\t\t\t(int)len, (int)lws_remaining_packet_payload(wsi),\n\t\t\tlws_is_final_fragment(wsi));\n\n\t\tif (*vhd-\u003eoptions \u0026 1) {\n\t\t\twhile (len) {\n\t\t\t\tsize_t s;\n\n\t\t\t\tm \u003d pss-\u003eposition_rx % REPEAT_STRING_LEN;\n\t\t\t\ts \u003d REPEAT_STRING_LEN - m;\n\t\t\t\tif (s \u003e len)\n\t\t\t\t\ts \u003d len;\n\t\t\t\tif (memcmp(in, \u0026redundant_string[m], s)) {\n\t\t\t\t\tlwsl_user(\u0022echo'd data doesn't match\u005cn\u0022);\n\t\t\t\t\treturn -1;\n\t\t\t\t}\n\t\t\t\tpss-\u003eposition_rx +\u003d s;\n\t\t\t\tin \u003d ((unsigned char *)in) + s;\n\t\t\t\tlen -\u003d s;\n\t\t\t}\n\t\t} else {\n\t\t\tp \u003d (uint8_t *)in;\n\t\t\tpss-\u003eposition_rx +\u003d len;\n\t\t\twhile (len--)\n\t\t\t\tif (*p++ !\u003d (uint8_t)rng(\u0026pss-\u003erng_rx)) {\n\t\t\t\t\tlwsl_user(\u0022echo'd data doesn't match\u005cn\u0022);\n\t\t\t\t\treturn -1;\n\t\t\t\t}\n\t\t}\n\n\t\t/* if we sent and received everything */\n\n\t\tif (pss-\u003eposition_rx \u003d\u003d MESSAGE_SIZE \u0026\u0026\n\t\t pss-\u003eposition_tx \u003d\u003d MESSAGE_SIZE)\n\t\t\t*vhd-\u003einterrupted \u003d 2;\n\n\t\tbreak;\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\tvhd-\u003eclient_wsi \u003d NULL;\n\t\tschedule_callback(wsi, LWS_CALLBACK_USER, 1);\n\t\tbreak;\n\n\tcase LWS_CALLBACK_CLIENT_CLOSED:\n\t\tvhd-\u003eclient_wsi \u003d NULL;\n\t\tschedule_callback(wsi, LWS_CALLBACK_USER, 1);\n\t\tbreak;\n\n\t/* rate-limited client connect retries */\n\n\tcase LWS_CALLBACK_USER:\n\t\tlwsl_notice(\u0022%s: LWS_CALLBACK_USER\u005cn\u0022, __func__);\n\t\tif (connect_client(vhd))\n\t\t\tschedule_callback(wsi, LWS_CALLBACK_USER, 1);\n\t\tbreak;\n\n\tdefault:\n\t\tbreak;\n\t}\n\n\treturn 0;\n}\n\n#define LWS_PLUGIN_PROTOCOL_MINIMAL_PMD_BULK \u005c\n\t{ \u005c\n\t\t\u0022lws-minimal-pmd-bulk\u0022, \u005c\n\t\tcallback_minimal_pmd_bulk, \u005c\n\t\tsizeof(struct per_session_data__minimal_pmd_bulk), \u005c\n\t\t4096, \u005c\n\t\t0, NULL, 0 \u005c\n\t}\n\n#if !defined (LWS_PLUGIN_STATIC)\n\n/* boilerplate needed if we are built as a dynamic plugin */\n\nstatic const struct lws_protocols protocols[] \u003d {\n\tLWS_PLUGIN_PROTOCOL_MINIMAL_PMD_BULK\n};\n\nint\ninit_protocol_minimal_pmd_bulk(struct lws_context *context,\n\t\t\t struct lws_plugin_capability *c)\n{\n\tif (c-\u003eapi_magic !\u003d LWS_PLUGIN_API_MAGIC) {\n\t\tlwsl_err(\u0022Plugin API %d, library API %d\u0022, LWS_PLUGIN_API_MAGIC,\n\t\t\t c-\u003eapi_magic);\n\t\treturn 1;\n\t}\n\n\tc-\u003eprotocols \u003d protocols;\n\tc-\u003ecount_protocols \u003d LWS_ARRAY_SIZE(protocols);\n\tc-\u003eextensions \u003d NULL;\n\tc-\u003ecount_extensions \u003d 0;\n\n\treturn 0;\n}\n\nint\ndestroy_protocol_minimal_pmd_bulk(struct lws_context *context)\n{\n\treturn 0;\n}\n#endif\n","s":{"c":1582972501,"u": 470}} ],"g": 2989,"chitpc": 0,"ehitpc": 0,"indexed":0 , "ab": 1, "si": 0, "db":0, "di":0, "sat":0, "lfc": "0000"}