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(); }
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; } } }
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()); }
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()); }
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; }
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); } }
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; }
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; }
//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()); }
void Connection::writePacket(const Net::Packet& packet) { std::string str = packet.getString(); sendBuffer.append(str.c_str(), str.size()); }