示例#1
0
void MainWindow::onServerReadMessage()
{
    QTextCodec *codec = QTextCodec::codecForName("UTF-8");
    QTextStream os(m_pTcpSocket);
    QString message = codec->toUnicode(m_pTcpSocket->readAll());
    RecieveMessage(message);
}
void CNetManager::Update()
{
	// If client is not yet made or has been deleted because of a DC, 
	// we make a new one, ready to receive a connection
	if(!_client)
	{
		_client = new CClientSocket();
	}

	// If hosting, we check for connecting client(s), accepting if any.
	if(_isHost)
	{
		if(_host->Accept(*_client))
		{   /* Sending info about our local characters to the client */
			SendInitialLocalCharactersInfo();
		}
	}
	
	// If there is any activity in a connected client, we receive the message.
	if(_client->SockRdyToBeRead())
	{
		RecieveMessage();
	}

	UpdateAllRemoteCharacters();
	SendLocalCharacterUpdates();

	// If we have any new messages to send, we send them
	if(_newMsgToSend)
	{
		if(Connected())
		{
			_client->Send(*_outMsg);
			_newMsgToSend = false;
		}
		else{log << WARN << "Trying to send a msg without being connected to a client" << endl; _newMsgToSend = false;}
	}
}
示例#3
0
文件: client.c 项目: krjohnn/tron
int main(int argc, char *argv[])
{
	int counter=0;
	FILE *fd = NULL;fopen("./client.out", "w+");
	if ( fd != NULL )
		setOutputType(fd);
	else
		setOutputType(stderr);

	
	setLogLevel(LOG_LEVEL_DEBUG);

	uint8_t keep_alive = 1;
	uint8_t msg_type = 0, timeout = 0;
	int sockfd = 0, n = 0,c=0,xadd=0,yadd=1,BCD=0,running=1, startx=30,starty=30;
	char recvBuff[1024];
	uint8_t gameStarted = 0,fakeResponded=0;
	struct EventPlayer event;
	sleep_time.tv_sec  = 0;
    	sleep_time.tv_nsec = 450000000;

	memset(recvBuff, '0',sizeof(recvBuff));


	if(argc != 3)
	{
		printf("\n Usage: %s <ip of server> <port of server>\n",argv[0]);
		return 1;
	}

	init_game();
	
	keep_alive = ConnectToServer(argv[1], argv[2], &sockfd);

	while (keep_alive)
	{
		
		void *buf = NULL;
		buf = RecieveMessage(sockfd, &msg_type, &timeout);
		
		if ( timeout == 5 ) {
			keep_alive = 0;
			ERROR("Connection to server timed out\n");
		} else {
			if(buf){
				switch(msg_type) {
					case 2:
						/* Player just connected to server;
							Message contains servers rules; */
						NOTICE("Connected to server, please stand by\n");
						CreateClientWorld(&game, buf);
						break;
					case 3:
						/* Update message received; calculate wait for button pressed */
						DEBUG("Update information recieved\n" /*TODO add data here for debuging */);
						updateClientWorld(&game,buf);
						gameStarted = 1;
						break;
					default:
						keep_alive = 0;
						DEBUG("Trashed unhandeled message type:%d\n", msg_type);
						break;
				}
				free(buf);
			}

			if (gameStarted)
			{
				
				//init_game();
				c = wgetch(key_detecter);
				
				if ( c != ERR ) {
					NOTICE(" WE PRESSED: %c\n",c);
					ClientMove(c, &game);//TODO pass c to world-done
				}		
				event.direction = (getSelf(&game))->direction;
				if ( c == ' ')
					event.shot = 1;
				else
					event.shot = 0;
				SendMessage(sockfd, &event, sizeof(event), PCKT_EVENT);
			 	DEBUG("Sending update event { %d, %d } to server \n", event.direction, event.shot );

	    	}
			drawWorld(&game);	
			getchar();		
			refresh();			
		}
	}
	NOTICE("Disconnected from server\n");
	if (fd != NULL)
		fclose(fd);
	//terminate_game();
	return 0;
}