rewrite and reoganize tools functions

* create stdext namespace which contains additional C++ algorithms
* organize stdext in string, math, cast and exception utilities
This commit is contained in:
Eduardo Bart
2012-05-28 10:06:26 -03:00
parent 2676eb4da3
commit 4c80d783d6
75 changed files with 856 additions and 652 deletions

View File

@@ -54,14 +54,14 @@ void Connection::terminate()
g_ioService.stop();
}
void Connection::connect(const std::string& host, uint16 port, const SimpleCallback& connectCallback)
void Connection::connect(const std::string& host, uint16 port, const std::function<void()>& connectCallback)
{
m_connected = false;
m_connecting = true;
m_error.clear();
m_connectCallback = connectCallback;
asio::ip::tcp::resolver::query query(host, Fw::unsafeCast<std::string>(port));
asio::ip::tcp::resolver::query query(host, stdext::unsafe_cast<std::string>(port));
auto weakSelf = ConnectionWeakPtr(shared_from_this());
m_resolver.async_resolve(query, [=](const boost::system::error_code& error, asio::ip::tcp::resolver::iterator endpointIterator) {

View File

@@ -48,7 +48,7 @@ public:
static void poll();
static void terminate();
void connect(const std::string& host, uint16 port, const SimpleCallback& connectCallback);
void connect(const std::string& host, uint16 port, const std::function<void()>& connectCallback);
void close();
void write(uint8* buffer, uint16 size);
@@ -68,7 +68,7 @@ protected:
void onTimeout(const boost::system::error_code& error);
void handleError(const boost::system::error_code& error);
SimpleCallback m_connectCallback;
std::function<void()> m_connectCallback;
ErrorCallback m_errorCallback;
RecvCallback m_recvCallback;

View File

@@ -54,7 +54,7 @@ uint8 InputMessage::getU8()
uint16 InputMessage::getU16()
{
checkRead(2);
uint16 v = Fw::readLE16(m_buffer + m_readPos);
uint16 v = stdext::readLE16(m_buffer + m_readPos);
m_readPos += 2;
return v;
}
@@ -62,7 +62,7 @@ uint16 InputMessage::getU16()
uint32 InputMessage::getU32()
{
checkRead(4);
uint32 v = Fw::readLE32(m_buffer + m_readPos);
uint32 v = stdext::readLE32(m_buffer + m_readPos);
m_readPos += 4;
return v;
}
@@ -70,7 +70,7 @@ uint32 InputMessage::getU32()
uint64 InputMessage::getU64()
{
checkRead(8);
uint64 v = Fw::readLE64(m_buffer + m_readPos);
uint64 v = stdext::readLE64(m_buffer + m_readPos);
m_readPos += 8;
return v;
}
@@ -104,7 +104,7 @@ void InputMessage::setHeaderSize(uint16 size)
bool InputMessage::readChecksum()
{
uint32_t receivedCheck = getU32();
uint32 checksum = Fw::getAdlerChecksum(m_buffer + m_readPos, getUnreadSize());
uint32 checksum = stdext::generate_adler_checksum(m_buffer + m_readPos, getUnreadSize());
return receivedCheck == checksum;
}

View File

@@ -1,12 +1,34 @@
/*
* Copyright (c) 2010-2012 OTClient <https://github.com/edubart/otclient>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#ifndef NETWORKEXCEPTION_H
#define NETWORKEXCEPTION_H
#include "declarations.h"
class NetworkException : public Exception
class NetworkException : public stdext::exception
{
public:
NetworkException(const std::string& what) : Exception(what) { }
NetworkException(const std::string& what) : stdext::exception(what) { }
};
#endif

View File

@@ -46,7 +46,7 @@ void OutputMessage::addU8(uint8 value)
void OutputMessage::addU16(uint16 value)
{
checkWrite(2);
Fw::writeLE16(m_buffer + m_writePos, value);
stdext::writeLE16(m_buffer + m_writePos, value);
m_writePos += 2;
m_messageSize += 2;
}
@@ -54,7 +54,7 @@ void OutputMessage::addU16(uint16 value)
void OutputMessage::addU32(uint32 value)
{
checkWrite(4);
Fw::writeLE32(m_buffer + m_writePos, value);
stdext::writeLE32(m_buffer + m_writePos, value);
m_writePos += 4;
m_messageSize += 4;
}
@@ -62,7 +62,7 @@ void OutputMessage::addU32(uint32 value)
void OutputMessage::addU64(uint64 value)
{
checkWrite(8);
Fw::writeLE64(m_buffer + m_writePos, value);
stdext::writeLE64(m_buffer + m_writePos, value);
m_writePos += 8;
m_messageSize += 8;
}
@@ -96,10 +96,10 @@ void OutputMessage::encryptRSA(int size, const std::string& key)
void OutputMessage::writeChecksum()
{
uint32 checksum = Fw::getAdlerChecksum(m_buffer + m_headerPos, m_messageSize);
uint32 checksum = stdext::generate_adler_checksum(m_buffer + m_headerPos, m_messageSize);
assert(m_headerPos - 4 >= 0);
m_headerPos -= 4;
Fw::writeLE32(m_buffer + m_headerPos, checksum);
stdext::writeLE32(m_buffer + m_headerPos, checksum);
m_messageSize += 4;
}
@@ -107,7 +107,7 @@ void OutputMessage::writeMessageSize()
{
assert(m_headerPos - 2 >= 0);
m_headerPos -= 2;
Fw::writeLE16(m_buffer + m_headerPos, m_messageSize);
stdext::writeLE16(m_buffer + m_headerPos, m_messageSize);
m_messageSize += 2;
}