int time_test() { int passed = 1; int32_t then, now, diff; then = platform_get_ticks(); platform_sleep(5); now = platform_get_ticks(); diff = now - then; if(diff < 4 || diff > 20) passed = 0; then = platform_get_ticks(); diff = then - now; if(diff < 0 || diff > 5) passed = 0; platform_sleep(50); now = platform_get_ticks(); diff = now - then; if(diff < 40 || diff > 70) passed = 0; return passed; }
int http_thread_func(void * data) { while (http_running) { request_mutex.lock(); if (!has_request) { request_mutex.unlock(); // XXX should use condition variable platform_sleep(0.25); continue; } HTTPRequest request = http_request; has_request = false; thread_id = current_id; request_mutex.unlock(); std::cout << "Got request" << std::endl; conn.close(); conn.set_host(request.host, 80); conn.setcallbacks(NULL, on_data, on_done, NULL); conn.request("POST", request.path.c_str(), http_headers, (const unsigned char*)&request.args[0], request.args.size()); while (conn.outstanding()) conn.pump(); } has_http_thread = false; return 0; }
static void evrythng_task(void* pvParameters) { cmd_opts* opts = (cmd_opts*)pvParameters; EvrythngInitHandle(&opts->evt_handle); EvrythngSetLogCallback(opts->evt_handle, log_callback); EvrythngSetConnectionCallbacks(opts->evt_handle, conlost_callback, conrestored_callback); EvrythngSetUrl(opts->evt_handle, opts->url); EvrythngSetKey(opts->evt_handle, opts->key); EvrythngSetThreadPriority(opts->evt_handle, 5); log("Connecting to %s", opts->url); while(EvrythngConnect(opts->evt_handle) != EVRYTHNG_SUCCESS) { log("Retrying"); platform_sleep(2000); } log("Evrythng client Connected"); if (opts->sub) { log("Subscribing to property %s", opts->prop); EvrythngSubThngProperty(opts->evt_handle, opts->thng, opts->prop, 1, print_property_callback); while (!stop) { platform_sleep(2000); } } else { while (!stop) { int value = rand() % 100; char msg[128]; sprintf(msg, "[{\"value\": %d}]", value); log("Publishing value %d to property %s", value, opts->prop); EvrythngPubThngProperty(opts->evt_handle, opts->thng, opts->prop, msg); platform_sleep(1000); } } EvrythngDisconnect(opts->evt_handle); EvrythngDestroyHandle(opts->evt_handle); vTaskEndScheduler(); }
int ConnectMain( int argc, char * argv[] ) { (void)argc; (void)argv; Allocator & allocator = GetDefaultAllocator(); uint64_t clientId = 0; RandomBytes( (uint8_t*) &clientId, 8 ); printf( "\nclient id is %.16" PRIx64 "\n", clientId ); Matcher matcher( allocator ); if ( !matcher.Initialize() ) { printf( "error: failed to initialize matcher\n" ); return 1; } matcher.RequestMatch( ProtocolId, clientId ); if ( matcher.GetStatus() == MATCHER_FAILED ) { printf( "\nerror: request match failed. is the matcher running?\n\n" ); return 1; } printf( "requesting match from https://localhost:8080\n" ); MatchResponse matchResponse; matcher.GetMatchResponse( matchResponse ); if ( matchResponse.numServerAddresses == 0 ) { printf( "error: could not find a match\n" ); return 1; } printf( "received match response\n" ); ClientServerPacketFactory packetFactory; GameNetworkTransport clientTransport( packetFactory ); if ( clientTransport.GetError() != SOCKET_ERROR_NONE ) { printf( "error: failed to initialize client socket\n" ); return 1; } printf( "client started on port %d\n", clientTransport.GetAddress().GetPort() ); GameClient client( GetDefaultAllocator(), clientTransport ); client.Connect( matchResponse.serverAddresses[0], matchResponse.connectTokenData, matchResponse.connectTokenNonce, matchResponse.clientToServerKey, matchResponse.serverToClientKey ); double time = 0.0; const double deltaTime = 0.1; signal( SIGINT, interrupt_handler ); while ( !quit ) { client.SendPackets(); clientTransport.WritePackets(); clientTransport.ReadPackets(); client.ReceivePackets(); client.CheckForTimeOut(); if ( client.IsDisconnected() ) break; time += deltaTime; client.AdvanceTime( time ); clientTransport.AdvanceTime( time ); if ( client.ConnectionFailed() ) break; platform_sleep( deltaTime ); } if ( quit ) printf( "\nclient stopped\n" ); printf( "\n" ); if ( client.IsConnected() ) client.Disconnect(); return 0; }
int ProfileMain() { srand( (unsigned int) time( NULL ) ); GamePacketFactory packetFactory; GameMessageFactory messageFactory; ServerData * serverData = new ServerData(); serverData->address = Address( "::1", ServerPort ); ClientData * clientData = new ClientData[MaxClients]; LocalMatcher matcher; for ( int i = 0; i < MaxClients; ++i ) { clientData[i].address = Address( "::1", ClientPort + i ); clientData[i].clientId = 1 + i; if ( !matcher.RequestMatch( clientData[i].clientId, clientData[i].connectTokenData, clientData[i].connectTokenNonce, clientData[i].clientToServerKey, clientData[i].serverToClientKey, clientData[i].numServerAddresses, clientData[i].serverAddresses ) ) { printf( "error: client %d request match failed\n", i ); delete serverData; delete [] clientData; return 1; } } serverData->transport = new SocketTransport( GetDefaultAllocator(), packetFactory, serverData->address, ProtocolId ); for ( int i = 0; i < MaxClients; ++i ) clientData[i].transport = new SocketTransport( GetDefaultAllocator(), packetFactory, clientData[i].address, ProtocolId ); serverData->server = new GameServer( GetDefaultAllocator(), *serverData->transport, messageFactory ); for ( int i = 0; i < MaxClients; ++i ) clientData[i].client = new GameClient( GetDefaultAllocator(), *clientData[i].transport, messageFactory ); serverData->server->SetServerAddress( serverData->address ); serverData->server->Start(); for ( int i = 0; i < MaxClients; ++i ) { clientData[i].client->Connect( serverData->address, clientData[i].connectTokenData, clientData[i].connectTokenNonce, clientData[i].clientToServerKey, clientData[i].serverToClientKey ); } signal( SIGINT, interrupt_handler ); double time = 0.0; while ( !quit ) { serverData->server->SendPackets(); for ( int i = 0; i < MaxClients; ++i ) clientData[i].client->SendPackets(); serverData->transport->WritePackets(); for ( int i = 0; i < MaxClients; ++i ) clientData[i].transport->WritePackets(); serverData->transport->ReadPackets(); for ( int i = 0; i < MaxClients; ++i ) clientData[i].transport->ReadPackets(); serverData->server->ReceivePackets(); for ( int i = 0; i < MaxClients; ++i ) clientData[i].client->ReceivePackets(); for ( int i = 0; i < MaxClients; ++i ) { if ( clientData[i].client->ConnectionFailed() ) { printf( "\nerror: client %d connect failed!", i ); quit = true; break; } } time += 0.1; for ( int i = 0; i < MaxClients; ++i ) { const int messagesToSend = random_int( 0, 64 ); for ( int j = 0; j < messagesToSend; ++j ) { if ( !clientData[i].client->CanSendMessage() ) break; Message * message = CreateRandomMessage( messageFactory, clientData[i].numMessagesSent ); if ( message ) { clientData[i].client->SendMessage( message ); clientData[i].numMessagesSent++; } } } for ( int i = 0; i < MaxClients; ++i ) { const int messagesToSend = random_int( 0, 64 ); for ( int j = 0; j < messagesToSend; ++j ) { if ( !serverData->server->CanSendMessage( i ) ) break; Message * message = CreateRandomMessage( messageFactory, serverData->numMessagesSent[i] ); if ( message ) { serverData->server->SendMessage( i, message ); serverData->numMessagesSent[i]++; } } } for ( int i = 0; i < MaxClients; ++i ) { while ( true ) { Message * message = serverData->server->ReceiveMessage( i ); if ( !message ) break; serverData->numMessagesReceived[i]++; // printf( "server received message %d from client %d\n", message->GetId(), i ); messageFactory.Release( message ); } } for ( int i = 0; i < MaxClients; ++i ) { while ( true ) { Message * message = clientData[i].client->ReceiveMessage(); if ( !message ) break; clientData[i].numMessagesReceived++; // printf( "client received message %d from client %d\n", message->GetId(), i ); messageFactory.Release( message ); } } serverData->server->CheckForTimeOut(); for ( int i = 0; i < MaxClients; ++i ) clientData[i].client->CheckForTimeOut(); serverData->server->AdvanceTime( time ); for ( int i = 0; i < MaxClients; ++i ) clientData[i].client->AdvanceTime( time ); serverData->transport->AdvanceTime( time ); for ( int i = 0; i < MaxClients; ++i ) clientData[i].transport->AdvanceTime( time ); platform_sleep( 0.1 ); } if ( quit ) { printf( "\n\nstopped\n" ); } delete [] clientData; delete serverData; return 0; }