Ejemplo n.º 1
0
void CWOP::main()
{
   char buf[255];
   while( threadRunning )
   {
      pthread_mutex_lock( &notify );
      if ( !threadRunning )
      {
         dbg.printf(NOTICE, "Exiting\n");
         return;
      }

      if ( !wd->now->isNewMinute() || wd->now->getMinute() % 10 != cfg.getInteger("cwop_ul_min"))
      {
         dbg.printf(DEBUG, "Not on minute bounary\n");
         continue;
      }

      if ( cfg.getInteger("test_only") != 1 )
      {
         createPacket();
         int randSleepTime = random() % 10;
         dbg.printf(DEBUG, "Sleeping %d seconds before sending data\n", randSleepTime );
         sleep( randSleepTime );
         char serverName[64];
         if ( !connectToServer( serverName, 64) )
         {
            dbg.printf(NOTICE, "Could not connect to any CWOP server\n");
            continue;
         }

         dbg.printf(DEBUG, "sending data to CWOP (%s)\n", serverName);
         if ( !receiveFromServer( buf, 20, 255 ) )
         {
            dbg.printf(ERR, "Could not read from server\n");
            close(sock);
            continue;
         }
         // Check if this is a real APRS server
         for ( int i = 0; i < 20; i++ )
         {
            if ( isalpha(buf[i]) )
               buf[i] = tolower( buf[i] );
         }

         if ( strstr( buf, "aprs" ) == 0 )
         {
            dbg.printf(ERR, "Expected '*aprs*', received: %s\n", buf );
            close( sock );
            continue;
         }

         if ( sendToServer( login ) != -1 )
         {
            if (!receiveFromServer( buf, 25, 255 ))
            {
               dbg.printf(ERR, "Could not read from server\n");
               close(sock);
               continue;
            }

            sendToServer( packet );
         }
         close( sock );
      }
      else
      {
         dbg.printf(NOTICE, "pretending: CWOP data uploading to %s\n", cfg.getString("cwop_server").c_str());
      }
   }
}
Ejemplo n.º 2
0
int jouer(Character character[2]){
		Client client;
		int *info = malloc(BUFF_SIZE_RECV * sizeof(int));

		int map[NB_BLOCS_LARGEUR][NB_BLOCS_HAUTEUR] = {0};
		int continuer,i,j, verif;

		char choix = '\0';
		int goodchoice;

		connexionToServer(&client);
		system("clear");
		printf("\t\tWaiting for another client\t\t\n");


		continuer = 1;
		goodchoice = 1;
		while(continuer){
				verif = 1;

				if(goodchoice == 1){
					printf("Waiting for new map.\n");
					if((info = receiveFromServer(client)) == -1) {
						printf("Connexion Lost\n");
						deconnexionFromServer(&client);
						continuer = 0;
						return 0;
					}
					if(info[0] == 30 && info[1] == 35) {
						if(info[3] == 0) {
							deconnexionFromServer(&client);
							continuer = 0;
							return 1;
						}
						else if(info[3] == 1) {
							deconnexionFromServer(&client);
							continuer = 0;
							return 0;
						}
					}
					else {
						for(i = 0; i < 300; i++) {
							if(!(info[i] < 9 || info[i] == 18)) {
								printf("Invalid data received Map\n");
								verif = 0;
							}
						}
						if(info[300] > 10 || info[301] > 10 || info[302] > 10 ||
						info[305] > 10 || info[306] > 10 || info[307] > 10 ||
					  info[303] > 19 || info[308] > 19 || info[304] > 14 || info[309] > 14 ||
				    info[303] < 0 || info[308] < 0 || info[304] < 0 || info[309] < 0) {
							printf("Invalid data received players\n");
							verif = 0;
						}

						if(verif == 0) {
							printf("Game finished because of trickery\n");
							deconnexionFromServer(&client);
							continuer = 0;
							return 0;
						}
						else {
							printf("Map info received, updating\n");
							convertInfo(info, character, map);
							display(map, character);
							printf("Map info updated\n");
						}
					}
				}


				goodchoice = 1;
				printf("Rentrez une touche (Z Q S D, A pour les bombes, P pour quitter)\n");
				scanf("%c", &choix);
				switch(choix){
						case 'z':
						case 'Z':
								sendToServer(client, HAUT);
								printf("Action sent to server.\n");
								break;
						case 'S':
						case 's':
								sendToServer(client, BAS);
								printf("Action sent to server.\n");
								break;
						case 'Q':
						case 'q':
								sendToServer(client, GAUCHE);
								printf("Action sent to server.\n");
								break;
						case 'D':
						case 'd':
								sendToServer(client, DROITE);
								printf("Action sent to server.\n");
								break;
						case 'A':
						case 'a':
								sendToServer(client, BOMBE);
								printf("Action sent to server.\n");
								break;
						case 'P':
						case 'p':
							sendToServer(client, EXIT);
							continuer = 0;
							deconnexionFromServer(&client);
							return 0;
							break;
						default:
								printf("ERROR : BAD INPUT\n");
								goodchoice = 0;
								break;
				}
			}
	deconnexionFromServer(&client);
	return 0;
}
Ejemplo n.º 3
0
/*The main loop - receives messages from the server or input the user. */
void twitClient::sendReceive()
{
    fd_set readfds,master;
    int bytesReceive,retVal = SUCCESS_EXIT, maxfd = (_socketfd > STDIN)?_socketfd:STDIN;
    char buf[PACKAGE_SIZE];
    std::vector<std::string> clientsNames;
    FD_ZERO(&readfds);
    FD_ZERO(&master);
    FD_SET(_socketfd,&master);
    FD_SET(STDIN,&master);

    if (_send == SUCCESS) // sending BEGIN successfully.
    {
		while(true)
		{
			readfds = master;
			select(maxfd + 1, &readfds,NULL, NULL, NULL);
			for (int i = 0; (i <= maxfd) && (_finish == false); i++)
			{
				if (FD_ISSET(i, &readfds))
				{
					if (i == _socketfd)
					{
						resetMessage(buf);
						bytesReceive = recv(i, buf, sizeof(buf), 0);
						if (bytesReceive <= 0)
						{
							if (bytesReceive == 0)
							{
								std::cout << SERVER_CRASHED << std::endl;
							}
							if (bytesReceive < 0)
							{
								std::cout << RECEIVE_ERROR << std::endl;
								retVal = FAILURE_EXIT;
							}
							_finish = true;
							close(i);
							FD_CLR(i,&master);
						}
						else if ((retVal = receiveFromServer(buf,clientsNames)) != NOT_EXIT)
						{
							_finish = true;
							close(i);
							FD_CLR(i,&master);
						}
					}
					else if (i == STDIN)
					{
						if ((getCommands() == true) || (_send == FAILURE))
						{
							_finish = true;
							close(_socketfd);
							FD_CLR(i,&master);
						}
					}
				}
			}

			if (_finish == true)
			{
				break;
			}
		}
    }
    if (retVal == FAILURE_EXIT)
    {
    	exit(FAILURE_EXIT);
    }
    if (_send == FAILURE)
    {
        exit(FAILURE_EXIT);
    }
}
Ejemplo n.º 4
0
void		Network::run()
{
  receiveFromServer();
}