/******************************************************************************
 * This function waits for commands over UART
 *****************************************************************************/
void commandLoop(void)
{
  uint8_t command = 0;
  
  sendWelcomeMessage();
  
  while(1) {
    
    /* Wait for command */
    command = BLUART_receive();
    
    switch(command)
    {
    case 'h':
      sendWelcomeMessage();
      break;
    case 'u':
      enterDownloadMode();
      break;
    case 'v':
      BLUART_sendString("Verifying firmware...");
      if ( verifyActiveFirmware() ) {
        BLUART_sendString("OK\r\n");
      } else {
        BLUART_sendString("INVALID\r\n");
      }
      break;
    case 'l':
      BLUART_sendString("Locking debug access...\r\n");
      enableDebugLock();
      break;
    case 'r':
      BLUART_sendString("Reset\r\n");
      NVIC_SystemReset();
      break;
    }
  }
}
void Communicator::handleClientConnection(int socket)
{
	mSocket = socket;

	int keep_alive = 1;
	struct timeval timeout;
	timeout.tv_sec = 10;
	timeout.tv_usec = 0;

	if (setsockopt (mSocket, SOL_SOCKET, SO_RCVTIMEO, (char *)&timeout, sizeof(timeout)) < 0)
	        syslog(LOG_ERR, "RCVTIMEO setsockopt failed");

	if (setsockopt (mSocket, SOL_SOCKET, SO_SNDTIMEO, (char *)&timeout, sizeof(timeout)) < 0)
	        syslog(LOG_ERR, "SNDTIMEO setsockopt failed");
//	if (setsockopt (mSocket, SOL_SOCKET, TCP_USER_TIMEOUT, &keep_alive, sizeof(int)) < 0 )
//		 syslog(LOG_ERR, "KEEPALIVE setsockopt failed");
	if (setsockopt(mSocket, SOL_SOCKET, SO_KEEPALIVE, &keep_alive, sizeof(int)) < 0)
		syslog(LOG_ERR, "KEEPALIVE setsockopt failed");

	// send welcome message
	sendWelcomeMessage();

	while(true) {
		if (!readFromClient()) {
			syslog(LOG_ERR, "Stoping client");
			break;
		}
	}

	sleep(2);

	closeUsbDevice();

	close(mSocket);

}
Exemple #3
0
int Server::launchServer()
{
	sf::SocketSelector selector;
	GridManager* m_gridManager = new GridManager();
	InputManager* m_inputManager = new InputManager(m_gridManager);

	m_gridManager->resetGrid(playerGrid, player2Grid, getWidth(), getHeight());
	
	sf::TcpListener listener;
	if (listener.listen(SERVER_PORT) != sf::Socket::Done)
	{
		// throw error
		return -1;
	}

	selector.add(listener);

	// accept connections
	while (true)
	{
		if (selector.wait(sf::milliseconds(500)))
		{
			if (selector.isReady(listener))
			{
				sf::TcpSocket* client = new sf::TcpSocket;
				if (listener.accept(*client) != sf::Socket::Done)
				{
					// throw error
					sf::Socket::Error;
					delete client;
					return -2;
				}
				else
				{
					connectedClients++;
					std::cout << connectedClients;
					sendWelcomeMessage(client);
					sockets.push_back(client);
					selector.add(*client);
				}
			}
			else
			{
				std::vector<sf::TcpSocket*> dead;
				for (auto* client : sockets)
				{
					if (selector.isReady(*client))
					{
						sf::Packet packet;
						if (client->receive(packet) == sf::Socket::Disconnected)
						{
							// stop monitoring
							connectedClients--;
							selector.remove(*client);
							
							// disconnect and record dead client
							client->disconnect();
							dead.push_back(client);

							std::cout << "Client disconnected." << std::endl;
						}
						else
						{
							std::string s;
							packet >> s;
							std::cout << s << std::endl;	
						}

						for (auto* rec : sockets)
						{
							if (rec == client) continue;
							rec->send(packet);
						}

						break;
					}
				}

				for (auto it = dead.begin(); it != dead.end(); ++it)
				{
					sockets.erase(std::find(sockets.begin(), sockets.end(), *it));
					delete *it;
					*it = nullptr;
				}
			}
		}
		else
		{