Пример #1
0
/*
* Run server loop
*/
void TrafficServer::run()
{
  std::cout << "TrafficServer: Started" << std::endl;

  while (sockfd) 
  {
      newsockfd = accept(sockfd, (struct sockaddr *) &cli_addr, &clilen);
      
      if (newsockfd != -1)
      {
        // Create comm thread
        std::thread commThread (&TrafficServer::communicate, &(*this), newsockfd );
        commThread.detach();
      } else {
        std::cout << "TrafficServer: Stopped" << std::endl;
        break;
      }
  }
  
}
Пример #2
0
int main()
{
	
// 	struct sigaction sigIntHandler;
// 	
// 	sigIntHandler.sa_handler = my_handler;
// 	sigemptyset(&sigIntHandler.sa_mask);
// 	sigIntHandler.sa_flags = 0;
// 	sigaction(SIGINT, &sigIntHandler, NULL);
// 	
// 	
	if( treadmill.initializeModbus() < 0) { exit(1); }
// 	
	std::vector<int> patchesVec = dataGen.getPatches();
	std::vector<int> patchTypesVec = dataGen.getPatchTypes();
	
	
	//Uncomment following line for automatic joint angle generation
//	dataGen.startAngleGen();

// 	while(true)
// 	{
// 		for(int iter = 0; iter< 6; iter++)
// 			std::cout << sharedMemory.sdata->joint_angles_rift[iter] << " ";
// 		std::cout << std::endl;
// 		sock.loadDubArrayToBuf( arrayToVec(sharedMemory.sdata->joint_angles_rift) );
// 		sock.loadDubArrayToBuf(dataGen.getAngles());
// 		printCharVec(sock.getBuf());	
// 	}
	
	std::cout << "Waiting for Unity to start..." << std::endl;
	std::vector<char> response = sock.recvData();
	
	printCharVec(response);
	std::cout << "Received." << std::endl;

	
	sock.loadIntArrayToBuf(patchesVec);
	printCharVec(sock.getBuf());
	sock.sendBuf();
	
	response = sock.recvData();
	printCharVec(response);
	
	
	sock.loadIntArrayToBuf(patchTypesVec);
	printCharVec(sock.getBuf());
	sock.sendBuf();
	
	
	boost::thread commThread(startComm);
	boost::thread pastValsThread(dataSaver);
	std::cout << "Running..." << std::endl;
	
	//int currentPatchType = dataGen.getPatchTypes()[currentPatch];
	
	
	while( unityRunning )
	{
		
		nextPatchType = patchTypesVec[currentPatch];
		std::cout << "next patch type: " << nextPatchType << ", next patch number: " << currentPatch << std::endl;
		
		
		
		switch (nextPatchType)
		{
		case 1:
			pertCycler(2e4, 1);
			currentPatch++;
			break;
		case 2:
			pertCycler(1e6, 2);
			currentPatch++;
			break;
		case 3:
			pertCycler(2e4, 3);
			currentPatch++;
			break;
		}
		
		while(distance < 0.8) {if(unityRunning == false) break; }
		
	}
	
	commThread.join();
	pastValsThread.join();
	
	sharedMemory.sdata->beep = false;
	
	return 0;
}
Пример #3
0
int main(int argc, char* args[])
{
	int iResult;
	iResult = connection.initConnection();
	if (iResult != 0)
	{
		printf("Failed to initialize server connection!\n");
		//Wait for 4s
		SDL_Delay(4000);
	}

	if (!initializeSDL())
	{
		printf("Failed to initialize! Exiting program.\n");
		//Wait for 4s
		SDL_Delay(4000);
	}
	else
	{
		window = createWindow("NettiiSaatana", 800, 600);
		renderer = createRenderer(window);

		if (window == nullptr || renderer == nullptr)
		{
			printf("Failed to create graphics context! Exiting program.\n");
			//Wait for 4s
			SDL_Delay(4000);
		}
		else
		{
			//Program running bool
			bool quitProgram = false;

			//Event handler
			SDL_Event sdlEvent;

			uint8_t* keyState = (uint8_t*)SDL_GetKeyboardState(NULL);
			KEYS_INFO keysInfo;
			//uint8_t* mouseButtonState = (uint8_t*)SDL_GetMouseState(NULL);

			int mouseX = 0, mouseY = 0;
			bool leftClick = false, bulletSpwnd = false;
			float difX, difY, magnitude;
			const float PI = 3.14159265;
			float velocityX, velocityY;


			SDL_SetRenderDrawColor(renderer, 0x00, 0x00, 0x00, 0x00);

			playerTex->loadImage(renderer, "groundtx.png");
			addPlayer(0);

			bulletTex->loadImage(renderer, "bullet.png");
			initBulletList(100);

			std::thread commThread(handleComms);
			commThread.detach();

			while (!quitProgram)
			{
				if (players.size() > playerNumber)
				{
					while (SDL_PollEvent(&sdlEvent) != 0)
					{
						if (sdlEvent.type == SDL_QUIT)
							quitProgram = true;

						if (sdlEvent.type == SDL_KEYDOWN || sdlEvent.type == SDL_KEYUP)
							keyState = (uint8_t*)SDL_GetKeyboardState(NULL);

						if (sdlEvent.type == SDL_MOUSEMOTION)
							SDL_GetMouseState(&mouseX, &mouseY);

						if (sdlEvent.type == SDL_MOUSEBUTTONDOWN)
						{
							if (SDL_GetMouseState(NULL, NULL) & SDL_BUTTON(SDL_BUTTON_LEFT))
								leftClick = true;
						}
						if (sdlEvent.type == SDL_MOUSEBUTTONUP)
						{
							if (!(SDL_GetMouseState(NULL, NULL) & SDL_BUTTON(SDL_BUTTON_LEFT)))
								leftClick = false;
							bulletSpwnd = false;
						}

					}

					keysInfo.w = 0;
					keysInfo.a = 0;
					keysInfo.s = 0;
					keysInfo.d = 0;

					if (keyState[SDL_SCANCODE_ESCAPE])
						quitProgram = true;

					if (keyState[SDL_SCANCODE_W])
						keysInfo.w = 1;
					if (keyState[SDL_SCANCODE_A])
						keysInfo.a = 1;
					if (keyState[SDL_SCANCODE_S])
						keysInfo.s = 1;
					if (keyState[SDL_SCANCODE_D])
						keysInfo.d = 1;


					float angle = std::atan2(((double)mouseY - (players[playerNumber]->getPosition().y + players[playerNumber]->getOrigin().y)), ((double)mouseX - (players[playerNumber]->getPosition().x + players[playerNumber]->getOrigin().x)));
					angle *= (180 / PI);
					if (angle < 0)
					{
						angle += 360;
					}
					else if (angle > 360)
					{
						angle -= 360;
					}

					iResult = connection.sendKeyStates(keysInfo, angle);
					if (iResult != 0)
					{
						printf("Failed to send key states to server!\n");
					}


					if (leftClick == true && bulletSpwnd == false)
					{
						bulletSpwnd = true;

						iResult = connection.sendShootCmd();
						if (iResult != 0)
						{
							printf("Failed to send shoot command to server!\n");
						}
					}

					//Clear screen
					SDL_RenderClear(renderer);

					//Render players to screen
					for (std::map<int, Sprite*>::iterator it = players.begin(); it != players.end(); it++)
					{
						(*it).second->draw(renderer);
					}

					//Render bullets to screen
					for (std::map<int, Bullet*>::iterator it = bullets.begin(); it != bullets.end(); it++)
					{
						if ((*it).second->isOn())
						{
							(*it).second->draw(renderer);
						}
					}

					//Update screen
					SDL_RenderPresent(renderer);
				}
			}
		}
	}
		

		//Destroy window
		SDL_DestroyWindow(window);

		//Quit SDL subsystems
		SDL_Quit();

		return 0;
}