void FNetworkFileServerHttp::Exit() { // let's start shutting down. // fires a LWS_CALLBACK_PROTOCOL_DESTROY callback, we clean up after ourselves there. libwebsocket_context_destroy(Context); Context = NULL; }
int idaapi websocket_thread(void *) { struct libwebsocket_context* context; struct lws_context_creation_info info; memset(&info, 0, sizeof info); info.port = 3003; info.iface = NULL; info.protocols = protocols; info.extensions = libwebsocket_get_internal_extensions(); info.gid = -1; info.uid = -1; info.options = 0; // i assume this does the bind? context = libwebsocket_create_context(&info); if (context == NULL) { msg("websocket init failed\n"); return -1; } msg("yay websockets\n"); while (websockets_running) { libwebsocket_service(context, 50); } libwebsocket_context_destroy(context); return 0; }
int WebSocketOpcode_finish(CSOUND *csound, void *opaqueReference) { WebSocketOpcode *self = opaqueReference; self->isRunning = false; csound->JoinThread(self->webSocket->processThread); libwebsocket_cancel_service(self->webSocket->context); libwebsocket_context_destroy(self->webSocket->context); int i; for (i = 0; i < self->outputArgumentCount; ++i) { csoundDestroyCircularBuffer(csound, self->outputArguments[i].circularBuffer); } for (i = 0; i < self->inputArgumentCount; ++i) { csoundDestroyCircularBuffer(csound, self->inputArguments[i].circularBuffer); } csound->Free(csound, self->webSocket->protocols); csound->Free(csound, self->webSocket->messageBuffer); csound->Free(csound, self->webSocket); if (self->inputArgumentCount > 0) { csound->Free(csound, self->inputArguments); } if (self->outputArgumentCount > 0) { csound->Free(csound, self->outputArguments); } return OK; }
//-------------------------------------------------------------- void Client::threadedFunction(){ while ( isThreadRunning() ){ for (int i=0; i<protocols.size(); ++i){ if (protocols[i].second != NULL){ //lock(); protocols[i].second->execute(); //unlock(); } } if (context != NULL && lwsconnection != NULL){ //libwebsocket_callback_on_writable(context,lwsconnection); connection->update(); if (lock()) { int n = libwebsocket_service(context, waitMillis); unlock(); } } else { stopThread(); if ( context != NULL ){ closeAndFree = true; libwebsocket_context_destroy( context ); context = NULL; lwsconnection = NULL; } if (connection != NULL){ connection = NULL; } } } }
int _ortc_prepare_websocket(ortc_context* context){ struct lws_context_creation_info info; if(context->lws_context) libwebsocket_context_destroy(context->lws_context); context->lws_context = NULL; if(context->host) free(context->host); context->host = NULL; if(context->server) free(context->server); context->server = NULL; memset(&info, 0, sizeof info); info.port = CONTEXT_PORT_NO_LISTEN; info.gid = -1; info.uid = -1; info.protocols = ortc_protocols; info.ssl_cipher_list = "RC4-MD5:RC4-SHA:AES128-SHA:AES256-SHA:HIGH:!DSS:!aNULL"; info.ka_time = 0; info.ka_interval = 0; info.ka_probes = 0; context->lws_context = libwebsocket_create_context(&info); if (context->lws_context == NULL) { _ortc_exception(context, "Creating libwebsocket context failed!"); return -1; } return 0; }
int main(int argc, char **argv) { int port = 8080; unsigned char buf[LWS_SEND_BUFFER_PRE_PADDING + 1024 + LWS_SEND_BUFFER_POST_PADDING]; struct libwebsocket_context *context = libwebsocket_create_context( port, "", protocols, libwebsocket_internal_extensions, nullptr, nullptr, -1, -1, 0); if (context == NULL) { fprintf(stderr, "libwebsocket init failed\n"); return -1; } buf[LWS_SEND_BUFFER_PRE_PADDING] = 'x'; int n = libwebsockets_fork_service_loop(context); if (n < 0) { fprintf(stderr, "Unable to fork service loop %d\n", n); return 1; } while(true) { sleep(1); libwebsockets_broadcast(&protocols[PROTOCOL_NRT_WS], &buf[LWS_SEND_BUFFER_PRE_PADDING], 1); } libwebsocket_context_destroy(context); return 0; }
int main() { struct lws_context_creation_info info; char *port; signal(SIGINT, sighandler); port = getenv("PORT"); memset(&info, 0, sizeof info); info.port = (port == NULL) ? DEFAULT_PORT : atoi(port); info.protocols = protocols; info.extensions = libwebsocket_get_internal_extensions(); info.gid = -1; info.uid = -1; context = libwebsocket_create_context(&info); if (context == NULL) { lwsl_err("libwebsocket init failed\n"); return -1; } while (!force_exit) { libwebsocket_service(context, 50); } libwebsocket_context_destroy(context); lwsl_notice("exited cleanly\n"); return 0; }
int main () { struct libwebsocket_context *context; struct lws_context_creation_info info; memset(&info, 0, sizeof info); info.port = 7681; info.iface = NULL; info.protocols = protocols; info.extensions = libwebsocket_get_internal_extensions(); info.ssl_cert_filepath = NULL; info.ssl_private_key_filepath = NULL; info.gid = -1; info.uid = -1; info.options = 0; context = libwebsocket_create_context(&info); printf ("starting server.\n"); while (1) { libwebsocket_service(context, 50); } libwebsocket_context_destroy(context); return 0; }
void ws_end(){ if(context){ libwebsocket_context_destroy(context); context=0; lwsl_notice("libwebsockets-test-server exited cleanly\n"); } }
//-------------------------------------------------------------- void Reactor::exit(){ if (context != NULL) { waitForThread(true); libwebsocket_context_destroy(context); context = NULL; } }
//-------------------------------------------------------------- void Reactor::exit(){ if (context != NULL) { stopThread(); libwebsocket_context_destroy(context); context = NULL; } }
void SignalingWebSocketServer::run() { while ( !bQuit ) { libwebsocket_service(context, 50); sig_thread->ProcessMessages(10); } usleep(10); libwebsocket_context_destroy(context); }
int websocket_destroy(int socket) { libwebsocket_context *context = contexts[socket].context; if(context == NULL) return -1; libwebsocket_context_destroy(context); contexts[socket].context = NULL; return 0; }
int main( void ) { int port = 8080; const char *interface = NULL; struct libwebsocket_context *context; struct lws_context_creation_info context_info; memset( &context_info, 0, sizeof( struct lws_context_creation_info ) ); context_info.port = 8080; context_info.iface = NULL; context_info.protocols = protocols; context_info.extensions = libwebsocket_get_internal_extensions(); context_info.ssl_cert_filepath = NULL; context_info.ssl_private_key_filepath = NULL; context_info.ssl_ca_filepath = NULL; context_info.ssl_cipher_list = NULL; context_info.http_proxy_address = NULL; context_info.http_proxy_port = 0; context_info.gid = -1; context_info.uid = -1; context_info.options = 0; context_info.user = NULL; context_info.ka_time = 0; context_info.ka_probes = 0; context_info.ka_interval = 0; const char *cert_path = NULL; const char *key_path = NULL; int opts = 0; // context = libwebsocket_create_context( port, interface, protocols, // libwebsocket_internal_extensions, // cert_path, key_path, -1, -1, opts ); context = libwebsocket_create_context( &context_info ); if ( context == NULL ) { fprintf( stderr, "libwebsocket init failed\n" ); return -1; } printf( "Starting server ...\n" ); while ( 1 ) { libwebsocket_service( context, 50 ); } libwebsocket_context_destroy( context ); return 0; }
bool WebSocketClient2::disconnectFromServer() { libwebsocket_close_and_free_session(this->m_lws_ctx, this->m_wsi, LWS_CLOSE_STATUS_GOINGAWAY); this->quit_cli_loop = true; this->m_wsi = NULL; libwebsocket_context_destroy(this->m_lws_ctx); this->m_lws_ctx = NULL; return true; }
void server_close(server_context* context) { if (context != NULL) { if (context->lws_context != NULL) { libwebsocket_context_destroy(context->lws_context); context->lws_context = NULL; context->send_buffer = NULL; context->send_length = 0; } } }
FWebSocketServer::~FWebSocketServer() { #if !PLATFORM_HTML5 if (Context) { libwebsocket_context_destroy(Context); Context = NULL; } delete Protocols; Protocols = NULL; #endif }
static void websocketShutdown() { assert(wsState); assert(wsState->websocketContext); wsState->connections.clear(); // destroying all the connections must have cleared these assert(wsState->wsiLookup.empty()); assert(wsState->pendingData.empty()); libwebsocket_context_destroy(wsState->websocketContext); wsState->websocketContext = NULL; delete wsState; wsState = NULL; }
//--------------------------------------------------------------------------------- // Purpose: called when the plugin is unloaded (turned off) //--------------------------------------------------------------------------------- void WebSpecPlugin::Unload( void ) { gameEventManager->RemoveListener( this ); // make sure we are unloaded from the event system ws_shouldListen = false; #if defined(_LINUX) || defined(_OSX) usleep(60*1000); #else Sleep(60); #endif libwebsocket_context_destroy(wsContext); ConVar_Unregister( ); DisconnectTier1Libraries( ); }
void* HostProcess_WebSocketListener(void* stuff) { HostStuff* phoststuff=(HostStuff*)stuff; struct libwebsocket_context *context; int opts = 0; struct lws_context_creation_info info; memset(&info, 0, sizeof info); info.port = WEB_PORT; info.iface = 0; info.protocols = protocols; info.extensions = libwebsocket_get_internal_extensions(); info.ssl_cert_filepath = NULL; info.ssl_private_key_filepath = NULL; info.gid = -1; info.uid = -1; info.options = opts; info.user=phoststuff; context = libwebsocket_create_context(&info); if (context == NULL) { fprintf(stderr, "libwebsocket init failed\n"); return 0; } printf("starting server...\n"); // infinite loop, to end this server send SIGTERM. (CTRL+C) while (1) { libwebsocket_service(context, 50); // libwebsocket_service will process all waiting events with their // callback functions and then wait 50 ms. // (this is a single threaded webserver and this will keep our server // from generating load while there are not requests to process) } libwebsocket_context_destroy(context); return 0; }
//============================================================================== // WsInput //------------------------------------------------------------------------------ void WsInput::wsConnect(WsInput* wsinput) { libwebsocket_context *context; lws_context_creation_info info; int debug_level = 7; memset(&info,0,sizeof info); info.port = 7681; lws_set_log_level(debug_level,da); lwsl_notice("libwebsockets chat server -\n"); libwebsocket_protocols protocols[] = { { "input", WsInput::wsCallBackData, 0, 128 }, {NULL,NULL,0,0} /* terminator */ }; info.iface = nullptr; info.protocols = protocols; info.ssl_cert_filepath = nullptr; info.ssl_private_key_filepath = nullptr; info.gid = -1; info.uid = -1; info.options = 0; info.user = nullptr; context = libwebsocket_create_context(&info); for(;;) { int n = libwebsocket_service(context,10); if(n >= 0 && wsinput->_isend) { break; } } libwebsocket_context_destroy(context); wsinput->_isend = false; }
FWebSocket::~FWebSocket() { RecievedCallBack.Unbind(); Flush(); #if !PLATFORM_HTML5 if ( !IsServerSide) { libwebsocket_context_destroy(Context); Context = NULL; delete Protocols; Protocols = NULL; } #endif #if PLATFORM_HTML5 close(SockFd); #endif }
int WebSocket::onSubThreadLoop() { if (_readyState == kStateClosed || _readyState == kStateClosing) { libwebsocket_context_destroy(_wsContext); // return 1 to exit the loop. return 1; } if (_wsContext && _readyState != kStateClosed && _readyState != kStateClosing) { libwebsocket_service(_wsContext, 0); } // Sleep 50 ms std::this_thread::sleep_for(std::chrono::milliseconds(50)); // return 0 to continue the loop. return 0; }
//-------------------------------------------------------------- void Client::close(){ // Self-initiated call to close() means we shouldn't try to reconnect bShouldReconnect = false; if (isThreadRunning()){ waitForThread(true); } else { return; } if ( context != NULL ){ //libwebsocket_close_and_free_session( context, lwsconnection, LWS_CLOSE_STATUS_NORMAL); closeAndFree = true; libwebsocket_context_destroy( context ); context = NULL; lwsconnection = NULL; } if ( connection != NULL){ //delete connection; connection = NULL; } }
int main(int argc, char **argv) { if (argc < 2) { cout << "Podaj adres serwera" << endl; return 1; } string server = string(argv[1]); if (!connect_to_server(server)) { cout << "Połączenie nie powiodło się." << endl; return 1; } if (argc < 3) { cout << "Podaj swój nick" << endl; return 1; } string nick = string(argv[2]); int n = 0; while (n >= 0 && !was_closed && !force_exit) { cout << "? "; string komunikat; cin >> komunikat; send_packet(nick + ": " + komunikat); n = service_websockets(); string s; while (receive_packet(s)) { cout << s << endl; } } cout << "Kończę." << endl; libwebsocket_context_destroy(context); return 0; }
int main(void) { // server url will be http://localhost:9000 int port = 9000; const char *interface = NULL; struct libwebsocket_context *context; // we're not using ssl const char *cert_path = NULL; const char *key_path = NULL; // no special options int opts = 0; // create libwebsocket context representing this server context = libwebsocket_create_context(port, interface, protocols, libwebsocket_internal_extensions, cert_path, key_path, -1, -1, opts, NULL); if (context == NULL) { fprintf(stderr, "libwebsocket init failed\n"); return -1; } printf("starting server...\n"); // infinite loop, to end this server send SIGTERM. (CTRL+C) while (1) { libwebsocket_service(context, 50); // libwebsocket_service will process all waiting events with their // callback functions and then wait 50 ms. // (this is a single threaded webserver and this will keep our server // from generating load while there are not requests to process) } libwebsocket_context_destroy(context); return 0; }
void ortc_free_context(ortc_context* context){ _ortc_quit_main_thread(context); pthread_mutex_destroy(&context->mutex_state); pthread_cond_destroy(&context->pcond); pthread_mutex_destroy(&context->mutex_msg); pthread_mutex_destroy(&context->mutex_cmd); if(context->host) free(context->host); if(context->server) free(context->server); _ortc_dlist_free(context->multiparts); _ortc_dlist_free(context->channels); _ortc_dlist_free(context->permissions); _ortc_dlist_free(context->messagesToSend); _ortc_dlist_free(context->ortcCommands); if(context->lws_context != NULL) libwebsocket_context_destroy(context->lws_context); context->lws_context = NULL; curl_global_cleanup(); free(context); }
int WebSocket::onSubThreadLoop() { if (_readyState == kStateClosed || _readyState == kStateClosing) { libwebsocket_context_destroy(_wsContext); // return 1 to exit the loop. return 1; } if (_wsContext && _readyState != kStateClosed && _readyState != kStateClosing) { libwebsocket_service(_wsContext, 0); } // Sleep 50 ms #ifdef WIN32 Sleep(50); #else usleep(50000); #endif // return 0 to continue the loop. return 0; }
int main(int argc, char **argv) { int n = 0; int ret = 0; // int port = 8543; int port = 9000; int use_ssl = 0; struct libwebsocket_context *context; // const char *address="192.168.6.176"; const char *address="192.168.6.114"; struct libwebsocket *wsi_dumb; int ietf_version = -1; /* latest */ struct lws_context_creation_info info; memset(&info, 0, sizeof info); fprintf(stderr, "DTS2B websockets client\n" "(C) Copyright 2014-2015 Mleaf_HEXI <*****@*****.**> " "licensed under LGPL2.1\n"); /* * create the websockets context. This tracks open connections and * knows how to route any traffic and which protocol version to use, * and if each connection is client or server side. * * For this client-only demo, we tell it to not listen on any port. */ info.port = CONTEXT_PORT_NO_LISTEN; info.protocols = protocols; #ifndef LWS_NO_EXTENSIONS info.extensions = libwebsocket_get_internal_extensions(); #endif info.gid = -1; info.uid = -1; context = libwebsocket_create_context(&info); if (context == NULL) { fprintf(stderr, "Creating libwebsocket context failed\n"); return 1; } /* create a client websocket using dumb increment protocol */ /* TITLE: libwebsocket_client_connect - Connect to another websocket server struct libwebsocket * libwebsocket_client_connect (struct libwebsocket_context * context, const char * address, int port, int ssl_connection, const char * path, const char * host, const char * origin, const char * protocol, int ietf_version_or_minus_one) Arguments context Websocket context address Remote server address, eg, "myserver.com" port Port to connect to on the remote server, eg, 80 ssl_connection 0 = ws://, 1 = wss:// encrypted, 2 = wss:// allow self signed certs path Websocket path on server host Hostname on server origin Socket origin name protocol Comma-separated list of protocols being asked for from the server, or just one. The server will pick the one it likes best. ietf_version_or_minus_one -1 to ask to connect using the default, latest protocol supported, or the specific protocol ordinal Description This function creates a connection to a remote server */ fprintf(stderr, "Connecting to %s:%u\n", address, port); wsi_dumb = libwebsocket_client_connect(context, address, port, use_ssl, "/websocket/uclient/MOCK_DTS2B_ACCESS_CODE_1234567890", address,"origin", protocols[0].name, ietf_version); if (wsi_dumb == NULL) { fprintf(stderr, "libwebsocket connect failed\n"); ret = 1; goto bail; } fprintf(stderr, "Waiting for connect...\n"); sqlite3_test();//sqlite3数据库测试 /* * sit there servicing the websocket context to handle incoming * packets, and drawing random circles on the mirror protocol websocket * nothing happens until the client websocket connection is * asynchronously established */ n = 0; while (n >= 0 && !was_closed && !force_exit) { n = libwebsocket_service(context, 10); if (n < 0) continue; if (wsi_mirror) continue; } bail: fprintf(stderr, "Exiting\n"); libwebsocket_context_destroy(context); return 1; }
int main(int argc, char **argv) { char cert_path[1024]; char key_path[1024]; int n = 0; int use_ssl = 0; int opts = 0; char interface_name[128] = ""; const char *iface = NULL; #ifndef WIN32 int syslog_options = LOG_PID | LOG_PERROR; #endif unsigned int oldus = 0; struct lws_context_creation_info info; int debug_level = 7; #ifndef LWS_NO_DAEMONIZE int daemonize = 0; #endif memset(&info, 0, sizeof info); info.port = 7681; while (n >= 0) { n = getopt_long(argc, argv, "eci:hsap:d:Dr:", options, NULL); if (n < 0) continue; switch (n) { case 'e': opts |= LWS_SERVER_OPTION_LIBEV; break; #ifndef LWS_NO_DAEMONIZE case 'D': daemonize = 1; #ifndef WIN32 syslog_options &= ~LOG_PERROR; #endif break; #endif case 'd': debug_level = atoi(optarg); break; case 's': use_ssl = 1; break; case 'a': opts |= LWS_SERVER_OPTION_ALLOW_NON_SSL_ON_SSL_PORT; break; case 'p': info.port = atoi(optarg); break; case 'i': strncpy(interface_name, optarg, sizeof interface_name); interface_name[(sizeof interface_name) - 1] = '\0'; iface = interface_name; break; case 'c': close_testing = 1; fprintf(stderr, " Close testing mode -- closes on " "client after 50 dumb increments" "and suppresses lws_mirror spam\n"); break; case 'r': resource_path = optarg; printf("Setting resource path to \"%s\"\n", resource_path); break; case 'h': fprintf(stderr, "Usage: test-server " "[--port=<p>] [--ssl] " "[-d <log bitfield>] " "[--resource_path <path>]\n"); exit(1); } } #if !defined(LWS_NO_DAEMONIZE) && !defined(WIN32) /* * normally lock path would be /var/lock/lwsts or similar, to * simplify getting started without having to take care about * permissions or running as root, set to /tmp/.lwsts-lock */ if (daemonize && lws_daemonize("/tmp/.lwsts-lock")) { fprintf(stderr, "Failed to daemonize\n"); return 1; } #endif signal(SIGINT, sighandler); #ifndef WIN32 /* we will only try to log things according to our debug_level */ setlogmask(LOG_UPTO (LOG_DEBUG)); openlog("lwsts", syslog_options, LOG_DAEMON); #endif /* tell the library what debug level to emit and to send it to syslog */ lws_set_log_level(debug_level, lwsl_emit_syslog); lwsl_notice("libwebsockets test server - " "(C) Copyright 2010-2013 Andy Green <*****@*****.**> - " "licensed under LGPL2.1\n"); #ifdef EXTERNAL_POLL max_poll_elements = getdtablesize(); pollfds = malloc(max_poll_elements * sizeof (struct pollfd)); fd_lookup = malloc(max_poll_elements * sizeof (int)); if (pollfds == NULL || fd_lookup == NULL) { lwsl_err("Out of memory pollfds=%d\n", max_poll_elements); return -1; } #endif info.iface = iface; info.protocols = protocols; #ifndef LWS_NO_EXTENSIONS info.extensions = libwebsocket_get_internal_extensions(); #endif if (!use_ssl) { info.ssl_cert_filepath = NULL; info.ssl_private_key_filepath = NULL; } else { if (strlen(resource_path) > sizeof(cert_path) - 32) { lwsl_err("resource path too long\n"); return -1; } sprintf(cert_path, "%s/libwebsockets-test-server.pem", resource_path); if (strlen(resource_path) > sizeof(key_path) - 32) { lwsl_err("resource path too long\n"); return -1; } sprintf(key_path, "%s/libwebsockets-test-server.key.pem", resource_path); info.ssl_cert_filepath = cert_path; info.ssl_private_key_filepath = key_path; } info.gid = -1; info.uid = -1; info.options = opts; context = libwebsocket_create_context(&info); if (context == NULL) { lwsl_err("libwebsocket init failed\n"); return -1; } n = 0; while (n >= 0 && !force_exit) { struct timeval tv; gettimeofday(&tv, NULL); /* * This provokes the LWS_CALLBACK_SERVER_WRITEABLE for every * live websocket connection using the DUMB_INCREMENT protocol, * as soon as it can take more packets (usually immediately) */ if (((unsigned int)tv.tv_usec - oldus) > 50000) { libwebsocket_callback_on_writable_all_protocol(&protocols[PROTOCOL_DUMB_INCREMENT]); oldus = tv.tv_usec; } #ifdef EXTERNAL_POLL /* * this represents an existing server's single poll action * which also includes libwebsocket sockets */ n = poll(pollfds, count_pollfds, 50); if (n < 0) continue; if (n) for (n = 0; n < count_pollfds; n++) if (pollfds[n].revents) /* * returns immediately if the fd does not * match anything under libwebsockets * control */ if (libwebsocket_service_fd(context, &pollfds[n]) < 0) goto done; #else /* * If libwebsockets sockets are all we care about, * you can use this api which takes care of the poll() * and looping through finding who needed service. * * If no socket needs service, it'll return anyway after * the number of ms in the second argument. */ n = libwebsocket_service(context, 50); #endif } #ifdef EXTERNAL_POLL done: #endif libwebsocket_context_destroy(context); lwsl_notice("libwebsockets-test-server exited cleanly\n"); #ifndef WIN32 closelog(); #endif return 0; }