Fixed requests.
This commit is contained in:
@@ -6,6 +6,8 @@
|
|||||||
#include <boost/asio.hpp>
|
#include <boost/asio.hpp>
|
||||||
#include "Network/NetworkClient.h"
|
#include "Network/NetworkClient.h"
|
||||||
|
|
||||||
|
typedef std::map<unsigned int, std::vector<std::pair<int, boost::shared_ptr<char>>>> PacketMap;
|
||||||
|
|
||||||
class UDPClient : public NetworkClient
|
class UDPClient : public NetworkClient
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
@@ -31,7 +33,7 @@ private:
|
|||||||
void readPartOfPacket();
|
void readPartOfPacket();
|
||||||
PacketID m_SendPacketID = 0;
|
PacketID m_SendPacketID = 0;
|
||||||
//map:(packetGroup, vector:(pair:(groupIndex, packetData)))
|
//map:(packetGroup, vector:(pair:(groupIndex, packetData)))
|
||||||
std::map<unsigned int, std::vector<std::pair<int, boost::shared_ptr<char>>>> m_PacketSegmentMap;
|
PacketMap m_PacketSegmentMap;
|
||||||
bool hasReceivedPacket(int packetGroup, int groupIndex);
|
bool hasReceivedPacket(int packetGroup, int groupIndex);
|
||||||
// 2^19
|
// 2^19
|
||||||
const int m_SizeOfSocketBuffer = 524288;
|
const int m_SizeOfSocketBuffer = 524288;
|
||||||
|
|||||||
@@ -100,12 +100,9 @@ void UDPClient::readPartOfPacket()
|
|||||||
if (sizeOfPacket == 0) {
|
if (sizeOfPacket == 0) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
int packetGroup = 0;
|
int packetGroup = *reinterpret_cast<int*>(m_ReadBuffer + sizeof(int));
|
||||||
memcpy(&packetGroup, m_ReadBuffer + sizeof(int), sizeof(int));
|
int packetGroupIndex = *reinterpret_cast<int*>(m_ReadBuffer + 2 * sizeof(int));
|
||||||
int packetGroupIndex = 0;
|
int packetGroupSize = *reinterpret_cast<int*>(m_ReadBuffer + 3 * sizeof(int));
|
||||||
memcpy(&packetGroupIndex, m_ReadBuffer + 2 * sizeof(int), sizeof(int));
|
|
||||||
int packetGroupSize = 0;
|
|
||||||
memcpy(&packetGroupSize, m_ReadBuffer + 3 * sizeof(int), sizeof(int));
|
|
||||||
//LOG_INFO("Packet group: %i. Group index: %i. Group size: %i. Packet size: %i.", packetGroup, packetGroupIndex, packetGroupSize, sizeOfPacket);
|
//LOG_INFO("Packet group: %i. Group index: %i. Group size: %i. Packet size: %i.", packetGroup, packetGroupIndex, packetGroupSize, sizeOfPacket);
|
||||||
if (sizeOfPacket > m_Socket->available()) {
|
if (sizeOfPacket > m_Socket->available()) {
|
||||||
LOG_WARNING("UDPClient::readBuffer(): We haven't got the whole packet yet.");
|
LOG_WARNING("UDPClient::readBuffer(): We haven't got the whole packet yet.");
|
||||||
@@ -127,10 +124,11 @@ void UDPClient::readPartOfPacket()
|
|||||||
if (hasReceivedPacket(packetGroup, packetGroupIndex)) {
|
if (hasReceivedPacket(packetGroup, packetGroupIndex)) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
//std::map<unsigned int, std::vector<std::pair<unsigned int, boost::shared_ptr<char>>>> packetSegmentMap;
|
|
||||||
// If group exists
|
// If group exists
|
||||||
if (m_PacketSegmentMap.find(packetGroup) != m_PacketSegmentMap.end()) {
|
PacketMap::iterator it;
|
||||||
m_PacketSegmentMap.at(packetGroup).push_back(std::make_pair(packetGroupIndex, std::move(packetData)));
|
it = m_PacketSegmentMap.find(packetGroup);
|
||||||
|
if (it != m_PacketSegmentMap.end()) {
|
||||||
|
it->second.push_back(std::make_pair(packetGroupIndex, std::move(packetData)));
|
||||||
} else { // Create group and add element
|
} else { // Create group and add element
|
||||||
m_PacketSegmentMap[packetGroup].push_back(std::make_pair(packetGroupIndex, std::move(packetData)));
|
m_PacketSegmentMap[packetGroup].push_back(std::make_pair(packetGroupIndex, std::move(packetData)));
|
||||||
}
|
}
|
||||||
@@ -139,8 +137,10 @@ void UDPClient::readPartOfPacket()
|
|||||||
|
|
||||||
bool UDPClient::hasReceivedPacket(int packetGroup, int groupIndex)
|
bool UDPClient::hasReceivedPacket(int packetGroup, int groupIndex)
|
||||||
{
|
{
|
||||||
if (m_PacketSegmentMap.find(packetGroup) != m_PacketSegmentMap.end()) {
|
PacketMap::iterator it;
|
||||||
const std::vector<std::pair<int, boost::shared_ptr<char>>>& loopPacketGroup = m_PacketSegmentMap.at(packetGroup);
|
it = m_PacketSegmentMap.find(packetGroup);
|
||||||
|
if (it != m_PacketSegmentMap.end()) {
|
||||||
|
const std::vector<std::pair<int, boost::shared_ptr<char>>>& loopPacketGroup = it->second;
|
||||||
for (size_t i = 0; i < loopPacketGroup.size(); i++) {
|
for (size_t i = 0; i < loopPacketGroup.size(); i++) {
|
||||||
if (loopPacketGroup.at(i).first == groupIndex) {
|
if (loopPacketGroup.at(i).first == groupIndex) {
|
||||||
return true;
|
return true;
|
||||||
@@ -184,7 +184,7 @@ bool UDPClient::GetNextPacket(Packet & packet)
|
|||||||
// A duplicate packet should not be present in the vector!
|
// A duplicate packet should not be present in the vector!
|
||||||
// Soo we will assume that this is true and only look if size
|
// Soo we will assume that this is true and only look if size
|
||||||
// of vector is correct.
|
// of vector is correct.
|
||||||
std::map<unsigned int, std::vector<std::pair<int, boost::shared_ptr<char>>>>::iterator it = m_PacketSegmentMap.begin();
|
PacketMap::iterator it = m_PacketSegmentMap.begin();
|
||||||
while (it != m_PacketSegmentMap.end()) {
|
while (it != m_PacketSegmentMap.end()) {
|
||||||
// pair(Group index, packetData)
|
// pair(Group index, packetData)
|
||||||
std::vector<std::pair<int, boost::shared_ptr<char>>>& currentVector = it->second;
|
std::vector<std::pair<int, boost::shared_ptr<char>>>& currentVector = it->second;
|
||||||
@@ -209,9 +209,9 @@ bool UDPClient::GetNextPacket(Packet & packet)
|
|||||||
packet.ReconstructFromData(currentVector.at(0).second.get(), packet.HeaderSize());
|
packet.ReconstructFromData(currentVector.at(0).second.get(), packet.HeaderSize());
|
||||||
// Add the rest of the packets.
|
// Add the rest of the packets.
|
||||||
int sizeOfData = 0;
|
int sizeOfData = 0;
|
||||||
for (size_t i = 0; i < currentVector.size(); i++) {
|
for (auto& packetSegment : currentVector) {
|
||||||
memcpy(&sizeOfData, currentVector.at(i).second.get(), sizeof(int));
|
memcpy(&sizeOfData, packetSegment.second.get(), sizeof(int));
|
||||||
packet.WriteData(currentVector.at(i).second.get() + packet.HeaderSize(), sizeOfData - packet.HeaderSize());
|
packet.WriteData(packetSegment.second.get() + packet.HeaderSize(), sizeOfData - packet.HeaderSize());
|
||||||
}
|
}
|
||||||
if (headerInfoPacket.GetMessageType() == MessageType::Snapshot) {
|
if (headerInfoPacket.GetMessageType() == MessageType::Snapshot) {
|
||||||
m_LastReceivedSnapshotGroup = packet.Group();
|
m_LastReceivedSnapshotGroup = packet.Group();
|
||||||
|
|||||||
@@ -17,9 +17,6 @@ void UDPServer::Send(Packet& packet, PlayerDefinition& playerDefinition)
|
|||||||
{
|
{
|
||||||
packet.UpdateSize();
|
packet.UpdateSize();
|
||||||
try {
|
try {
|
||||||
//Debug
|
|
||||||
int debugTheSixeOfpacket = packet.Size();
|
|
||||||
//Debug end
|
|
||||||
// Remove header from packet.
|
// Remove header from packet.
|
||||||
packet.ReadData(packet.HeaderSize());
|
packet.ReadData(packet.HeaderSize());
|
||||||
int totalBytesSent = 0;
|
int totalBytesSent = 0;
|
||||||
@@ -61,14 +58,7 @@ void UDPServer::Send(Packet& packet, PlayerDefinition& playerDefinition)
|
|||||||
|
|
||||||
void UDPServer::SendToConnectedPlayers(Packet& packet, std::map<PlayerID, PlayerDefinition>& playersTosendTo)
|
void UDPServer::SendToConnectedPlayers(Packet& packet, std::map<PlayerID, PlayerDefinition>& playersTosendTo)
|
||||||
{
|
{
|
||||||
// Remove a player if hen crashes.
|
|
||||||
// Return a vector with disconnected players.
|
|
||||||
// Work in progress
|
|
||||||
packet.UpdateSize();
|
packet.UpdateSize();
|
||||||
|
|
||||||
//Debug
|
|
||||||
int debugTheSixeOfpacket = packet.Size();
|
|
||||||
//Debug end
|
|
||||||
// Remove header from packet.
|
// Remove header from packet.
|
||||||
packet.ReadData(packet.HeaderSize());
|
packet.ReadData(packet.HeaderSize());
|
||||||
int totalBytesSent = 0;
|
int totalBytesSent = 0;
|
||||||
|
|||||||
Reference in New Issue
Block a user