void updatePacketList( const eConnectionMode::type mode, ClientPacketHandler::tPacketList &packets, PartialPacket &temp, const core::memory::ConstBlob &data) { if (mode == eConnectionMode::UDP_ASYNC || mode == eConnectionMode::UDP_BLOCKING) { // Handle UDP as each send must equate to a packet Packet packet; if (data.size() > (sizeof(Packet::Header) + PACKET_MAX_SIZE_UDP)) { return; } memcpy(&packet, data.data(), data.size()); if (packet.getHeader().m_size != data.size() - sizeof(Packet::Header)) { return; } packets.push_back(packet); } else { u32 remaining = data.size(); while (remaining > 0) { if (temp.addData( remaining, core::memory::ConstBlob(data.data() + (data.size() - remaining), remaining), mode)) { if (temp.valid()) { packets.push_back(temp.get()); } else { temp.clear(); } } } } }
u32 HttpRequest::parsePartial(const core::memory::ConstBlob &data) { u32 consumed = 0; consumed += m_header.parsePartial(data); if (m_header.done() && consumed < data.size()) { std::copy(data.data() + consumed, data.data() + data.size(), std::back_inserter(m_body)); return data.size(); } return consumed; };
bool Packet::addPayload(const core::memory::ConstBlob &newData) { const size_t newSize = getHeader().m_size + newData.size(); if (newSize + sizeof(Packet::Header) > PACKET_MAX_SIZE_TCP) { return false; } core::memory::Blob payload = getMutablePayload(newSize); memcpy(payload.data() + getHeader().m_size, newData.data(), newData.size()); getMutableHeader().m_size = newSize; return true; }
u32 HttpHeader::parsePartial(const core::memory::ConstBlob &data) { ASSERT(!done()); u32 pos = 0; m_header.reserve(MAX_HEADER_SIZE); while (!done() && pos < data.size()) { m_header.push_back(*(data.data() + pos)); pos++; } return pos; }
bool PartialPacket::addData(u32 &remaining, const core::memory::ConstBlob &data, const eConnectionMode::type mode) { remaining = data.size(); u32 processed = 0; if (m_done) { return true; } if (m_size < sizeof(Packet::Header)) { const u32 consumed = std::min(remaining, sizeof(Packet::Header) - m_size); std::memcpy(reinterpret_cast<u8 *>(&m_partial.getMutableHeader()) + m_size, data.data() + processed, consumed); m_size += consumed; processed += consumed; remaining -= consumed; if (m_size < sizeof(Packet::Header)) { return false; } } ASSERT(m_size >= sizeof(Packet::Header)); const u32 completedPayload = m_size - sizeof(Packet::Header); const u32 maxPayload = (mode == eConnectionMode::TCP_ASYNC || mode == eConnectionMode::TCP_BLOCKING) ? PACKET_MAX_SIZE_TCP : PACKET_MAX_SIZE_UDP; if (m_partial.getHeader().m_size > maxPayload) { m_done = true; return true; } u32 payloadRemaining = m_partial.getHeader().m_size - completedPayload; if (payloadRemaining > 0) { const u32 consumed = std::min(remaining, payloadRemaining); core::memory::Blob payload = m_partial.getMutablePayload(m_partial.getHeader().m_size); std::memcpy(payload.data() + m_size - sizeof(Packet::Header), data.data() + processed, consumed); m_size += consumed; processed += consumed; remaining -= consumed; payloadRemaining -= consumed; } m_done = payloadRemaining == 0; m_valid = m_done; return m_done; }
virtual u32 write(const core::memory::ConstBlob &blob) { TEST(testing::assertTrue(m_pos + blob.size() < ARRAY_LENGTH(m_buffer))); std::copy(blob.data(), blob.data() + blob.size(), m_buffer + m_pos); m_pos += blob.size(); return blob.size(); }