Use std::string for CommandContainer text instead of giant static buffer
Use move semantics for CommandContainer instance where feasible
This commit is contained in:
@@ -330,10 +330,26 @@ void Packet::Recv_string(char *buffer, size_t size, StringValidationSettings set
|
||||
str_validate(bufp, last, settings);
|
||||
}
|
||||
|
||||
/**
|
||||
* Reads a string till it finds a '\0' in the stream.
|
||||
* @param buffer The buffer to put the data into.
|
||||
* @param settings The string validation settings.
|
||||
*/
|
||||
void Packet::Recv_string(std::string &buffer, StringValidationSettings settings)
|
||||
{
|
||||
/* Don't allow reading from a closed socket */
|
||||
if (cs->HasClientQuit()) return;
|
||||
|
||||
size_t length = ttd_strnlen((const char *)(this->buffer + this->pos), this->size - this->pos - 1);
|
||||
buffer.assign((const char *)(this->buffer + this->pos), length);
|
||||
this->pos += length + 1;
|
||||
str_validate(const_cast<char *>(buffer.c_str()), buffer.c_str() + buffer.size(), settings);
|
||||
}
|
||||
|
||||
/**
|
||||
* Reads binary data.
|
||||
* @param buffer The buffer to put the data into.
|
||||
* @param size The size of the buffer.
|
||||
* @param size The size of the data.
|
||||
*/
|
||||
void Packet::Recv_binary(char *buffer, size_t size)
|
||||
{
|
||||
@@ -344,4 +360,18 @@ void Packet::Recv_binary(char *buffer, size_t size)
|
||||
this->pos += (PacketSize) size;
|
||||
}
|
||||
|
||||
/**
|
||||
* Reads binary data.
|
||||
* @param buffer The buffer to put the data into.
|
||||
* @param size The size of the data.
|
||||
*/
|
||||
void Packet::Recv_binary(std::string &buffer, size_t size)
|
||||
{
|
||||
/* Don't allow reading from a closed socket */
|
||||
if (cs->HasClientQuit()) return;
|
||||
|
||||
buffer.assign((const char *) &this->buffer[this->pos], size);
|
||||
this->pos += (PacketSize) size;
|
||||
}
|
||||
|
||||
#endif /* ENABLE_NETWORK */
|
||||
|
@@ -17,6 +17,7 @@
|
||||
#include "config.h"
|
||||
#include "core.h"
|
||||
#include "../../string_type.h"
|
||||
#include <string>
|
||||
|
||||
#ifdef ENABLE_NETWORK
|
||||
|
||||
@@ -88,7 +89,9 @@ public:
|
||||
uint32 Recv_uint32();
|
||||
uint64 Recv_uint64();
|
||||
void Recv_string(char *buffer, size_t size, StringValidationSettings settings = SVS_REPLACE_WITH_QUESTION_MARK);
|
||||
void Recv_string(std::string &buffer, StringValidationSettings settings = SVS_REPLACE_WITH_QUESTION_MARK);
|
||||
void Recv_binary(char *buffer, size_t size);
|
||||
void Recv_binary(std::string &buffer, size_t size);
|
||||
};
|
||||
|
||||
#endif /* ENABLE_NETWORK */
|
||||
|
@@ -18,6 +18,7 @@
|
||||
#include "tcp.h"
|
||||
#include "../network_type.h"
|
||||
#include "../../core/pool_type.hpp"
|
||||
#include <memory>
|
||||
|
||||
#ifdef ENABLE_NETWORK
|
||||
|
||||
@@ -135,13 +136,16 @@ class CommandQueue {
|
||||
CommandPacket *last; ///< The last packet in the queue; only valid when first != NULL.
|
||||
uint count; ///< The number of items in the queue.
|
||||
|
||||
void Append(CommandPacket *p, bool move);
|
||||
|
||||
public:
|
||||
/** Initialise the command queue. */
|
||||
CommandQueue() : first(NULL), last(NULL), count(0) {}
|
||||
/** Clear the command queue. */
|
||||
~CommandQueue() { this->Free(); }
|
||||
void Append(CommandPacket *p);
|
||||
CommandPacket *Pop(bool ignore_paused = false);
|
||||
void Append(CommandPacket &p) { this->Append(&p, false); }
|
||||
void Append(CommandPacket &&p) { this->Append(&p, true); }
|
||||
std::unique_ptr<CommandPacket> Pop(bool ignore_paused = false);
|
||||
CommandPacket *Peek(bool ignore_paused = false);
|
||||
void Free();
|
||||
/** Get the number of items in the queue. */
|
||||
|
Reference in New Issue
Block a user