コード例 #1
0
ファイル: connection.cpp プロジェクト: nitrotrigger/kp2
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();
}
コード例 #2
0
ファイル: connection.cpp プロジェクト: ruisleipa/kp2
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;
		}

	}
}
コード例 #3
0
ファイル: Socket.cpp プロジェクト: spiricn/Wt
void Socket::sendPacket(const net::Packet& packet){
	uint32_t payloadSize = packet.getPayload().getCapacity();

	// send payload size
	sendBfr(&payloadSize, sizeof(uint32_t));

	// send the actual data
	sendBfr(packet.getPayload().getData(), packet.getPayload().getCapacity());
}
コード例 #4
0
ファイル: Socket.cpp プロジェクト: spiricn/Wt
void Socket::recvPacket(net::Packet& packet){
	uint32_t payloadSize = 0;

	// recieve payload size
	recvBfr(&payloadSize, sizeof(uint32_t));

	// initialize payload memory
	packet.clear();
	packet.getPayload().grow(payloadSize);

	recvBfr(packet.getPayload().getData(), packet.getPayload().getCapacity());
}
コード例 #5
0
ファイル: playerproxy.cpp プロジェクト: ruisleipa/kp2
void PlayerProxy::makeCall(const std::string& method, const Json::Value& arguments)
{
	Json::Value call;

	call["objectType"] = "player";
	call["objectIndex"] = getIndex();
	call["method"] = method;
	call["arguments"] = arguments;

	Net::Packet packet;
	packet.setType(Protocol::REMOTE_INVOCATION);

	Json::FastWriter fw;

	packet << fw.write(call);

	connection.writeToServer(packet);

	std::cout << call;
}
コード例 #6
0
void		NetworkModule::sendUDPPacket(Net::Packet &packet,
				std::list<Client*> const &list,
				bool needId, Client *player)
{
	for (std::list<Client*>::const_iterator it = list.begin();
		 it != list.end(); it++)
	{
		if (player == *it || (*it)->getUDPAddr().getSize() == 0)
			continue ;
		if (needId)
		{
			uint32_t	id;
			packet.wr_ptr(9);
			id = (*it)->getPacketId();
			packet << id;
			(*it)->addPacket(id, packet);
		}
		packet.setDestination((*it)->getUDPAddr());
		this->_udp.handleOutputPacket(packet);
	}
}
コード例 #7
0
ファイル: UdpHandler.cpp プロジェクト: Koukan/rtype
int			UdpHandler::spawn(Net::Packet &packet, uint64_t)
{
	uint32_t	id_packet;

	if (packet.size() != 29)
		return 0;
	packet >> id_packet;
	this->testPacketId(id_packet);
	GameCommand *gc = new GameCommand("spawn");
	packet >> gc->idResource;
	packet >> gc->idObject;
	packet >> gc->x;
	packet >> gc->y;
	packet >> gc->vx;
	packet >> gc->vy;
	/*if (_latency > 50)
	{		
		gc->x += (static_cast<double>(_latency) / 1000) * gc->vx;
		gc->y += (static_cast<double>(_latency) / 1000) * gc->vy;
	}*/
	//std::cout << "spawn de type " << gc->idResource << " x:" << gc->x << " y:" << gc->y << " vx:" << gc->vx << " vy:" << gc->vy << std::endl;
	CommandDispatcher::get().pushCommand(*gc);
	return 1;
}
コード例 #8
0
ファイル: UdpHandler.cpp プロジェクト: Koukan/rtype
int			UdpHandler::handleInputPacket(Net::Packet &packet)
{
	static int			(UdpHandler::* const methods[])(Net::Packet&, uint64_t) = {
			&UdpHandler::spawn,
			&UdpHandler::destroy,
			&UdpHandler::move,
			NULL,
			NULL,
			&UdpHandler::retrieve,
			&UdpHandler::ping,
			&UdpHandler::pong
	};
	uint64_t			time;
	uint8_t				type;

	if (packet.size() < 9)
		return 0;
	packet >> time;
	packet >> type;

	if (type < sizeof(methods) / sizeof(*methods) && methods[type] != NULL)
		return (this->*methods[type])(packet, time);
	return 0;
}
コード例 #9
0
ファイル: connection.cpp プロジェクト: ruisleipa/kp2
//TODO: implement correct sending (remote call api)
void Connection::writeToServer(const Net::Packet& packet)
{
	std::string str = packet.getString();

	socket.write(str.c_str(), str.size());
}
コード例 #10
0
ファイル: connection.cpp プロジェクト: nitrotrigger/kp2
void Connection::writePacket(const Net::Packet& packet)
{
	std::string str = packet.getString();

	sendBuffer.append(str.c_str(), str.size());
}