diff --git a/src/mangosd/RASocket.cpp b/src/mangosd/RASocket.cpp index 9ae12189131..2aee7b5acf9 100644 --- a/src/mangosd/RASocket.cpp +++ b/src/mangosd/RASocket.cpp @@ -40,8 +40,6 @@ RASocket::RASocket(boost::asio::io_service &service, std::function buffer(ReadLengthRemaining()); + std::string buffer; + buffer.resize(ReadLengthRemaining()); Read(&buffer[0], buffer.size()); - bool completeCommand = false; - size_t newLine; - for (newLine = 0; newLine < buffer.size(); ++newLine) + static const std::string NEWLINE = "\n\r"; + + auto pos = 0; + + while (pos != std::string::npos) { - if (buffer[newLine] == '\r' || buffer[newLine] == '\n') - { - if (newLine > 0) - std::copy(buffer.cbegin(), buffer.cbegin() + newLine - 1, std::back_inserter(m_commandBuffer)); + auto newLine = buffer.find_first_of(NEWLINE, pos); + + m_input += buffer.substr(pos, newLine - pos); + + pos = buffer.find_first_not_of(NEWLINE, newLine); - completeCommand = true; + if (newLine == std::string::npos) break; - } - } - // no newline found? save what we have and return - if (newLine == buffer.size()) - { - std::copy(buffer.cbegin(), buffer.cend(), std::back_inserter(m_commandBuffer)); - return true; + if (!HandleInput()) + return false; } + return true; +} + +bool RASocket::HandleInput() +{ auto const minLevel = static_cast(sConfig.GetIntDefault("RA.MinLevel", AccountTypes::SEC_ADMINISTRATOR)); switch (m_authLevel) @@ -109,15 +111,13 @@ bool RASocket::ProcessIncomingData() /// }; - m_commandBuffer.clear(); - - // there might be additional data here, skip any newline characters first - for (; newLine < buffer.size(); ++newLine) - { - if (buffer[newLine] != '\r' && buffer[newLine] != '\n') - break; - } - - if (newLine < buffer.size()) - std::copy(buffer.cbegin() + newLine, buffer.cend(), std::back_inserter(m_commandBuffer)); + m_input.clear(); return true; } diff --git a/src/mangosd/RASocket.h b/src/mangosd/RASocket.h index 81988f91ed0..0208b3362ae 100644 --- a/src/mangosd/RASocket.h +++ b/src/mangosd/RASocket.h @@ -42,18 +42,17 @@ class RASocket : public MaNGOS::Socket Authenticated }; - static const int InitialBufferSize = 64; - const bool m_secure; bool m_restricted; - std::vector m_commandBuffer; + std::string m_input; AuthLevel m_authLevel; AccountTypes m_accountLevel; uint32 m_accountId; virtual bool ProcessIncomingData() override; + bool HandleInput(); void Send(const std::string &message); public: