Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
rtri
committed
Jul 2, 2020
1 parent
bd446e1
commit 699c8a2
Showing
4 changed files
with
75 additions
and
58 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,50 +1,60 @@ | ||
/* This file is part of the Spring engine (GPL v2 or later), see LICENSE.html */ | ||
|
||
|
||
#include "ChatMessage.h" | ||
|
||
#include "Net/Protocol/BaseNetProtocol.h" | ||
#include "System/Net/PackPacket.h" | ||
#include "System/Net/UnpackPacket.h" | ||
#include <cinttypes> | ||
|
||
using namespace netcode; | ||
|
||
ChatMessage::ChatMessage(int from, int dest, const std::string& chat) | ||
: fromPlayer(from) | ||
, destination(dest) | ||
, msg(chat) | ||
{ | ||
// restrict all (player and autohost) chat-messages before they get Pack()'ed | ||
msg.resize(std::min(chat.size(), MAX_MSG_SIZE)); | ||
|
||
const auto beg = chat.begin(); | ||
const auto end = chat.begin() + msg.size(); | ||
|
||
std::fill(msg.begin(), msg.end(), 0); | ||
std::copy(beg, end, msg.begin()); | ||
} | ||
|
||
ChatMessage::ChatMessage(std::shared_ptr<const netcode::RawPacket> data) | ||
{ | ||
assert(data->data[0] == NETMSG_CHAT); | ||
UnpackPacket packet(data, 2); | ||
unsigned char from; | ||
unsigned char dest; | ||
assert(data->length <= (4 * sizeof(uint8_t) + MAX_MESSAGE_SIZE + 1)); | ||
|
||
netcode::UnpackPacket packet(data, 2); | ||
|
||
uint8_t from; | ||
uint8_t dest; | ||
|
||
packet >> from; | ||
packet >> dest; | ||
packet >> msg; | ||
|
||
fromPlayer = from; | ||
destination = dest; | ||
} | ||
|
||
const netcode::RawPacket* ChatMessage::Pack() const | ||
{ | ||
unsigned size = (4 * sizeof(unsigned char)) + (msg.size() + 1); | ||
std::uint8_t csize = (size > UINT8_MAX) ? UINT8_MAX : size; | ||
|
||
PackPacket* buffer = new PackPacket(size, NETMSG_CHAT); | ||
*buffer << csize; | ||
*buffer << (unsigned char)fromPlayer; | ||
*buffer << (unsigned char)destination; | ||
if (size > UINT16_MAX) { | ||
std::string msg_(msg); | ||
msg_.resize(size - csize); | ||
*buffer << msg_; | ||
} else { | ||
*buffer << msg; | ||
} | ||
// message id (uint8), size (uint8), from (uint8), dest (uint8), len(msg)+null | ||
// msg.size() itself is clamped to MAX_MSG_SIZE == UINT8_MAX/2 by construction | ||
constexpr uint8_t headerSize = 4 * sizeof(uint8_t); | ||
const uint8_t messageSize = static_cast<uint8_t>(msg.size() + 1); | ||
const uint8_t packetSize = headerSize + messageSize; | ||
|
||
assert(packetSize <= (headerSize + MAX_MESSAGE_SIZE + 1)); | ||
|
||
netcode::PackPacket* buffer = new netcode::PackPacket(packetSize, NETMSG_CHAT); | ||
|
||
*buffer << packetSize; | ||
*buffer << static_cast<uint8_t>(fromPlayer); | ||
*buffer << static_cast<uint8_t>(destination); | ||
*buffer << msg; | ||
|
||
return buffer; | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters