mirror of
https://github.com/HikikoMarmy/Champions-Reborn-Server.git
synced 2026-04-05 08:59:54 -03:00
Utilities and nice-to-haves
This commit is contained in:
95
Common/ByteBufferReader.hpp
Normal file
95
Common/ByteBufferReader.hpp
Normal file
@@ -0,0 +1,95 @@
|
||||
#pragma once
|
||||
#include <vector>
|
||||
#include <cstdint>
|
||||
#include <stdexcept>
|
||||
#include <cstring>
|
||||
|
||||
class ByteBufferReader {
|
||||
public:
|
||||
ByteBufferReader( const uint8_t *data, size_t size )
|
||||
: buffer( data ), bufferSize( size ), offset( 0 )
|
||||
{
|
||||
}
|
||||
|
||||
explicit ByteBufferReader( const std::vector<uint8_t> &vec )
|
||||
: ByteBufferReader( vec.data(), vec.size() )
|
||||
{
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
T read()
|
||||
{
|
||||
if( offset + sizeof( T ) > bufferSize )
|
||||
throw std::out_of_range( "BufferReader: read out of bounds" );
|
||||
|
||||
T value;
|
||||
std::memcpy( &value, buffer + offset, sizeof( T ) );
|
||||
offset += sizeof( T );
|
||||
return value;
|
||||
}
|
||||
|
||||
void readBytes( void *dest, size_t size )
|
||||
{
|
||||
if( offset + size > bufferSize )
|
||||
throw std::out_of_range( "BufferReader: readBytes out of bounds" );
|
||||
|
||||
std::memcpy( dest, buffer + offset, size );
|
||||
offset += size;
|
||||
}
|
||||
|
||||
std::string readString( size_t length )
|
||||
{
|
||||
if( offset + length > bufferSize )
|
||||
throw std::out_of_range( "BufferReader: readString out of bounds" );
|
||||
std::string str( reinterpret_cast< const char * >( buffer + offset ), length );
|
||||
offset += length;
|
||||
return str;
|
||||
}
|
||||
|
||||
|
||||
template<typename T, size_t N>
|
||||
std::array<T, N> readArray()
|
||||
{
|
||||
std::array<T, N> arr;
|
||||
for( size_t i = 0; i < N; ++i )
|
||||
arr[ i ] = read<T>();
|
||||
return arr;
|
||||
}
|
||||
|
||||
void skip( size_t count )
|
||||
{
|
||||
if( offset + count > bufferSize )
|
||||
throw std::out_of_range( "BufferReader: skip out of bounds" );
|
||||
offset += count;
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
T peek() const
|
||||
{
|
||||
if( offset + sizeof( T ) > bufferSize )
|
||||
throw std::out_of_range( "BufferReader: peek out of bounds" );
|
||||
T value;
|
||||
std::memcpy( &value, buffer + offset, sizeof( T ) );
|
||||
return value;
|
||||
}
|
||||
|
||||
bool eof() const
|
||||
{
|
||||
return offset >= bufferSize;
|
||||
}
|
||||
|
||||
size_t tell() const
|
||||
{
|
||||
return offset;
|
||||
}
|
||||
|
||||
size_t remaining() const
|
||||
{
|
||||
return bufferSize - offset;
|
||||
}
|
||||
|
||||
private:
|
||||
const uint8_t *buffer;
|
||||
size_t bufferSize;
|
||||
size_t offset;
|
||||
};
|
||||
22
Common/ForwardDecl.h
Normal file
22
Common/ForwardDecl.h
Normal file
@@ -0,0 +1,22 @@
|
||||
#pragma once
|
||||
|
||||
#include <memory>
|
||||
|
||||
class RealmUser;
|
||||
using sptr_user = std::shared_ptr< RealmUser >;
|
||||
using wptr_user = std::weak_ptr< RealmUser >;
|
||||
|
||||
class GameSession;
|
||||
using sptr_game_session = std::shared_ptr< GameSession >;
|
||||
|
||||
class RealmSocket;
|
||||
using sptr_socket = std::shared_ptr< RealmSocket >;
|
||||
|
||||
class ByteBuffer;
|
||||
using sptr_byte_stream = std::shared_ptr< ByteBuffer >;
|
||||
|
||||
class ChatRoomSession;
|
||||
using sptr_chat_room_session = std::shared_ptr< ChatRoomSession >;
|
||||
|
||||
class RealmCharacter;
|
||||
using sptr_realm_character = std::shared_ptr< RealmCharacter >;
|
||||
@@ -1,6 +1,9 @@
|
||||
#include <algorithm>
|
||||
#include <cmath>
|
||||
#include <string>
|
||||
#include <cstdint>
|
||||
#include <winsock2.h>
|
||||
#include <ws2tcpip.h>
|
||||
#include <windows.h>
|
||||
|
||||
#include "Utility.h"
|
||||
@@ -48,6 +51,17 @@ uint32_t Util::ByteSwap( uint32_t val )
|
||||
( ( val >> 24 ) & 0x000000FF );
|
||||
}
|
||||
|
||||
std::string Util::IPFromAddr( const sockaddr_in &addr )
|
||||
{
|
||||
char buffer[ INET_ADDRSTRLEN ] = {};
|
||||
const char *result = inet_ntop( AF_INET, &addr.sin_addr, buffer, sizeof( buffer ) );
|
||||
|
||||
if( result )
|
||||
return std::string( buffer );
|
||||
else
|
||||
return {};
|
||||
}
|
||||
|
||||
std::string Util::WideToUTF8( const std::wstring &wstr )
|
||||
{
|
||||
if( wstr.empty() ) return {};
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
|
||||
#include <cstdint>
|
||||
#include <string>
|
||||
#include <WinSock2.h>
|
||||
|
||||
namespace Util
|
||||
{
|
||||
@@ -17,6 +18,8 @@ namespace Util
|
||||
return ( value >= min && value <= max );
|
||||
}
|
||||
|
||||
std::string IPFromAddr( const sockaddr_in &addr );
|
||||
|
||||
std::string WideToUTF8( const std::wstring &wstr );
|
||||
std::wstring UTF8ToWide( const std::string &str );
|
||||
}
|
||||
Reference in New Issue
Block a user