コード例 #1
0
/* Receives from the network the packages and passes them to "parsePackets" for them to be handled */
void *ReceivingPackets(void *ptr1)
{
	char *op;
	int adl=sizeof(server),i,ready=0;
	char msg_recv[KAID_STRING_LENGTH];
	fd_set fds;//struct timeval tt;
	
	for(;;)
	{	
		FD_ZERO(&fds);FD_SET(sock,&fds);
		//tt.tv_sec=0;tt.tv_usec=50000;
		ready=select(sock+1,&fds,NULL,NULL,NULL);
		if(ready<0) 
		{
			error_nr=11;
			continue;
		}
		else if(ready && FD_ISSET(sock,&fds))
		{
			if(discovered==0) // only for discover
			{
				i=recvfrom(sock,msg_recv,sizeof(msg_recv)-1,0,(struct sockaddr *)&rcv,(socklen_t *)&adl);
				if(i>0)
					msg_recv[i]=0;
				#ifdef DEBUG_SOCK
				printf("ENGINE HERE EXPECTED: %s\n",msg_recv);
				#endif
				op=strtok(msg_recv,";");
				if(op==NULL)
					continue;
				if(parseopcode(op) == EV_ENGINE_HERE)
						GoDiscovered();
			}
			else // other one's (this has an aditional check if the message came from *our* attached engine
			{
			
				EnterCriticalSect(); // just to be sure there's no weird errors, although socks are full duplex...
				i=recvfrom(sock,msg_recv,sizeof(msg_recv)-1,0,(struct sockaddr *)&rcv,(socklen_t *)&adl);
				LeaveCriticalSect();
				// lets discharge the packet if it's not from "our" attached server.
				if( (rcv.sin_addr.s_addr != server.sin_addr.s_addr) && (rcv.sin_port != server.sin_port)) continue;
				if(i<=0) continue;
				else if(msg_recv[0]=='K') // someone could be sending some trash... with this little if we can "filter" only packets that start with K..(an additional check to not crash strtok() in some weird 'i-dont-know' way..
					{
						msg_recv[i]=0;
						#ifdef DEBUG_SOCK
							printf("Receiving(%s:%d):%s\n",inet_ntoa(rcv.sin_addr),ntohs(rcv.sin_port),msg_recv);fflush(stdout);
						#endif
						parsePackets(msg_recv);
					}
			}
		}
		pthread_testcancel();
	}
}
コード例 #2
0
ファイル: player.cpp プロジェクト: harkaranbrar/asteroids
Player::Player(QGraphicsItem *parent): QGraphicsPixmapItem(parent)
{

            bulletsound = new QMediaPlayer(); // create a media player
            bulletsound->setMedia(QUrl("qrc:/sounds/SHOOTIN3.wav")); // set sound from resource file


    setPixmap(QPixmap(":/img/Ship.png")); // set player image

    QTimer * timer = new QTimer();

        QTimer * netTimer =  new QTimer();
        writeUdpSocket = new QUdpSocket(this);
        readUdpSocket = new QUdpSocket(this);
        readUdpSocket->bind(45454, QUdpSocket::ShareAddress);
        connect(timer,SIGNAL(timeout()), this, SLOT(updateNetwork()));
        connect(readUdpSocket, SIGNAL(readyRead()), this, SLOT(parsePackets()));
        netTimer->start(50.00);

        connect(timer,SIGNAL(timeout()),this, SLOT (move()));
        timer->start(33.33);

}
コード例 #3
0
ファイル: ltwindow.cpp プロジェクト: HxCory/f1lt
LTWindow::LTWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::LTWindow), eventData(EventData::getInstance())
{    
    ui->setupUi(this);    

    currDriver = -1;

    streamReader = new DataStreamReader(this);
    prefs = new PreferencesDialog(this);
    settings = new QSettings(F1LTCore::iniFile(), QSettings::IniFormat, this);
    loginDialog = new LoginDialog(this);
    ltFilesManagerDialog = new LTFilesManagerDialog(this);
    trackRecordsDialog = new TrackRecordsDialog(this);
    saw = new SessionAnalysisWidget();
    stw = new SessionTimesWidget();
    driverTrackerWidget = new DriverTrackerWidget();
    aboutDialog = new AboutDialog(this);
    updatesCheckerDialog = new UpdatesCheckerDialog(this);

//    ui->trackStatusWidget->setupItems();

    connect(streamReader, SIGNAL(tryAuthorize()), this, SLOT(tryAuthorize()));
    connect(streamReader, SIGNAL(authorized(QString)), this, SLOT(authorized(QString)));
    connect(streamReader, SIGNAL(eventDataChanged(const DataUpdates&)), this, SLOT(eventDataChanged(const DataUpdates&)));
    connect(streamReader, SIGNAL(driverDataChanged(int, const DataUpdates&)), this, SLOT(driverDataChanged(int, const DataUpdates&)));
    connect(streamReader, SIGNAL(dataChanged(const DataUpdates&)), this, SLOT(dataChanged(const DataUpdates&)));
    connect(streamReader, SIGNAL(sessionStarted()), this, SLOT(sessionStarted()));
    connect(streamReader, SIGNAL(authorizationError()), this, SLOT(authorizationError()));
    connect(streamReader, SIGNAL(error(QAbstractSocket::SocketError)), this, SLOT(error(QAbstractSocket::SocketError)));
    connect(streamReader, SIGNAL(error(QNetworkReply::NetworkError)), this, SLOT(error(QNetworkReply::NetworkError)));
    connect(streamReader, SIGNAL(noLiveSession(bool, QString)), this, SLOT(showNoSessionBoard(bool, QString)));

    connect(updatesCheckerDialog, SIGNAL(newVersionAvailable()), this, SLOT(onNewVersionAvailable()));

    sessionTimer = new SessionTimer(this);
    connect(sessionTimer, SIGNAL(updateWeather()), this, SLOT(updateWeather()));

    connect(&SeasonData::getInstance(), SIGNAL(seasonDataChanged()), &ImagesFactory::getInstance(), SLOT(reloadGraphics()));
    connect(&SeasonData::getInstance(), SIGNAL(seasonDataChanged()), &ColorsManager::getInstance(), SLOT(calculateDefaultDriverColors()));
    connect(&SeasonData::getInstance(), SIGNAL(seasonDataChanged()), saw, SLOT(setupColors()));


    connect(prefs, SIGNAL(driversColorsChanged()), saw, SLOT(setupColors()));

    eventRecorder = new EventRecorder(sessionTimer, this);
    eventPlayer = new EventPlayer(this);

    delayWidget = new DelayWidget(this);
    connect(delayWidget, SIGNAL(delayChanged(int, int)), streamReader, SLOT(setDelay(int, int)));
    connect(delayWidget, SIGNAL(delayChanged(int, int)), sessionTimer, SLOT(setDelay(int, int)));
    connect(sessionTimer, SIGNAL(synchronizingTimer(bool)), delayWidget, SLOT(synchronizingTimer(bool)));
    connect(sessionTimer, SIGNAL(synchronizingTimer(bool)), driverTrackerWidget, SLOT(pauseTimer(bool)));

    connect(ui->messageBoardWidget, SIGNAL(connectClicked()), this, SLOT(on_actionConnect_triggered()));
    connect(ui->messageBoardWidget, SIGNAL(playClicked()), this, SLOT(on_actionOpen_triggered()));
    connect(ui->messageBoardWidget, SIGNAL(loadClicked()), this, SLOT(on_actionLT_files_data_base_triggered()));

    loadSettings();
    ColorsManager::getInstance().calculateDefaultDriverColors();
    saw->setupColors();

    delayWidgetAction = ui->mainToolBar->addWidget(delayWidget);
    delayWidgetAction->setVisible(true);

    eventPlayerAction = ui->mainToolBar->addWidget(eventPlayer);
    eventPlayerAction->setVisible(false);
    recording = false;
    playing = false;

    connectionProgress = new QProgressDialog(this);

    connect(sessionTimer, SIGNAL(timeout()), this, SLOT(timeout()));
    connect(eventRecorder, SIGNAL(recordingStopped()), this, SLOT(autoStopRecording()));
    connect(eventPlayer, SIGNAL(playClicked(int)), this, SLOT(eventPlayerPlayClicked(int)));
    connect(eventPlayer, SIGNAL(pauseClicked()), this, SLOT(eventPlayerPauseClicked()));
    connect(eventPlayer, SIGNAL(rewindToStartClicked()), this, SLOT(eventPlayerRewindToStartClicked()));
    connect(eventPlayer, SIGNAL(forwardToEndClicked()), this, SLOT(eventPlayerForwardToEndClicked()));
    connect(eventPlayer, SIGNAL(rewindClicked()), this, SLOT(eventPlayerRewindClicked()));
    connect(eventPlayer, SIGNAL(stopClicked()), this, SLOT(eventPlayerStopClicked()));
    connect(eventPlayer, SIGNAL(nextPackets(QVector<Packet>)), streamReader, SLOT(parsePackets(QVector<Packet>)));

    connect(ui->ltWidget, SIGNAL(driverSelected(int)), ui->driverDataWidget, SLOT(printDriverData(int)));
    connect(ui->ltWidget, SIGNAL(driverDoubleClicked(int)), this, SLOT(ltWidgetDriverSelected(int)));


    ui->messageBoardWidget->setVisible(false);

    QStringList args = qApp->arguments();
    if (args.size() > 1)
    {
        if (eventPlayer->loadFromFile(args.at(1)) == false)
        {
            QMessageBox::critical(this, "Error opening file!", "Could not open specified file, or the file is corrupted.");
            connectToServer();
            return;
        }
        setWindowTitle("FILT - " + args.at(1));
        ui->actionRecord->setVisible(false);
        ui->actionStop_recording->setVisible(false);
        eventPlayerAction->setVisible(true);
        delayWidgetAction->setVisible(false);

        playing = true;

        eventPlayer->startPlaying();
    }
    else
    {
    	if (settings->value("ui/auto_connect").toBool())
    		connectToServer();
    	else
    	{
    		ui->messageBoardWidget->showStartupBoard();
    		showSessionBoard(true);
    	}
    }    

}
コード例 #4
0
/* Receives from the network the packages and passes them to "parsePackets" for them to be handled */
DWORD WINAPI ReceivingPackets(LPVOID lpParam)
{
	char *op;
	int adl=sizeof(server),i;
	char msg_recv[KAID_STRING_LENGTH];
	struct sockaddr_in rcv;
	//fd_set fds;struct timeval tt;
	DWORD dataAvailable;
	unsigned long nonblocking=1;

	memset(&msg_recv, 0, sizeof(msg_recv));
	//tt.tv_sec=0;tt.tv_usec=500000;

	ioctlsocket(sock, FIONBIO, &nonblocking);
	for(;;)
	{
		//FD_ZERO(&fds);FD_SET(sock,&fds);
		// TODO: i need to put here the code to kill this thread...
		dataAvailable = 0;
		EnterCriticalSection(&critical_section_network);
		ioctlsocket(sock, FIONREAD, &dataAvailable);
		LeaveCriticalSection(&critical_section_network);

		while (dataAvailable>0)
		{
			EnterCriticalSection(&critical_section_network);
			i=recvfrom(sock,(CHAR*)msg_recv,sizeof(msg_recv)-1,0,(struct sockaddr *)&rcv,&adl);
			LeaveCriticalSection(&critical_section_network);

			if(discovered==0)
			{
				if(i>0)
						msg_recv[i]=0;
					#ifdef DEBUG_SOCK
					Log("ENGINE HERE EXPECTED: %s\n",msg_recv);
					#endif
					op=strtok(msg_recv,";");
					if(op==NULL)
						continue;
					if(parseopcode(op) == EV_ENGINE_HERE)
							GoDiscovered();			
			}
			else
			{
				if( (rcv.sin_addr.s_addr != server.sin_addr.s_addr) && (rcv.sin_port != server.sin_port)) continue;
				if(i<=0) continue;
				else if(msg_recv[0]=='K') // someone could be sending some trash... with this little if we can "filter" only packets that start with K..(an additional check to not crash strtok() in some weird 'i-dont-know' way..
					{
						msg_recv[i]=0;
						#ifdef _DEBUG
						Log("Receiving:%s\n",msg_recv);
						#endif
						parsePackets(msg_recv);
					}
			}
			dataAvailable = 0;
			EnterCriticalSection(&critical_section_network);
			ioctlsocket(sock, FIONREAD, &dataAvailable);
			LeaveCriticalSection(&critical_section_network);
		}
		//pthread_testcancel();
	}
}