Exemplo n.º 1
0
void Connection::processReceivedData()
{
	int bytesRead;

	while((bytesRead = socket->read(scrapBuffer, BUFFERSIZE)) > 0)
		receiveBuffer.append(scrapBuffer, bytesRead);
	
	if(bytesRead == -1)
		return;
	
	while(1)
	{
		Net::Packet packet;
	
		try
		{
			packet.readFromBuffer(receiveBuffer);
		}
		catch(Net::EndOfDataException)
		{
			break;		
		}
	}
	
	writeBufferedData();
}
Exemplo n.º 2
0
void Connection::processPackets()
{
	if(socket.state() != QTcpSocket::ConnectedState)
		return;

	int read;

	while((read = socket.read(scrapBuffer, BUFFERSIZE)) > 0)
		receiveBuffer.append(scrapBuffer, read);

	if(read == -1)
		close();

	while(1)
	{
		Net::Packet packet;

		try
		{
			packet.readFromBuffer(receiveBuffer);

			if(packet.getType() == Protocol::GAME_STATE)
			{
				emit receivingGameState();

				std::string s;

				packet >> s;

				std::stringstream ss(s);

				Json::Value v;

				ss >> v;

				std::cout << v;

				try
				{
					state.reset(new Client::State(v, objectFactory));

					emit ready(state.get());
				}
				catch(std::runtime_error& e)
				{
					emit error(e.what());
				}
			}
		}
		catch(Net::EndOfDataException)
		{
			/*
			We don't have enough data for a packet, so we return.
			*/
			return;
		}

	}
}