mirror of
https://github.com/HikikoMarmy/Champions-Reborn-Server.git
synced 2026-04-05 08:59:54 -03:00
Big Refactor.
General support for encryption and decryption. Game Session creation. Discovery Server. Still broken as hell, but less so?
This commit is contained in:
384
Crypto/NorrathCrypt.cpp
Normal file
384
Crypto/NorrathCrypt.cpp
Normal file
@@ -0,0 +1,384 @@
|
||||
#include "NorrathCrypt.h"
|
||||
|
||||
rijndael::rijndael( const KeyLength keyLength )
|
||||
{
|
||||
switch( keyLength )
|
||||
{
|
||||
case KeyLength::_128:
|
||||
this->Nk = 4;
|
||||
this->Nr = 10;
|
||||
break;
|
||||
case KeyLength::_192:
|
||||
this->Nk = 6;
|
||||
this->Nr = 12;
|
||||
break;
|
||||
case KeyLength::_256:
|
||||
this->Nk = 8;
|
||||
this->Nr = 14;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
unsigned char *rijndael::EncryptECB( const unsigned char in[], unsigned int inLen,
|
||||
const unsigned char key[] )
|
||||
{
|
||||
CheckLength( inLen );
|
||||
unsigned char *out = new unsigned char[ inLen ];
|
||||
unsigned char *roundKeys = new unsigned char[ 4 * Nb * ( Nr + 1 ) ];
|
||||
KeyExpansion( key, roundKeys );
|
||||
for( unsigned int i = 0; i < inLen; i += blockBytesLen )
|
||||
{
|
||||
EncryptBlock( in + i, out + i, roundKeys );
|
||||
}
|
||||
|
||||
delete[] roundKeys;
|
||||
|
||||
return out;
|
||||
}
|
||||
|
||||
unsigned char *rijndael::DecryptECB( const unsigned char in[], unsigned int inLen,
|
||||
const unsigned char key[] )
|
||||
{
|
||||
CheckLength( inLen );
|
||||
unsigned char *out = new unsigned char[ inLen ];
|
||||
unsigned char *roundKeys = new unsigned char[ 4 * Nb * ( Nr + 1 ) ];
|
||||
KeyExpansion( key, roundKeys );
|
||||
|
||||
for( unsigned int i = 0; i < inLen; i += blockBytesLen )
|
||||
{
|
||||
DecryptBlock( in + i, out + i, roundKeys );
|
||||
}
|
||||
|
||||
delete[] roundKeys;
|
||||
|
||||
return out;
|
||||
}
|
||||
|
||||
void rijndael::CheckLength( unsigned int len )
|
||||
{
|
||||
if( len % blockBytesLen != 0 )
|
||||
{
|
||||
throw std::length_error( "Plaintext length must be divisible by " +
|
||||
std::to_string( blockBytesLen ) );
|
||||
}
|
||||
}
|
||||
|
||||
void rijndael::EncryptBlock( const unsigned char in[], unsigned char out[],
|
||||
unsigned char *roundKeys )
|
||||
{
|
||||
unsigned char state[ 4 ][ Nb ];
|
||||
unsigned int i, j, round;
|
||||
|
||||
for( i = 0; i < 4; i++ )
|
||||
{
|
||||
for( j = 0; j < Nb; j++ )
|
||||
{
|
||||
state[ i ][ j ] = in[ i + 4 * j ];
|
||||
}
|
||||
}
|
||||
|
||||
AddRoundKey( state, roundKeys );
|
||||
|
||||
for( round = 1; round <= Nr - 1; round++ )
|
||||
{
|
||||
SubBytes( state );
|
||||
ShiftRows( state );
|
||||
MixColumns( state );
|
||||
AddRoundKey( state, roundKeys + round * 4 * Nb );
|
||||
}
|
||||
|
||||
SubBytes( state );
|
||||
ShiftRows( state );
|
||||
AddRoundKey( state, roundKeys + Nr * 4 * Nb );
|
||||
|
||||
for( i = 0; i < 4; i++ )
|
||||
{
|
||||
for( j = 0; j < Nb; j++ )
|
||||
{
|
||||
out[ i + 4 * j ] = state[ i ][ j ];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void rijndael::DecryptBlock( const unsigned char in[], unsigned char out[],
|
||||
unsigned char *roundKeys )
|
||||
{
|
||||
unsigned char state[ 4 ][ Nb ];
|
||||
unsigned int i, j, round;
|
||||
|
||||
for( i = 0; i < 4; i++ )
|
||||
{
|
||||
for( j = 0; j < Nb; j++ )
|
||||
{
|
||||
state[ i ][ j ] = in[ i + 4 * j ];
|
||||
}
|
||||
}
|
||||
|
||||
AddRoundKey( state, roundKeys + Nr * 4 * Nb );
|
||||
|
||||
for( round = Nr - 1; round >= 1; round-- )
|
||||
{
|
||||
InvSubBytes( state );
|
||||
InvShiftRows( state );
|
||||
AddRoundKey( state, roundKeys + round * 4 * Nb );
|
||||
InvMixColumns( state );
|
||||
}
|
||||
|
||||
InvSubBytes( state );
|
||||
InvShiftRows( state );
|
||||
AddRoundKey( state, roundKeys );
|
||||
|
||||
for( i = 0; i < 4; i++ )
|
||||
{
|
||||
for( j = 0; j < Nb; j++ )
|
||||
{
|
||||
out[ i + 4 * j ] = state[ i ][ j ];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void rijndael::SubBytes( unsigned char state[ 4 ][ Nb ] )
|
||||
{
|
||||
unsigned int i, j;
|
||||
unsigned char t;
|
||||
for( i = 0; i < 4; i++ )
|
||||
{
|
||||
for( j = 0; j < Nb; j++ )
|
||||
{
|
||||
t = state[ i ][ j ];
|
||||
state[ i ][ j ] = sbox[ t / 16 ][ t % 16 ];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void rijndael::ShiftRow( unsigned char state[ 4 ][ Nb ], unsigned int i,
|
||||
unsigned int n ) // shift row i on n write_positions
|
||||
{
|
||||
unsigned char tmp[ Nb ];
|
||||
for( unsigned int j = 0; j < Nb; j++ )
|
||||
{
|
||||
tmp[ j ] = state[ i ][ ( j + n ) % Nb ];
|
||||
}
|
||||
memcpy( state[ i ], tmp, Nb * sizeof( unsigned char ) );
|
||||
}
|
||||
|
||||
void rijndael::ShiftRows( unsigned char state[ 4 ][ Nb ] )
|
||||
{
|
||||
ShiftRow( state, 1, 1 );
|
||||
ShiftRow( state, 2, 2 );
|
||||
ShiftRow( state, 3, 3 );
|
||||
}
|
||||
|
||||
unsigned char rijndael::xtime( unsigned char b ) // multiply on x
|
||||
{
|
||||
return ( b << 1 ) ^ ( ( ( b >> 7 ) & 1 ) * 0x1b );
|
||||
}
|
||||
|
||||
void rijndael::MixColumns( unsigned char state[ 4 ][ Nb ] )
|
||||
{
|
||||
unsigned char temp_state[ 4 ][ Nb ];
|
||||
|
||||
for( size_t i = 0; i < 4; ++i )
|
||||
{
|
||||
memset( temp_state[ i ], 0, 4 );
|
||||
}
|
||||
|
||||
for( size_t i = 0; i < 4; ++i )
|
||||
{
|
||||
for( size_t k = 0; k < 4; ++k )
|
||||
{
|
||||
for( size_t j = 0; j < 4; ++j )
|
||||
{
|
||||
if( CMDS[ i ][ k ] == 1 )
|
||||
temp_state[ i ][ j ] ^= state[ k ][ j ];
|
||||
else
|
||||
temp_state[ i ][ j ] ^= GF_MUL_TABLE[ CMDS[ i ][ k ] ][ state[ k ][ j ] ];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for( size_t i = 0; i < 4; ++i )
|
||||
{
|
||||
memcpy( state[ i ], temp_state[ i ], 4 );
|
||||
}
|
||||
}
|
||||
|
||||
void rijndael::AddRoundKey( unsigned char state[ 4 ][ Nb ], unsigned char *key )
|
||||
{
|
||||
unsigned int i, j;
|
||||
for( i = 0; i < 4; i++ )
|
||||
{
|
||||
for( j = 0; j < Nb; j++ )
|
||||
{
|
||||
state[ i ][ j ] = state[ i ][ j ] ^ key[ i + 4 * j ];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void rijndael::SubWord( unsigned char *a )
|
||||
{
|
||||
int i;
|
||||
for( i = 0; i < 4; i++ )
|
||||
{
|
||||
a[ i ] = sbox[ a[ i ] / 16 ][ a[ i ] % 16 ];
|
||||
}
|
||||
}
|
||||
|
||||
void rijndael::RotWord( unsigned char *a )
|
||||
{
|
||||
unsigned char c = a[ 0 ];
|
||||
a[ 0 ] = a[ 1 ];
|
||||
a[ 1 ] = a[ 2 ];
|
||||
a[ 2 ] = a[ 3 ];
|
||||
a[ 3 ] = c;
|
||||
}
|
||||
|
||||
void rijndael::XorWords( unsigned char *a, unsigned char *b, unsigned char *c )
|
||||
{
|
||||
int i;
|
||||
for( i = 0; i < 4; i++ )
|
||||
{
|
||||
c[ i ] = a[ i ] ^ b[ i ];
|
||||
}
|
||||
}
|
||||
|
||||
void rijndael::Rcon( unsigned char *a, unsigned int n )
|
||||
{
|
||||
unsigned int i;
|
||||
unsigned char c = 1;
|
||||
for( i = 0; i < n - 1; i++ )
|
||||
{
|
||||
c = xtime( c );
|
||||
}
|
||||
|
||||
a[ 0 ] = c;
|
||||
a[ 1 ] = a[ 2 ] = a[ 3 ] = 0;
|
||||
}
|
||||
|
||||
void rijndael::KeyExpansion( const unsigned char key[], unsigned char w[] )
|
||||
{
|
||||
unsigned char temp[ 4 ];
|
||||
unsigned char rcon[ 4 ];
|
||||
|
||||
unsigned int i = 0;
|
||||
while( i < 4 * Nk )
|
||||
{
|
||||
w[ i ] = key[ i ];
|
||||
i++;
|
||||
}
|
||||
|
||||
i = 4 * Nk;
|
||||
while( i < 4 * Nb * ( Nr + 1 ) )
|
||||
{
|
||||
temp[ 0 ] = w[ i - 4 + 0 ];
|
||||
temp[ 1 ] = w[ i - 4 + 1 ];
|
||||
temp[ 2 ] = w[ i - 4 + 2 ];
|
||||
temp[ 3 ] = w[ i - 4 + 3 ];
|
||||
|
||||
if( i / 4 % Nk == 0 )
|
||||
{
|
||||
RotWord( temp );
|
||||
SubWord( temp );
|
||||
Rcon( rcon, i / ( Nk * 4 ) );
|
||||
XorWords( temp, rcon, temp );
|
||||
}
|
||||
else if( Nk > 6 && i / 4 % Nk == 4 )
|
||||
{
|
||||
SubWord( temp );
|
||||
}
|
||||
|
||||
w[ i + 0 ] = w[ i - 4 * Nk ] ^ temp[ 0 ];
|
||||
w[ i + 1 ] = w[ i + 1 - 4 * Nk ] ^ temp[ 1 ];
|
||||
w[ i + 2 ] = w[ i + 2 - 4 * Nk ] ^ temp[ 2 ];
|
||||
w[ i + 3 ] = w[ i + 3 - 4 * Nk ] ^ temp[ 3 ];
|
||||
i += 4;
|
||||
}
|
||||
}
|
||||
|
||||
void rijndael::InvSubBytes( unsigned char state[ 4 ][ Nb ] )
|
||||
{
|
||||
unsigned int i, j;
|
||||
unsigned char t;
|
||||
for( i = 0; i < 4; i++ )
|
||||
{
|
||||
for( j = 0; j < Nb; j++ )
|
||||
{
|
||||
t = state[ i ][ j ];
|
||||
state[ i ][ j ] = inv_sbox[ t / 16 ][ t % 16 ];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void rijndael::InvMixColumns( unsigned char state[ 4 ][ Nb ] )
|
||||
{
|
||||
unsigned char temp_state[ 4 ][ Nb ];
|
||||
|
||||
for( size_t i = 0; i < 4; ++i )
|
||||
{
|
||||
memset( temp_state[ i ], 0, 4 );
|
||||
}
|
||||
|
||||
for( size_t i = 0; i < 4; ++i )
|
||||
{
|
||||
for( size_t k = 0; k < 4; ++k )
|
||||
{
|
||||
for( size_t j = 0; j < 4; ++j )
|
||||
{
|
||||
temp_state[ i ][ j ] ^= GF_MUL_TABLE[ INV_CMDS[ i ][ k ] ][ state[ k ][ j ] ];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for( size_t i = 0; i < 4; ++i )
|
||||
{
|
||||
memcpy( state[ i ], temp_state[ i ], 4 );
|
||||
}
|
||||
}
|
||||
|
||||
void rijndael::InvShiftRows( unsigned char state[ 4 ][ Nb ] )
|
||||
{
|
||||
ShiftRow( state, 1, Nb - 1 );
|
||||
ShiftRow( state, 2, Nb - 2 );
|
||||
ShiftRow( state, 3, Nb - 3 );
|
||||
}
|
||||
|
||||
void rijndael::XorBlocks( const unsigned char *a, const unsigned char *b,
|
||||
unsigned char *c, unsigned int len )
|
||||
{
|
||||
for( unsigned int i = 0; i < len; i++ )
|
||||
{
|
||||
c[ i ] = a[ i ] ^ b[ i ];
|
||||
}
|
||||
}
|
||||
|
||||
std::vector<unsigned char> rijndael::ArrayToVector( unsigned char *a,
|
||||
unsigned int len )
|
||||
{
|
||||
std::vector<unsigned char> v( a, a + len * sizeof( unsigned char ) );
|
||||
return v;
|
||||
}
|
||||
|
||||
unsigned char *rijndael::VectorToArray( std::vector<unsigned char> &a )
|
||||
{
|
||||
return a.data();
|
||||
}
|
||||
|
||||
std::vector<unsigned char> rijndael::EncryptECB( std::vector<unsigned char> in,
|
||||
std::vector<unsigned char> key )
|
||||
{
|
||||
unsigned char *out = EncryptECB( VectorToArray( in ), ( unsigned int )in.size(),
|
||||
VectorToArray( key ) );
|
||||
std::vector<unsigned char> v = ArrayToVector( out, in.size() );
|
||||
delete[] out;
|
||||
return v;
|
||||
}
|
||||
|
||||
std::vector<unsigned char> rijndael::DecryptECB( std::vector<unsigned char> in,
|
||||
std::vector<unsigned char> key )
|
||||
{
|
||||
unsigned char *out = DecryptECB( VectorToArray( in ), ( unsigned int )in.size(),
|
||||
VectorToArray( key ) );
|
||||
std::vector<unsigned char> v = ArrayToVector( out, ( unsigned int )in.size() );
|
||||
delete[] out;
|
||||
return v;
|
||||
}
|
||||
287
Crypto/NorrathCrypt.h
Normal file
287
Crypto/NorrathCrypt.h
Normal file
@@ -0,0 +1,287 @@
|
||||
#pragma once
|
||||
|
||||
#include <cstdio>
|
||||
#include <cstring>
|
||||
#include <iostream>
|
||||
#include <stdexcept>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
enum class KeyLength {
|
||||
_128, _192, _256
|
||||
};
|
||||
|
||||
class rijndael {
|
||||
private:
|
||||
static constexpr unsigned int Nb = 4;
|
||||
static constexpr unsigned int blockBytesLen = 4 * Nb * sizeof( unsigned char );
|
||||
|
||||
unsigned int Nk;
|
||||
unsigned int Nr;
|
||||
|
||||
void SubBytes( unsigned char state[ 4 ][ Nb ] );
|
||||
|
||||
void ShiftRow( unsigned char state[ 4 ][ Nb ], unsigned int i,
|
||||
unsigned int n ); // shift row i on n write_positions
|
||||
|
||||
void ShiftRows( unsigned char state[ 4 ][ Nb ] );
|
||||
|
||||
unsigned char xtime( unsigned char b ); // multiply on x
|
||||
|
||||
void MixColumns( unsigned char state[ 4 ][ Nb ] );
|
||||
|
||||
void AddRoundKey( unsigned char state[ 4 ][ Nb ], unsigned char *key );
|
||||
|
||||
void SubWord( unsigned char *a );
|
||||
|
||||
void RotWord( unsigned char *a );
|
||||
|
||||
void XorWords( unsigned char *a, unsigned char *b, unsigned char *c );
|
||||
|
||||
void Rcon( unsigned char *a, unsigned int n );
|
||||
|
||||
void InvSubBytes( unsigned char state[ 4 ][ Nb ] );
|
||||
|
||||
void InvMixColumns( unsigned char state[ 4 ][ Nb ] );
|
||||
|
||||
void InvShiftRows( unsigned char state[ 4 ][ Nb ] );
|
||||
|
||||
void CheckLength( unsigned int len );
|
||||
|
||||
void KeyExpansion( const unsigned char key[], unsigned char w[] );
|
||||
|
||||
void EncryptBlock( const unsigned char in[], unsigned char out[],
|
||||
unsigned char *roundKeys );
|
||||
|
||||
void DecryptBlock( const unsigned char in[], unsigned char out[],
|
||||
unsigned char *roundKeys );
|
||||
|
||||
void XorBlocks( const unsigned char *a, const unsigned char *b,
|
||||
unsigned char *c, unsigned int len );
|
||||
|
||||
std::vector<unsigned char> ArrayToVector( unsigned char *a, unsigned int len );
|
||||
|
||||
unsigned char *VectorToArray( std::vector<unsigned char> &a );
|
||||
|
||||
public:
|
||||
explicit rijndael( const KeyLength keyLength = KeyLength::_256 );
|
||||
|
||||
unsigned char *EncryptECB( const unsigned char in[], unsigned int inLen,
|
||||
const unsigned char key[] );
|
||||
|
||||
unsigned char *DecryptECB( const unsigned char in[], unsigned int inLen,
|
||||
const unsigned char key[] );
|
||||
|
||||
|
||||
std::vector<unsigned char> EncryptECB( std::vector<unsigned char> in,
|
||||
std::vector<unsigned char> key );
|
||||
|
||||
std::vector<unsigned char> DecryptECB( std::vector<unsigned char> in,
|
||||
std::vector<unsigned char> key );
|
||||
|
||||
};
|
||||
|
||||
const unsigned char sbox[ 16 ][ 16 ] = {
|
||||
{0x63, 0x7c, 0x77, 0x7b, 0xf2, 0x6b, 0x6f, 0xc5, 0x30, 0x01, 0x67, 0x2b, 0xfe, 0xd7, 0xab, 0x76},
|
||||
{0xca, 0x82, 0xc9, 0x7d, 0xfa, 0x59, 0x47, 0xf0, 0xad, 0xd4, 0xa2, 0xaf, 0x9c, 0xa4, 0x72, 0xc0},
|
||||
{0xb7, 0xfd, 0x93, 0x26, 0x36, 0x3f, 0xf7, 0xcc, 0x34, 0xa5, 0xe5, 0xf1, 0x71, 0xd8, 0x31, 0x15},
|
||||
{0x04, 0xc7, 0x23, 0xc3, 0x18, 0x96, 0x05, 0x9a, 0x07, 0x12, 0x80, 0xe2, 0xeb, 0x27, 0xb2, 0x75},
|
||||
{0x09, 0x83, 0x2c, 0x1a, 0x1b, 0x6e, 0x5a, 0xa0, 0x52, 0x3b, 0xd6, 0xb3, 0x29, 0xe3, 0x2f, 0x84},
|
||||
{0x53, 0xd1, 0x00, 0xed, 0x20, 0xfc, 0xb1, 0x5b, 0x6a, 0xcb, 0xbe, 0x39, 0x4a, 0x4c, 0x58, 0xcf},
|
||||
{0xd0, 0xef, 0xaa, 0xfb, 0x43, 0x4d, 0x33, 0x85, 0x45, 0xf9, 0x02, 0x7f, 0x50, 0x3c, 0x9f, 0xa8},
|
||||
{0x51, 0xa3, 0x40, 0x8f, 0x92, 0x9d, 0x38, 0xf5, 0xbc, 0xb6, 0xda, 0x21, 0x10, 0xff, 0xf3, 0xd2},
|
||||
{0xcd, 0x0c, 0x13, 0xec, 0x5f, 0x97, 0x44, 0x17, 0xc4, 0xa7, 0x7e, 0x3d, 0x64, 0x5d, 0x19, 0x73},
|
||||
{0x60, 0x81, 0x4f, 0xdc, 0x22, 0x2a, 0x90, 0x88, 0x46, 0xee, 0xb8, 0x14, 0xde, 0x5e, 0x0b, 0xdb},
|
||||
{0xe0, 0x32, 0x3a, 0x0a, 0x49, 0x06, 0x24, 0x5c, 0xc2, 0xd3, 0xac, 0x62, 0x91, 0x95, 0xe4, 0x79},
|
||||
{0xe7, 0xc8, 0x37, 0x6d, 0x8d, 0xd5, 0x4e, 0xa9, 0x6c, 0x56, 0xf4, 0xea, 0x65, 0x7a, 0xae, 0x08},
|
||||
{0xba, 0x78, 0x25, 0x2e, 0x1c, 0xa6, 0xb4, 0xc6, 0xe8, 0xdd, 0x74, 0x1f, 0x4b, 0xbd, 0x8b, 0x8a},
|
||||
{0x70, 0x3e, 0xb5, 0x66, 0x48, 0x03, 0xf6, 0x0e, 0x61, 0x35, 0x57, 0xb9, 0x86, 0xc1, 0x1d, 0x9e},
|
||||
{0xe1, 0xf8, 0x98, 0x11, 0x69, 0xd9, 0x8e, 0x94, 0x9b, 0x1e, 0x87, 0xe9, 0xce, 0x55, 0x28, 0xdf},
|
||||
{0x8c, 0xa1, 0x89, 0x0d, 0xbf, 0xe6, 0x42, 0x68, 0x41, 0x99, 0x2d, 0x0f, 0xb0, 0x54, 0xbb, 0x16}
|
||||
};
|
||||
|
||||
const unsigned char inv_sbox[ 16 ][ 16 ] = {
|
||||
{0x52, 0x09, 0x6a, 0xd5, 0x30, 0x36, 0xa5, 0x38, 0xbf, 0x40, 0xa3, 0x9e, 0x81, 0xf3, 0xd7, 0xfb},
|
||||
{0x7c, 0xe3, 0x39, 0x82, 0x9b, 0x2f, 0xff, 0x87, 0x34, 0x8e, 0x43, 0x44, 0xc4, 0xde, 0xe9, 0xcb},
|
||||
{0x54, 0x7b, 0x94, 0x32, 0xa6, 0xc2, 0x23, 0x3d, 0xee, 0x4c, 0x95, 0x0b, 0x42, 0xfa, 0xc3, 0x4e},
|
||||
{0x08, 0x2e, 0xa1, 0x66, 0x28, 0xd9, 0x24, 0xb2, 0x76, 0x5b, 0xa2, 0x49, 0x6d, 0x8b, 0xd1, 0x25},
|
||||
{0x72, 0xf8, 0xf6, 0x64, 0x86, 0x68, 0x98, 0x16, 0xd4, 0xa4, 0x5c, 0xcc, 0x5d, 0x65, 0xb6, 0x92},
|
||||
{0x6c, 0x70, 0x48, 0x50, 0xfd, 0xed, 0xb9, 0xda, 0x5e, 0x15, 0x46, 0x57, 0xa7, 0x8d, 0x9d, 0x84},
|
||||
{0x90, 0xd8, 0xab, 0x00, 0x8c, 0xbc, 0xd3, 0x0a, 0xf7, 0xe4, 0x58, 0x05, 0xb8, 0xb3, 0x45, 0x06},
|
||||
{0xd0, 0x2c, 0x1e, 0x8f, 0xca, 0x3f, 0x0f, 0x02, 0xc1, 0xaf, 0xbd, 0x03, 0x01, 0x13, 0x8a, 0x6b},
|
||||
{0x3a, 0x91, 0x11, 0x41, 0x4f, 0x67, 0xdc, 0xea, 0x97, 0xf2, 0xcf, 0xce, 0xf0, 0xb4, 0xe6, 0x73},
|
||||
{0x96, 0xac, 0x74, 0x22, 0xe7, 0xad, 0x35, 0x85, 0xe2, 0xf9, 0x37, 0xe8, 0x1c, 0x75, 0xdf, 0x6e},
|
||||
{0x47, 0xf1, 0x1a, 0x71, 0x1d, 0x29, 0xc5, 0x89, 0x6f, 0xb7, 0x62, 0x0e, 0xaa, 0x18, 0xbe, 0x1b},
|
||||
{0xfc, 0x56, 0x3e, 0x4b, 0xc6, 0xd2, 0x79, 0x20, 0x9a, 0xdb, 0xc0, 0xfe, 0x78, 0xcd, 0x5a, 0xf4},
|
||||
{0x1f, 0xdd, 0xa8, 0x33, 0x88, 0x07, 0xc7, 0x31, 0xb1, 0x12, 0x10, 0x59, 0x27, 0x80, 0xec, 0x5f},
|
||||
{0x60, 0x51, 0x7f, 0xa9, 0x19, 0xb5, 0x4a, 0x0d, 0x2d, 0xe5, 0x7a, 0x9f, 0x93, 0xc9, 0x9c, 0xef},
|
||||
{0xa0, 0xe0, 0x3b, 0x4d, 0xae, 0x2a, 0xf5, 0xb0, 0xc8, 0xeb, 0xbb, 0x3c, 0x83, 0x53, 0x99, 0x61},
|
||||
{0x17, 0x2b, 0x04, 0x7e, 0xba, 0x77, 0xd6, 0x26, 0xe1, 0x69, 0x14, 0x63, 0x55, 0x21, 0x0c, 0x7d}
|
||||
};
|
||||
|
||||
/// Galois Multiplication lookup tables
|
||||
static const unsigned char GF_MUL_TABLE[ 15 ][ 256 ] = {
|
||||
{},
|
||||
{},
|
||||
|
||||
// mul 2
|
||||
{0x00, 0x02, 0x04, 0x06, 0x08, 0x0a, 0x0c, 0x0e, 0x10, 0x12, 0x14, 0x16,
|
||||
0x18, 0x1a, 0x1c, 0x1e, 0x20, 0x22, 0x24, 0x26, 0x28, 0x2a, 0x2c, 0x2e,
|
||||
0x30, 0x32, 0x34, 0x36, 0x38, 0x3a, 0x3c, 0x3e, 0x40, 0x42, 0x44, 0x46,
|
||||
0x48, 0x4a, 0x4c, 0x4e, 0x50, 0x52, 0x54, 0x56, 0x58, 0x5a, 0x5c, 0x5e,
|
||||
0x60, 0x62, 0x64, 0x66, 0x68, 0x6a, 0x6c, 0x6e, 0x70, 0x72, 0x74, 0x76,
|
||||
0x78, 0x7a, 0x7c, 0x7e, 0x80, 0x82, 0x84, 0x86, 0x88, 0x8a, 0x8c, 0x8e,
|
||||
0x90, 0x92, 0x94, 0x96, 0x98, 0x9a, 0x9c, 0x9e, 0xa0, 0xa2, 0xa4, 0xa6,
|
||||
0xa8, 0xaa, 0xac, 0xae, 0xb0, 0xb2, 0xb4, 0xb6, 0xb8, 0xba, 0xbc, 0xbe,
|
||||
0xc0, 0xc2, 0xc4, 0xc6, 0xc8, 0xca, 0xcc, 0xce, 0xd0, 0xd2, 0xd4, 0xd6,
|
||||
0xd8, 0xda, 0xdc, 0xde, 0xe0, 0xe2, 0xe4, 0xe6, 0xe8, 0xea, 0xec, 0xee,
|
||||
0xf0, 0xf2, 0xf4, 0xf6, 0xf8, 0xfa, 0xfc, 0xfe, 0x1b, 0x19, 0x1f, 0x1d,
|
||||
0x13, 0x11, 0x17, 0x15, 0x0b, 0x09, 0x0f, 0x0d, 0x03, 0x01, 0x07, 0x05,
|
||||
0x3b, 0x39, 0x3f, 0x3d, 0x33, 0x31, 0x37, 0x35, 0x2b, 0x29, 0x2f, 0x2d,
|
||||
0x23, 0x21, 0x27, 0x25, 0x5b, 0x59, 0x5f, 0x5d, 0x53, 0x51, 0x57, 0x55,
|
||||
0x4b, 0x49, 0x4f, 0x4d, 0x43, 0x41, 0x47, 0x45, 0x7b, 0x79, 0x7f, 0x7d,
|
||||
0x73, 0x71, 0x77, 0x75, 0x6b, 0x69, 0x6f, 0x6d, 0x63, 0x61, 0x67, 0x65,
|
||||
0x9b, 0x99, 0x9f, 0x9d, 0x93, 0x91, 0x97, 0x95, 0x8b, 0x89, 0x8f, 0x8d,
|
||||
0x83, 0x81, 0x87, 0x85, 0xbb, 0xb9, 0xbf, 0xbd, 0xb3, 0xb1, 0xb7, 0xb5,
|
||||
0xab, 0xa9, 0xaf, 0xad, 0xa3, 0xa1, 0xa7, 0xa5, 0xdb, 0xd9, 0xdf, 0xdd,
|
||||
0xd3, 0xd1, 0xd7, 0xd5, 0xcb, 0xc9, 0xcf, 0xcd, 0xc3, 0xc1, 0xc7, 0xc5,
|
||||
0xfb, 0xf9, 0xff, 0xfd, 0xf3, 0xf1, 0xf7, 0xf5, 0xeb, 0xe9, 0xef, 0xed,
|
||||
0xe3, 0xe1, 0xe7, 0xe5},
|
||||
|
||||
// mul 3
|
||||
{0x00, 0x03, 0x06, 0x05, 0x0c, 0x0f, 0x0a, 0x09, 0x18, 0x1b, 0x1e, 0x1d,
|
||||
0x14, 0x17, 0x12, 0x11, 0x30, 0x33, 0x36, 0x35, 0x3c, 0x3f, 0x3a, 0x39,
|
||||
0x28, 0x2b, 0x2e, 0x2d, 0x24, 0x27, 0x22, 0x21, 0x60, 0x63, 0x66, 0x65,
|
||||
0x6c, 0x6f, 0x6a, 0x69, 0x78, 0x7b, 0x7e, 0x7d, 0x74, 0x77, 0x72, 0x71,
|
||||
0x50, 0x53, 0x56, 0x55, 0x5c, 0x5f, 0x5a, 0x59, 0x48, 0x4b, 0x4e, 0x4d,
|
||||
0x44, 0x47, 0x42, 0x41, 0xc0, 0xc3, 0xc6, 0xc5, 0xcc, 0xcf, 0xca, 0xc9,
|
||||
0xd8, 0xdb, 0xde, 0xdd, 0xd4, 0xd7, 0xd2, 0xd1, 0xf0, 0xf3, 0xf6, 0xf5,
|
||||
0xfc, 0xff, 0xfa, 0xf9, 0xe8, 0xeb, 0xee, 0xed, 0xe4, 0xe7, 0xe2, 0xe1,
|
||||
0xa0, 0xa3, 0xa6, 0xa5, 0xac, 0xaf, 0xaa, 0xa9, 0xb8, 0xbb, 0xbe, 0xbd,
|
||||
0xb4, 0xb7, 0xb2, 0xb1, 0x90, 0x93, 0x96, 0x95, 0x9c, 0x9f, 0x9a, 0x99,
|
||||
0x88, 0x8b, 0x8e, 0x8d, 0x84, 0x87, 0x82, 0x81, 0x9b, 0x98, 0x9d, 0x9e,
|
||||
0x97, 0x94, 0x91, 0x92, 0x83, 0x80, 0x85, 0x86, 0x8f, 0x8c, 0x89, 0x8a,
|
||||
0xab, 0xa8, 0xad, 0xae, 0xa7, 0xa4, 0xa1, 0xa2, 0xb3, 0xb0, 0xb5, 0xb6,
|
||||
0xbf, 0xbc, 0xb9, 0xba, 0xfb, 0xf8, 0xfd, 0xfe, 0xf7, 0xf4, 0xf1, 0xf2,
|
||||
0xe3, 0xe0, 0xe5, 0xe6, 0xef, 0xec, 0xe9, 0xea, 0xcb, 0xc8, 0xcd, 0xce,
|
||||
0xc7, 0xc4, 0xc1, 0xc2, 0xd3, 0xd0, 0xd5, 0xd6, 0xdf, 0xdc, 0xd9, 0xda,
|
||||
0x5b, 0x58, 0x5d, 0x5e, 0x57, 0x54, 0x51, 0x52, 0x43, 0x40, 0x45, 0x46,
|
||||
0x4f, 0x4c, 0x49, 0x4a, 0x6b, 0x68, 0x6d, 0x6e, 0x67, 0x64, 0x61, 0x62,
|
||||
0x73, 0x70, 0x75, 0x76, 0x7f, 0x7c, 0x79, 0x7a, 0x3b, 0x38, 0x3d, 0x3e,
|
||||
0x37, 0x34, 0x31, 0x32, 0x23, 0x20, 0x25, 0x26, 0x2f, 0x2c, 0x29, 0x2a,
|
||||
0x0b, 0x08, 0x0d, 0x0e, 0x07, 0x04, 0x01, 0x02, 0x13, 0x10, 0x15, 0x16,
|
||||
0x1f, 0x1c, 0x19, 0x1a},
|
||||
|
||||
{},
|
||||
{},
|
||||
{},
|
||||
{},
|
||||
{},
|
||||
|
||||
// mul 9
|
||||
{0x00, 0x09, 0x12, 0x1b, 0x24, 0x2d, 0x36, 0x3f, 0x48, 0x41, 0x5a, 0x53,
|
||||
0x6c, 0x65, 0x7e, 0x77, 0x90, 0x99, 0x82, 0x8b, 0xb4, 0xbd, 0xa6, 0xaf,
|
||||
0xd8, 0xd1, 0xca, 0xc3, 0xfc, 0xf5, 0xee, 0xe7, 0x3b, 0x32, 0x29, 0x20,
|
||||
0x1f, 0x16, 0x0d, 0x04, 0x73, 0x7a, 0x61, 0x68, 0x57, 0x5e, 0x45, 0x4c,
|
||||
0xab, 0xa2, 0xb9, 0xb0, 0x8f, 0x86, 0x9d, 0x94, 0xe3, 0xea, 0xf1, 0xf8,
|
||||
0xc7, 0xce, 0xd5, 0xdc, 0x76, 0x7f, 0x64, 0x6d, 0x52, 0x5b, 0x40, 0x49,
|
||||
0x3e, 0x37, 0x2c, 0x25, 0x1a, 0x13, 0x08, 0x01, 0xe6, 0xef, 0xf4, 0xfd,
|
||||
0xc2, 0xcb, 0xd0, 0xd9, 0xae, 0xa7, 0xbc, 0xb5, 0x8a, 0x83, 0x98, 0x91,
|
||||
0x4d, 0x44, 0x5f, 0x56, 0x69, 0x60, 0x7b, 0x72, 0x05, 0x0c, 0x17, 0x1e,
|
||||
0x21, 0x28, 0x33, 0x3a, 0xdd, 0xd4, 0xcf, 0xc6, 0xf9, 0xf0, 0xeb, 0xe2,
|
||||
0x95, 0x9c, 0x87, 0x8e, 0xb1, 0xb8, 0xa3, 0xaa, 0xec, 0xe5, 0xfe, 0xf7,
|
||||
0xc8, 0xc1, 0xda, 0xd3, 0xa4, 0xad, 0xb6, 0xbf, 0x80, 0x89, 0x92, 0x9b,
|
||||
0x7c, 0x75, 0x6e, 0x67, 0x58, 0x51, 0x4a, 0x43, 0x34, 0x3d, 0x26, 0x2f,
|
||||
0x10, 0x19, 0x02, 0x0b, 0xd7, 0xde, 0xc5, 0xcc, 0xf3, 0xfa, 0xe1, 0xe8,
|
||||
0x9f, 0x96, 0x8d, 0x84, 0xbb, 0xb2, 0xa9, 0xa0, 0x47, 0x4e, 0x55, 0x5c,
|
||||
0x63, 0x6a, 0x71, 0x78, 0x0f, 0x06, 0x1d, 0x14, 0x2b, 0x22, 0x39, 0x30,
|
||||
0x9a, 0x93, 0x88, 0x81, 0xbe, 0xb7, 0xac, 0xa5, 0xd2, 0xdb, 0xc0, 0xc9,
|
||||
0xf6, 0xff, 0xe4, 0xed, 0x0a, 0x03, 0x18, 0x11, 0x2e, 0x27, 0x3c, 0x35,
|
||||
0x42, 0x4b, 0x50, 0x59, 0x66, 0x6f, 0x74, 0x7d, 0xa1, 0xa8, 0xb3, 0xba,
|
||||
0x85, 0x8c, 0x97, 0x9e, 0xe9, 0xe0, 0xfb, 0xf2, 0xcd, 0xc4, 0xdf, 0xd6,
|
||||
0x31, 0x38, 0x23, 0x2a, 0x15, 0x1c, 0x07, 0x0e, 0x79, 0x70, 0x6b, 0x62,
|
||||
0x5d, 0x54, 0x4f, 0x46},
|
||||
|
||||
{},
|
||||
|
||||
// mul 11
|
||||
{0x00, 0x0b, 0x16, 0x1d, 0x2c, 0x27, 0x3a, 0x31, 0x58, 0x53, 0x4e, 0x45,
|
||||
0x74, 0x7f, 0x62, 0x69, 0xb0, 0xbb, 0xa6, 0xad, 0x9c, 0x97, 0x8a, 0x81,
|
||||
0xe8, 0xe3, 0xfe, 0xf5, 0xc4, 0xcf, 0xd2, 0xd9, 0x7b, 0x70, 0x6d, 0x66,
|
||||
0x57, 0x5c, 0x41, 0x4a, 0x23, 0x28, 0x35, 0x3e, 0x0f, 0x04, 0x19, 0x12,
|
||||
0xcb, 0xc0, 0xdd, 0xd6, 0xe7, 0xec, 0xf1, 0xfa, 0x93, 0x98, 0x85, 0x8e,
|
||||
0xbf, 0xb4, 0xa9, 0xa2, 0xf6, 0xfd, 0xe0, 0xeb, 0xda, 0xd1, 0xcc, 0xc7,
|
||||
0xae, 0xa5, 0xb8, 0xb3, 0x82, 0x89, 0x94, 0x9f, 0x46, 0x4d, 0x50, 0x5b,
|
||||
0x6a, 0x61, 0x7c, 0x77, 0x1e, 0x15, 0x08, 0x03, 0x32, 0x39, 0x24, 0x2f,
|
||||
0x8d, 0x86, 0x9b, 0x90, 0xa1, 0xaa, 0xb7, 0xbc, 0xd5, 0xde, 0xc3, 0xc8,
|
||||
0xf9, 0xf2, 0xef, 0xe4, 0x3d, 0x36, 0x2b, 0x20, 0x11, 0x1a, 0x07, 0x0c,
|
||||
0x65, 0x6e, 0x73, 0x78, 0x49, 0x42, 0x5f, 0x54, 0xf7, 0xfc, 0xe1, 0xea,
|
||||
0xdb, 0xd0, 0xcd, 0xc6, 0xaf, 0xa4, 0xb9, 0xb2, 0x83, 0x88, 0x95, 0x9e,
|
||||
0x47, 0x4c, 0x51, 0x5a, 0x6b, 0x60, 0x7d, 0x76, 0x1f, 0x14, 0x09, 0x02,
|
||||
0x33, 0x38, 0x25, 0x2e, 0x8c, 0x87, 0x9a, 0x91, 0xa0, 0xab, 0xb6, 0xbd,
|
||||
0xd4, 0xdf, 0xc2, 0xc9, 0xf8, 0xf3, 0xee, 0xe5, 0x3c, 0x37, 0x2a, 0x21,
|
||||
0x10, 0x1b, 0x06, 0x0d, 0x64, 0x6f, 0x72, 0x79, 0x48, 0x43, 0x5e, 0x55,
|
||||
0x01, 0x0a, 0x17, 0x1c, 0x2d, 0x26, 0x3b, 0x30, 0x59, 0x52, 0x4f, 0x44,
|
||||
0x75, 0x7e, 0x63, 0x68, 0xb1, 0xba, 0xa7, 0xac, 0x9d, 0x96, 0x8b, 0x80,
|
||||
0xe9, 0xe2, 0xff, 0xf4, 0xc5, 0xce, 0xd3, 0xd8, 0x7a, 0x71, 0x6c, 0x67,
|
||||
0x56, 0x5d, 0x40, 0x4b, 0x22, 0x29, 0x34, 0x3f, 0x0e, 0x05, 0x18, 0x13,
|
||||
0xca, 0xc1, 0xdc, 0xd7, 0xe6, 0xed, 0xf0, 0xfb, 0x92, 0x99, 0x84, 0x8f,
|
||||
0xbe, 0xb5, 0xa8, 0xa3},
|
||||
|
||||
{},
|
||||
|
||||
// mul 13
|
||||
{0x00, 0x0d, 0x1a, 0x17, 0x34, 0x39, 0x2e, 0x23, 0x68, 0x65, 0x72, 0x7f,
|
||||
0x5c, 0x51, 0x46, 0x4b, 0xd0, 0xdd, 0xca, 0xc7, 0xe4, 0xe9, 0xfe, 0xf3,
|
||||
0xb8, 0xb5, 0xa2, 0xaf, 0x8c, 0x81, 0x96, 0x9b, 0xbb, 0xb6, 0xa1, 0xac,
|
||||
0x8f, 0x82, 0x95, 0x98, 0xd3, 0xde, 0xc9, 0xc4, 0xe7, 0xea, 0xfd, 0xf0,
|
||||
0x6b, 0x66, 0x71, 0x7c, 0x5f, 0x52, 0x45, 0x48, 0x03, 0x0e, 0x19, 0x14,
|
||||
0x37, 0x3a, 0x2d, 0x20, 0x6d, 0x60, 0x77, 0x7a, 0x59, 0x54, 0x43, 0x4e,
|
||||
0x05, 0x08, 0x1f, 0x12, 0x31, 0x3c, 0x2b, 0x26, 0xbd, 0xb0, 0xa7, 0xaa,
|
||||
0x89, 0x84, 0x93, 0x9e, 0xd5, 0xd8, 0xcf, 0xc2, 0xe1, 0xec, 0xfb, 0xf6,
|
||||
0xd6, 0xdb, 0xcc, 0xc1, 0xe2, 0xef, 0xf8, 0xf5, 0xbe, 0xb3, 0xa4, 0xa9,
|
||||
0x8a, 0x87, 0x90, 0x9d, 0x06, 0x0b, 0x1c, 0x11, 0x32, 0x3f, 0x28, 0x25,
|
||||
0x6e, 0x63, 0x74, 0x79, 0x5a, 0x57, 0x40, 0x4d, 0xda, 0xd7, 0xc0, 0xcd,
|
||||
0xee, 0xe3, 0xf4, 0xf9, 0xb2, 0xbf, 0xa8, 0xa5, 0x86, 0x8b, 0x9c, 0x91,
|
||||
0x0a, 0x07, 0x10, 0x1d, 0x3e, 0x33, 0x24, 0x29, 0x62, 0x6f, 0x78, 0x75,
|
||||
0x56, 0x5b, 0x4c, 0x41, 0x61, 0x6c, 0x7b, 0x76, 0x55, 0x58, 0x4f, 0x42,
|
||||
0x09, 0x04, 0x13, 0x1e, 0x3d, 0x30, 0x27, 0x2a, 0xb1, 0xbc, 0xab, 0xa6,
|
||||
0x85, 0x88, 0x9f, 0x92, 0xd9, 0xd4, 0xc3, 0xce, 0xed, 0xe0, 0xf7, 0xfa,
|
||||
0xb7, 0xba, 0xad, 0xa0, 0x83, 0x8e, 0x99, 0x94, 0xdf, 0xd2, 0xc5, 0xc8,
|
||||
0xeb, 0xe6, 0xf1, 0xfc, 0x67, 0x6a, 0x7d, 0x70, 0x53, 0x5e, 0x49, 0x44,
|
||||
0x0f, 0x02, 0x15, 0x18, 0x3b, 0x36, 0x21, 0x2c, 0x0c, 0x01, 0x16, 0x1b,
|
||||
0x38, 0x35, 0x22, 0x2f, 0x64, 0x69, 0x7e, 0x73, 0x50, 0x5d, 0x4a, 0x47,
|
||||
0xdc, 0xd1, 0xc6, 0xcb, 0xe8, 0xe5, 0xf2, 0xff, 0xb4, 0xb9, 0xae, 0xa3,
|
||||
0x80, 0x8d, 0x9a, 0x97},
|
||||
|
||||
// mul 14
|
||||
{0x00, 0x0e, 0x1c, 0x12, 0x38, 0x36, 0x24, 0x2a, 0x70, 0x7e, 0x6c, 0x62,
|
||||
0x48, 0x46, 0x54, 0x5a, 0xe0, 0xee, 0xfc, 0xf2, 0xd8, 0xd6, 0xc4, 0xca,
|
||||
0x90, 0x9e, 0x8c, 0x82, 0xa8, 0xa6, 0xb4, 0xba, 0xdb, 0xd5, 0xc7, 0xc9,
|
||||
0xe3, 0xed, 0xff, 0xf1, 0xab, 0xa5, 0xb7, 0xb9, 0x93, 0x9d, 0x8f, 0x81,
|
||||
0x3b, 0x35, 0x27, 0x29, 0x03, 0x0d, 0x1f, 0x11, 0x4b, 0x45, 0x57, 0x59,
|
||||
0x73, 0x7d, 0x6f, 0x61, 0xad, 0xa3, 0xb1, 0xbf, 0x95, 0x9b, 0x89, 0x87,
|
||||
0xdd, 0xd3, 0xc1, 0xcf, 0xe5, 0xeb, 0xf9, 0xf7, 0x4d, 0x43, 0x51, 0x5f,
|
||||
0x75, 0x7b, 0x69, 0x67, 0x3d, 0x33, 0x21, 0x2f, 0x05, 0x0b, 0x19, 0x17,
|
||||
0x76, 0x78, 0x6a, 0x64, 0x4e, 0x40, 0x52, 0x5c, 0x06, 0x08, 0x1a, 0x14,
|
||||
0x3e, 0x30, 0x22, 0x2c, 0x96, 0x98, 0x8a, 0x84, 0xae, 0xa0, 0xb2, 0xbc,
|
||||
0xe6, 0xe8, 0xfa, 0xf4, 0xde, 0xd0, 0xc2, 0xcc, 0x41, 0x4f, 0x5d, 0x53,
|
||||
0x79, 0x77, 0x65, 0x6b, 0x31, 0x3f, 0x2d, 0x23, 0x09, 0x07, 0x15, 0x1b,
|
||||
0xa1, 0xaf, 0xbd, 0xb3, 0x99, 0x97, 0x85, 0x8b, 0xd1, 0xdf, 0xcd, 0xc3,
|
||||
0xe9, 0xe7, 0xf5, 0xfb, 0x9a, 0x94, 0x86, 0x88, 0xa2, 0xac, 0xbe, 0xb0,
|
||||
0xea, 0xe4, 0xf6, 0xf8, 0xd2, 0xdc, 0xce, 0xc0, 0x7a, 0x74, 0x66, 0x68,
|
||||
0x42, 0x4c, 0x5e, 0x50, 0x0a, 0x04, 0x16, 0x18, 0x32, 0x3c, 0x2e, 0x20,
|
||||
0xec, 0xe2, 0xf0, 0xfe, 0xd4, 0xda, 0xc8, 0xc6, 0x9c, 0x92, 0x80, 0x8e,
|
||||
0xa4, 0xaa, 0xb8, 0xb6, 0x0c, 0x02, 0x10, 0x1e, 0x34, 0x3a, 0x28, 0x26,
|
||||
0x7c, 0x72, 0x60, 0x6e, 0x44, 0x4a, 0x58, 0x56, 0x37, 0x39, 0x2b, 0x25,
|
||||
0x0f, 0x01, 0x13, 0x1d, 0x47, 0x49, 0x5b, 0x55, 0x7f, 0x71, 0x63, 0x6d,
|
||||
0xd7, 0xd9, 0xcb, 0xc5, 0xef, 0xe1, 0xf3, 0xfd, 0xa7, 0xa9, 0xbb, 0xb5,
|
||||
0x9f, 0x91, 0x83, 0x8d} };
|
||||
|
||||
/// circulant MDS matrix
|
||||
static const unsigned char CMDS[ 4 ][ 4 ] = {
|
||||
{2, 3, 1, 1}, {1, 2, 3, 1}, {1, 1, 2, 3}, {3, 1, 1, 2} };
|
||||
|
||||
/// Inverse circulant MDS matrix
|
||||
static const unsigned char INV_CMDS[ 4 ][ 4 ] = {
|
||||
{14, 11, 13, 9}, {9, 14, 11, 13}, {13, 9, 14, 11}, {11, 13, 9, 14} };
|
||||
354
Discovery Server/DiscoveryServer.cpp
Normal file
354
Discovery Server/DiscoveryServer.cpp
Normal file
@@ -0,0 +1,354 @@
|
||||
// ╔╗╔╔═╗╦═╗╦═╗╔═╗╔╦╗╦ ╦
|
||||
// ║║║║ ║╠╦╝╠╦╝╠═╣ ║ ╠═╣
|
||||
// ╝╚╝╚═╝╩╚═╩╚═╩ ╩ ╩ ╩ ╩
|
||||
// ╔╦╗╦╔═╗╔═╗╔═╗╦ ╦╔═╗╦═╗╦ ╦ ╔═╗╔═╗╦═╗╦ ╦╔═╗╦═╗
|
||||
// ║║║╚═╗║ ║ ║╚╗╔╝║╣ ╠╦╝╚╦╝ ╚═╗║╣ ╠╦╝╚╗╔╝║╣ ╠╦╝
|
||||
// ═╩╝╩╚═╝╚═╝╚═╝ ╚╝ ╚═╝╩╚═ ╩ ╚═╝╚═╝╩╚═ ╚╝ ╚═╝╩╚═
|
||||
|
||||
|
||||
#include "../global_define.h"
|
||||
#include "../Lobby Server/Event/NotifyClientReqConnect.h"
|
||||
|
||||
DiscoveryServer::DiscoveryServer()
|
||||
{
|
||||
m_running = false;
|
||||
|
||||
m_sessionMap.clear();
|
||||
m_recvBuffer.resize( 1024 );
|
||||
}
|
||||
|
||||
DiscoveryServer::~DiscoveryServer()
|
||||
{
|
||||
Log::Info( "Discovery Server stopped." );
|
||||
}
|
||||
|
||||
void DiscoveryServer::Start( std::string ip, int32_t port )
|
||||
{
|
||||
if( false == OpenDiscoverySocket( ip, port ) )
|
||||
{
|
||||
Log::Error( "Failed to open discovery socket." );
|
||||
return;
|
||||
}
|
||||
|
||||
m_running = true;
|
||||
m_thread = std::thread( &DiscoveryServer::Run, this );
|
||||
|
||||
Log::Info( "Discovery Server started %s:%d", ip.c_str(), port );
|
||||
}
|
||||
|
||||
void DiscoveryServer::Stop()
|
||||
{
|
||||
m_running = false;
|
||||
if( m_thread.joinable() )
|
||||
{
|
||||
m_thread.join();
|
||||
}
|
||||
}
|
||||
|
||||
void DiscoveryServer::Run()
|
||||
{
|
||||
FD_SET readSet;
|
||||
FD_SET writeSet;
|
||||
|
||||
timeval timeout = { 0, 1000 };
|
||||
|
||||
m_timer.Start();
|
||||
|
||||
while( m_running )
|
||||
{
|
||||
FD_ZERO( &readSet );
|
||||
FD_ZERO( &writeSet );
|
||||
|
||||
FD_SET( m_socket->fd, &readSet );
|
||||
|
||||
auto it = m_sessionMap.begin();
|
||||
for( it; it != m_sessionMap.end(); )
|
||||
{
|
||||
auto &socket = it->second->m_socket;
|
||||
|
||||
//if( ::GetTickCount64() - socket->last_recv_time > 5001 )
|
||||
//{
|
||||
// Log::Debug( "Disconnect timeout socket." );
|
||||
// it = m_sessionMap.erase( it );
|
||||
// continue;
|
||||
//}
|
||||
|
||||
if( socket->m_pendingWriteQueue.empty() )
|
||||
{
|
||||
it++;
|
||||
continue;
|
||||
}
|
||||
|
||||
FD_SET( socket->fd, &writeSet );
|
||||
|
||||
it++;
|
||||
}
|
||||
|
||||
auto result = select( 0, &readSet, &writeSet, nullptr, &timeout );
|
||||
|
||||
if( result == SOCKET_ERROR )
|
||||
{
|
||||
std::this_thread::sleep_for( std::chrono::milliseconds( 1 ) );
|
||||
break;
|
||||
}
|
||||
|
||||
if( FD_ISSET( m_socket->fd, &readSet ) )
|
||||
{
|
||||
ReadSocket( m_socket );
|
||||
}
|
||||
|
||||
for( auto &session : m_sessionMap )
|
||||
{
|
||||
auto &socket = session.second->m_socket;
|
||||
|
||||
if( FD_ISSET( socket->fd, &writeSet ) )
|
||||
{
|
||||
WriteSocket( socket );
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
bool DiscoveryServer::OpenDiscoverySocket( std::string ip, int32_t port )
|
||||
{
|
||||
SOCKET fd = socket( AF_INET, SOCK_DGRAM, 0 );
|
||||
|
||||
if( fd == INVALID_SOCKET )
|
||||
{
|
||||
Log::Error( "Failed to create socket." );
|
||||
return false;
|
||||
}
|
||||
|
||||
sockaddr_in serverInfo;
|
||||
serverInfo.sin_family = AF_INET;
|
||||
serverInfo.sin_addr.s_addr = inet_addr( ip.c_str() );
|
||||
serverInfo.sin_port = htons( port );
|
||||
|
||||
if( bind( fd, ( SOCKADDR * )&serverInfo, sizeof( serverInfo ) ) == SOCKET_ERROR )
|
||||
{
|
||||
Log::Error( "Failed to bind socket." );
|
||||
closesocket( fd );
|
||||
return false;
|
||||
}
|
||||
|
||||
m_socket = std::make_shared< RealmUDPSocket >();
|
||||
m_socket->local_address = serverInfo;
|
||||
|
||||
m_socket->fd = fd;
|
||||
m_socket->port = port;
|
||||
m_socket->flag.is_listener = true;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
sptr_udp_socket DiscoveryServer::CreateDiscoverySocket( sockaddr_in *remoteAddr )
|
||||
{
|
||||
auto socket = std::make_shared< RealmUDPSocket >();
|
||||
|
||||
socket->fd = m_socket->fd;
|
||||
|
||||
socket->local_address = m_socket->local_address;
|
||||
socket->remote_address = *remoteAddr;
|
||||
socket->peer_ip_address = inet_ntoa( remoteAddr->sin_addr );
|
||||
socket->peer_port = ntohs( remoteAddr->sin_port );
|
||||
|
||||
socket->last_recv_time = ::GetTickCount64();
|
||||
|
||||
Log::Debug( "Create new session socket for %s:%d", socket->peer_ip_address.c_str(), socket->peer_port );
|
||||
|
||||
return socket;
|
||||
}
|
||||
|
||||
sptr_discovery_record DiscoveryServer::CreateNewDiscoveryRecord( sockaddr_in *clientAddr, std::wstring sessionId )
|
||||
{
|
||||
auto user = RealmUserManager::Get().GetUser( sessionId );
|
||||
|
||||
if( user == nullptr )
|
||||
{
|
||||
Log::Error( "User not found for discovery! [%S]", sessionId.c_str() );
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
auto discoverySocket = CreateDiscoverySocket( clientAddr );
|
||||
auto session = DiscoverySession::Create( discoverySocket );
|
||||
|
||||
session->m_ownerSessionId = sessionId;
|
||||
|
||||
m_sessionMap[ *clientAddr ] = session;
|
||||
|
||||
user->m_discoverySocket = discoverySocket;
|
||||
|
||||
Log::Debug( "Create new session for %s:%d", inet_ntoa( clientAddr->sin_addr ), clientAddr->sin_port );
|
||||
|
||||
return session;
|
||||
}
|
||||
|
||||
sptr_discovery_record DiscoveryServer::GetDiscoveryRecord( sockaddr_in *clientAddr )
|
||||
{
|
||||
auto it = m_sessionMap.find( *clientAddr );
|
||||
|
||||
if( it == m_sessionMap.end() )
|
||||
{
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
return it->second;
|
||||
}
|
||||
|
||||
std::wstring DiscoveryServer::GetSessionId( sptr_byte_stream stream )
|
||||
{
|
||||
uint32_t length = stream->read<uint32_t>();
|
||||
auto decryptedBuffer = stream->read_encrypted_bytes( length );
|
||||
|
||||
std::wstring sessionId( length, '\0' );
|
||||
|
||||
std::memcpy( sessionId.data(), decryptedBuffer.data(), decryptedBuffer.size() );
|
||||
|
||||
return sessionId;
|
||||
}
|
||||
|
||||
|
||||
void DiscoveryServer::AcceptNewClient( sockaddr_in *clientAddr, sptr_byte_stream stream )
|
||||
{
|
||||
if( stream->data.size() < 36 )
|
||||
{
|
||||
Log::Error( "Invalid Discovery Handshake Packet" );
|
||||
return;
|
||||
}
|
||||
|
||||
//auto sessionId = GetSessionId( stream );
|
||||
auto sessionId = stream->read_encrypted_utf16( false );
|
||||
|
||||
if( sessionId.empty() || sessionId.size() != 16 )
|
||||
{
|
||||
Log::Error( "Invalid session id." );
|
||||
return;
|
||||
}
|
||||
|
||||
auto record = CreateNewDiscoveryRecord( clientAddr, sessionId );
|
||||
|
||||
if( record == nullptr )
|
||||
{
|
||||
Log::Error( "Failed to create new discovery record." );
|
||||
return;
|
||||
}
|
||||
|
||||
auto game = GameSessionManager::Get().FindGame( 0 );
|
||||
auto &roomOwner = game->m_userList[ 0 ];
|
||||
|
||||
auto ipAddr = inet_ntoa( clientAddr->sin_addr );
|
||||
auto port = ntohs( clientAddr->sin_port );
|
||||
|
||||
NotifyClientRequestConnect msg( ipAddr, port );
|
||||
roomOwner->m_realmSocket->send( msg.Serialize() );
|
||||
//auto &discovery = DiscoveryServer::Get();
|
||||
|
||||
SendDiscoveryClientHandshake( record, stream );
|
||||
}
|
||||
|
||||
void DiscoveryServer::ReadSocket( sptr_udp_socket socket )
|
||||
{
|
||||
if( socket->flag.disconnected )
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
sockaddr_in clientAddr;
|
||||
int clientAddrLen = sizeof( clientAddr );
|
||||
|
||||
// Receive data from the client
|
||||
auto bytesReceived = recvfrom( socket->fd, ( char * )m_recvBuffer.data(), ( int )m_recvBuffer.size(), 0, ( struct sockaddr * )&clientAddr, &clientAddrLen );
|
||||
|
||||
if( bytesReceived == SOCKET_ERROR )
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
if( bytesReceived == 0 )
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
if( bytesReceived > 0 )
|
||||
{
|
||||
socket->last_recv_time = m_timer.GetElapsedTimeMilliseconds();
|
||||
|
||||
HandleRequest( &clientAddr, std::make_shared< ByteStream >( m_recvBuffer.data(), bytesReceived ) );
|
||||
}
|
||||
}
|
||||
|
||||
void DiscoveryServer::WriteSocket( sptr_udp_socket socket )
|
||||
{
|
||||
if( socket->flag.disconnected )
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
if( socket->m_pendingWriteQueue.empty() )
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
auto &stream = socket->m_pendingWriteQueue.front();
|
||||
|
||||
auto bytesSent = sendto( socket->fd, ( char * )stream->data.data(), ( int )stream->data.size(), 0, ( struct sockaddr * )&socket->remote_address, sizeof(socket->remote_address));
|
||||
|
||||
if( bytesSent == SOCKET_ERROR )
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
if( bytesSent > 0 )
|
||||
{
|
||||
socket->m_pendingWriteQueue.pop();
|
||||
}
|
||||
}
|
||||
|
||||
void DiscoveryServer::HandleRequest( sockaddr_in *clientAddr, sptr_byte_stream stream )
|
||||
{
|
||||
auto record = GetDiscoveryRecord( clientAddr );
|
||||
|
||||
if( record == nullptr )
|
||||
{
|
||||
AcceptNewClient( clientAddr, stream );
|
||||
return;
|
||||
}
|
||||
|
||||
auto remoteIp = inet_ntoa( clientAddr->sin_addr );
|
||||
auto remotePort = clientAddr->sin_port;
|
||||
|
||||
Log::Debug( "%s:%d", remoteIp, remotePort );
|
||||
|
||||
auto state = stream->read_u32();
|
||||
|
||||
switch( state )
|
||||
{
|
||||
case 12:
|
||||
{
|
||||
SendDiscoveryClientPing( record, stream );
|
||||
} break;
|
||||
|
||||
default:
|
||||
{
|
||||
Log::Packet( stream->data, stream->data.size(), false );
|
||||
} break;
|
||||
}
|
||||
}
|
||||
|
||||
void DiscoveryServer::SendDiscoveryClientHandshake( sptr_discovery_record record, sptr_byte_stream stream )
|
||||
{
|
||||
ByteStream response;
|
||||
response.write_u8( 0x07 ); // REALM_SERVER_CLIENT_INITIAL
|
||||
|
||||
record->m_socket->send( response );
|
||||
}
|
||||
|
||||
void DiscoveryServer::SendDiscoveryClientPing( sptr_discovery_record record, sptr_byte_stream stream )
|
||||
{
|
||||
auto remoteIp = stream->read_sz_utf8();
|
||||
|
||||
ByteStream response;
|
||||
response.write_u8( 0x08 ); // REALM_SERVER_CLIENT_PING
|
||||
|
||||
record->m_socket->send( response );
|
||||
}
|
||||
63
Discovery Server/DiscoveryServer.h
Normal file
63
Discovery Server/DiscoveryServer.h
Normal file
@@ -0,0 +1,63 @@
|
||||
#pragma once
|
||||
|
||||
#include <memory>
|
||||
#include <array>
|
||||
#include <unordered_map>
|
||||
|
||||
#include "DiscoverySession.h"
|
||||
|
||||
class DiscoveryServer
|
||||
{
|
||||
public:
|
||||
static DiscoveryServer& Get()
|
||||
{
|
||||
std::lock_guard< std::mutex > lock( m_mutex );
|
||||
if( m_instance == nullptr )
|
||||
{
|
||||
m_instance.reset( new DiscoveryServer() );
|
||||
}
|
||||
|
||||
return *m_instance;
|
||||
}
|
||||
|
||||
DiscoveryServer( const DiscoveryServer & ) = delete;
|
||||
DiscoveryServer &operator=( const DiscoveryServer & ) = delete;
|
||||
DiscoveryServer();
|
||||
~DiscoveryServer();
|
||||
|
||||
void Start( std::string ip, int32_t port );
|
||||
void Stop();
|
||||
void Run();
|
||||
bool isRunning() const
|
||||
{
|
||||
return m_running;
|
||||
}
|
||||
|
||||
private:
|
||||
bool OpenDiscoverySocket( std::string ip, int32_t port );
|
||||
sptr_udp_socket CreateDiscoverySocket( sockaddr_in *clientAddr );
|
||||
|
||||
sptr_discovery_record CreateNewDiscoveryRecord( sockaddr_in *clientAddr, std::wstring sessionId );
|
||||
sptr_discovery_record GetDiscoveryRecord( sockaddr_in *clientAddr );
|
||||
std::wstring GetSessionId( sptr_byte_stream stream );
|
||||
|
||||
void AcceptNewClient( sockaddr_in *clientAddr, sptr_byte_stream stream );
|
||||
void ReadSocket( sptr_udp_socket socket );
|
||||
void WriteSocket( sptr_udp_socket socket );
|
||||
void HandleRequest( sockaddr_in *clientAddr, sptr_byte_stream stream );
|
||||
|
||||
void SendDiscoveryClientHandshake( sptr_discovery_record record, sptr_byte_stream stream );
|
||||
void SendDiscoveryClientPing( sptr_discovery_record record, sptr_byte_stream stream );
|
||||
|
||||
private:
|
||||
static inline std::unique_ptr< DiscoveryServer > m_instance;
|
||||
static inline std::mutex m_mutex;
|
||||
Timer m_timer;
|
||||
|
||||
std::atomic< bool > m_running;
|
||||
std::thread m_thread;
|
||||
|
||||
sptr_udp_socket m_socket;
|
||||
std::vector< uint8_t > m_recvBuffer;
|
||||
std::unordered_map< sockaddr_in, sptr_discovery_record, sockaddr_in_hash, sockaddr_in_equal> m_sessionMap;
|
||||
};
|
||||
21
Discovery Server/DiscoverySession.cpp
Normal file
21
Discovery Server/DiscoverySession.cpp
Normal file
@@ -0,0 +1,21 @@
|
||||
#include "../global_define.h"
|
||||
#include "DiscoverySession.h"
|
||||
|
||||
DiscoverySession::DiscoverySession( sptr_udp_socket socket )
|
||||
{
|
||||
m_socket = socket;
|
||||
m_ownerSessionId = L"";
|
||||
m_userSessionIds.fill( L"" );
|
||||
}
|
||||
|
||||
DiscoverySession::~DiscoverySession()
|
||||
{
|
||||
m_socket.reset();
|
||||
m_ownerSessionId.clear();
|
||||
m_userSessionIds.fill( L"" );
|
||||
}
|
||||
|
||||
sptr_discovery_record DiscoverySession::Create( sptr_udp_socket socket )
|
||||
{
|
||||
return std::shared_ptr<DiscoverySession>( new DiscoverySession( socket ) );
|
||||
}
|
||||
39
Discovery Server/DiscoverySession.h
Normal file
39
Discovery Server/DiscoverySession.h
Normal file
@@ -0,0 +1,39 @@
|
||||
#pragma once
|
||||
|
||||
#include <memory>
|
||||
#include <array>
|
||||
#include <string>
|
||||
|
||||
struct sockaddr_in_hash {
|
||||
std::size_t operator()( const sockaddr_in &addr ) const
|
||||
{
|
||||
std::size_t h1 = std::hash<int>()( addr.sin_family );
|
||||
std::size_t h2 = std::hash<unsigned short>()( addr.sin_port );
|
||||
std::size_t h3 = std::hash<unsigned long>()( addr.sin_addr.s_addr );
|
||||
return h1 ^ ( h2 << 1 ) ^ ( h3 << 2 );
|
||||
}
|
||||
};
|
||||
|
||||
struct sockaddr_in_equal {
|
||||
bool operator()( const sockaddr_in &lhs, const sockaddr_in &rhs ) const
|
||||
{
|
||||
return lhs.sin_family == rhs.sin_family &&
|
||||
lhs.sin_port == rhs.sin_port &&
|
||||
lhs.sin_addr.s_addr == rhs.sin_addr.s_addr;
|
||||
}
|
||||
};
|
||||
|
||||
class DiscoverySession {
|
||||
public:
|
||||
static std::shared_ptr< DiscoverySession > Create( sptr_udp_socket socket );
|
||||
|
||||
DiscoverySession( sptr_udp_socket socket );
|
||||
~DiscoverySession();
|
||||
|
||||
sptr_udp_socket m_socket;
|
||||
std::wstring m_ownerSessionId;
|
||||
|
||||
std::array< std::wstring, 4 > m_userSessionIds;
|
||||
};
|
||||
|
||||
typedef std::shared_ptr< DiscoverySession > sptr_discovery_record;
|
||||
0
Game/GameSession.cpp
Normal file
0
Game/GameSession.cpp
Normal file
51
Game/GameSession.h
Normal file
51
Game/GameSession.h
Normal file
@@ -0,0 +1,51 @@
|
||||
#pragma once
|
||||
|
||||
#include <string>
|
||||
#include <array>
|
||||
|
||||
class GameSession {
|
||||
public:
|
||||
GameSession()
|
||||
{
|
||||
m_gameIndex = 0;
|
||||
m_minimumLevel = 0;
|
||||
m_maximumLevel = 0;
|
||||
|
||||
m_hostSessionId = L"";
|
||||
m_gameName = L"";
|
||||
|
||||
m_userList.fill( nullptr );
|
||||
}
|
||||
|
||||
~GameSession()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
sptr_user GetOwner()
|
||||
{
|
||||
return m_userList[ 0 ];
|
||||
}
|
||||
|
||||
sptr_user GetUser( const size_t index )
|
||||
{
|
||||
if( index < 0 || index >= m_userList.size() )
|
||||
{
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
return m_userList[ index ];
|
||||
}
|
||||
|
||||
int32_t m_gameIndex;
|
||||
std::wstring m_hostSessionId;
|
||||
std::wstring m_gameName;
|
||||
|
||||
int32_t m_minimumLevel;
|
||||
int32_t m_maximumLevel;
|
||||
|
||||
// User Information
|
||||
std::array< sptr_user, 4 > m_userList;
|
||||
};
|
||||
|
||||
typedef std::shared_ptr< GameSession > sptr_game_session;
|
||||
165
Game/GameSessionManager.cpp
Normal file
165
Game/GameSessionManager.cpp
Normal file
@@ -0,0 +1,165 @@
|
||||
#include "../global_define.h"
|
||||
#include "GameSessionManager.h"
|
||||
|
||||
GameSessionManager::GameSessionManager()
|
||||
{
|
||||
m_gameIndex = 0;
|
||||
m_publicGameSessionList.clear();
|
||||
m_privateGameSessionList.clear();
|
||||
}
|
||||
|
||||
GameSessionManager::~GameSessionManager()
|
||||
{
|
||||
}
|
||||
|
||||
bool GameSessionManager::CreatePublicGameSession( sptr_user owner, std::wstring gameName, int32_t minimumLevel, int32_t maximumLevel )
|
||||
{
|
||||
auto &hostSessionId = owner->m_sessionId;
|
||||
|
||||
// Check if the host session id is already in use
|
||||
for( auto &gameSession : m_publicGameSessionList )
|
||||
{
|
||||
if( gameSession->m_hostSessionId == hostSessionId )
|
||||
{
|
||||
Log::Error( "Host session id is already in use! [%S]", hostSessionId.c_str() );
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
auto new_session = std::make_shared< GameSession >();
|
||||
|
||||
new_session->m_gameIndex = m_gameIndex++;
|
||||
new_session->m_hostSessionId = hostSessionId;
|
||||
new_session->m_gameName = gameName;
|
||||
new_session->m_minimumLevel = minimumLevel;
|
||||
new_session->m_maximumLevel = maximumLevel;
|
||||
|
||||
new_session->m_userList[ 0 ] = owner;
|
||||
|
||||
m_publicGameSessionList.push_back( new_session );
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool GameSessionManager::CreatePrivateGameSession( sptr_user owner, std::wstring gameName, int32_t minimumLevel, int32_t maximumLevel )
|
||||
{
|
||||
auto &hostSessionId = owner->m_sessionId;
|
||||
|
||||
// Check if the game name or host session id is already in use
|
||||
for( auto &gameSession : m_privateGameSessionList )
|
||||
{
|
||||
if( gameSession->m_hostSessionId == hostSessionId )
|
||||
{
|
||||
Log::Error( "Host session id is already in use! [%S]", hostSessionId.c_str() );
|
||||
return false;
|
||||
}
|
||||
|
||||
if( gameSession->m_gameName == gameName )
|
||||
{
|
||||
Log::Error( "Game name is already in use! [%S]", gameName.c_str() );
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
auto new_session = std::make_shared< GameSession >();
|
||||
|
||||
new_session->m_gameIndex = -1;
|
||||
new_session->m_hostSessionId = hostSessionId;
|
||||
new_session->m_gameName = gameName;
|
||||
new_session->m_minimumLevel = minimumLevel;
|
||||
new_session->m_maximumLevel = maximumLevel;
|
||||
|
||||
m_privateGameSessionList.push_back( new_session );
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
bool GameSessionManager::UpdateGameSessionDiscovery( sptr_user owner, std::string hostIp, int32_t hostPort )
|
||||
{
|
||||
auto &hostSessionId = owner->m_sessionId;
|
||||
|
||||
for( auto &gameSession : m_publicGameSessionList )
|
||||
{
|
||||
if( gameSession->m_hostSessionId == hostSessionId )
|
||||
{
|
||||
//gameSession->m_hostIp = hostIp;
|
||||
//gameSession->m_hostPort = hostPort;
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
for( auto &gameSession : m_privateGameSessionList )
|
||||
{
|
||||
if( gameSession->m_hostSessionId == hostSessionId )
|
||||
{
|
||||
//gameSession->m_hostIp = hostIp;
|
||||
//gameSession->m_hostPort = hostPort;
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
Log::Error( "Failed to update game session discovery information! [%S]", hostSessionId.c_str() );
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
sptr_game_session GameSessionManager::FindGame( const std::wstring sessionId )
|
||||
{
|
||||
for( auto &gameSession : m_publicGameSessionList )
|
||||
{
|
||||
if( gameSession->m_hostSessionId == sessionId )
|
||||
{
|
||||
return gameSession;
|
||||
}
|
||||
}
|
||||
|
||||
for( auto &gameSession : m_privateGameSessionList )
|
||||
{
|
||||
if( gameSession->m_hostSessionId == sessionId )
|
||||
{
|
||||
return gameSession;
|
||||
}
|
||||
}
|
||||
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
sptr_game_session GameSessionManager::FindGame( const int32_t gameId )
|
||||
{
|
||||
for( auto &gameSession : m_publicGameSessionList )
|
||||
{
|
||||
if( gameSession->m_gameIndex == gameId )
|
||||
{
|
||||
return gameSession;
|
||||
}
|
||||
}
|
||||
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
bool GameSessionManager::UserJoinGame( const int32_t gameId, sptr_user joiningUser )
|
||||
{
|
||||
auto gameSession = FindGame( gameId );
|
||||
|
||||
if( gameSession == nullptr )
|
||||
{
|
||||
Log::Error( "Game session not found! [%d]", gameId );
|
||||
return false;
|
||||
}
|
||||
|
||||
for( auto &user : gameSession->m_userList )
|
||||
{
|
||||
if( user == nullptr )
|
||||
{
|
||||
user = joiningUser;
|
||||
user->m_state = RealmUser::UserState::JoinPending;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
Log::Error( "Game session is full! [%d]", gameId );
|
||||
|
||||
return false;
|
||||
}
|
||||
41
Game/GameSessionManager.h
Normal file
41
Game/GameSessionManager.h
Normal file
@@ -0,0 +1,41 @@
|
||||
#pragma once
|
||||
|
||||
#include "GameSession.h"
|
||||
|
||||
class GameSessionManager {
|
||||
private:
|
||||
static inline std::unique_ptr< GameSessionManager > m_instance;
|
||||
static inline std::mutex m_mutex;
|
||||
|
||||
int32_t m_gameIndex;
|
||||
std::vector< sptr_game_session > m_publicGameSessionList;
|
||||
std::vector< sptr_game_session > m_privateGameSessionList;
|
||||
|
||||
public:
|
||||
GameSessionManager();
|
||||
~GameSessionManager();
|
||||
GameSessionManager( const GameSessionManager & ) = delete;
|
||||
GameSessionManager &operator=( const GameSessionManager & ) = delete;
|
||||
|
||||
static GameSessionManager& Get()
|
||||
{
|
||||
std::lock_guard< std::mutex > lock( m_mutex );
|
||||
if( m_instance == nullptr )
|
||||
{
|
||||
m_instance.reset( new GameSessionManager() );
|
||||
}
|
||||
|
||||
return *m_instance;
|
||||
}
|
||||
|
||||
public:
|
||||
|
||||
bool CreatePublicGameSession( sptr_user owner, std::wstring gameName, int32_t minimumLevel, int32_t maximumLevel );
|
||||
bool CreatePrivateGameSession( sptr_user owner, std::wstring gameName, int32_t minimumLevel, int32_t maximumLevel );
|
||||
bool UpdateGameSessionDiscovery( sptr_user owner, std::string hostIp, int32_t hostPort );
|
||||
|
||||
sptr_game_session FindGame( const std::wstring sessionId );
|
||||
sptr_game_session FindGame( const int32_t gameId );
|
||||
|
||||
bool UserJoinGame( const int32_t gameId, sptr_user joiningUser );
|
||||
};
|
||||
28
Game/RealmUser.cpp
Normal file
28
Game/RealmUser.cpp
Normal file
@@ -0,0 +1,28 @@
|
||||
#include "../global_define.h"
|
||||
|
||||
RealmUser::RealmUser()
|
||||
{
|
||||
m_state = UserState::MainMenu;
|
||||
m_realmSocket = nullptr;
|
||||
m_discoverySocket = nullptr;
|
||||
m_sessionId = L"";
|
||||
}
|
||||
|
||||
RealmUser::~RealmUser()
|
||||
{
|
||||
m_state = UserState::MainMenu;
|
||||
|
||||
if( m_realmSocket )
|
||||
{
|
||||
m_realmSocket->flag.disconnected = true;
|
||||
m_realmSocket.reset();
|
||||
}
|
||||
|
||||
if( m_discoverySocket )
|
||||
{
|
||||
m_discoverySocket->flag.disconnected = true;
|
||||
m_discoverySocket.reset();
|
||||
}
|
||||
|
||||
m_sessionId = L"";
|
||||
}
|
||||
20
Game/RealmUser.h
Normal file
20
Game/RealmUser.h
Normal file
@@ -0,0 +1,20 @@
|
||||
#pragma once
|
||||
|
||||
class RealmUser {
|
||||
public:
|
||||
RealmUser();
|
||||
~RealmUser();
|
||||
|
||||
enum class UserState {
|
||||
MainMenu,
|
||||
JoinPending,
|
||||
InGameLobby,
|
||||
InGameSession,
|
||||
} m_state;
|
||||
|
||||
sptr_tcp_socket m_realmSocket;
|
||||
sptr_udp_socket m_discoverySocket;
|
||||
std::wstring m_sessionId;
|
||||
};
|
||||
|
||||
typedef std::shared_ptr< RealmUser > sptr_user;
|
||||
105
Game/RealmUserManager.cpp
Normal file
105
Game/RealmUserManager.cpp
Normal file
@@ -0,0 +1,105 @@
|
||||
#include "../global_define.h"
|
||||
|
||||
RealmUserManager::RealmUserManager()
|
||||
{
|
||||
m_users.clear();
|
||||
}
|
||||
|
||||
RealmUserManager::~RealmUserManager()
|
||||
{
|
||||
}
|
||||
|
||||
std::wstring RealmUserManager::GenerateSessionId()
|
||||
{
|
||||
// TODO : Use something better than rand()
|
||||
std::wstring sessionId;
|
||||
for( int i = 0; i < MAX_SESSION_ID_LENGTH; i++ )
|
||||
{
|
||||
sessionId += L"0123456789ABCDEF"[ rand() % 16 ];
|
||||
}
|
||||
|
||||
return sessionId;
|
||||
}
|
||||
|
||||
sptr_user RealmUserManager::CreateUser( sptr_tcp_socket socket, std::wstring userId, std::wstring userPw )
|
||||
{
|
||||
Log::Debug( "ClientManager::CreateUser() - Created new user" );
|
||||
|
||||
auto user = std::make_shared< RealmUser >();
|
||||
|
||||
user->m_sessionId = GenerateSessionId();
|
||||
user->m_realmSocket = socket;
|
||||
|
||||
m_users.push_back( user );
|
||||
|
||||
return user;
|
||||
}
|
||||
|
||||
void RealmUserManager::RemoveUser( sptr_user user )
|
||||
{
|
||||
auto it = std::find( m_users.begin(), m_users.end(), user );
|
||||
if( it == m_users.end() )
|
||||
{
|
||||
Log::Error( "RemoveUser : [%S] not found", user->m_sessionId.c_str() );
|
||||
return;
|
||||
}
|
||||
|
||||
Log::Debug( "RemoveUser : [%S]", user->m_sessionId.c_str() );
|
||||
m_users.erase( it );
|
||||
}
|
||||
|
||||
void RealmUserManager::RemoveUser( const std::wstring &sessionId )
|
||||
{
|
||||
auto it = std::find_if( m_users.begin(), m_users.end(), [ &sessionId ]( sptr_user user )
|
||||
{
|
||||
return user->m_sessionId == sessionId;
|
||||
} );
|
||||
|
||||
if( it == m_users.end() )
|
||||
{
|
||||
Log::Error( "RemoveUser : [%S] not found", sessionId.c_str() );
|
||||
return;
|
||||
}
|
||||
|
||||
Log::Debug( "RemoveUser : [%S]", sessionId.c_str() );
|
||||
m_users.erase( it );
|
||||
}
|
||||
|
||||
sptr_user RealmUserManager::GetUser( const std::wstring &sessionId )
|
||||
{
|
||||
for( auto &user : m_users )
|
||||
{
|
||||
if( user->m_sessionId == sessionId )
|
||||
{
|
||||
return user;
|
||||
}
|
||||
}
|
||||
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
sptr_user RealmUserManager::GetUser( sptr_tcp_socket socket )
|
||||
{
|
||||
for( auto &user : m_users )
|
||||
{
|
||||
if( user->m_realmSocket == socket )
|
||||
{
|
||||
return user;
|
||||
}
|
||||
}
|
||||
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
sptr_user RealmUserManager::GetUser( sptr_udp_socket socket )
|
||||
{
|
||||
for( auto &user : m_users )
|
||||
{
|
||||
if( user->m_discoverySocket == socket )
|
||||
{
|
||||
return user;
|
||||
}
|
||||
}
|
||||
|
||||
return nullptr;
|
||||
}
|
||||
40
Game/RealmUserManager.h
Normal file
40
Game/RealmUserManager.h
Normal file
@@ -0,0 +1,40 @@
|
||||
#pragma once
|
||||
|
||||
#include "RealmUser.h"
|
||||
|
||||
class RealmUserManager {
|
||||
private:
|
||||
static const int MAX_SESSION_ID_LENGTH = 16;
|
||||
static inline std::unique_ptr< RealmUserManager > m_instance;
|
||||
static inline std::mutex m_mutex;
|
||||
|
||||
std::vector< sptr_user > m_users;
|
||||
public:
|
||||
static RealmUserManager& Get()
|
||||
{
|
||||
std::lock_guard< std::mutex > lock( m_mutex );
|
||||
if( m_instance == nullptr )
|
||||
{
|
||||
m_instance.reset( new RealmUserManager() );
|
||||
}
|
||||
|
||||
return *m_instance;
|
||||
}
|
||||
|
||||
RealmUserManager( const RealmUserManager & ) = delete;
|
||||
RealmUserManager &operator=( const RealmUserManager & ) = delete;
|
||||
RealmUserManager();
|
||||
~RealmUserManager();
|
||||
|
||||
public:
|
||||
static std::wstring GenerateSessionId();
|
||||
sptr_user CreateUser( sptr_tcp_socket socket, std::wstring userId, std::wstring userPw );
|
||||
void RemoveUser( sptr_user user );
|
||||
void RemoveUser( const std::wstring &sessionId );
|
||||
|
||||
sptr_user GetUser( const std::wstring &sessionId );
|
||||
sptr_user GetUser( sptr_tcp_socket socket );
|
||||
sptr_user GetUser( sptr_udp_socket socket );
|
||||
|
||||
private:
|
||||
};
|
||||
17
Gateway Server/EventHandlers/GatewayEvents.h
Normal file
17
Gateway Server/EventHandlers/GatewayEvents.h
Normal file
@@ -0,0 +1,17 @@
|
||||
#pragma once
|
||||
|
||||
#include <map>
|
||||
#include <functional>
|
||||
#include <memory>
|
||||
|
||||
#include "../../Network/GenericNetRequest.hpp"
|
||||
#include "GetServerAddressEvent.h"
|
||||
|
||||
const std::map< int16_t, std::function< std::unique_ptr< GenericRequest >() > > LOBBY_EVENT_LOOKUP =
|
||||
{
|
||||
{ 0x43, []() -> std::unique_ptr< GenericRequest >
|
||||
{
|
||||
return std::make_unique< RequestGetServerAddress >();
|
||||
}
|
||||
}
|
||||
};
|
||||
32
Gateway Server/EventHandlers/GetServerAddressEvent.cpp
Normal file
32
Gateway Server/EventHandlers/GetServerAddressEvent.cpp
Normal file
@@ -0,0 +1,32 @@
|
||||
#include "../../global_define.h"
|
||||
#include "GetServerAddressEvent.h"
|
||||
|
||||
void RequestGetServerAddress::Deserialize( sptr_tcp_socket socket, sptr_byte_stream stream )
|
||||
{
|
||||
DeserializeHeader( stream );
|
||||
}
|
||||
|
||||
sptr_generic_response RequestGetServerAddress::ProcessRequest( sptr_tcp_socket socket, sptr_byte_stream stream )
|
||||
{
|
||||
Deserialize( socket, stream );
|
||||
|
||||
return std::make_shared< ResultGetServerAddress >( this, "192.168.1.248", 40810 );
|
||||
}
|
||||
|
||||
ResultGetServerAddress::ResultGetServerAddress( GenericRequest *request, std::string ip, int32_t port ) : GenericResponse( *request )
|
||||
{
|
||||
m_ip = ip;
|
||||
m_port = port;
|
||||
}
|
||||
|
||||
ByteStream& ResultGetServerAddress::Serialize()
|
||||
{
|
||||
m_stream.write_u16( m_packetId );
|
||||
m_stream.write_u32( m_requestId );
|
||||
m_stream.write_u32( 0 );
|
||||
|
||||
m_stream.write_sz_utf8( m_ip );
|
||||
m_stream.write( m_port );
|
||||
|
||||
return m_stream;
|
||||
}
|
||||
20
Gateway Server/EventHandlers/GetServerAddressEvent.h
Normal file
20
Gateway Server/EventHandlers/GetServerAddressEvent.h
Normal file
@@ -0,0 +1,20 @@
|
||||
#pragma once
|
||||
|
||||
class RequestGetServerAddress : public GenericRequest {
|
||||
public:
|
||||
static std::unique_ptr< RequestGetServerAddress > Create()
|
||||
{
|
||||
return std::make_unique< RequestGetServerAddress >();
|
||||
}
|
||||
sptr_generic_response ProcessRequest( sptr_tcp_socket socket, sptr_byte_stream stream ) override;
|
||||
void Deserialize( sptr_tcp_socket socket, sptr_byte_stream stream ) override;
|
||||
};
|
||||
|
||||
class ResultGetServerAddress : public GenericResponse {
|
||||
public:
|
||||
std::string m_ip;
|
||||
int32_t m_port;
|
||||
|
||||
ResultGetServerAddress( GenericRequest *request, std::string ip, int32_t port );
|
||||
ByteStream &Serialize();
|
||||
};
|
||||
279
Gateway Server/GatewayServer.cpp
Normal file
279
Gateway Server/GatewayServer.cpp
Normal file
@@ -0,0 +1,279 @@
|
||||
// ╔╗╔╔═╗╦═╗╦═╗╔═╗╔╦╗╦ ╦
|
||||
// ║║║║ ║╠╦╝╠╦╝╠═╣ ║ ╠═╣
|
||||
// ╝╚╝╚═╝╩╚═╩╚═╩ ╩ ╩ ╩ ╩
|
||||
// ╔═╗╔═╗╔╦╗╔═╗╦ ╦╔═╗╦ ╦ ╔═╗╔═╗╦═╗╦ ╦╔═╗╦═╗
|
||||
// ║ ╦╠═╣ ║ ║╣ ║║║╠═╣╚╦╝ ╚═╗║╣ ╠╦╝╚╗╔╝║╣ ╠╦╝
|
||||
// ╚═╝╩ ╩ ╩ ╚═╝╚╩╝╩ ╩ ╩ ╚═╝╚═╝╩╚═ ╚╝ ╚═╝╩╚═
|
||||
|
||||
#include <map>
|
||||
#include <functional>
|
||||
#include "../global_define.h"
|
||||
|
||||
#include "GatewayServer.h"
|
||||
#include "EventHandlers/GatewayEvents.h"
|
||||
|
||||
typedef std::map< int16_t, std::function< std::unique_ptr< GenericRequest >() > > CommandMap;
|
||||
|
||||
const CommandMap GATEWAY_REQUEST_LOOKUP =
|
||||
{
|
||||
{ 0x43, []() -> std::unique_ptr< GenericRequest >
|
||||
{
|
||||
return std::make_unique< RequestGetServerAddress >();
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
GatewayServer::GatewayServer()
|
||||
{
|
||||
m_running = false;
|
||||
m_listenSocket = INVALID_SOCKET;
|
||||
|
||||
m_clientSockets.clear();
|
||||
m_recvBuffer.resize( 1024 );
|
||||
}
|
||||
|
||||
GatewayServer::~GatewayServer()
|
||||
{
|
||||
Log::Info( "Gateway Server stopped." );
|
||||
m_timer.Stop();
|
||||
}
|
||||
|
||||
void GatewayServer::Start( std::string ip, int32_t port )
|
||||
{
|
||||
m_listenSocket = ::WSASocket( AF_INET, SOCK_STREAM, IPPROTO_TCP, NULL, 0, WSA_FLAG_OVERLAPPED );
|
||||
if( m_listenSocket == INVALID_SOCKET )
|
||||
{
|
||||
Log::Error( "WSASocket() failed" );
|
||||
return;
|
||||
}
|
||||
|
||||
// Bind the socket
|
||||
sockaddr_in service;
|
||||
service.sin_family = AF_INET;
|
||||
service.sin_port = htons( port );
|
||||
service.sin_addr.s_addr = inet_addr( ip.c_str() );
|
||||
|
||||
|
||||
if( bind( m_listenSocket, ( SOCKADDR * )&service, sizeof( service ) ) == SOCKET_ERROR )
|
||||
{
|
||||
Log::Error( "bind() failed" );
|
||||
return;
|
||||
}
|
||||
|
||||
// Listen on the socket
|
||||
if( listen( m_listenSocket, SOMAXCONN ) == SOCKET_ERROR )
|
||||
{
|
||||
Log::Error( "listen() failed" );
|
||||
return;
|
||||
}
|
||||
|
||||
// Start the server
|
||||
m_running = true;
|
||||
m_thread = std::thread( &GatewayServer::Run, this );
|
||||
|
||||
Log::Info( "Gateway Server started on %s:%d", ip.c_str(), port );
|
||||
}
|
||||
|
||||
void GatewayServer::Stop()
|
||||
{
|
||||
m_running = false;
|
||||
if( m_thread.joinable() )
|
||||
{
|
||||
m_thread.join();
|
||||
}
|
||||
}
|
||||
|
||||
void GatewayServer::Run()
|
||||
{
|
||||
FD_SET readSet;
|
||||
FD_SET writeSet;
|
||||
FD_SET exceptSet;
|
||||
|
||||
timeval timeout = { 0, 1000 };
|
||||
|
||||
m_timer.Start();
|
||||
|
||||
while( m_running )
|
||||
{
|
||||
FD_ZERO( &readSet );
|
||||
FD_ZERO( &writeSet );
|
||||
FD_ZERO( &exceptSet );
|
||||
|
||||
FD_SET( m_listenSocket, &readSet );
|
||||
|
||||
// Process clients
|
||||
for( auto &client : m_clientSockets )
|
||||
{
|
||||
FD_SET( client->fd, &readSet );
|
||||
FD_SET( client->fd, &writeSet );
|
||||
FD_SET( client->fd, &exceptSet );
|
||||
}
|
||||
|
||||
auto result = select( 0, &readSet, &writeSet, &exceptSet, &timeout );
|
||||
|
||||
if( result == SOCKET_ERROR )
|
||||
{
|
||||
std::this_thread::sleep_for( std::chrono::milliseconds( 1 ) );
|
||||
break;
|
||||
}
|
||||
|
||||
if( FD_ISSET( m_listenSocket, &readSet ) )
|
||||
{
|
||||
AcceptNewClient();
|
||||
}
|
||||
|
||||
for( auto &client : m_clientSockets )
|
||||
{
|
||||
if( FD_ISSET( client->fd, &readSet ) )
|
||||
{
|
||||
ReadSocket( client );
|
||||
}
|
||||
|
||||
if( FD_ISSET( client->fd, &writeSet ) )
|
||||
{
|
||||
WriteSocket( client );
|
||||
}
|
||||
|
||||
if( FD_ISSET( client->fd, &exceptSet ) )
|
||||
{
|
||||
// Handle exception
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void GatewayServer::AcceptNewClient()
|
||||
{
|
||||
sockaddr_in clientInfo;
|
||||
int32_t addrSize = sizeof( clientInfo );
|
||||
|
||||
SOCKET clientSocket = accept( m_listenSocket, ( SOCKADDR * )&clientInfo, &addrSize );
|
||||
if( clientSocket == INVALID_SOCKET )
|
||||
{
|
||||
Log::Error( "accept() failed" );
|
||||
return;
|
||||
}
|
||||
|
||||
auto new_client = std::make_shared< RealmTCPSocket >();
|
||||
new_client->fd = clientSocket;
|
||||
new_client->remote_address = clientInfo;
|
||||
new_client->peer_ip_address = inet_ntoa( clientInfo.sin_addr );
|
||||
|
||||
m_clientSockets.push_back( new_client );
|
||||
|
||||
Log::Info( "[GATEWAY] New client connected : (%s)", new_client->peer_ip_address.c_str() );
|
||||
}
|
||||
|
||||
void GatewayServer::ReadSocket( sptr_tcp_socket socket )
|
||||
{
|
||||
if( socket->flag.disconnected )
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
auto bytesReceived = recv( socket->fd, ( char * )m_recvBuffer.data(), ( int )m_recvBuffer.size(), 0 );
|
||||
|
||||
if( bytesReceived == SOCKET_ERROR )
|
||||
{
|
||||
auto error = WSAGetLastError();
|
||||
Log::Info( "Socket Error [%d].", error );
|
||||
socket->flag.disconnected = true;
|
||||
return;
|
||||
}
|
||||
|
||||
if( bytesReceived == 0 )
|
||||
{
|
||||
Log::Info( "Socket Disconnected." );
|
||||
socket->flag.disconnected = true;
|
||||
return;
|
||||
}
|
||||
|
||||
// Append the received data to the sockets processing buffer.
|
||||
// There's definitely a more elegant way of handling data here,
|
||||
// but this is just easier for now.
|
||||
socket->m_pendingReadBuffer.insert( socket->m_pendingReadBuffer.end(), m_recvBuffer.begin(), m_recvBuffer.begin() + bytesReceived );
|
||||
|
||||
// Handle valid packets in the buffer.
|
||||
while( socket->m_pendingReadBuffer.size() > 0 )
|
||||
{
|
||||
auto packetSize = htonl( *( int32_t * )&socket->m_pendingReadBuffer[ 0 ] );
|
||||
|
||||
if( packetSize > socket->m_pendingReadBuffer.size() )
|
||||
{
|
||||
break;
|
||||
}
|
||||
|
||||
Log::Packet( socket->m_pendingReadBuffer, packetSize, false );
|
||||
|
||||
auto stream = std::make_shared< ByteStream >( socket->m_pendingReadBuffer.data() + 4, packetSize - 4 );
|
||||
|
||||
// Erase the packet from the buffer
|
||||
socket->m_pendingReadBuffer.erase( socket->m_pendingReadBuffer.begin(), socket->m_pendingReadBuffer.begin() + packetSize );
|
||||
|
||||
// Process the packet
|
||||
HandleRequest( socket, stream );
|
||||
}
|
||||
}
|
||||
|
||||
void GatewayServer::WriteSocket( sptr_tcp_socket socket )
|
||||
{
|
||||
if( socket->flag.disconnected )
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
if( socket->m_pendingWriteBuffer.empty() )
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
size_t totalBytesSent = 0;
|
||||
|
||||
Log::Packet( socket->m_pendingWriteBuffer, ( int )socket->m_pendingWriteBuffer.size(), true );
|
||||
|
||||
while( true )
|
||||
{
|
||||
auto chunkSize = std::min< size_t >( socket->m_pendingWriteBuffer.size(), 1024 );
|
||||
auto bytesSent = send( socket->fd, ( char * )socket->m_pendingWriteBuffer.data(), ( int )chunkSize, 0 );
|
||||
|
||||
if( bytesSent == SOCKET_ERROR )
|
||||
{
|
||||
socket->flag.disconnected = true;
|
||||
return;
|
||||
}
|
||||
|
||||
totalBytesSent += bytesSent;
|
||||
|
||||
if( bytesSent < chunkSize )
|
||||
{
|
||||
break;
|
||||
}
|
||||
|
||||
if( totalBytesSent == socket->m_pendingWriteBuffer.size() )
|
||||
{
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
socket->m_pendingWriteBuffer.erase( socket->m_pendingWriteBuffer.begin(), socket->m_pendingWriteBuffer.begin() + totalBytesSent );
|
||||
}
|
||||
|
||||
void GatewayServer::HandleRequest( sptr_tcp_socket socket, sptr_byte_stream stream )
|
||||
{
|
||||
auto packetId = stream->read< uint16_t >();
|
||||
stream->set_position( 0 );
|
||||
|
||||
auto it = GATEWAY_REQUEST_LOOKUP.find( packetId );
|
||||
if( it == GATEWAY_REQUEST_LOOKUP.end() )
|
||||
{
|
||||
Log::Error( "[GATEWAY] Unknown packet id : 0x%04X", packetId );
|
||||
return;
|
||||
}
|
||||
|
||||
Log::Debug( "[GATEWAY] Request processed : 0x%04X", packetId );
|
||||
|
||||
auto request = it->second();
|
||||
auto res = request->ProcessRequest( socket, stream );
|
||||
|
||||
socket->send( res );
|
||||
}
|
||||
46
Gateway Server/GatewayServer.h
Normal file
46
Gateway Server/GatewayServer.h
Normal file
@@ -0,0 +1,46 @@
|
||||
#pragma once
|
||||
|
||||
#include <memory>
|
||||
|
||||
class GatewayServer
|
||||
{
|
||||
static inline std::shared_ptr< GatewayServer > m_instance;
|
||||
public:
|
||||
|
||||
static std::shared_ptr< GatewayServer > Get()
|
||||
{
|
||||
if( m_instance == nullptr )
|
||||
{
|
||||
m_instance = std::make_shared< GatewayServer >();
|
||||
}
|
||||
|
||||
return m_instance;
|
||||
}
|
||||
|
||||
GatewayServer();
|
||||
~GatewayServer();
|
||||
|
||||
void Start( std::string ip, int32_t port );
|
||||
void Stop();
|
||||
bool isRunning() const
|
||||
{
|
||||
return m_running;
|
||||
}
|
||||
|
||||
private:
|
||||
|
||||
Timer m_timer;
|
||||
std::atomic< bool > m_running;
|
||||
std::thread m_thread;
|
||||
|
||||
SOCKET m_listenSocket;
|
||||
std::vector< sptr_tcp_socket > m_clientSockets;
|
||||
std::vector< uint8_t > m_recvBuffer;
|
||||
|
||||
void Run();
|
||||
void AcceptNewClient();
|
||||
|
||||
void ReadSocket( sptr_tcp_socket socket );
|
||||
void WriteSocket( sptr_tcp_socket socket );
|
||||
void HandleRequest( sptr_tcp_socket socket, sptr_byte_stream stream );
|
||||
};
|
||||
17
Lobby Server/Event/NotifyClientDiscovered.cpp
Normal file
17
Lobby Server/Event/NotifyClientDiscovered.cpp
Normal file
@@ -0,0 +1,17 @@
|
||||
#include "../../global_define.h"
|
||||
#include "NotifyClientDiscovered.h"
|
||||
|
||||
NotifyClientDiscovered::NotifyClientDiscovered( std::string clientIp, int32_t clientPort ) : GenericMessage( 0x40 )
|
||||
{
|
||||
m_clientIp = clientIp;
|
||||
m_clientPort = clientPort;
|
||||
}
|
||||
|
||||
ByteStream &NotifyClientDiscovered::Serialize()
|
||||
{
|
||||
m_stream.write_u16( m_packetId );
|
||||
m_stream.write_sz_utf8( m_clientIp );
|
||||
m_stream.write_u32( m_clientPort );
|
||||
|
||||
return m_stream;
|
||||
}
|
||||
11
Lobby Server/Event/NotifyClientDiscovered.h
Normal file
11
Lobby Server/Event/NotifyClientDiscovered.h
Normal file
@@ -0,0 +1,11 @@
|
||||
#pragma once
|
||||
|
||||
class NotifyClientDiscovered : public GenericMessage {
|
||||
private:
|
||||
std::string m_clientIp;
|
||||
int32_t m_clientPort;
|
||||
|
||||
public:
|
||||
NotifyClientDiscovered( std::string clientIp, int32_t clientPort );
|
||||
ByteStream &Serialize() override;
|
||||
};
|
||||
17
Lobby Server/Event/NotifyClientReqConnect.cpp
Normal file
17
Lobby Server/Event/NotifyClientReqConnect.cpp
Normal file
@@ -0,0 +1,17 @@
|
||||
#include "../../global_define.h"
|
||||
#include "NotifyClientReqConnect.h"
|
||||
|
||||
NotifyClientRequestConnect::NotifyClientRequestConnect( std::string clientIp, int32_t clientPort ) : GenericMessage( 0x3F )
|
||||
{
|
||||
m_clientIp = clientIp;
|
||||
m_clientPort = clientPort;
|
||||
}
|
||||
|
||||
ByteStream &NotifyClientRequestConnect::Serialize()
|
||||
{
|
||||
m_stream.write_u16( m_packetId );
|
||||
m_stream.write_sz_utf8( m_clientIp );
|
||||
m_stream.write_u32( m_clientPort );
|
||||
|
||||
return m_stream;
|
||||
}
|
||||
12
Lobby Server/Event/NotifyClientReqConnect.h
Normal file
12
Lobby Server/Event/NotifyClientReqConnect.h
Normal file
@@ -0,0 +1,12 @@
|
||||
#pragma once
|
||||
|
||||
class NotifyClientRequestConnect : public GenericMessage
|
||||
{
|
||||
private:
|
||||
std::string m_clientIp;
|
||||
int32_t m_clientPort;
|
||||
|
||||
public:
|
||||
NotifyClientRequestConnect( std::string clientIp, int32_t clientPort );
|
||||
ByteStream &Serialize() override;
|
||||
};
|
||||
17
Lobby Server/Event/NotifyGameDiscovered.cpp
Normal file
17
Lobby Server/Event/NotifyGameDiscovered.cpp
Normal file
@@ -0,0 +1,17 @@
|
||||
#include "../../global_define.h"
|
||||
#include "NotifyGameDiscovered.h"
|
||||
|
||||
NotifyGameDiscovered::NotifyGameDiscovered( std::string clientIp, int32_t clientPort ) : GenericMessage( 0x3E )
|
||||
{
|
||||
m_clientIp = clientIp;
|
||||
m_clientPort = clientPort;
|
||||
}
|
||||
|
||||
ByteStream &NotifyGameDiscovered::Serialize()
|
||||
{
|
||||
m_stream.write_u16( m_packetId );
|
||||
m_stream.write_sz_utf8( m_clientIp );
|
||||
m_stream.write_u32( m_clientPort );
|
||||
|
||||
return m_stream;
|
||||
}
|
||||
11
Lobby Server/Event/NotifyGameDiscovered.h
Normal file
11
Lobby Server/Event/NotifyGameDiscovered.h
Normal file
@@ -0,0 +1,11 @@
|
||||
#pragma once
|
||||
|
||||
class NotifyGameDiscovered : public GenericMessage {
|
||||
private:
|
||||
std::string m_clientIp;
|
||||
int32_t m_clientPort;
|
||||
|
||||
public:
|
||||
NotifyGameDiscovered( std::string clientIp, int32_t clientPort );
|
||||
ByteStream &Serialize() override;
|
||||
};
|
||||
36
Lobby Server/Event/RequestCancelGame.cpp
Normal file
36
Lobby Server/Event/RequestCancelGame.cpp
Normal file
@@ -0,0 +1,36 @@
|
||||
#include "../../global_define.h"
|
||||
#include "RequestCancelGame.h"
|
||||
|
||||
void RequestCancelGame::Deserialize( sptr_tcp_socket socket, sptr_byte_stream stream )
|
||||
{
|
||||
DeserializeHeader( stream );
|
||||
|
||||
m_sessionId = stream->read_encrypted_utf16();
|
||||
}
|
||||
|
||||
sptr_generic_response RequestCancelGame::ProcessRequest( sptr_tcp_socket socket, sptr_byte_stream stream )
|
||||
{
|
||||
Deserialize( socket, stream );
|
||||
|
||||
// TODO:
|
||||
// Cancel the game
|
||||
// Notify the players via the Discovery Server
|
||||
|
||||
Log::Debug( "RequestCancelGame : %S", m_sessionId.c_str() );
|
||||
|
||||
return std::make_shared< ResultCancelGame >( this );
|
||||
}
|
||||
|
||||
ResultCancelGame::ResultCancelGame( GenericRequest *request ) : GenericResponse( *request )
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
ByteStream &ResultCancelGame::Serialize()
|
||||
{
|
||||
m_stream.write_u16( m_packetId );
|
||||
m_stream.write_u32( m_requestId );
|
||||
m_stream.write_u32( 0 );
|
||||
|
||||
return m_stream;
|
||||
}
|
||||
22
Lobby Server/Event/RequestCancelGame.h
Normal file
22
Lobby Server/Event/RequestCancelGame.h
Normal file
@@ -0,0 +1,22 @@
|
||||
#pragma once
|
||||
|
||||
class RequestCancelGame : public GenericRequest
|
||||
{
|
||||
private:
|
||||
std::wstring m_sessionId;
|
||||
|
||||
public:
|
||||
static std::unique_ptr< RequestCancelGame > Create()
|
||||
{
|
||||
return std::make_unique< RequestCancelGame >();
|
||||
}
|
||||
|
||||
sptr_generic_response ProcessRequest( sptr_tcp_socket socket, sptr_byte_stream stream ) override;
|
||||
void Deserialize( sptr_tcp_socket socket, sptr_byte_stream stream ) override;
|
||||
};
|
||||
|
||||
class ResultCancelGame : public GenericResponse {
|
||||
public:
|
||||
ResultCancelGame( GenericRequest *request );
|
||||
ByteStream &Serialize();
|
||||
};
|
||||
47
Lobby Server/Event/RequestCreateAccount.cpp
Normal file
47
Lobby Server/Event/RequestCreateAccount.cpp
Normal file
@@ -0,0 +1,47 @@
|
||||
#include "../../global_define.h"
|
||||
#include "RequestCreateAccount.h"
|
||||
|
||||
void RequestCreateAccount::Deserialize( sptr_tcp_socket socket, sptr_byte_stream stream )
|
||||
{
|
||||
DeserializeHeader( stream );
|
||||
|
||||
auto username = stream->read_encrypted_utf16();
|
||||
auto password = stream->read_encrypted_utf16();
|
||||
auto emailAddress = stream->read_encrypted_utf16();
|
||||
auto dateOfBirth = stream->read_encrypted_utf16();
|
||||
}
|
||||
|
||||
sptr_generic_response RequestCreateAccount::ProcessRequest( sptr_tcp_socket socket, sptr_byte_stream stream )
|
||||
{
|
||||
Deserialize( socket, stream );
|
||||
|
||||
Log::Debug( "Account creation isn't supported. Random SessionID generated." );
|
||||
|
||||
auto &userMng = RealmUserManager::Get();
|
||||
auto user = userMng.CreateUser( socket, L"foo", L"bar" );
|
||||
|
||||
if( nullptr == user )
|
||||
{
|
||||
Log::Error( "RequestCreateAccount::ProcessRequest() - User not found!" );
|
||||
return std::make_shared< ResultCreateAccount >( this, CREATE_ACCOUNT_REPLY::ERROR_FATAL, L"" );
|
||||
}
|
||||
|
||||
return std::make_shared< ResultCreateAccount >( this, CREATE_ACCOUNT_REPLY::SUCCESS, user->m_sessionId );
|
||||
}
|
||||
|
||||
ResultCreateAccount::ResultCreateAccount( GenericRequest *request, int32_t reply, std::wstring sessionId ) : GenericResponse( *request )
|
||||
{
|
||||
m_reply = reply;
|
||||
m_sessionId = sessionId;
|
||||
}
|
||||
|
||||
ByteStream &ResultCreateAccount::Serialize()
|
||||
{
|
||||
m_stream.write_u16( m_packetId );
|
||||
m_stream.write_u32( m_requestId );
|
||||
m_stream.write_u32( m_reply );
|
||||
|
||||
m_stream.write_encrypted_utf16( m_sessionId );
|
||||
|
||||
return m_stream;
|
||||
}
|
||||
34
Lobby Server/Event/RequestCreateAccount.h
Normal file
34
Lobby Server/Event/RequestCreateAccount.h
Normal file
@@ -0,0 +1,34 @@
|
||||
#pragma once
|
||||
|
||||
// Account Creation is used in the Network Beta for CoN
|
||||
// but it isn't used or supported here because retail
|
||||
// uses "foo" and "bar" to login without user data.
|
||||
|
||||
class RequestCreateAccount : public GenericRequest
|
||||
{
|
||||
enum CREATE_ACCOUNT_REPLY {
|
||||
SUCCESS = 0,
|
||||
ERROR_FATAL,
|
||||
ERROR_NOT_EXIST
|
||||
};
|
||||
public:
|
||||
static std::unique_ptr< RequestCreateAccount > Create()
|
||||
{
|
||||
return std::make_unique< RequestCreateAccount >();
|
||||
}
|
||||
|
||||
CREATE_ACCOUNT_REPLY m_reply;
|
||||
|
||||
sptr_generic_response ProcessRequest( sptr_tcp_socket socket, sptr_byte_stream stream ) override;
|
||||
void Deserialize( sptr_tcp_socket socket, sptr_byte_stream stream ) override;
|
||||
};
|
||||
|
||||
class ResultCreateAccount : public GenericResponse {
|
||||
private:
|
||||
std::wstring m_sessionId;
|
||||
int32_t m_reply;
|
||||
|
||||
public:
|
||||
ResultCreateAccount( GenericRequest *request, int32_t reply, std::wstring sessionId );
|
||||
ByteStream &Serialize();
|
||||
};
|
||||
58
Lobby Server/Event/RequestCreatePrivateGame.cpp
Normal file
58
Lobby Server/Event/RequestCreatePrivateGame.cpp
Normal file
@@ -0,0 +1,58 @@
|
||||
#include "../../global_define.h"
|
||||
|
||||
#include "RequestCreatePrivateGame.h"
|
||||
|
||||
void RequestCreatePrivateGame::Deserialize( sptr_tcp_socket socket, sptr_byte_stream stream )
|
||||
{
|
||||
DeserializeHeader( stream );
|
||||
|
||||
m_sessionId = stream->read_encrypted_utf16();
|
||||
m_gameName = stream->read_utf16();
|
||||
}
|
||||
|
||||
sptr_generic_response RequestCreatePrivateGame::ProcessRequest( sptr_tcp_socket socket, sptr_byte_stream stream )
|
||||
{
|
||||
Deserialize( socket, stream );
|
||||
|
||||
auto user = RealmUserManager::Get().GetUser( socket );
|
||||
|
||||
if( user == nullptr )
|
||||
{
|
||||
Log::Error( "User not found! [%S]", m_sessionId.c_str() );
|
||||
return std::make_shared< ResultCreatePrivateGame >( this, CREATE_REPLY::FATAL_ERROR, "", 0 );
|
||||
}
|
||||
|
||||
auto &game_manager = GameSessionManager::Get();
|
||||
|
||||
auto result = game_manager.CreatePrivateGameSession( user, m_gameName, 0, 9999 );
|
||||
|
||||
if( !result )
|
||||
{
|
||||
Log::Error( "RequestCreatePrivateGame::ProcessRequest() - Failed to create private game session!" );
|
||||
return std::make_shared< ResultCreatePrivateGame >( this, CREATE_REPLY::GENERAL_ERROR, "", 0 );
|
||||
}
|
||||
|
||||
Log::Info( "[%S] Create Private Game: %S", m_sessionId.c_str(), m_gameName.c_str() );
|
||||
|
||||
// Send the discovery server information to the client
|
||||
return std::make_shared< ResultCreatePrivateGame >( this, CREATE_REPLY::SUCCESS, "192.168.1.248", 40820 );
|
||||
}
|
||||
|
||||
ResultCreatePrivateGame::ResultCreatePrivateGame( GenericRequest *request, int32_t reply, std::string discoveryIp, int32_t discoveryPort ) : GenericResponse( *request )
|
||||
{
|
||||
m_reply = reply;
|
||||
m_discoveryIp = discoveryIp;
|
||||
m_discoveryPort = discoveryPort;
|
||||
}
|
||||
|
||||
ByteStream &ResultCreatePrivateGame::Serialize()
|
||||
{
|
||||
m_stream.write_u16( m_packetId );
|
||||
m_stream.write_u32( m_requestId );
|
||||
m_stream.write_u32( m_reply );
|
||||
|
||||
m_stream.write_sz_utf8( m_discoveryIp );
|
||||
m_stream.write( m_discoveryPort );
|
||||
|
||||
return m_stream;
|
||||
}
|
||||
33
Lobby Server/Event/RequestCreatePrivateGame.h
Normal file
33
Lobby Server/Event/RequestCreatePrivateGame.h
Normal file
@@ -0,0 +1,33 @@
|
||||
#pragma once
|
||||
|
||||
class RequestCreatePrivateGame : public GenericRequest
|
||||
{
|
||||
private:
|
||||
std::wstring m_sessionId;
|
||||
std::wstring m_gameName;
|
||||
|
||||
enum CREATE_REPLY {
|
||||
SUCCESS = 0,
|
||||
FATAL_ERROR,
|
||||
GENERAL_ERROR,
|
||||
};
|
||||
public:
|
||||
static std::unique_ptr< RequestCreatePrivateGame > Create()
|
||||
{
|
||||
return std::make_unique< RequestCreatePrivateGame >();
|
||||
}
|
||||
|
||||
sptr_generic_response ProcessRequest( sptr_tcp_socket socket, sptr_byte_stream stream ) override;
|
||||
void Deserialize( sptr_tcp_socket socket, sptr_byte_stream stream ) override;
|
||||
};
|
||||
|
||||
class ResultCreatePrivateGame : public GenericResponse {
|
||||
private:
|
||||
int32_t m_reply;
|
||||
std::string m_discoveryIp;
|
||||
int32_t m_discoveryPort;
|
||||
|
||||
public:
|
||||
ResultCreatePrivateGame( GenericRequest *request, int32_t reply, std::string discoveryIp = "", int32_t discoveryPort = 0 );
|
||||
ByteStream &Serialize();
|
||||
};
|
||||
65
Lobby Server/Event/RequestCreatePublicGame.cpp
Normal file
65
Lobby Server/Event/RequestCreatePublicGame.cpp
Normal file
@@ -0,0 +1,65 @@
|
||||
#include "../../global_define.h"
|
||||
|
||||
#include "RequestCreatePublicGame.h"
|
||||
|
||||
void RequestCreatePublicGame::Deserialize( sptr_tcp_socket socket, sptr_byte_stream stream )
|
||||
{
|
||||
DeserializeHeader( stream );
|
||||
|
||||
m_sessionId = stream->read_encrypted_utf16();
|
||||
|
||||
// Some kind of match attributes
|
||||
auto unknown_a = stream->read_u16();
|
||||
auto unknown_b = stream->read_u32();
|
||||
auto unknown_c = stream->read_u32();
|
||||
auto unknown_d = stream->read_u32();
|
||||
|
||||
m_gameName = stream->read_utf16();
|
||||
}
|
||||
|
||||
sptr_generic_response RequestCreatePublicGame::ProcessRequest( sptr_tcp_socket socket, sptr_byte_stream stream )
|
||||
{
|
||||
Deserialize( socket, stream );
|
||||
|
||||
auto user = RealmUserManager::Get().GetUser( socket );
|
||||
|
||||
if( user == nullptr )
|
||||
{
|
||||
Log::Error( "User not found! [%S]", m_sessionId.c_str() );
|
||||
return std::make_shared< ResultCreatePublicGame >( this, CREATE_REPLY::FATAL_ERROR, "", 0 );
|
||||
}
|
||||
|
||||
auto &game_manager = GameSessionManager::Get();
|
||||
|
||||
auto result = game_manager.CreatePublicGameSession( user, m_gameName, 0, 9999 );
|
||||
|
||||
if( !result )
|
||||
{
|
||||
Log::Error( "RequestCreatePublicGame::ProcessRequest() - Failed to create public game session!" );
|
||||
return std::make_shared< ResultCreatePublicGame >( this, CREATE_REPLY::GENERAL_ERROR, "", 0 );
|
||||
}
|
||||
|
||||
Log::Info( "[%S] Create Public Game: %S", m_sessionId.c_str(), m_gameName.c_str() );
|
||||
|
||||
// Send the discovery server information to the client
|
||||
return std::make_shared< ResultCreatePublicGame >( this, CREATE_REPLY::SUCCESS, "192.168.1.248", 40820 );
|
||||
}
|
||||
|
||||
ResultCreatePublicGame::ResultCreatePublicGame( GenericRequest *request, int32_t reply, std::string discoveryIp, int32_t discoveryPort ) : GenericResponse( *request )
|
||||
{
|
||||
m_reply = reply;
|
||||
m_discoveryIp = discoveryIp;
|
||||
m_discoveryPort = discoveryPort;
|
||||
}
|
||||
|
||||
ByteStream &ResultCreatePublicGame::Serialize()
|
||||
{
|
||||
m_stream.write_u16( m_packetId );
|
||||
m_stream.write_u32( m_requestId );
|
||||
m_stream.write_u32( m_reply );
|
||||
|
||||
m_stream.write_sz_utf8( m_discoveryIp );
|
||||
m_stream.write( m_discoveryPort );
|
||||
|
||||
return m_stream;
|
||||
}
|
||||
35
Lobby Server/Event/RequestCreatePublicGame.h
Normal file
35
Lobby Server/Event/RequestCreatePublicGame.h
Normal file
@@ -0,0 +1,35 @@
|
||||
#pragma once
|
||||
|
||||
class RequestCreatePublicGame : public GenericRequest
|
||||
{
|
||||
private:
|
||||
std::wstring m_sessionId;
|
||||
std::wstring m_gameName;
|
||||
|
||||
int32_t m_minimumLevel;
|
||||
int32_t m_maximumLevel;
|
||||
|
||||
enum CREATE_REPLY {
|
||||
SUCCESS = 0,
|
||||
FATAL_ERROR,
|
||||
GENERAL_ERROR,
|
||||
};
|
||||
public:
|
||||
static std::unique_ptr< RequestCreatePublicGame > Create()
|
||||
{
|
||||
return std::make_unique< RequestCreatePublicGame >();
|
||||
}
|
||||
sptr_generic_response ProcessRequest( sptr_tcp_socket socket, sptr_byte_stream stream ) override;
|
||||
void Deserialize( sptr_tcp_socket socket, sptr_byte_stream stream ) override;
|
||||
};
|
||||
|
||||
class ResultCreatePublicGame : public GenericResponse {
|
||||
private:
|
||||
int32_t m_reply;
|
||||
std::string m_discoveryIp;
|
||||
int32_t m_discoveryPort;
|
||||
|
||||
public:
|
||||
ResultCreatePublicGame( GenericRequest *request, int32_t reply, std::string discoveryIp = "", int32_t discoveryPort = 0 );
|
||||
ByteStream &Serialize();
|
||||
};
|
||||
59
Lobby Server/Event/RequestDoClientDiscovery.cpp
Normal file
59
Lobby Server/Event/RequestDoClientDiscovery.cpp
Normal file
@@ -0,0 +1,59 @@
|
||||
#include "../../global_define.h"
|
||||
#include "RequestDoClientDiscovery.h"
|
||||
#include "NotifyClientReqConnect.h"
|
||||
|
||||
void RequestDoClientDiscovery::Deserialize( sptr_tcp_socket socket, sptr_byte_stream stream )
|
||||
{
|
||||
DeserializeHeader( stream );
|
||||
|
||||
m_sessionId = stream->read_encrypted_utf16();
|
||||
m_gameId = stream->read_u32();
|
||||
}
|
||||
|
||||
sptr_generic_response RequestDoClientDiscovery::ProcessRequest( sptr_tcp_socket socket, sptr_byte_stream stream )
|
||||
{
|
||||
Deserialize( socket, stream );
|
||||
|
||||
auto user = RealmUserManager::Get().GetUser( socket );
|
||||
|
||||
if( user == nullptr )
|
||||
{
|
||||
Log::Error( "User not found! [%s]", m_sessionId.c_str() );
|
||||
return std::make_shared< ResultDoClientDiscovery >( this, DISCOVERY_REPLY::FATAL_ERROR, "", 0 );
|
||||
}
|
||||
|
||||
if( user->m_sessionId != m_sessionId )
|
||||
{
|
||||
Log::Error( "Session ID mismatch! [%s]", m_sessionId.c_str() );
|
||||
return std::make_shared< ResultDoClientDiscovery >( this, DISCOVERY_REPLY::FATAL_ERROR, "", 0 );
|
||||
}
|
||||
|
||||
auto result = GameSessionManager::Get().UserJoinGame( m_gameId, user );
|
||||
|
||||
if( result == false )
|
||||
{
|
||||
Log::Error( "Failed to join game! [%d]", m_gameId );
|
||||
return std::make_shared< ResultDoClientDiscovery >( this, DISCOVERY_REPLY::GAME_FULL, "", 0 );
|
||||
}
|
||||
|
||||
return std::make_shared< ResultDoClientDiscovery >( this, DISCOVERY_REPLY::SUCCESS, "192.168.1.248", 40820 );
|
||||
}
|
||||
|
||||
ResultDoClientDiscovery::ResultDoClientDiscovery( GenericRequest *request, int32_t reply, std::string ip, int32_t port ) : GenericResponse( *request )
|
||||
{
|
||||
m_reply = reply;
|
||||
m_discoveryIP = ip;
|
||||
m_discoveryPort = port;
|
||||
}
|
||||
|
||||
ByteStream &ResultDoClientDiscovery::Serialize()
|
||||
{
|
||||
m_stream.write_u16( m_packetId );
|
||||
m_stream.write_u32( m_requestId );
|
||||
m_stream.write_u32( m_reply );
|
||||
|
||||
m_stream.write_sz_utf8( m_discoveryIP );
|
||||
m_stream.write( m_discoveryPort );
|
||||
|
||||
return m_stream;
|
||||
}
|
||||
34
Lobby Server/Event/RequestDoClientDiscovery.h
Normal file
34
Lobby Server/Event/RequestDoClientDiscovery.h
Normal file
@@ -0,0 +1,34 @@
|
||||
#pragma once
|
||||
|
||||
class RequestDoClientDiscovery : public GenericRequest
|
||||
{
|
||||
private:
|
||||
enum DISCOVERY_REPLY {
|
||||
SUCCESS = 0,
|
||||
FATAL_ERROR = 1,
|
||||
GAME_FULL = 2,
|
||||
};
|
||||
|
||||
std::wstring m_sessionId;
|
||||
int32_t m_gameId;
|
||||
|
||||
public:
|
||||
static std::unique_ptr< RequestDoClientDiscovery > Create()
|
||||
{
|
||||
return std::make_unique< RequestDoClientDiscovery >();
|
||||
}
|
||||
|
||||
sptr_generic_response ProcessRequest( sptr_tcp_socket socket, sptr_byte_stream stream ) override;
|
||||
void Deserialize( sptr_tcp_socket socket, sptr_byte_stream stream ) override;
|
||||
};
|
||||
|
||||
class ResultDoClientDiscovery : public GenericResponse {
|
||||
private:
|
||||
int32_t m_reply;
|
||||
std::string m_discoveryIP;
|
||||
int32_t m_discoveryPort;
|
||||
|
||||
public:
|
||||
ResultDoClientDiscovery( GenericRequest *request, int32_t reply, std::string ip = "", int32_t port = 0);
|
||||
ByteStream &Serialize();
|
||||
};
|
||||
33
Lobby Server/Event/RequestGetEncryptionKey.cpp
Normal file
33
Lobby Server/Event/RequestGetEncryptionKey.cpp
Normal file
@@ -0,0 +1,33 @@
|
||||
#include "../../global_define.h"
|
||||
#include "RequestGetEncryptionKey.h"
|
||||
|
||||
void RequestGetEncryptionKey::Deserialize( sptr_tcp_socket socket, sptr_byte_stream stream )
|
||||
{
|
||||
DeserializeHeader( stream );
|
||||
}
|
||||
|
||||
sptr_generic_response RequestGetEncryptionKey::ProcessRequest( sptr_tcp_socket socket, sptr_byte_stream stream )
|
||||
{
|
||||
Deserialize( socket, stream );
|
||||
|
||||
auto publicKey = stream->read_utf8();
|
||||
auto unknown = stream->read_u32();
|
||||
|
||||
return std::make_shared< ResultGetEncryptionKey >( this );
|
||||
}
|
||||
|
||||
ResultGetEncryptionKey::ResultGetEncryptionKey( GenericRequest *request ) : GenericResponse( *request )
|
||||
{
|
||||
m_symKey = RealmCrypt::getSymmetricKey();
|
||||
}
|
||||
|
||||
ByteStream& ResultGetEncryptionKey::Serialize()
|
||||
{
|
||||
m_stream.write_u16( m_packetId );
|
||||
m_stream.write_u32( m_requestId );
|
||||
m_stream.write_u32( 0 );
|
||||
|
||||
m_stream.write_encrypted_bytes( m_symKey );
|
||||
|
||||
return m_stream;
|
||||
}
|
||||
21
Lobby Server/Event/RequestGetEncryptionKey.h
Normal file
21
Lobby Server/Event/RequestGetEncryptionKey.h
Normal file
@@ -0,0 +1,21 @@
|
||||
#pragma once
|
||||
|
||||
class RequestGetEncryptionKey : public GenericRequest
|
||||
{
|
||||
public:
|
||||
static std::unique_ptr< RequestGetEncryptionKey > Create()
|
||||
{
|
||||
return std::make_unique< RequestGetEncryptionKey >();
|
||||
}
|
||||
|
||||
sptr_generic_response ProcessRequest( sptr_tcp_socket socket, sptr_byte_stream stream ) override;
|
||||
void Deserialize( sptr_tcp_socket socket, sptr_byte_stream stream ) override;
|
||||
};
|
||||
|
||||
class ResultGetEncryptionKey : public GenericResponse {
|
||||
public:
|
||||
std::vector< uint8_t > m_symKey;
|
||||
|
||||
ResultGetEncryptionKey( GenericRequest *request );
|
||||
ByteStream &Serialize();
|
||||
};
|
||||
35
Lobby Server/Event/RequestGetRules.cpp
Normal file
35
Lobby Server/Event/RequestGetRules.cpp
Normal file
@@ -0,0 +1,35 @@
|
||||
#include "../../global_define.h"
|
||||
#include "RequestGetRules.h"
|
||||
|
||||
void RequestGetRules::Deserialize( sptr_tcp_socket socket, sptr_byte_stream stream )
|
||||
{
|
||||
DeserializeHeader( stream );
|
||||
|
||||
m_language = stream->read_sz_utf8();
|
||||
}
|
||||
|
||||
sptr_generic_response RequestGetRules::ProcessRequest( sptr_tcp_socket socket, sptr_byte_stream stream )
|
||||
{
|
||||
Deserialize( socket, stream );
|
||||
|
||||
// TODO: Get rules/eula based on language
|
||||
std::wstring rules = L"Welcome to the Norrath Emulated Server!\n\n";
|
||||
|
||||
return std::make_shared< ResultGetRules >( this, rules );
|
||||
}
|
||||
|
||||
ResultGetRules::ResultGetRules( GenericRequest *request, std::wstring rules ) : GenericResponse( *request )
|
||||
{
|
||||
m_rules = rules;
|
||||
}
|
||||
|
||||
ByteStream &ResultGetRules::Serialize()
|
||||
{
|
||||
m_stream.write_u16( m_packetId );
|
||||
m_stream.write_u32( m_requestId );
|
||||
m_stream.write_u32( 0 );
|
||||
|
||||
m_stream.write_utf16( m_rules );
|
||||
|
||||
return m_stream;
|
||||
}
|
||||
25
Lobby Server/Event/RequestGetRules.h
Normal file
25
Lobby Server/Event/RequestGetRules.h
Normal file
@@ -0,0 +1,25 @@
|
||||
#pragma once
|
||||
|
||||
class RequestGetRules : public GenericRequest
|
||||
{
|
||||
private:
|
||||
std::string m_language;
|
||||
|
||||
public:
|
||||
static std::unique_ptr< RequestGetRules > Create()
|
||||
{
|
||||
return std::make_unique< RequestGetRules >();
|
||||
}
|
||||
|
||||
sptr_generic_response ProcessRequest( sptr_tcp_socket socket, sptr_byte_stream stream ) override;
|
||||
void Deserialize( sptr_tcp_socket socket, sptr_byte_stream stream ) override;
|
||||
};
|
||||
|
||||
class ResultGetRules : public GenericResponse {
|
||||
private:
|
||||
std::wstring m_rules;
|
||||
|
||||
public:
|
||||
ResultGetRules( GenericRequest *request, std::wstring rules );
|
||||
ByteStream &Serialize();
|
||||
};
|
||||
56
Lobby Server/Event/RequestLogin.cpp
Normal file
56
Lobby Server/Event/RequestLogin.cpp
Normal file
@@ -0,0 +1,56 @@
|
||||
#include "../../global_define.h"
|
||||
|
||||
#include "RequestLogin.h"
|
||||
|
||||
void RequestLogin::Deserialize( sptr_tcp_socket socket, sptr_byte_stream stream )
|
||||
{
|
||||
DeserializeHeader( stream );
|
||||
|
||||
m_username = stream->read_encrypted_utf16();
|
||||
m_password = stream->read_encrypted_utf16();
|
||||
}
|
||||
|
||||
sptr_generic_response RequestLogin::ProcessRequest( sptr_tcp_socket socket, sptr_byte_stream stream )
|
||||
{
|
||||
Deserialize( socket, stream );
|
||||
|
||||
if( m_username.empty() || m_password.empty() )
|
||||
{
|
||||
Log::Error( "RequestLogin::ProcessRequest() - Username or password is empty" );
|
||||
return std::make_shared< ResultLogin >( this, LOGIN_REPLY::NOT_EXIST, L"" );
|
||||
}
|
||||
|
||||
if( m_username == L"foo" && m_password == L"bar" )
|
||||
{
|
||||
// Retail CoN does not use any login information.
|
||||
Log::Debug( "RequestLogin : Champions of Norrath v2.0" );
|
||||
}
|
||||
else
|
||||
{
|
||||
// Network Beta CoN uses login information, but it's invalid because of version 2.0.
|
||||
Log::Debug( "RequestLogin : Champions of Norrath v1.0" );
|
||||
}
|
||||
|
||||
auto &userMng = RealmUserManager::Get();
|
||||
|
||||
auto user = userMng.CreateUser( socket, m_username, m_password );
|
||||
|
||||
return std::make_shared< ResultLogin >( this, LOGIN_REPLY::SUCCESS, user->m_sessionId );
|
||||
}
|
||||
|
||||
ResultLogin::ResultLogin( GenericRequest *request, int32_t reply, std::wstring sessionId ) : GenericResponse( *request )
|
||||
{
|
||||
m_reply = reply;
|
||||
m_sessionId = sessionId;
|
||||
}
|
||||
|
||||
ByteStream &ResultLogin::Serialize()
|
||||
{
|
||||
m_stream.write_u16( m_packetId );
|
||||
m_stream.write_u32( m_requestId );
|
||||
m_stream.write_u32( m_reply );
|
||||
|
||||
m_stream.write_encrypted_utf16( m_sessionId );
|
||||
|
||||
return m_stream;
|
||||
}
|
||||
40
Lobby Server/Event/RequestLogin.h
Normal file
40
Lobby Server/Event/RequestLogin.h
Normal file
@@ -0,0 +1,40 @@
|
||||
#pragma once
|
||||
|
||||
// Account Login is used in the Network Beta for CoN.
|
||||
// In the retail version, the game simply logs in with
|
||||
// "foo" and "bar" as the username and password.
|
||||
//
|
||||
// A unique Session ID is generated and assigned to the player.
|
||||
|
||||
class RequestLogin : public GenericRequest
|
||||
{
|
||||
private:
|
||||
enum LOGIN_REPLY {
|
||||
SUCCESS = 0,
|
||||
FATAL_ERROR,
|
||||
NOT_EXIST,
|
||||
};
|
||||
|
||||
std::wstring m_username;
|
||||
std::wstring m_password;
|
||||
std::wstring m_sessionId;
|
||||
|
||||
public:
|
||||
static std::unique_ptr< RequestLogin > Create()
|
||||
{
|
||||
return std::make_unique< RequestLogin >();
|
||||
}
|
||||
|
||||
sptr_generic_response ProcessRequest( sptr_tcp_socket socket, sptr_byte_stream stream ) override;
|
||||
void Deserialize( sptr_tcp_socket socket, sptr_byte_stream stream ) override;
|
||||
};
|
||||
|
||||
class ResultLogin : public GenericResponse {
|
||||
private:
|
||||
std::wstring m_sessionId;
|
||||
int32_t m_reply;
|
||||
|
||||
public:
|
||||
ResultLogin( GenericRequest *request, int32_t reply, std::wstring sessionId );
|
||||
ByteStream &Serialize();
|
||||
};
|
||||
44
Lobby Server/Event/RequestLogout.cpp
Normal file
44
Lobby Server/Event/RequestLogout.cpp
Normal file
@@ -0,0 +1,44 @@
|
||||
#include "../../global_define.h"
|
||||
#include "RequestLogout.h"
|
||||
|
||||
void RequestLogout::Deserialize( sptr_tcp_socket socket, sptr_byte_stream stream )
|
||||
{
|
||||
DeserializeHeader( stream );
|
||||
m_sessionId = stream->read_encrypted_utf16();
|
||||
}
|
||||
|
||||
sptr_generic_response RequestLogout::ProcessRequest( sptr_tcp_socket socket, sptr_byte_stream stream )
|
||||
{
|
||||
Deserialize( socket, stream );
|
||||
|
||||
auto &userMng = RealmUserManager::Get();
|
||||
|
||||
auto user = userMng.GetUser( m_sessionId );
|
||||
|
||||
if( nullptr == user )
|
||||
{
|
||||
Log::Error( "RequestLogout::ProcessRequest() - User not found!" );
|
||||
return std::make_shared< ResultLogout >( this, 1 );
|
||||
}
|
||||
|
||||
// TODO: Any other cleanup here?
|
||||
Log::Debug( "[%S] Logout", m_sessionId.c_str() );
|
||||
|
||||
userMng.RemoveUser( m_sessionId );
|
||||
|
||||
return std::make_shared< ResultLogout >( this, 0 );
|
||||
}
|
||||
|
||||
ResultLogout::ResultLogout( GenericRequest *request, int32_t reply ) : GenericResponse( *request )
|
||||
{
|
||||
m_reply = reply;
|
||||
}
|
||||
|
||||
ByteStream &ResultLogout::Serialize()
|
||||
{
|
||||
m_stream.write_u16( m_packetId );
|
||||
m_stream.write_u32( m_requestId );
|
||||
m_stream.write_u32( m_reply );
|
||||
|
||||
return m_stream;
|
||||
}
|
||||
23
Lobby Server/Event/RequestLogout.h
Normal file
23
Lobby Server/Event/RequestLogout.h
Normal file
@@ -0,0 +1,23 @@
|
||||
#pragma once
|
||||
|
||||
class RequestLogout : public GenericRequest
|
||||
{
|
||||
private:
|
||||
std::wstring m_sessionId;
|
||||
|
||||
public:
|
||||
static std::unique_ptr< RequestLogout > Create()
|
||||
{
|
||||
return std::make_unique< RequestLogout >();
|
||||
}
|
||||
sptr_generic_response ProcessRequest( sptr_tcp_socket socket, sptr_byte_stream stream ) override;
|
||||
void Deserialize( sptr_tcp_socket socket, sptr_byte_stream stream ) override;
|
||||
};
|
||||
|
||||
class ResultLogout : public GenericResponse {
|
||||
private:
|
||||
int32_t m_reply;
|
||||
public:
|
||||
ResultLogout( GenericRequest *request, int32_t reply );
|
||||
ByteStream &Serialize();
|
||||
};
|
||||
88
Lobby Server/Event/RequestMatchGame.cpp
Normal file
88
Lobby Server/Event/RequestMatchGame.cpp
Normal file
@@ -0,0 +1,88 @@
|
||||
#include "../../global_define.h"
|
||||
#include "RequestMatchGame.h"
|
||||
|
||||
void RequestMatchGame::Deserialize( sptr_tcp_socket socket, sptr_byte_stream stream )
|
||||
{
|
||||
DeserializeHeader( stream );
|
||||
|
||||
m_sessionId = stream->read_encrypted_utf16();
|
||||
|
||||
auto unknown_a = stream->read_u16();
|
||||
auto unknown_b = stream->read_u32();
|
||||
auto unknown_c = stream->read_u32();
|
||||
auto unknown_d = stream->read_u32();
|
||||
|
||||
auto unknown_e = stream->read_u32();
|
||||
|
||||
// Match Game Node Count
|
||||
for( int i = 0; i < unknown_e; i++ )
|
||||
{
|
||||
auto node_a = stream->read_u16();
|
||||
auto node_b = stream->read_u32();
|
||||
|
||||
auto node_c = stream->read_utf16();
|
||||
|
||||
auto node_d = stream->read_u32();
|
||||
auto node_e = stream->read_u32();
|
||||
auto node_f = stream->read_u32();
|
||||
auto node_g = stream->read_u16();
|
||||
}
|
||||
|
||||
auto unknown_f = stream->read_u8();
|
||||
auto unknown_g = stream->read_u32();
|
||||
auto unknown_h = stream->read_u32();
|
||||
|
||||
int dbg = 0;
|
||||
}
|
||||
|
||||
sptr_generic_response RequestMatchGame::ProcessRequest( sptr_tcp_socket socket, sptr_byte_stream stream )
|
||||
{
|
||||
Deserialize( socket, stream );
|
||||
|
||||
Log::Debug( "RequestMatchGame : %S", m_sessionId.c_str() );
|
||||
|
||||
return std::make_shared< ResultMatchGame >( this );
|
||||
}
|
||||
|
||||
ResultMatchGame::ResultMatchGame( GenericRequest *request ) : GenericResponse( *request )
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
ByteStream &ResultMatchGame::Serialize()
|
||||
{
|
||||
m_stream.write_u16( m_packetId );
|
||||
m_stream.write_u32( m_requestId );
|
||||
m_stream.write_u32( 0 ); // Connection State
|
||||
|
||||
m_stream.write_u32( 5 );
|
||||
|
||||
for( int i = 0; i < 5; i++ )
|
||||
{
|
||||
m_stream.write_utf16( L"Unknown_A " + std::to_wstring( i ) );
|
||||
}
|
||||
|
||||
m_stream.write_u32( 5 );
|
||||
|
||||
for( int i = 0; i < 5; i++ )
|
||||
{
|
||||
m_stream.write_utf16( L"Game Name " + std::to_wstring( i ) );
|
||||
}
|
||||
|
||||
m_stream.write_u32( 5 );
|
||||
|
||||
for( int i = 0; i < 5; i++ )
|
||||
{
|
||||
m_stream.write_utf16( L"Unknown_B " + std::to_wstring( i ) );
|
||||
}
|
||||
|
||||
|
||||
// Room Unique ID (Used when selecting)
|
||||
m_stream.write_u32( 5 );
|
||||
for( int i = 0; i < 5; i++ )
|
||||
{
|
||||
m_stream.write_u32( i );
|
||||
}
|
||||
|
||||
return m_stream;
|
||||
}
|
||||
21
Lobby Server/Event/RequestMatchGame.h
Normal file
21
Lobby Server/Event/RequestMatchGame.h
Normal file
@@ -0,0 +1,21 @@
|
||||
#pragma once
|
||||
|
||||
class RequestMatchGame : public GenericRequest
|
||||
{
|
||||
private:
|
||||
std::wstring m_sessionId;
|
||||
|
||||
public:
|
||||
static std::unique_ptr< RequestMatchGame > Create()
|
||||
{
|
||||
return std::make_unique< RequestMatchGame >();
|
||||
}
|
||||
sptr_generic_response ProcessRequest( sptr_tcp_socket socket, sptr_byte_stream stream ) override;
|
||||
void Deserialize( sptr_tcp_socket socket, sptr_byte_stream stream ) override;
|
||||
};
|
||||
|
||||
class ResultMatchGame : public GenericResponse {
|
||||
public:
|
||||
ResultMatchGame( GenericRequest *request );
|
||||
ByteStream &Serialize();
|
||||
};
|
||||
32
Lobby Server/Event/RequestTouchSession.cpp
Normal file
32
Lobby Server/Event/RequestTouchSession.cpp
Normal file
@@ -0,0 +1,32 @@
|
||||
#include "../../global_define.h"
|
||||
#include "RequestTouchSession.h"
|
||||
|
||||
void RequestTouchSession::Deserialize( sptr_tcp_socket socket, sptr_byte_stream stream )
|
||||
{
|
||||
DeserializeHeader( stream );
|
||||
|
||||
m_sessionId = stream->read_encrypted_utf16();
|
||||
}
|
||||
|
||||
sptr_generic_response RequestTouchSession::ProcessRequest( sptr_tcp_socket socket, sptr_byte_stream stream )
|
||||
{
|
||||
Deserialize( socket, stream );
|
||||
|
||||
Log::Debug( "RequestTouchSession : %S", m_sessionId.c_str() );
|
||||
|
||||
return std::make_shared< ResultTouchSession >( this );
|
||||
}
|
||||
|
||||
ResultTouchSession::ResultTouchSession( GenericRequest *request ) : GenericResponse( *request )
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
ByteStream &ResultTouchSession::Serialize()
|
||||
{
|
||||
m_stream.write_u16( m_packetId );
|
||||
m_stream.write_u32( m_requestId );
|
||||
m_stream.write_u32( 0 );
|
||||
|
||||
return m_stream;
|
||||
}
|
||||
21
Lobby Server/Event/RequestTouchSession.h
Normal file
21
Lobby Server/Event/RequestTouchSession.h
Normal file
@@ -0,0 +1,21 @@
|
||||
#pragma once
|
||||
|
||||
class RequestTouchSession : public GenericRequest
|
||||
{
|
||||
private:
|
||||
std::wstring m_sessionId;
|
||||
|
||||
public:
|
||||
static std::unique_ptr< RequestTouchSession > Create()
|
||||
{
|
||||
return std::make_unique< RequestTouchSession >();
|
||||
}
|
||||
sptr_generic_response ProcessRequest( sptr_tcp_socket socket, sptr_byte_stream stream ) override;
|
||||
void Deserialize( sptr_tcp_socket socket, sptr_byte_stream stream ) override;
|
||||
};
|
||||
|
||||
class ResultTouchSession : public GenericResponse {
|
||||
public:
|
||||
ResultTouchSession( GenericRequest *request );
|
||||
ByteStream &Serialize();
|
||||
};
|
||||
75
Lobby Server/EventLookup.h
Normal file
75
Lobby Server/EventLookup.h
Normal file
@@ -0,0 +1,75 @@
|
||||
#pragma once
|
||||
|
||||
#include <map>
|
||||
#include <functional>
|
||||
|
||||
#include "Event/RequestCancelGame.h"
|
||||
#include "Event/RequestCreateAccount.h"
|
||||
#include "Event/RequestCreatePrivateGame.h"
|
||||
#include "Event/RequestCreatePublicGame.h"
|
||||
#include "Event/RequestLogin.h"
|
||||
#include "Event/RequestLogout.h"
|
||||
#include "Event/RequestMatchGame.h"
|
||||
#include "Event/RequestTouchSession.h"
|
||||
#include "Event/RequestDoClientDiscovery.h"
|
||||
#include "Event/RequestGetEncryptionKey.h"
|
||||
#include "Event/RequestGetRules.h"
|
||||
|
||||
const std::map< int16_t, std::function< std::unique_ptr< GenericRequest >() > > LOBBY_REQUEST_EVENT =
|
||||
{
|
||||
{ 0x0005, []() -> std::unique_ptr< GenericRequest >
|
||||
{
|
||||
return std::make_unique< RequestCancelGame >();
|
||||
}
|
||||
},
|
||||
{ 0x0006, []() -> std::unique_ptr< GenericRequest >
|
||||
{
|
||||
return std::make_unique< RequestCreateAccount >();
|
||||
}
|
||||
},
|
||||
{ 0x0008, []() -> std::unique_ptr< GenericRequest >
|
||||
{
|
||||
return std::make_unique< RequestCreatePrivateGame >();
|
||||
}
|
||||
},
|
||||
{ 0x000A, []() -> std::unique_ptr< GenericRequest >
|
||||
{
|
||||
return std::make_unique< RequestCreatePublicGame >();
|
||||
}
|
||||
},
|
||||
{ 0x0016, []() -> std::unique_ptr< GenericRequest >
|
||||
{
|
||||
return std::make_unique< RequestLogin >();
|
||||
}
|
||||
},
|
||||
{ 0x0017, []() -> std::unique_ptr< GenericRequest >
|
||||
{
|
||||
return std::make_unique< RequestLogout >();
|
||||
}
|
||||
},
|
||||
{ 0x0018, []() -> std::unique_ptr< GenericRequest >
|
||||
{
|
||||
return std::make_unique< RequestMatchGame >();
|
||||
}
|
||||
},
|
||||
{ 0x0024, []() -> std::unique_ptr< GenericRequest >
|
||||
{
|
||||
return std::make_unique< RequestTouchSession >();
|
||||
}
|
||||
},
|
||||
{ 0x0025, []() -> std::unique_ptr< GenericRequest >
|
||||
{
|
||||
return std::make_unique< RequestDoClientDiscovery >();
|
||||
}
|
||||
},
|
||||
{ 0x0027, []() -> std::unique_ptr< GenericRequest >
|
||||
{
|
||||
return std::make_unique< RequestGetEncryptionKey >();
|
||||
}
|
||||
},
|
||||
{ 0x0042, []() -> std::unique_ptr< GenericRequest >
|
||||
{
|
||||
return std::make_unique< RequestGetRules >();
|
||||
}
|
||||
}
|
||||
};
|
||||
255
Lobby Server/LobbyServer.cpp
Normal file
255
Lobby Server/LobbyServer.cpp
Normal file
@@ -0,0 +1,255 @@
|
||||
// ╔╗╔╔═╗╦═╗╦═╗╔═╗╔╦╗╦ ╦
|
||||
// ║║║║ ║╠╦╝╠╦╝╠═╣ ║ ╠═╣
|
||||
// ╝╚╝╚═╝╩╚═╩╚═╩ ╩ ╩ ╩ ╩
|
||||
// ╦ ╔═╗╔╗ ╔╗ ╦ ╦ ╔═╗╔═╗╦═╗╦ ╦╔═╗╦═╗
|
||||
// ║ ║ ║╠╩╗╠╩╗╚╦╝ ╚═╗║╣ ╠╦╝╚╗╔╝║╣ ╠╦╝
|
||||
// ╩═╝╚═╝╚═╝╚═╝ ╩ ╚═╝╚═╝╩╚═ ╚╝ ╚═╝╩╚═
|
||||
|
||||
#include "../global_define.h"
|
||||
|
||||
#include "EventLookup.h"
|
||||
#include "LobbyServer.h"
|
||||
|
||||
LobbyServer::LobbyServer()
|
||||
{
|
||||
m_running = false;
|
||||
m_listenSocket = INVALID_SOCKET;
|
||||
|
||||
m_clientSockets.clear();
|
||||
m_recvBuffer.resize( 1024 );
|
||||
}
|
||||
|
||||
LobbyServer::~LobbyServer()
|
||||
{
|
||||
}
|
||||
|
||||
void LobbyServer::Start( std::string ip, int32_t port )
|
||||
{
|
||||
m_listenSocket = ::WSASocket( AF_INET, SOCK_STREAM, IPPROTO_TCP, NULL, 0, WSA_FLAG_OVERLAPPED );
|
||||
if( m_listenSocket == INVALID_SOCKET )
|
||||
{
|
||||
Log::Error( "WSASocket() failed" );
|
||||
return;
|
||||
}
|
||||
|
||||
// Bind the socket
|
||||
sockaddr_in service;
|
||||
service.sin_family = AF_INET;
|
||||
service.sin_port = htons( port );
|
||||
service.sin_addr.s_addr = inet_addr( ip.c_str() );
|
||||
|
||||
if( bind( m_listenSocket, ( SOCKADDR * )&service, sizeof( service ) ) == SOCKET_ERROR )
|
||||
{
|
||||
Log::Error( "bind() failed" );
|
||||
return;
|
||||
}
|
||||
|
||||
// Listen on the socket
|
||||
if( listen( m_listenSocket, SOMAXCONN ) == SOCKET_ERROR )
|
||||
{
|
||||
Log::Error( "listen() failed" );
|
||||
return;
|
||||
}
|
||||
|
||||
// Start the server
|
||||
m_running = true;
|
||||
m_thread = std::thread( &LobbyServer::Run, this );
|
||||
|
||||
Log::Info( "Lobby Server started on %s:%d", ip.c_str(), port );
|
||||
}
|
||||
|
||||
void LobbyServer::Stop()
|
||||
{
|
||||
m_running = false;
|
||||
if( m_thread.joinable() )
|
||||
{
|
||||
m_thread.join();
|
||||
}
|
||||
}
|
||||
|
||||
void LobbyServer::Run()
|
||||
{
|
||||
FD_SET readSet;
|
||||
FD_SET writeSet;
|
||||
|
||||
timeval timeout = { 0, 1000 };
|
||||
|
||||
while( m_running )
|
||||
{
|
||||
FD_ZERO( &readSet );
|
||||
FD_ZERO( &writeSet );
|
||||
|
||||
FD_SET( m_listenSocket, &readSet );
|
||||
|
||||
// Process clients
|
||||
for( auto &client : m_clientSockets )
|
||||
{
|
||||
FD_SET( client->fd, &readSet );
|
||||
FD_SET( client->fd, &writeSet );
|
||||
}
|
||||
|
||||
auto result = select( 0, &readSet, &writeSet, NULL, &timeout );
|
||||
|
||||
if( result == SOCKET_ERROR )
|
||||
{
|
||||
std::this_thread::sleep_for( std::chrono::milliseconds( 1 ) );
|
||||
continue;
|
||||
}
|
||||
|
||||
if( FD_ISSET( m_listenSocket, &readSet ) )
|
||||
{
|
||||
AcceptNewClient();
|
||||
}
|
||||
|
||||
for( auto &client : m_clientSockets )
|
||||
{
|
||||
if( FD_ISSET( client->fd, &readSet ) )
|
||||
{
|
||||
ReadSocket( client );
|
||||
}
|
||||
|
||||
if( FD_ISSET( client->fd, &writeSet ) )
|
||||
{
|
||||
WriteSocket( client );
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void LobbyServer::AcceptNewClient()
|
||||
{
|
||||
sockaddr_in clientInfo{};
|
||||
int32_t addrSize = sizeof( clientInfo );
|
||||
|
||||
SOCKET clientSocket = accept( m_listenSocket, ( SOCKADDR * )&clientInfo, &addrSize );
|
||||
if( clientSocket == INVALID_SOCKET )
|
||||
{
|
||||
Log::Error( "accept() failed" );
|
||||
return;
|
||||
}
|
||||
|
||||
auto new_socket = std::make_shared< RealmTCPSocket >();
|
||||
new_socket->fd = clientSocket;
|
||||
new_socket->remote_address = clientInfo;
|
||||
new_socket->peer_ip_address = inet_ntoa( clientInfo.sin_addr );
|
||||
|
||||
m_clientSockets.push_back( new_socket );
|
||||
|
||||
Log::Info( "[LOBBY] New client connected : (%s)", new_socket->peer_ip_address.c_str() );
|
||||
}
|
||||
|
||||
void LobbyServer::ReadSocket( sptr_tcp_socket socket )
|
||||
{
|
||||
if( socket->flag.disconnected )
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
auto bytesReceived = recv( socket->fd, ( char * )m_recvBuffer.data(), m_recvBuffer.size(), 0 );
|
||||
|
||||
if( bytesReceived == SOCKET_ERROR )
|
||||
{
|
||||
auto error = WSAGetLastError();
|
||||
Log::Info( "Socket Error [%d].", error );
|
||||
socket->flag.disconnected = true;
|
||||
return;
|
||||
}
|
||||
|
||||
if( bytesReceived == 0 )
|
||||
{
|
||||
Log::Info( "Socket Disconnected." );
|
||||
socket->flag.disconnected = true;
|
||||
return;
|
||||
}
|
||||
|
||||
// Append the received data to the sockets processing buffer.
|
||||
// There's definitely a more elegant way of handling data here,
|
||||
// but this is just easier for now.
|
||||
socket->m_pendingReadBuffer.insert( socket->m_pendingReadBuffer.end(), m_recvBuffer.begin(), m_recvBuffer.begin() + bytesReceived );
|
||||
|
||||
// Handle valid packets in the buffer.
|
||||
while( socket->m_pendingReadBuffer.size() > 0 )
|
||||
{
|
||||
auto packetSize = htonl( *( int32_t * )&socket->m_pendingReadBuffer[ 0 ] );
|
||||
|
||||
if( packetSize > socket->m_pendingReadBuffer.size() )
|
||||
{
|
||||
break;
|
||||
}
|
||||
|
||||
//Log::Packet( socket->m_pendingReadBuffer, packetSize, false );
|
||||
|
||||
auto stream = std::make_shared< ByteStream >( socket->m_pendingReadBuffer.data() + 4, packetSize - 4 );
|
||||
|
||||
// Erase the packet from the buffer
|
||||
socket->m_pendingReadBuffer.erase( socket->m_pendingReadBuffer.begin(), socket->m_pendingReadBuffer.begin() + packetSize );
|
||||
|
||||
// Process the packet
|
||||
HandleRequest( socket, stream );
|
||||
}
|
||||
}
|
||||
|
||||
void LobbyServer::WriteSocket( sptr_tcp_socket socket )
|
||||
{
|
||||
if( socket->flag.disconnected )
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
if( socket->m_pendingWriteBuffer.empty() )
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
size_t totalBytesSent = 0;
|
||||
|
||||
//Log::Packet( socket->m_pendingWriteBuffer, socket->m_pendingWriteBuffer.size(), false );
|
||||
|
||||
while( true )
|
||||
{
|
||||
auto chunkSize = std::min< int >( socket->m_pendingWriteBuffer.size(), 1024 );
|
||||
auto bytesSent = send( socket->fd, ( char * )socket->m_pendingWriteBuffer.data(), chunkSize, 0 );
|
||||
|
||||
if( bytesSent == SOCKET_ERROR )
|
||||
{
|
||||
socket->flag.disconnected = true;
|
||||
return;
|
||||
}
|
||||
|
||||
totalBytesSent += bytesSent;
|
||||
|
||||
if( bytesSent < chunkSize )
|
||||
{
|
||||
break;
|
||||
}
|
||||
|
||||
if( totalBytesSent == socket->m_pendingWriteBuffer.size() )
|
||||
{
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
socket->m_pendingWriteBuffer.erase( socket->m_pendingWriteBuffer.begin(), socket->m_pendingWriteBuffer.begin() + totalBytesSent );
|
||||
}
|
||||
|
||||
void LobbyServer::HandleRequest( sptr_tcp_socket socket, sptr_byte_stream stream )
|
||||
{
|
||||
auto packetId = stream->read< uint16_t >();
|
||||
stream->set_position( 0 );
|
||||
|
||||
auto it = LOBBY_REQUEST_EVENT.find( packetId );
|
||||
if( it == LOBBY_REQUEST_EVENT.end() )
|
||||
{
|
||||
Log::Error( "[LOBBY] Unknown packet id : 0x%04X", packetId );
|
||||
return;
|
||||
}
|
||||
|
||||
Log::Debug( "[LOBBY] Request processed : 0x%04X", packetId );
|
||||
|
||||
auto request = it->second();
|
||||
|
||||
if( auto res = request->ProcessRequest( socket, stream ) )
|
||||
{
|
||||
socket->send( res );
|
||||
}
|
||||
}
|
||||
81
Lobby Server/LobbyServer.h
Normal file
81
Lobby Server/LobbyServer.h
Normal file
@@ -0,0 +1,81 @@
|
||||
#pragma once
|
||||
|
||||
#include <memory>
|
||||
#include <mutex>
|
||||
|
||||
class GameRoom {
|
||||
public:
|
||||
GameRoom()
|
||||
{
|
||||
m_gameType = GameType::PRIVATE;
|
||||
m_roomId = 0;
|
||||
m_ownerSessionId = L"";
|
||||
m_gameName = L"";
|
||||
m_minimumLevel = 0;
|
||||
m_maximumLevel = 0;
|
||||
}
|
||||
|
||||
~GameRoom()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
enum GameType {
|
||||
PRIVATE,
|
||||
PUBLIC
|
||||
} m_gameType;
|
||||
|
||||
int32_t m_roomId;
|
||||
std::wstring m_ownerSessionId;
|
||||
std::wstring m_gameName;
|
||||
int32_t m_minimumLevel;
|
||||
int32_t m_maximumLevel;
|
||||
};
|
||||
|
||||
typedef std::shared_ptr< GameRoom > sptr_game_room;
|
||||
|
||||
class LobbyServer
|
||||
{
|
||||
private:
|
||||
static inline std::unique_ptr< LobbyServer > m_instance;
|
||||
static inline std::mutex m_mutex;
|
||||
|
||||
Timer m_timer;
|
||||
std::atomic< bool > m_running;
|
||||
std::thread m_thread;
|
||||
|
||||
public:
|
||||
static LobbyServer& Get()
|
||||
{
|
||||
std::lock_guard< std::mutex > lock( m_mutex );
|
||||
if( m_instance == nullptr )
|
||||
{
|
||||
m_instance.reset( new LobbyServer() );
|
||||
}
|
||||
|
||||
return *m_instance;
|
||||
}
|
||||
|
||||
LobbyServer( const LobbyServer & ) = delete;
|
||||
LobbyServer &operator=( const LobbyServer & ) = delete;
|
||||
LobbyServer();
|
||||
~LobbyServer();
|
||||
|
||||
void Start( std::string ip, int32_t port );
|
||||
void Stop();
|
||||
bool isRunning() const
|
||||
{
|
||||
return m_running;
|
||||
}
|
||||
|
||||
private:
|
||||
SOCKET m_listenSocket;
|
||||
std::vector< sptr_tcp_socket > m_clientSockets;
|
||||
std::vector< uint8_t > m_recvBuffer;
|
||||
|
||||
void Run();
|
||||
void AcceptNewClient();
|
||||
void ReadSocket( sptr_tcp_socket socket );
|
||||
void WriteSocket( sptr_tcp_socket socket );
|
||||
void HandleRequest( sptr_tcp_socket socket, sptr_byte_stream stream );
|
||||
};
|
||||
19
Network/GenericNetMessage.hpp
Normal file
19
Network/GenericNetMessage.hpp
Normal file
@@ -0,0 +1,19 @@
|
||||
#pragma once
|
||||
|
||||
#include "../misc/ByteStream.h"
|
||||
|
||||
class GenericMessage
|
||||
{
|
||||
public:
|
||||
uint16_t m_packetId;
|
||||
ByteStream m_stream;
|
||||
|
||||
GenericMessage( uint16_t packetId ) : m_packetId( packetId )
|
||||
{
|
||||
}
|
||||
|
||||
virtual ~GenericMessage() = default;
|
||||
virtual ByteStream& Serialize() = 0;
|
||||
};
|
||||
|
||||
typedef std::shared_ptr< GenericMessage > sptr_generic_message;
|
||||
28
Network/GenericNetRequest.hpp
Normal file
28
Network/GenericNetRequest.hpp
Normal file
@@ -0,0 +1,28 @@
|
||||
#pragma once
|
||||
|
||||
#include "../Network/RealmSocket.h"
|
||||
#include "../misc/ByteStream.h"
|
||||
|
||||
class GenericResponse;
|
||||
typedef std::shared_ptr< GenericResponse > sptr_generic_response;
|
||||
|
||||
class GenericRequest
|
||||
{
|
||||
public:
|
||||
int16_t m_packetId;
|
||||
uint32_t m_requestId;
|
||||
|
||||
virtual ~GenericRequest() = default;
|
||||
|
||||
virtual sptr_generic_response ProcessRequest( sptr_tcp_socket socket, sptr_byte_stream stream ) = 0;
|
||||
void DeserializeHeader( sptr_byte_stream stream )
|
||||
{
|
||||
m_packetId = stream->read_u16();
|
||||
m_requestId = stream->read_u32();
|
||||
auto _ = stream->read_u32(); // Always 2 from client.
|
||||
};
|
||||
virtual void Deserialize( sptr_tcp_socket socket, sptr_byte_stream stream ) = 0;
|
||||
};
|
||||
|
||||
typedef std::shared_ptr< GenericRequest > sptr_generic_request;
|
||||
|
||||
21
Network/GenericNetResponse.hpp
Normal file
21
Network/GenericNetResponse.hpp
Normal file
@@ -0,0 +1,21 @@
|
||||
#pragma once
|
||||
|
||||
#include "GenericNetRequest.hpp"
|
||||
#include "../misc/ByteStream.h"
|
||||
|
||||
class GenericResponse
|
||||
{
|
||||
public:
|
||||
uint16_t m_packetId;
|
||||
uint32_t m_requestId;
|
||||
ByteStream m_stream;
|
||||
|
||||
GenericResponse( GenericRequest &request ) : m_packetId( request.m_packetId ), m_requestId( request.m_requestId )
|
||||
{
|
||||
}
|
||||
|
||||
virtual ~GenericResponse() = default;
|
||||
virtual ByteStream& Serialize() = 0;
|
||||
};
|
||||
|
||||
typedef std::shared_ptr< GenericResponse > sptr_generic_response;
|
||||
102
Network/RealmSocket.cpp
Normal file
102
Network/RealmSocket.cpp
Normal file
@@ -0,0 +1,102 @@
|
||||
|
||||
#include "../global_define.h"
|
||||
|
||||
RealmSocket::RealmSocket()
|
||||
{
|
||||
fd = INVALID_SOCKET;
|
||||
|
||||
memset( &local_address, 0, sizeof( local_address ) );
|
||||
memset( &remote_address, 0, sizeof( remote_address ) );
|
||||
port = 0;
|
||||
|
||||
flag.disconnected = 0;
|
||||
flag.is_listener = 0;
|
||||
flag.want_more_read_data = 0;
|
||||
flag.want_more_write_data = 0;
|
||||
|
||||
last_write_position = 0;
|
||||
|
||||
latency = 0;
|
||||
last_recv_time = 0;
|
||||
last_send_time = 0;
|
||||
}
|
||||
|
||||
RealmSocket::~RealmSocket()
|
||||
{
|
||||
if( INVALID_SOCKET != fd )
|
||||
{
|
||||
closesocket( fd );
|
||||
}
|
||||
|
||||
fd = INVALID_SOCKET;
|
||||
|
||||
memset( &local_address, 0, sizeof( local_address ) );
|
||||
memset( &remote_address, 0, sizeof( remote_address ) );
|
||||
port = 0;
|
||||
|
||||
flag.disconnected = 0;
|
||||
flag.is_listener = 0;
|
||||
flag.want_more_read_data = 0;
|
||||
flag.want_more_write_data = 0;
|
||||
|
||||
last_write_position = 0;
|
||||
|
||||
latency = 0;
|
||||
last_recv_time = 0;
|
||||
last_send_time = 0;
|
||||
}
|
||||
|
||||
RealmTCPSocket::RealmTCPSocket()
|
||||
{
|
||||
m_pendingWriteBuffer.reserve( WRITE_BUFFER_SIZE );
|
||||
}
|
||||
|
||||
RealmTCPSocket::~RealmTCPSocket()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
void RealmTCPSocket::send( const sptr_byte_stream stream )
|
||||
{
|
||||
auto packetSize = htonl( stream->get_position() );
|
||||
|
||||
m_pendingWriteBuffer.insert( m_pendingWriteBuffer.end(), ( uint8_t * )&packetSize, ( uint8_t * )&packetSize + 4 );
|
||||
m_pendingWriteBuffer.insert( m_pendingWriteBuffer.end(), stream->data.begin(), stream->data.end() );
|
||||
}
|
||||
|
||||
void RealmTCPSocket::send( const ByteStream &stream )
|
||||
{
|
||||
auto packetSize = htonl( stream.get_position() );
|
||||
|
||||
m_pendingWriteBuffer.insert( m_pendingWriteBuffer.end(), ( uint8_t * )&packetSize, ( uint8_t * )&packetSize + 4 );
|
||||
m_pendingWriteBuffer.insert( m_pendingWriteBuffer.end(), stream.data.begin(), stream.data.end() );
|
||||
}
|
||||
|
||||
void RealmTCPSocket::send( const sptr_generic_response response )
|
||||
{
|
||||
auto &stream = response->Serialize();
|
||||
auto netSize = htonl( stream.get_position() + 4 );
|
||||
|
||||
m_pendingWriteBuffer.insert( m_pendingWriteBuffer.end(), ( uint8_t * )&netSize, ( uint8_t * )&netSize + 4 );
|
||||
m_pendingWriteBuffer.insert( m_pendingWriteBuffer.end(), stream.data.begin(), stream.data.end() );
|
||||
|
||||
//Log::Packet( stream->data, packetSize, true );
|
||||
}
|
||||
|
||||
RealmUDPSocket::RealmUDPSocket()
|
||||
{
|
||||
}
|
||||
|
||||
RealmUDPSocket::~RealmUDPSocket()
|
||||
{
|
||||
}
|
||||
|
||||
void RealmUDPSocket::send( const sptr_byte_stream stream )
|
||||
{
|
||||
m_pendingWriteQueue.push( stream );
|
||||
}
|
||||
|
||||
void RealmUDPSocket::send( const ByteStream &stream )
|
||||
{
|
||||
m_pendingWriteQueue.push( std::make_shared< ByteStream >( stream ) );
|
||||
}
|
||||
122
Network/RealmSocket.h
Normal file
122
Network/RealmSocket.h
Normal file
@@ -0,0 +1,122 @@
|
||||
#pragma once
|
||||
|
||||
#include <vector>
|
||||
#include <queue>
|
||||
#include <memory>
|
||||
#include <mutex>
|
||||
|
||||
class GenericResponse;
|
||||
typedef std::shared_ptr< GenericResponse > sptr_generic_response;
|
||||
|
||||
class RealmSocket
|
||||
{
|
||||
public:
|
||||
RealmSocket();
|
||||
~RealmSocket();
|
||||
|
||||
virtual void send( const sptr_byte_stream stream ) = 0;
|
||||
virtual void send( const ByteStream &stream ) = 0;
|
||||
|
||||
struct s_flag
|
||||
{
|
||||
bool disconnected;
|
||||
bool is_listener;
|
||||
bool want_more_read_data;
|
||||
bool want_more_write_data;
|
||||
} flag;
|
||||
|
||||
// Comparison operator for sorting
|
||||
bool operator<( const RealmSocket &rhs ) const
|
||||
{
|
||||
return fd < rhs.fd;
|
||||
}
|
||||
|
||||
// Comparison operator for comparing
|
||||
bool operator==( const RealmSocket &rhs ) const
|
||||
{
|
||||
return fd == rhs.fd;
|
||||
}
|
||||
|
||||
SOCKET fd;
|
||||
uint16_t port;
|
||||
sockaddr_in local_address;
|
||||
sockaddr_in remote_address;
|
||||
|
||||
std::string peer_ip_address;
|
||||
int32_t peer_port;
|
||||
|
||||
uint32_t last_write_position;
|
||||
|
||||
uint64_t latency;
|
||||
uint64_t last_recv_time;
|
||||
uint64_t last_send_time;
|
||||
|
||||
std::mutex write_mutex;
|
||||
std::mutex read_mutex;
|
||||
|
||||
std::vector< uint8_t > read_buffer;
|
||||
//std::list< sptr_packet > read_queue;
|
||||
//std::list< sptr_packet > write_queue;
|
||||
};
|
||||
|
||||
class RealmTCPSocket : public RealmSocket
|
||||
{
|
||||
private:
|
||||
const size_t WRITE_BUFFER_SIZE = 65535;
|
||||
|
||||
public:
|
||||
RealmTCPSocket();
|
||||
~RealmTCPSocket();
|
||||
|
||||
// Comparison operator for sorting
|
||||
bool operator<( const RealmTCPSocket &rhs ) const
|
||||
{
|
||||
return fd < rhs.fd;
|
||||
}
|
||||
|
||||
// Comparison operator for comparing
|
||||
bool operator==( const RealmTCPSocket &rhs ) const
|
||||
{
|
||||
return fd == rhs.fd;
|
||||
}
|
||||
|
||||
void send( const sptr_byte_stream stream ) override;
|
||||
void send( const ByteStream &stream ) override;
|
||||
void send( const sptr_generic_response response );
|
||||
|
||||
public:
|
||||
std::vector< uint8_t > m_pendingWriteBuffer;
|
||||
std::vector< uint8_t > m_pendingReadBuffer;
|
||||
};
|
||||
|
||||
typedef std::shared_ptr< RealmTCPSocket > sptr_tcp_socket;
|
||||
|
||||
class RealmUDPSocket : public RealmSocket
|
||||
{
|
||||
private:
|
||||
const int DATAGRAM_SIZE = 1024;
|
||||
|
||||
public:
|
||||
RealmUDPSocket();
|
||||
~RealmUDPSocket();
|
||||
|
||||
// Comparison operator for sorting
|
||||
bool operator<( const RealmUDPSocket &rhs ) const
|
||||
{
|
||||
return fd < rhs.fd;
|
||||
}
|
||||
|
||||
// Comparison operator for comparing
|
||||
bool operator==( const RealmUDPSocket &rhs ) const
|
||||
{
|
||||
return fd == rhs.fd;
|
||||
}
|
||||
|
||||
void send( const sptr_byte_stream stream ) override;
|
||||
void send( const ByteStream &stream ) override;
|
||||
|
||||
public:
|
||||
std::queue< sptr_byte_stream > m_pendingWriteQueue;
|
||||
};
|
||||
|
||||
typedef std::shared_ptr< RealmUDPSocket > sptr_udp_socket;
|
||||
@@ -76,17 +76,19 @@
|
||||
<EnableManagedIncrementalBuild>true</EnableManagedIncrementalBuild>
|
||||
<TargetName>NorrathServer</TargetName>
|
||||
<OutDir>.\bin\</OutDir>
|
||||
<IncludePath>$(IncludePath)</IncludePath>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
|
||||
<LinkIncremental>true</LinkIncremental>
|
||||
<OutDir>.\bin\</OutDir>
|
||||
<TargetName>MasterServer</TargetName>
|
||||
<TargetName>NorrathServer_64</TargetName>
|
||||
<EnableManagedIncrementalBuild>true</EnableManagedIncrementalBuild>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
|
||||
<LinkIncremental>false</LinkIncremental>
|
||||
<OutDir>.\bin\</OutDir>
|
||||
<TargetName>NorrathServer</TargetName>
|
||||
<IncludePath>$(VC_IncludePath);$(WindowsSDK_IncludePath);</IncludePath>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
|
||||
<LinkIncremental>false</LinkIncremental>
|
||||
@@ -96,7 +98,7 @@
|
||||
<PrecompiledHeader>NotUsing</PrecompiledHeader>
|
||||
<WarningLevel>Level3</WarningLevel>
|
||||
<Optimization>Disabled</Optimization>
|
||||
<PreprocessorDefinitions>WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
|
||||
<PreprocessorDefinitions>WIN32;_DEBUG;_CONSOLE; LTC_RIJNDAEL;%(PreprocessorDefinitions)</PreprocessorDefinitions>
|
||||
<AdditionalIncludeDirectories>%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
|
||||
<MultiProcessorCompilation>true</MultiProcessorCompilation>
|
||||
<MinimalRebuild>false</MinimalRebuild>
|
||||
@@ -123,18 +125,19 @@
|
||||
<WarningLevel>Level3</WarningLevel>
|
||||
<Optimization>Disabled</Optimization>
|
||||
<PreprocessorDefinitions>_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
|
||||
<AdditionalIncludeDirectories>..\dependency\wolfssl;..\dependency\boost;..\dependency\json;..\dependency\clementine_ui\include;..\tools\src\;..\dependency\asio\include\;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
|
||||
<AdditionalIncludeDirectories>%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
|
||||
<MultiProcessorCompilation>true</MultiProcessorCompilation>
|
||||
<LanguageStandard>stdcpp17</LanguageStandard>
|
||||
<LanguageStandard>stdcpp20</LanguageStandard>
|
||||
</ClCompile>
|
||||
<Link>
|
||||
<SubSystem>Console</SubSystem>
|
||||
<AdditionalDependencies>odbc32.lib;ws2_32.lib;wolfssl.lib;%(AdditionalDependencies)</AdditionalDependencies>
|
||||
<AdditionalDependencies>ws2_32.lib;%(AdditionalDependencies)</AdditionalDependencies>
|
||||
<AdditionalLibraryDirectories>.\lib</AdditionalLibraryDirectories>
|
||||
<OutputFile>.\bin\MasterServer64.exe</OutputFile>
|
||||
<OutputFile>.\bin\NorrathServer_64.exe</OutputFile>
|
||||
</Link>
|
||||
<PostBuildEvent>
|
||||
<Command>echo F|xcopy /y /f "$(SolutionDir)../dependency/clementine_ui/bin/clementine_ui.dll" "$(TargetDir)/clementine_ui.dll" </Command>
|
||||
<Command>
|
||||
</Command>
|
||||
</PostBuildEvent>
|
||||
</ItemDefinitionGroup>
|
||||
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
|
||||
@@ -182,51 +185,85 @@
|
||||
</PostBuildEvent>
|
||||
</ItemDefinitionGroup>
|
||||
<ItemGroup>
|
||||
<ClInclude Include="configuration.h" />
|
||||
<ClInclude Include="game\client.h" />
|
||||
<ClInclude Include="game\client_manager.h" />
|
||||
<ClInclude Include="Crypto\NorrathCrypt.h" />
|
||||
<ClInclude Include="Discovery Server\DiscoveryServer.h" />
|
||||
<ClInclude Include="Discovery Server\DiscoverySession.h" />
|
||||
<ClInclude Include="Game\RealmUser.h" />
|
||||
<ClInclude Include="Game\RealmUserManager.h" />
|
||||
<ClInclude Include="Game\GameSession.h" />
|
||||
<ClInclude Include="Game\GameSessionManager.h" />
|
||||
<ClInclude Include="Gateway Server\EventHandlers\GatewayEvents.h" />
|
||||
<ClInclude Include="Gateway Server\EventHandlers\GetServerAddressEvent.h" />
|
||||
<ClInclude Include="Gateway Server\GatewayServer.h" />
|
||||
<ClInclude Include="global_define.h" />
|
||||
<ClInclude Include="misc\AES.h" />
|
||||
<ClInclude Include="Lobby Server\EventLookup.h" />
|
||||
<ClInclude Include="Lobby Server\Event\NotifyClientDiscovered.h" />
|
||||
<ClInclude Include="Lobby Server\Event\NotifyClientReqConnect.h" />
|
||||
<ClInclude Include="Lobby Server\Event\NotifyGameDiscovered.h" />
|
||||
<ClInclude Include="Lobby Server\Event\RequestCancelGame.h" />
|
||||
<ClInclude Include="Lobby Server\Event\RequestCreateAccount.h" />
|
||||
<ClInclude Include="Lobby Server\Event\RequestCreatePrivateGame.h" />
|
||||
<ClInclude Include="Lobby Server\Event\RequestCreatePublicGame.h" />
|
||||
<ClInclude Include="Lobby Server\Event\RequestDoClientDiscovery.h" />
|
||||
<ClInclude Include="Lobby Server\Event\RequestGetEncryptionKey.h" />
|
||||
<ClInclude Include="Lobby Server\Event\RequestGetRules.h" />
|
||||
<ClInclude Include="Lobby Server\Event\RequestLogin.h" />
|
||||
<ClInclude Include="Lobby Server\Event\RequestLogout.h" />
|
||||
<ClInclude Include="Lobby Server\Event\RequestMatchGame.h" />
|
||||
<ClInclude Include="Lobby Server\Event\RequestTouchSession.h" />
|
||||
<ClInclude Include="Lobby Server\LobbyServer.h" />
|
||||
<ClInclude Include="logging.h" />
|
||||
<ClInclude Include="misc\ByteStream.h" />
|
||||
<ClInclude Include="misc\Encryptor.h" />
|
||||
<ClInclude Include="misc\RealmCrypt.h" />
|
||||
<ClInclude Include="misc\math.h" />
|
||||
<ClInclude Include="misc\threadsafe_queue.hpp" />
|
||||
<ClInclude Include="misc\Timer.h" />
|
||||
<ClInclude Include="network\protocol_broker.h" />
|
||||
<ClInclude Include="network\protocol_game.h" />
|
||||
<ClInclude Include="network\packet.h" />
|
||||
<ClInclude Include="network\protocol_gateway.h" />
|
||||
<ClInclude Include="network\socket.h" />
|
||||
<ClInclude Include="network\socket_manager.h" />
|
||||
<ClInclude Include="Network\GenericNetMessage.hpp" />
|
||||
<ClInclude Include="Network\GenericNetRequest.hpp" />
|
||||
<ClInclude Include="Network\GenericNetResponse.hpp" />
|
||||
<ClInclude Include="network\RealmSocket.h" />
|
||||
<ClInclude Include="resource.h" />
|
||||
<ClInclude Include="stdafx.h" />
|
||||
<ClInclude Include="targetver.h" />
|
||||
<ClInclude Include="ui\logging.h" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ClCompile Include="configuration.cpp" />
|
||||
<ClCompile Include="game\client.cpp" />
|
||||
<ClCompile Include="game\client_manager.cpp" />
|
||||
<ClCompile Include="Crypto\NorrathCrypt.cpp" />
|
||||
<ClCompile Include="Discovery Server\DiscoveryServer.cpp" />
|
||||
<ClCompile Include="Discovery Server\DiscoverySession.cpp" />
|
||||
<ClCompile Include="Game\RealmUser.cpp" />
|
||||
<ClCompile Include="Game\RealmUserManager.cpp" />
|
||||
<ClCompile Include="Game\GameSession.cpp" />
|
||||
<ClCompile Include="Game\GameSessionManager.cpp" />
|
||||
<ClCompile Include="Gateway Server\EventHandlers\GetServerAddressEvent.cpp" />
|
||||
<ClCompile Include="Gateway Server\GatewayServer.cpp" />
|
||||
<ClCompile Include="global_define.cpp" />
|
||||
<ClCompile Include="misc\AES.cpp" />
|
||||
<ClCompile Include="Lobby Server\Event\NotifyClientDiscovered.cpp" />
|
||||
<ClCompile Include="Lobby Server\Event\NotifyClientReqConnect.cpp" />
|
||||
<ClCompile Include="Lobby Server\Event\NotifyGameDiscovered.cpp" />
|
||||
<ClCompile Include="Lobby Server\Event\RequestCancelGame.cpp" />
|
||||
<ClCompile Include="Lobby Server\Event\RequestCreateAccount.cpp" />
|
||||
<ClCompile Include="Lobby Server\Event\RequestCreatePrivateGame.cpp" />
|
||||
<ClCompile Include="Lobby Server\Event\RequestCreatePublicGame.cpp" />
|
||||
<ClCompile Include="Lobby Server\Event\RequestDoClientDiscovery.cpp" />
|
||||
<ClCompile Include="Lobby Server\Event\RequestGetEncryptionKey.cpp" />
|
||||
<ClCompile Include="Lobby Server\Event\RequestGetRules.cpp" />
|
||||
<ClCompile Include="Lobby Server\Event\RequestLogin.cpp" />
|
||||
<ClCompile Include="Lobby Server\Event\RequestLogout.cpp" />
|
||||
<ClCompile Include="Lobby Server\Event\RequestMatchGame.cpp" />
|
||||
<ClCompile Include="Lobby Server\Event\RequestTouchSession.cpp" />
|
||||
<ClCompile Include="Lobby Server\LobbyServer.cpp" />
|
||||
<ClCompile Include="logging.cpp" />
|
||||
<ClCompile Include="misc\ByteStream.cpp" />
|
||||
<ClCompile Include="misc\Encryptor.cpp" />
|
||||
<ClCompile Include="misc\RealmCrypt.cpp" />
|
||||
<ClCompile Include="main.cpp" />
|
||||
<ClCompile Include="misc\math.cpp" />
|
||||
<ClCompile Include="misc\Timer.cpp" />
|
||||
<ClCompile Include="network\protocol_broker.cpp" />
|
||||
<ClCompile Include="network\protocol_game.cpp" />
|
||||
<ClCompile Include="network\protocol_gateway.cpp" />
|
||||
<ClCompile Include="network\packet.cpp" />
|
||||
<ClCompile Include="network\socket.cpp" />
|
||||
<ClCompile Include="network\socket_manager.cpp" />
|
||||
<ClCompile Include="network\RealmSocket.cpp" />
|
||||
<ClCompile Include="stdafx.cpp">
|
||||
<PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">Create</PrecompiledHeader>
|
||||
<PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">Create</PrecompiledHeader>
|
||||
<PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">Create</PrecompiledHeader>
|
||||
<PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Release|x64'">Create</PrecompiledHeader>
|
||||
</ClCompile>
|
||||
<ClCompile Include="ui\logging.cpp" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ResourceCompile Include="Norrath Server.rc" />
|
||||
|
||||
@@ -9,29 +9,62 @@
|
||||
<UniqueIdentifier>{93995380-89BD-4b04-88EB-625FBE52EBFB}</UniqueIdentifier>
|
||||
<Extensions>h;hh;hpp;hxx;hm;inl;inc;xsd</Extensions>
|
||||
</Filter>
|
||||
<Filter Include="Header Files\network">
|
||||
<UniqueIdentifier>{d03ff7f7-63d1-43a7-b2cc-4b585130f545}</UniqueIdentifier>
|
||||
<Filter Include="Source Files\Gateway Server">
|
||||
<UniqueIdentifier>{9eabece2-9fe0-499d-a5b0-f001d155f4d3}</UniqueIdentifier>
|
||||
</Filter>
|
||||
<Filter Include="Source Files\network">
|
||||
<UniqueIdentifier>{d2399894-b1e4-4a31-86ac-a4fa3a1b7e76}</UniqueIdentifier>
|
||||
</Filter>
|
||||
<Filter Include="Header Files\game">
|
||||
<UniqueIdentifier>{20330632-08bb-481a-bee5-3148c43dd451}</UniqueIdentifier>
|
||||
</Filter>
|
||||
<Filter Include="Header Files\ui">
|
||||
<UniqueIdentifier>{767ee381-3651-4dd0-bc78-7037cefe7006}</UniqueIdentifier>
|
||||
</Filter>
|
||||
<Filter Include="Source Files\game">
|
||||
<UniqueIdentifier>{cfe30c00-4a52-4659-94af-3ac2712690c6}</UniqueIdentifier>
|
||||
</Filter>
|
||||
<Filter Include="Header Files\misc">
|
||||
<UniqueIdentifier>{8b5e9b37-079d-4c08-99b0-c05b88543b70}</UniqueIdentifier>
|
||||
</Filter>
|
||||
<Filter Include="Source Files\misc">
|
||||
<Filter Include="Source Files\Common">
|
||||
<UniqueIdentifier>{0ecae2be-b6b6-4ee2-bcb2-9252f189acca}</UniqueIdentifier>
|
||||
</Filter>
|
||||
<Filter Include="Source Files\ui">
|
||||
<UniqueIdentifier>{4a905260-4faf-4748-be14-8971a80b46b5}</UniqueIdentifier>
|
||||
<Filter Include="Header Files\Common">
|
||||
<UniqueIdentifier>{8b5e9b37-079d-4c08-99b0-c05b88543b70}</UniqueIdentifier>
|
||||
</Filter>
|
||||
<Filter Include="Source Files\Network">
|
||||
<UniqueIdentifier>{d2399894-b1e4-4a31-86ac-a4fa3a1b7e76}</UniqueIdentifier>
|
||||
</Filter>
|
||||
<Filter Include="Header Files\Network">
|
||||
<UniqueIdentifier>{d03ff7f7-63d1-43a7-b2cc-4b585130f545}</UniqueIdentifier>
|
||||
</Filter>
|
||||
<Filter Include="Header Files\Gateway Server">
|
||||
<UniqueIdentifier>{b87d23fc-9dc6-4e3a-949a-6d1b3ac2efb0}</UniqueIdentifier>
|
||||
</Filter>
|
||||
<Filter Include="Source Files\Lobby Server">
|
||||
<UniqueIdentifier>{92e2e64a-8125-49a7-8eb0-5e96f8d7d7d0}</UniqueIdentifier>
|
||||
</Filter>
|
||||
<Filter Include="Header Files\Lobby Server">
|
||||
<UniqueIdentifier>{5f4b890f-79df-40dc-971a-5463a861107f}</UniqueIdentifier>
|
||||
</Filter>
|
||||
<Filter Include="Header Files\Game">
|
||||
<UniqueIdentifier>{39350066-8ddc-4899-8102-5f346667aa37}</UniqueIdentifier>
|
||||
</Filter>
|
||||
<Filter Include="Source Files\Game">
|
||||
<UniqueIdentifier>{3b0176b6-97aa-4e67-ab27-60626f71b573}</UniqueIdentifier>
|
||||
</Filter>
|
||||
<Filter Include="Source Files\Gateway Server\EventHandlers">
|
||||
<UniqueIdentifier>{f90649a3-247a-4a65-9ec2-3fca02c7af52}</UniqueIdentifier>
|
||||
</Filter>
|
||||
<Filter Include="Header Files\Gateway Server\EventHandlers">
|
||||
<UniqueIdentifier>{01a6a552-7c0d-4ca4-b4d1-5c05d6048fda}</UniqueIdentifier>
|
||||
</Filter>
|
||||
<Filter Include="Crypto">
|
||||
<UniqueIdentifier>{d4bad384-e0dc-4704-8471-c91277eb6d52}</UniqueIdentifier>
|
||||
</Filter>
|
||||
<Filter Include="Header Files\Crypto">
|
||||
<UniqueIdentifier>{a8fcffaa-8fc1-4dbc-a005-b5ddb78ee586}</UniqueIdentifier>
|
||||
</Filter>
|
||||
<Filter Include="Source Files\Crypto">
|
||||
<UniqueIdentifier>{845ad25e-a3d9-42ac-826b-43ddeb655c25}</UniqueIdentifier>
|
||||
</Filter>
|
||||
<Filter Include="Header Files\Discovery Server">
|
||||
<UniqueIdentifier>{d2bb2db4-f015-43f6-a014-6c5d3d620ba4}</UniqueIdentifier>
|
||||
</Filter>
|
||||
<Filter Include="Source Files\Discovery Server">
|
||||
<UniqueIdentifier>{76928587-566c-4842-9fa3-1a5baec12238}</UniqueIdentifier>
|
||||
</Filter>
|
||||
<Filter Include="Header Files\Lobby Server\Event">
|
||||
<UniqueIdentifier>{fe6aba50-18f0-46a1-b154-4226b3ca42ab}</UniqueIdentifier>
|
||||
</Filter>
|
||||
<Filter Include="Source Files\Lobby Server\Event">
|
||||
<UniqueIdentifier>{6432e486-7341-4eb8-a6c0-c21ecd2e92f8}</UniqueIdentifier>
|
||||
</Filter>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
@@ -41,59 +74,119 @@
|
||||
<ClInclude Include="targetver.h">
|
||||
<Filter>Header Files</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="network\socket.h">
|
||||
<Filter>Header Files\network</Filter>
|
||||
<ClInclude Include="network\RealmSocket.h">
|
||||
<Filter>Header Files\Network</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="global_define.h">
|
||||
<Filter>Header Files</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="game\client.h">
|
||||
<Filter>Header Files\game</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="configuration.h">
|
||||
<Filter>Header Files</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="misc\math.h">
|
||||
<Filter>Header Files\misc</Filter>
|
||||
<Filter>Header Files\Common</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="misc\Timer.h">
|
||||
<Filter>Header Files\misc</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="game\client_manager.h">
|
||||
<Filter>Header Files\game</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="network\socket_manager.h">
|
||||
<Filter>Header Files\network</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="network\packet.h">
|
||||
<Filter>Header Files\network</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="ui\logging.h">
|
||||
<Filter>Header Files\ui</Filter>
|
||||
<Filter>Header Files\Common</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="misc\threadsafe_queue.hpp">
|
||||
<Filter>Header Files\misc</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="network\protocol_game.h">
|
||||
<Filter>Header Files\network</Filter>
|
||||
<Filter>Header Files\Common</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="resource.h">
|
||||
<Filter>Header Files</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="misc\Encryptor.h">
|
||||
<Filter>Header Files\misc</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="network\protocol_gateway.h">
|
||||
<Filter>Header Files\network</Filter>
|
||||
<ClInclude Include="misc\RealmCrypt.h">
|
||||
<Filter>Header Files\Common</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="misc\ByteStream.h">
|
||||
<Filter>Header Files\misc</Filter>
|
||||
<Filter>Header Files\Common</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="misc\AES.h">
|
||||
<Filter>Header Files\misc</Filter>
|
||||
<ClInclude Include="logging.h">
|
||||
<Filter>Header Files</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="network\protocol_broker.h">
|
||||
<Filter>Header Files\network</Filter>
|
||||
<ClInclude Include="Gateway Server\GatewayServer.h">
|
||||
<Filter>Header Files\Gateway Server</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="Network\GenericNetRequest.hpp">
|
||||
<Filter>Header Files\Network</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="Network\GenericNetResponse.hpp">
|
||||
<Filter>Header Files\Network</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="Lobby Server\LobbyServer.h">
|
||||
<Filter>Header Files\Lobby Server</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="Game\RealmUser.h">
|
||||
<Filter>Header Files\Game</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="Game\RealmUserManager.h">
|
||||
<Filter>Header Files\Game</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="Gateway Server\EventHandlers\GatewayEvents.h">
|
||||
<Filter>Header Files\Gateway Server\EventHandlers</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="Gateway Server\EventHandlers\GetServerAddressEvent.h">
|
||||
<Filter>Header Files\Gateway Server\EventHandlers</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="Crypto\NorrathCrypt.h">
|
||||
<Filter>Header Files\Crypto</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="Lobby Server\EventLookup.h">
|
||||
<Filter>Header Files\Lobby Server</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="Discovery Server\DiscoveryServer.h">
|
||||
<Filter>Header Files\Discovery Server</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="Discovery Server\DiscoverySession.h">
|
||||
<Filter>Header Files\Discovery Server</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="Game\GameSession.h">
|
||||
<Filter>Header Files\Game</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="Game\GameSessionManager.h">
|
||||
<Filter>Header Files\Game</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="Lobby Server\Event\RequestCancelGame.h">
|
||||
<Filter>Header Files\Lobby Server\Event</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="Lobby Server\Event\RequestCreateAccount.h">
|
||||
<Filter>Header Files\Lobby Server\Event</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="Lobby Server\Event\RequestCreatePrivateGame.h">
|
||||
<Filter>Header Files\Lobby Server\Event</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="Lobby Server\Event\RequestCreatePublicGame.h">
|
||||
<Filter>Header Files\Lobby Server\Event</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="Lobby Server\Event\RequestDoClientDiscovery.h">
|
||||
<Filter>Header Files\Lobby Server\Event</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="Lobby Server\Event\RequestGetEncryptionKey.h">
|
||||
<Filter>Header Files\Lobby Server\Event</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="Lobby Server\Event\RequestGetRules.h">
|
||||
<Filter>Header Files\Lobby Server\Event</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="Lobby Server\Event\RequestLogin.h">
|
||||
<Filter>Header Files\Lobby Server\Event</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="Lobby Server\Event\RequestLogout.h">
|
||||
<Filter>Header Files\Lobby Server\Event</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="Lobby Server\Event\RequestMatchGame.h">
|
||||
<Filter>Header Files\Lobby Server\Event</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="Lobby Server\Event\RequestTouchSession.h">
|
||||
<Filter>Header Files\Lobby Server\Event</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="Lobby Server\Event\NotifyClientDiscovered.h">
|
||||
<Filter>Header Files\Lobby Server\Event</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="Lobby Server\Event\NotifyClientReqConnect.h">
|
||||
<Filter>Header Files\Lobby Server\Event</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="Lobby Server\Event\NotifyGameDiscovered.h">
|
||||
<Filter>Header Files\Lobby Server\Event</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="Network\GenericNetMessage.hpp">
|
||||
<Filter>Header Files\Network</Filter>
|
||||
</ClInclude>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
@@ -103,53 +196,95 @@
|
||||
<ClCompile Include="global_define.cpp">
|
||||
<Filter>Source Files</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="configuration.cpp">
|
||||
<Filter>Source Files</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="game\client.cpp">
|
||||
<Filter>Source Files\game</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="misc\Timer.cpp">
|
||||
<Filter>Source Files\misc</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="misc\math.cpp">
|
||||
<Filter>Source Files\misc</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="game\client_manager.cpp">
|
||||
<Filter>Source Files\game</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="network\socket_manager.cpp">
|
||||
<Filter>Source Files\network</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="network\packet.cpp">
|
||||
<Filter>Source Files\network</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="ui\logging.cpp">
|
||||
<Filter>Source Files\ui</Filter>
|
||||
<Filter>Source Files\Common</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="main.cpp">
|
||||
<Filter>Source Files</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="network\protocol_gateway.cpp">
|
||||
<Filter>Source Files\network</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="misc\Encryptor.cpp">
|
||||
<Filter>Source Files\misc</Filter>
|
||||
<ClCompile Include="misc\RealmCrypt.cpp">
|
||||
<Filter>Source Files\Common</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="misc\ByteStream.cpp">
|
||||
<Filter>Source Files\misc</Filter>
|
||||
<Filter>Source Files\Common</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="network\protocol_game.cpp">
|
||||
<Filter>Source Files\network</Filter>
|
||||
<ClCompile Include="Gateway Server\GatewayServer.cpp">
|
||||
<Filter>Source Files\Gateway Server</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="network\socket.cpp">
|
||||
<ClCompile Include="logging.cpp">
|
||||
<Filter>Source Files</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="misc\AES.cpp">
|
||||
<Filter>Source Files\misc</Filter>
|
||||
<ClCompile Include="network\RealmSocket.cpp">
|
||||
<Filter>Source Files\Network</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="network\protocol_broker.cpp">
|
||||
<Filter>Source Files\network</Filter>
|
||||
<ClCompile Include="Lobby Server\LobbyServer.cpp">
|
||||
<Filter>Source Files\Lobby Server</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="Game\RealmUser.cpp">
|
||||
<Filter>Source Files\Game</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="Game\RealmUserManager.cpp">
|
||||
<Filter>Source Files\Game</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="Gateway Server\EventHandlers\GetServerAddressEvent.cpp">
|
||||
<Filter>Source Files\Gateway Server\EventHandlers</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="Crypto\NorrathCrypt.cpp">
|
||||
<Filter>Source Files\Crypto</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="Discovery Server\DiscoveryServer.cpp">
|
||||
<Filter>Source Files\Discovery Server</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="Discovery Server\DiscoverySession.cpp">
|
||||
<Filter>Source Files\Discovery Server</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="Game\GameSession.cpp">
|
||||
<Filter>Source Files\Game</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="Game\GameSessionManager.cpp">
|
||||
<Filter>Source Files\Game</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="Lobby Server\Event\RequestCancelGame.cpp">
|
||||
<Filter>Source Files\Lobby Server\Event</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="Lobby Server\Event\RequestCreateAccount.cpp">
|
||||
<Filter>Source Files\Lobby Server\Event</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="Lobby Server\Event\RequestCreatePrivateGame.cpp">
|
||||
<Filter>Source Files\Lobby Server\Event</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="Lobby Server\Event\RequestCreatePublicGame.cpp">
|
||||
<Filter>Source Files\Lobby Server\Event</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="Lobby Server\Event\RequestDoClientDiscovery.cpp">
|
||||
<Filter>Source Files\Lobby Server\Event</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="Lobby Server\Event\RequestGetEncryptionKey.cpp">
|
||||
<Filter>Source Files\Lobby Server\Event</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="Lobby Server\Event\RequestGetRules.cpp">
|
||||
<Filter>Source Files\Lobby Server\Event</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="Lobby Server\Event\RequestLogin.cpp">
|
||||
<Filter>Source Files\Lobby Server\Event</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="Lobby Server\Event\RequestLogout.cpp">
|
||||
<Filter>Source Files\Lobby Server\Event</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="Lobby Server\Event\RequestMatchGame.cpp">
|
||||
<Filter>Source Files\Lobby Server\Event</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="Lobby Server\Event\RequestTouchSession.cpp">
|
||||
<Filter>Source Files\Lobby Server\Event</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="Lobby Server\Event\NotifyClientDiscovered.cpp">
|
||||
<Filter>Source Files\Lobby Server\Event</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="Lobby Server\Event\NotifyClientReqConnect.cpp">
|
||||
<Filter>Source Files\Lobby Server\Event</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="Lobby Server\Event\NotifyGameDiscovered.cpp">
|
||||
<Filter>Source Files\Lobby Server\Event</Filter>
|
||||
</ClCompile>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
|
||||
@@ -1,11 +0,0 @@
|
||||
#include "..\global_define.h"
|
||||
|
||||
CClient::CClient()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
CClient::~CClient()
|
||||
{
|
||||
|
||||
}
|
||||
@@ -1,15 +0,0 @@
|
||||
#pragma once
|
||||
|
||||
#include "../misc/Encryptor.h"
|
||||
|
||||
class CClient {
|
||||
public:
|
||||
CClient();
|
||||
~CClient();
|
||||
|
||||
sptr_socket socket;
|
||||
|
||||
Encryptor encryptor;
|
||||
};
|
||||
|
||||
typedef std::shared_ptr< CClient > sptr_client;
|
||||
@@ -1,48 +0,0 @@
|
||||
#include "../global_define.h"
|
||||
|
||||
// Spawn a new client
|
||||
bool CClientManager::spawn_new( sptr_socket socket, sptr_client &ret )
|
||||
{
|
||||
sptr_client client = std::make_shared< CClient >();
|
||||
client->socket = socket;
|
||||
|
||||
// The result if the 'insert'
|
||||
std::pair< std::map< SOCKET, sptr_client >::iterator, bool > result;
|
||||
|
||||
// Insert the client into the map - keyed by its socket.
|
||||
result = client_map.insert( std::make_pair( socket->fd, client ) );
|
||||
|
||||
ret = ( *result.first ).second;
|
||||
|
||||
return result.second;
|
||||
}
|
||||
|
||||
// Find an existing client in the service
|
||||
bool CClientManager::get_client( sptr_socket socket, sptr_client &ret )
|
||||
{
|
||||
std::map< SOCKET, sptr_client >::iterator it;
|
||||
if( client_map.end() == ( it = client_map.find( socket->fd ) ) ) return false;
|
||||
|
||||
ret = ( *it ).second;
|
||||
return true;
|
||||
}
|
||||
|
||||
// Find and remove a client by its socket
|
||||
void CClientManager::remove_client( sptr_socket socket )
|
||||
{
|
||||
std::map< SOCKET, sptr_client >::iterator it;
|
||||
if( client_map.end() == ( it = client_map.find( socket->fd ) ) ) return;
|
||||
|
||||
client_map.erase( it );
|
||||
}
|
||||
|
||||
// Remove a client and mark the socket for disconnection
|
||||
void CClientManager::disconnect( sptr_client c )
|
||||
{
|
||||
std::map< SOCKET, sptr_client >::iterator it;
|
||||
if( client_map.end() == ( it = client_map.find( c->socket->fd ) ) ) return;
|
||||
|
||||
c->socket->flag.disconnected = 1;
|
||||
|
||||
client_map.erase( it );
|
||||
}
|
||||
@@ -1,29 +0,0 @@
|
||||
#pragma once
|
||||
|
||||
#include <map>
|
||||
|
||||
class CClientManager {
|
||||
public:
|
||||
CClientManager()
|
||||
{
|
||||
client_map.clear();
|
||||
}
|
||||
|
||||
~CClientManager()
|
||||
{
|
||||
client_map.clear();
|
||||
}
|
||||
|
||||
// Add a new client
|
||||
bool spawn_new( sptr_socket socket, sptr_client &ret );
|
||||
|
||||
// Fine an existing client in the service
|
||||
bool get_client( sptr_socket socket, sptr_client &ret );
|
||||
|
||||
// Remove an existing client in the service
|
||||
void remove_client( sptr_socket socket );
|
||||
|
||||
void disconnect( sptr_client client );
|
||||
|
||||
std::map< SOCKET, sptr_client > client_map;
|
||||
};
|
||||
@@ -1,28 +0,0 @@
|
||||
#include "global_define.h"
|
||||
|
||||
CLogManager logging;
|
||||
|
||||
std::unique_ptr< CTimer > server_time;
|
||||
std::unique_ptr< CClientManager > client_manager;
|
||||
std::unique_ptr< CSocketManager > socket_manager;
|
||||
|
||||
void Initialize_Global()
|
||||
{
|
||||
try
|
||||
{
|
||||
// Start Timer
|
||||
server_time = std::make_unique< CTimer >();
|
||||
server_time->Start();
|
||||
|
||||
// Start Client Manager
|
||||
client_manager = std::make_unique < CClientManager >();
|
||||
|
||||
// Start Network
|
||||
socket_manager = std::make_unique < CSocketManager >();
|
||||
socket_manager->initialize();
|
||||
}
|
||||
catch( std::exception e )
|
||||
{
|
||||
logging.error( "%s\n", e.what() );
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,33 +1,32 @@
|
||||
#pragma once
|
||||
|
||||
#define _WINSOCK_DEPRECATED_NO_WARNINGS
|
||||
#define FD_SETSIZE 1024
|
||||
#include <WinSock2.h>
|
||||
|
||||
#include <Windows.h>
|
||||
#include <cstdint>
|
||||
#include <memory>
|
||||
|
||||
#include "misc/math.h"
|
||||
#include "misc/Timer.h"
|
||||
|
||||
#include "misc/threadsafe_queue.hpp"
|
||||
#include "misc/Encryptor.h"
|
||||
#include "misc/ByteStream.h"
|
||||
|
||||
#include "ui/logging.h"
|
||||
#include "configuration.h"
|
||||
#include "misc/threadsafe_queue.hpp"
|
||||
#include "misc/RealmCrypt.h"
|
||||
#include "misc/ByteStream.h"
|
||||
|
||||
#include "network/socket.h"
|
||||
#include "network/packet.h"
|
||||
#include "network/socket_manager.h"
|
||||
#include "Network/RealmSocket.h"
|
||||
|
||||
#include "game/client.h"
|
||||
#include "game/client_manager.h"
|
||||
#include "network/protocol_gateway.h"
|
||||
#include "network/protocol_game.h"
|
||||
#include "Game/RealmUserManager.h"
|
||||
#include "Game/GameSessionManager.h"
|
||||
|
||||
extern CLogManager logging;
|
||||
extern std::unique_ptr< CTimer > server_time;
|
||||
extern std::unique_ptr< CClientManager > client_manager;
|
||||
extern std::unique_ptr< CSocketManager > socket_manager;
|
||||
#include "Network/GenericNetRequest.hpp"
|
||||
#include "Network/GenericNetResponse.hpp"
|
||||
#include "Network/GenericNetMessage.hpp"
|
||||
|
||||
void Initialize_Global();
|
||||
#include "Gateway Server/GatewayServer.h"
|
||||
#include "Lobby Server/LobbyServer.h"
|
||||
#include "Discovery Server/DiscoveryServer.h"
|
||||
|
||||
#include "logging.h"
|
||||
#include "configuration.h"
|
||||
@@ -1,4 +1,5 @@
|
||||
#include "../global_define.h"
|
||||
|
||||
#include "logging.h"
|
||||
|
||||
static const char* LOG_PATH[] = {
|
||||
"./generic",
|
||||
@@ -6,19 +7,16 @@ static const char* LOG_PATH[] = {
|
||||
"./error"
|
||||
};
|
||||
|
||||
#include <mutex>
|
||||
static std::mutex log_lock;
|
||||
|
||||
void command_callback( void* ptr, size_t len )
|
||||
Log::Log()
|
||||
{
|
||||
const wchar_t* p = reinterpret_cast< const wchar_t* >( ptr );
|
||||
std::wstring command( p, p + len );
|
||||
|
||||
// Temp
|
||||
logging.information( "Server command: %S", command.c_str() );
|
||||
current_open_hour = 0;
|
||||
}
|
||||
|
||||
const char* CLogManager::get_time_stamp()
|
||||
Log::~Log()
|
||||
{
|
||||
}
|
||||
|
||||
const char *Log::GetTimeStamp()
|
||||
{
|
||||
static char timestamp[ 64 ] = "";
|
||||
|
||||
@@ -38,7 +36,7 @@ const char* CLogManager::get_time_stamp()
|
||||
return timestamp;
|
||||
}
|
||||
|
||||
void CLogManager::check_file_status( LOG_TYPE type )
|
||||
void Log::CheckFileStatus( LOG_TYPE type )
|
||||
{
|
||||
time_t t; time( &t );
|
||||
struct tm date_tm;
|
||||
@@ -68,7 +66,7 @@ void CLogManager::check_file_status( LOG_TYPE type )
|
||||
}
|
||||
}
|
||||
|
||||
void CLogManager::write_log( LOG_TYPE type, std::string format )
|
||||
void Log::WriteToLog( LOG_TYPE type, std::string format )
|
||||
{
|
||||
log_lock.lock();
|
||||
|
||||
@@ -79,6 +77,7 @@ void CLogManager::write_log( LOG_TYPE type, std::string format )
|
||||
{
|
||||
case LOG_TYPE::log_generic: printf( "[INFO]: " ); break;
|
||||
case LOG_TYPE::log_debug: printf( "[DEBUG]: " ); break;
|
||||
case LOG_TYPE::log_warn: printf( "[WARN]: " ); break;
|
||||
case LOG_TYPE::log_error: printf( "[ERROR]: " ); break;
|
||||
}
|
||||
|
||||
@@ -86,13 +85,13 @@ void CLogManager::write_log( LOG_TYPE type, std::string format )
|
||||
|
||||
printf( "%s\n", format.c_str() );
|
||||
|
||||
check_file_status( type );
|
||||
file_stream[ type ] << get_time_stamp() << format << '\n';
|
||||
CheckFileStatus( type );
|
||||
file_stream[ type ] << GetTimeStamp() << format << '\n';
|
||||
file_stream[ type ].close();
|
||||
log_lock.unlock();
|
||||
}
|
||||
|
||||
void CLogManager::information( std::string format, ... )
|
||||
void Log::Info( std::string format, ... )
|
||||
{
|
||||
std::vector< char > buf( 512 );
|
||||
va_list args;
|
||||
@@ -100,10 +99,10 @@ void CLogManager::information( std::string format, ... )
|
||||
vsnprintf_s( &buf[ 0 ], buf.size(), buf.size() + strlen( format.c_str() ), format.c_str(), args );
|
||||
va_end( args );
|
||||
|
||||
write_log( log_generic, &buf[ 0 ] );
|
||||
WriteToLog( log_generic, &buf[ 0 ] );
|
||||
}
|
||||
|
||||
void CLogManager::debug( std::string format, ... )
|
||||
void Log::Warn( std::string format, ... )
|
||||
{
|
||||
std::vector< char > buf( 512 );
|
||||
va_list args;
|
||||
@@ -111,10 +110,10 @@ void CLogManager::debug( std::string format, ... )
|
||||
vsnprintf_s( &buf[ 0 ], buf.size(), buf.size() + strlen( format.c_str() ), format.c_str(), args );
|
||||
va_end( args );
|
||||
|
||||
write_log( log_debug, &buf[ 0 ] );
|
||||
WriteToLog( log_warn, &buf[ 0 ] );
|
||||
}
|
||||
|
||||
void CLogManager::error( std::string format, ... )
|
||||
void Log::Debug( std::string format, ... )
|
||||
{
|
||||
std::vector< char > buf( 512 );
|
||||
va_list args;
|
||||
@@ -122,10 +121,21 @@ void CLogManager::error( std::string format, ... )
|
||||
vsnprintf_s( &buf[ 0 ], buf.size(), buf.size() + strlen( format.c_str() ), format.c_str(), args );
|
||||
va_end( args );
|
||||
|
||||
write_log( log_error, &buf[ 0 ] );
|
||||
WriteToLog( log_debug, &buf[ 0 ] );
|
||||
}
|
||||
|
||||
void CLogManager::packet( std::vector< uint8_t > p, bool send )
|
||||
void Log::Error( std::string format, ... )
|
||||
{
|
||||
std::vector< char > buf( 512 );
|
||||
va_list args;
|
||||
va_start( args, format );
|
||||
vsnprintf_s( &buf[ 0 ], buf.size(), buf.size() + strlen( format.c_str() ), format.c_str(), args );
|
||||
va_end( args );
|
||||
|
||||
WriteToLog( log_error, &buf[ 0 ] );
|
||||
}
|
||||
|
||||
void Log::Packet( std::vector< uint8_t > p, bool send )
|
||||
{
|
||||
log_lock.lock();
|
||||
|
||||
@@ -191,7 +201,7 @@ void CLogManager::packet( std::vector< uint8_t > p, bool send )
|
||||
log_lock.unlock();
|
||||
}
|
||||
|
||||
void CLogManager::packet( std::vector<uint8_t> p, uint32_t size, bool send )
|
||||
void Log::Packet( std::vector<uint8_t> p, uint32_t size, bool send )
|
||||
{
|
||||
log_lock.lock();
|
||||
|
||||
@@ -255,3 +265,4 @@ void CLogManager::packet( std::vector<uint8_t> p, uint32_t size, bool send )
|
||||
log_lock.unlock();
|
||||
|
||||
}
|
||||
|
||||
47
logging.h
Normal file
47
logging.h
Normal file
@@ -0,0 +1,47 @@
|
||||
#pragma once
|
||||
|
||||
#include <fstream>
|
||||
#include <mutex>
|
||||
#include <cstdarg>
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#include <Windows.h>
|
||||
|
||||
class Log {
|
||||
private:
|
||||
enum LOG_TYPE {
|
||||
log_generic = 0,
|
||||
log_debug,
|
||||
log_error,
|
||||
log_warn,
|
||||
num_log_type
|
||||
};
|
||||
|
||||
static inline int32_t current_open_hour;
|
||||
static inline std::fstream file_stream[ num_log_type ];
|
||||
static inline std::mutex log_lock;
|
||||
|
||||
static const char* GetTimeStamp();
|
||||
static void CheckFileStatus( LOG_TYPE type );
|
||||
static void WriteToLog( LOG_TYPE type, std::string format );
|
||||
public:
|
||||
|
||||
static Log &Get()
|
||||
{
|
||||
static Log instance;
|
||||
return instance;
|
||||
}
|
||||
|
||||
Log();
|
||||
~Log();
|
||||
|
||||
static void Info( std::string format, ... );
|
||||
static void Warn( std::string format, ... );
|
||||
static void Debug( std::string format, ... );
|
||||
static void Error( std::string format, ... );
|
||||
static void Packet( std::vector< uint8_t > p, bool send );
|
||||
static void Packet( std::vector< uint8_t > p, uint32_t size, bool send );
|
||||
|
||||
|
||||
};
|
||||
36
main.cpp
36
main.cpp
@@ -1,7 +1,7 @@
|
||||
#include "stdafx.h"
|
||||
#include "global_define.h"
|
||||
|
||||
void ShowStartup()
|
||||
static void ShowStartup()
|
||||
{
|
||||
printf
|
||||
(
|
||||
@@ -16,23 +16,47 @@ int main()
|
||||
{
|
||||
ShowStartup();
|
||||
|
||||
if( false == ServerConfig::Get().Load( "config.json" ) )
|
||||
WORD wVersionRequest = MAKEWORD( 2, 2 );
|
||||
WSADATA wsaData;
|
||||
|
||||
if( WSAStartup( wVersionRequest, &wsaData ) != 0 )
|
||||
{
|
||||
Log::Error( "WSAStartup() failed" );
|
||||
return 0;
|
||||
}
|
||||
|
||||
Initialize_Global();
|
||||
Log::Info( "Server Start..." );
|
||||
|
||||
logging.information( "Server Start..." );
|
||||
auto gateway_server = GatewayServer::Get();
|
||||
gateway_server->Start( "192.168.1.248", 40801 );
|
||||
|
||||
LobbyServer::Get().Start( "192.168.1.248", 40810 );
|
||||
DiscoveryServer::Get().Start( "192.168.1.248", 40820 );
|
||||
|
||||
while( true )
|
||||
{
|
||||
server_time->Tick();
|
||||
if( !gateway_server->isRunning() )
|
||||
{
|
||||
Log::Error( "Gateway Server is not running. Exiting." );
|
||||
break;
|
||||
}
|
||||
|
||||
process_networking();
|
||||
//if( !lobby_server.isRunning() )
|
||||
//{
|
||||
// Log::Error( "Lobby Server is not running. Exiting." );
|
||||
// break;
|
||||
//}
|
||||
//
|
||||
//if( !discovery_server.isRunning() )
|
||||
//{
|
||||
// Log::Error( "Discovery Server is not running. Exiting." );
|
||||
// break;
|
||||
//}
|
||||
|
||||
std::this_thread::sleep_for( std::chrono::milliseconds( 1 ) );
|
||||
}
|
||||
|
||||
gateway_server->Stop();
|
||||
|
||||
return 0;
|
||||
}
|
||||
454
misc/AES.cpp
454
misc/AES.cpp
@@ -1,454 +0,0 @@
|
||||
#include "AES.h"
|
||||
|
||||
AES::AES(const AESKeyLength keyLength) {
|
||||
switch (keyLength) {
|
||||
case AESKeyLength::AES_128:
|
||||
this->Nk = 4;
|
||||
this->Nr = 10;
|
||||
break;
|
||||
case AESKeyLength::AES_192:
|
||||
this->Nk = 6;
|
||||
this->Nr = 12;
|
||||
break;
|
||||
case AESKeyLength::AES_256:
|
||||
this->Nk = 8;
|
||||
this->Nr = 14;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
unsigned char *AES::EncryptECB(const unsigned char in[], unsigned int inLen,
|
||||
const unsigned char key[]) {
|
||||
CheckLength(inLen);
|
||||
unsigned char *out = new unsigned char[inLen];
|
||||
unsigned char *roundKeys = new unsigned char[4 * Nb * (Nr + 1)];
|
||||
KeyExpansion(key, roundKeys);
|
||||
for (unsigned int i = 0; i < inLen; i += blockBytesLen) {
|
||||
EncryptBlock(in + i, out + i, roundKeys);
|
||||
}
|
||||
|
||||
delete[] roundKeys;
|
||||
|
||||
return out;
|
||||
}
|
||||
|
||||
unsigned char *AES::DecryptECB(const unsigned char in[], unsigned int inLen,
|
||||
const unsigned char key[]) {
|
||||
CheckLength(inLen);
|
||||
unsigned char *out = new unsigned char[inLen];
|
||||
unsigned char *roundKeys = new unsigned char[4 * Nb * (Nr + 1)];
|
||||
KeyExpansion(key, roundKeys);
|
||||
for (unsigned int i = 0; i < inLen; i += blockBytesLen) {
|
||||
DecryptBlock(in + i, out + i, roundKeys);
|
||||
}
|
||||
|
||||
delete[] roundKeys;
|
||||
|
||||
return out;
|
||||
}
|
||||
|
||||
unsigned char *AES::EncryptCBC(const unsigned char in[], unsigned int inLen,
|
||||
const unsigned char key[],
|
||||
const unsigned char *iv) {
|
||||
CheckLength(inLen);
|
||||
unsigned char *out = new unsigned char[inLen];
|
||||
unsigned char block[blockBytesLen];
|
||||
unsigned char *roundKeys = new unsigned char[4 * Nb * (Nr + 1)];
|
||||
KeyExpansion(key, roundKeys);
|
||||
memcpy(block, iv, blockBytesLen);
|
||||
for (unsigned int i = 0; i < inLen; i += blockBytesLen) {
|
||||
XorBlocks(block, in + i, block, blockBytesLen);
|
||||
EncryptBlock(block, out + i, roundKeys);
|
||||
memcpy(block, out + i, blockBytesLen);
|
||||
}
|
||||
|
||||
delete[] roundKeys;
|
||||
|
||||
return out;
|
||||
}
|
||||
|
||||
unsigned char *AES::DecryptCBC(const unsigned char in[], unsigned int inLen,
|
||||
const unsigned char key[],
|
||||
const unsigned char *iv) {
|
||||
CheckLength(inLen);
|
||||
unsigned char *out = new unsigned char[inLen];
|
||||
unsigned char block[blockBytesLen];
|
||||
unsigned char *roundKeys = new unsigned char[4 * Nb * (Nr + 1)];
|
||||
KeyExpansion(key, roundKeys);
|
||||
memcpy(block, iv, blockBytesLen);
|
||||
for (unsigned int i = 0; i < inLen; i += blockBytesLen) {
|
||||
DecryptBlock(in + i, out + i, roundKeys);
|
||||
XorBlocks(block, out + i, out + i, blockBytesLen);
|
||||
memcpy(block, in + i, blockBytesLen);
|
||||
}
|
||||
|
||||
delete[] roundKeys;
|
||||
|
||||
return out;
|
||||
}
|
||||
|
||||
unsigned char *AES::EncryptCFB(const unsigned char in[], unsigned int inLen,
|
||||
const unsigned char key[],
|
||||
const unsigned char *iv) {
|
||||
CheckLength(inLen);
|
||||
unsigned char *out = new unsigned char[inLen];
|
||||
unsigned char block[blockBytesLen];
|
||||
unsigned char encryptedBlock[blockBytesLen];
|
||||
unsigned char *roundKeys = new unsigned char[4 * Nb * (Nr + 1)];
|
||||
KeyExpansion(key, roundKeys);
|
||||
memcpy(block, iv, blockBytesLen);
|
||||
for (unsigned int i = 0; i < inLen; i += blockBytesLen) {
|
||||
EncryptBlock(block, encryptedBlock, roundKeys);
|
||||
XorBlocks(in + i, encryptedBlock, out + i, blockBytesLen);
|
||||
memcpy(block, out + i, blockBytesLen);
|
||||
}
|
||||
|
||||
delete[] roundKeys;
|
||||
|
||||
return out;
|
||||
}
|
||||
|
||||
unsigned char *AES::DecryptCFB(const unsigned char in[], unsigned int inLen,
|
||||
const unsigned char key[],
|
||||
const unsigned char *iv) {
|
||||
CheckLength(inLen);
|
||||
unsigned char *out = new unsigned char[inLen];
|
||||
unsigned char block[blockBytesLen];
|
||||
unsigned char encryptedBlock[blockBytesLen];
|
||||
unsigned char *roundKeys = new unsigned char[4 * Nb * (Nr + 1)];
|
||||
KeyExpansion(key, roundKeys);
|
||||
memcpy(block, iv, blockBytesLen);
|
||||
for (unsigned int i = 0; i < inLen; i += blockBytesLen) {
|
||||
EncryptBlock(block, encryptedBlock, roundKeys);
|
||||
XorBlocks(in + i, encryptedBlock, out + i, blockBytesLen);
|
||||
memcpy(block, in + i, blockBytesLen);
|
||||
}
|
||||
|
||||
delete[] roundKeys;
|
||||
|
||||
return out;
|
||||
}
|
||||
|
||||
void AES::CheckLength(unsigned int len) {
|
||||
if (len % blockBytesLen != 0) {
|
||||
throw std::length_error("Plaintext length must be divisible by " +
|
||||
std::to_string(blockBytesLen));
|
||||
}
|
||||
}
|
||||
|
||||
void AES::EncryptBlock(const unsigned char in[], unsigned char out[],
|
||||
unsigned char *roundKeys) {
|
||||
unsigned char state[4][Nb];
|
||||
unsigned int i, j, round;
|
||||
|
||||
for (i = 0; i < 4; i++) {
|
||||
for (j = 0; j < Nb; j++) {
|
||||
state[i][j] = in[i + 4 * j];
|
||||
}
|
||||
}
|
||||
|
||||
AddRoundKey(state, roundKeys);
|
||||
|
||||
for (round = 1; round <= Nr - 1; round++) {
|
||||
SubBytes(state);
|
||||
ShiftRows(state);
|
||||
MixColumns(state);
|
||||
AddRoundKey(state, roundKeys + round * 4 * Nb);
|
||||
}
|
||||
|
||||
SubBytes(state);
|
||||
ShiftRows(state);
|
||||
AddRoundKey(state, roundKeys + Nr * 4 * Nb);
|
||||
|
||||
for (i = 0; i < 4; i++) {
|
||||
for (j = 0; j < Nb; j++) {
|
||||
out[i + 4 * j] = state[i][j];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void AES::DecryptBlock(const unsigned char in[], unsigned char out[],
|
||||
unsigned char *roundKeys) {
|
||||
unsigned char state[4][Nb];
|
||||
unsigned int i, j, round;
|
||||
|
||||
for (i = 0; i < 4; i++) {
|
||||
for (j = 0; j < Nb; j++) {
|
||||
state[i][j] = in[i + 4 * j];
|
||||
}
|
||||
}
|
||||
|
||||
AddRoundKey(state, roundKeys + Nr * 4 * Nb);
|
||||
|
||||
for (round = Nr - 1; round >= 1; round--) {
|
||||
InvSubBytes(state);
|
||||
InvShiftRows(state);
|
||||
AddRoundKey(state, roundKeys + round * 4 * Nb);
|
||||
InvMixColumns(state);
|
||||
}
|
||||
|
||||
InvSubBytes(state);
|
||||
InvShiftRows(state);
|
||||
AddRoundKey(state, roundKeys);
|
||||
|
||||
for (i = 0; i < 4; i++) {
|
||||
for (j = 0; j < Nb; j++) {
|
||||
out[i + 4 * j] = state[i][j];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void AES::SubBytes(unsigned char state[4][Nb]) {
|
||||
unsigned int i, j;
|
||||
unsigned char t;
|
||||
for (i = 0; i < 4; i++) {
|
||||
for (j = 0; j < Nb; j++) {
|
||||
t = state[i][j];
|
||||
state[i][j] = sbox[t / 16][t % 16];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void AES::ShiftRow(unsigned char state[4][Nb], unsigned int i,
|
||||
unsigned int n) // shift row i on n write_positions
|
||||
{
|
||||
unsigned char tmp[Nb];
|
||||
for (unsigned int j = 0; j < Nb; j++) {
|
||||
tmp[j] = state[i][(j + n) % Nb];
|
||||
}
|
||||
memcpy(state[i], tmp, Nb * sizeof(unsigned char));
|
||||
}
|
||||
|
||||
void AES::ShiftRows(unsigned char state[4][Nb]) {
|
||||
ShiftRow(state, 1, 1);
|
||||
ShiftRow(state, 2, 2);
|
||||
ShiftRow(state, 3, 3);
|
||||
}
|
||||
|
||||
unsigned char AES::xtime(unsigned char b) // multiply on x
|
||||
{
|
||||
return (b << 1) ^ (((b >> 7) & 1) * 0x1b);
|
||||
}
|
||||
|
||||
void AES::MixColumns(unsigned char state[4][Nb]) {
|
||||
unsigned char temp_state[4][Nb];
|
||||
|
||||
for (size_t i = 0; i < 4; ++i) {
|
||||
memset(temp_state[i], 0, 4);
|
||||
}
|
||||
|
||||
for (size_t i = 0; i < 4; ++i) {
|
||||
for (size_t k = 0; k < 4; ++k) {
|
||||
for (size_t j = 0; j < 4; ++j) {
|
||||
if (CMDS[i][k] == 1)
|
||||
temp_state[i][j] ^= state[k][j];
|
||||
else
|
||||
temp_state[i][j] ^= GF_MUL_TABLE[CMDS[i][k]][state[k][j]];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for (size_t i = 0; i < 4; ++i) {
|
||||
memcpy(state[i], temp_state[i], 4);
|
||||
}
|
||||
}
|
||||
|
||||
void AES::AddRoundKey(unsigned char state[4][Nb], unsigned char *key) {
|
||||
unsigned int i, j;
|
||||
for (i = 0; i < 4; i++) {
|
||||
for (j = 0; j < Nb; j++) {
|
||||
state[i][j] = state[i][j] ^ key[i + 4 * j];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void AES::SubWord(unsigned char *a) {
|
||||
int i;
|
||||
for (i = 0; i < 4; i++) {
|
||||
a[i] = sbox[a[i] / 16][a[i] % 16];
|
||||
}
|
||||
}
|
||||
|
||||
void AES::RotWord(unsigned char *a) {
|
||||
unsigned char c = a[0];
|
||||
a[0] = a[1];
|
||||
a[1] = a[2];
|
||||
a[2] = a[3];
|
||||
a[3] = c;
|
||||
}
|
||||
|
||||
void AES::XorWords(unsigned char *a, unsigned char *b, unsigned char *c) {
|
||||
int i;
|
||||
for (i = 0; i < 4; i++) {
|
||||
c[i] = a[i] ^ b[i];
|
||||
}
|
||||
}
|
||||
|
||||
void AES::Rcon(unsigned char *a, unsigned int n) {
|
||||
unsigned int i;
|
||||
unsigned char c = 1;
|
||||
for (i = 0; i < n - 1; i++) {
|
||||
c = xtime(c);
|
||||
}
|
||||
|
||||
a[0] = c;
|
||||
a[1] = a[2] = a[3] = 0;
|
||||
}
|
||||
|
||||
void AES::KeyExpansion(const unsigned char key[], unsigned char w[]) {
|
||||
unsigned char temp[4];
|
||||
unsigned char rcon[4];
|
||||
|
||||
unsigned int i = 0;
|
||||
while (i < 4 * Nk) {
|
||||
w[i] = key[i];
|
||||
i++;
|
||||
}
|
||||
|
||||
i = 4 * Nk;
|
||||
while (i < 4 * Nb * (Nr + 1)) {
|
||||
temp[0] = w[i - 4 + 0];
|
||||
temp[1] = w[i - 4 + 1];
|
||||
temp[2] = w[i - 4 + 2];
|
||||
temp[3] = w[i - 4 + 3];
|
||||
|
||||
if (i / 4 % Nk == 0) {
|
||||
RotWord(temp);
|
||||
SubWord(temp);
|
||||
Rcon(rcon, i / (Nk * 4));
|
||||
XorWords(temp, rcon, temp);
|
||||
} else if (Nk > 6 && i / 4 % Nk == 4) {
|
||||
SubWord(temp);
|
||||
}
|
||||
|
||||
w[i + 0] = w[i - 4 * Nk] ^ temp[0];
|
||||
w[i + 1] = w[i + 1 - 4 * Nk] ^ temp[1];
|
||||
w[i + 2] = w[i + 2 - 4 * Nk] ^ temp[2];
|
||||
w[i + 3] = w[i + 3 - 4 * Nk] ^ temp[3];
|
||||
i += 4;
|
||||
}
|
||||
}
|
||||
|
||||
void AES::InvSubBytes(unsigned char state[4][Nb]) {
|
||||
unsigned int i, j;
|
||||
unsigned char t;
|
||||
for (i = 0; i < 4; i++) {
|
||||
for (j = 0; j < Nb; j++) {
|
||||
t = state[i][j];
|
||||
state[i][j] = inv_sbox[t / 16][t % 16];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void AES::InvMixColumns(unsigned char state[4][Nb]) {
|
||||
unsigned char temp_state[4][Nb];
|
||||
|
||||
for (size_t i = 0; i < 4; ++i) {
|
||||
memset(temp_state[i], 0, 4);
|
||||
}
|
||||
|
||||
for (size_t i = 0; i < 4; ++i) {
|
||||
for (size_t k = 0; k < 4; ++k) {
|
||||
for (size_t j = 0; j < 4; ++j) {
|
||||
temp_state[i][j] ^= GF_MUL_TABLE[INV_CMDS[i][k]][state[k][j]];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for (size_t i = 0; i < 4; ++i) {
|
||||
memcpy(state[i], temp_state[i], 4);
|
||||
}
|
||||
}
|
||||
|
||||
void AES::InvShiftRows(unsigned char state[4][Nb]) {
|
||||
ShiftRow(state, 1, Nb - 1);
|
||||
ShiftRow(state, 2, Nb - 2);
|
||||
ShiftRow(state, 3, Nb - 3);
|
||||
}
|
||||
|
||||
void AES::XorBlocks(const unsigned char *a, const unsigned char *b,
|
||||
unsigned char *c, unsigned int len) {
|
||||
for (unsigned int i = 0; i < len; i++) {
|
||||
c[i] = a[i] ^ b[i];
|
||||
}
|
||||
}
|
||||
|
||||
void AES::printHexArray(unsigned char a[], unsigned int n) {
|
||||
for (unsigned int i = 0; i < n; i++) {
|
||||
printf("%02x ", a[i]);
|
||||
}
|
||||
}
|
||||
|
||||
void AES::printHexVector(std::vector<unsigned char> a) {
|
||||
for (unsigned int i = 0; i < a.size(); i++) {
|
||||
printf("%02x ", a[i]);
|
||||
}
|
||||
}
|
||||
|
||||
std::vector<unsigned char> AES::ArrayToVector(unsigned char *a,
|
||||
unsigned int len) {
|
||||
std::vector<unsigned char> v(a, a + len * sizeof(unsigned char));
|
||||
return v;
|
||||
}
|
||||
|
||||
unsigned char *AES::VectorToArray(std::vector<unsigned char> &a) {
|
||||
return a.data();
|
||||
}
|
||||
|
||||
std::vector<unsigned char> AES::EncryptECB(std::vector<unsigned char> in,
|
||||
std::vector<unsigned char> key) {
|
||||
unsigned char *out = EncryptECB(VectorToArray(in), (unsigned int)in.size(),
|
||||
VectorToArray(key));
|
||||
std::vector<unsigned char> v = ArrayToVector(out, in.size());
|
||||
delete[] out;
|
||||
return v;
|
||||
}
|
||||
|
||||
std::vector<unsigned char> AES::DecryptECB(std::vector<unsigned char> in,
|
||||
std::vector<unsigned char> key) {
|
||||
unsigned char *out = DecryptECB(VectorToArray(in), (unsigned int)in.size(),
|
||||
VectorToArray(key));
|
||||
std::vector<unsigned char> v = ArrayToVector(out, (unsigned int)in.size());
|
||||
delete[] out;
|
||||
return v;
|
||||
}
|
||||
|
||||
std::vector<unsigned char> AES::EncryptCBC(std::vector<unsigned char> in,
|
||||
std::vector<unsigned char> key,
|
||||
std::vector<unsigned char> iv) {
|
||||
unsigned char *out = EncryptCBC(VectorToArray(in), (unsigned int)in.size(),
|
||||
VectorToArray(key), VectorToArray(iv));
|
||||
std::vector<unsigned char> v = ArrayToVector(out, in.size());
|
||||
delete[] out;
|
||||
return v;
|
||||
}
|
||||
|
||||
std::vector<unsigned char> AES::DecryptCBC(std::vector<unsigned char> in,
|
||||
std::vector<unsigned char> key,
|
||||
std::vector<unsigned char> iv) {
|
||||
unsigned char *out = DecryptCBC(VectorToArray(in), (unsigned int)in.size(),
|
||||
VectorToArray(key), VectorToArray(iv));
|
||||
std::vector<unsigned char> v = ArrayToVector(out, (unsigned int)in.size());
|
||||
delete[] out;
|
||||
return v;
|
||||
}
|
||||
|
||||
std::vector<unsigned char> AES::EncryptCFB(std::vector<unsigned char> in,
|
||||
std::vector<unsigned char> key,
|
||||
std::vector<unsigned char> iv) {
|
||||
unsigned char *out = EncryptCFB(VectorToArray(in), (unsigned int)in.size(),
|
||||
VectorToArray(key), VectorToArray(iv));
|
||||
std::vector<unsigned char> v = ArrayToVector(out, in.size());
|
||||
delete[] out;
|
||||
return v;
|
||||
}
|
||||
|
||||
std::vector<unsigned char> AES::DecryptCFB(std::vector<unsigned char> in,
|
||||
std::vector<unsigned char> key,
|
||||
std::vector<unsigned char> iv) {
|
||||
unsigned char *out = DecryptCFB(VectorToArray(in), (unsigned int)in.size(),
|
||||
VectorToArray(key), VectorToArray(iv));
|
||||
std::vector<unsigned char> v = ArrayToVector(out, (unsigned int)in.size());
|
||||
delete[] out;
|
||||
return v;
|
||||
}
|
||||
348
misc/AES.h
348
misc/AES.h
@@ -1,348 +0,0 @@
|
||||
#ifndef _AES_H_
|
||||
#define _AES_H_
|
||||
|
||||
#include <cstdio>
|
||||
#include <cstring>
|
||||
#include <iostream>
|
||||
#include <stdexcept>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
enum class AESKeyLength { AES_128, AES_192, AES_256 };
|
||||
|
||||
class AES {
|
||||
private:
|
||||
static constexpr unsigned int Nb = 4;
|
||||
static constexpr unsigned int blockBytesLen = 4 * Nb * sizeof(unsigned char);
|
||||
|
||||
unsigned int Nk;
|
||||
unsigned int Nr;
|
||||
|
||||
void SubBytes(unsigned char state[4][Nb]);
|
||||
|
||||
void ShiftRow(unsigned char state[4][Nb], unsigned int i,
|
||||
unsigned int n); // shift row i on n write_positions
|
||||
|
||||
void ShiftRows(unsigned char state[4][Nb]);
|
||||
|
||||
unsigned char xtime(unsigned char b); // multiply on x
|
||||
|
||||
void MixColumns(unsigned char state[4][Nb]);
|
||||
|
||||
void AddRoundKey(unsigned char state[4][Nb], unsigned char *key);
|
||||
|
||||
void SubWord(unsigned char *a);
|
||||
|
||||
void RotWord(unsigned char *a);
|
||||
|
||||
void XorWords(unsigned char *a, unsigned char *b, unsigned char *c);
|
||||
|
||||
void Rcon(unsigned char *a, unsigned int n);
|
||||
|
||||
void InvSubBytes(unsigned char state[4][Nb]);
|
||||
|
||||
void InvMixColumns(unsigned char state[4][Nb]);
|
||||
|
||||
void InvShiftRows(unsigned char state[4][Nb]);
|
||||
|
||||
void CheckLength(unsigned int len);
|
||||
|
||||
void KeyExpansion(const unsigned char key[], unsigned char w[]);
|
||||
|
||||
void EncryptBlock(const unsigned char in[], unsigned char out[],
|
||||
unsigned char *roundKeys);
|
||||
|
||||
void DecryptBlock(const unsigned char in[], unsigned char out[],
|
||||
unsigned char *roundKeys);
|
||||
|
||||
void XorBlocks(const unsigned char *a, const unsigned char *b,
|
||||
unsigned char *c, unsigned int len);
|
||||
|
||||
std::vector<unsigned char> ArrayToVector(unsigned char *a, unsigned int len);
|
||||
|
||||
unsigned char *VectorToArray(std::vector<unsigned char> &a);
|
||||
|
||||
public:
|
||||
explicit AES(const AESKeyLength keyLength = AESKeyLength::AES_256);
|
||||
|
||||
unsigned char *EncryptECB(const unsigned char in[], unsigned int inLen,
|
||||
const unsigned char key[]);
|
||||
|
||||
unsigned char *DecryptECB(const unsigned char in[], unsigned int inLen,
|
||||
const unsigned char key[]);
|
||||
|
||||
unsigned char *EncryptCBC(const unsigned char in[], unsigned int inLen,
|
||||
const unsigned char key[], const unsigned char *iv);
|
||||
|
||||
unsigned char *DecryptCBC(const unsigned char in[], unsigned int inLen,
|
||||
const unsigned char key[], const unsigned char *iv);
|
||||
|
||||
unsigned char *EncryptCFB(const unsigned char in[], unsigned int inLen,
|
||||
const unsigned char key[], const unsigned char *iv);
|
||||
|
||||
unsigned char *DecryptCFB(const unsigned char in[], unsigned int inLen,
|
||||
const unsigned char key[], const unsigned char *iv);
|
||||
|
||||
std::vector<unsigned char> EncryptECB(std::vector<unsigned char> in,
|
||||
std::vector<unsigned char> key);
|
||||
|
||||
std::vector<unsigned char> DecryptECB(std::vector<unsigned char> in,
|
||||
std::vector<unsigned char> key);
|
||||
|
||||
std::vector<unsigned char> EncryptCBC(std::vector<unsigned char> in,
|
||||
std::vector<unsigned char> key,
|
||||
std::vector<unsigned char> iv);
|
||||
|
||||
std::vector<unsigned char> DecryptCBC(std::vector<unsigned char> in,
|
||||
std::vector<unsigned char> key,
|
||||
std::vector<unsigned char> iv);
|
||||
|
||||
std::vector<unsigned char> EncryptCFB(std::vector<unsigned char> in,
|
||||
std::vector<unsigned char> key,
|
||||
std::vector<unsigned char> iv);
|
||||
|
||||
std::vector<unsigned char> DecryptCFB(std::vector<unsigned char> in,
|
||||
std::vector<unsigned char> key,
|
||||
std::vector<unsigned char> iv);
|
||||
|
||||
void printHexArray(unsigned char a[], unsigned int n);
|
||||
|
||||
void printHexVector(std::vector<unsigned char> a);
|
||||
};
|
||||
|
||||
const unsigned char sbox[16][16] = {
|
||||
{0x63, 0x7c, 0x77, 0x7b, 0xf2, 0x6b, 0x6f, 0xc5, 0x30, 0x01, 0x67, 0x2b,
|
||||
0xfe, 0xd7, 0xab, 0x76},
|
||||
{0xca, 0x82, 0xc9, 0x7d, 0xfa, 0x59, 0x47, 0xf0, 0xad, 0xd4, 0xa2, 0xaf,
|
||||
0x9c, 0xa4, 0x72, 0xc0},
|
||||
{0xb7, 0xfd, 0x93, 0x26, 0x36, 0x3f, 0xf7, 0xcc, 0x34, 0xa5, 0xe5, 0xf1,
|
||||
0x71, 0xd8, 0x31, 0x15},
|
||||
{0x04, 0xc7, 0x23, 0xc3, 0x18, 0x96, 0x05, 0x9a, 0x07, 0x12, 0x80, 0xe2,
|
||||
0xeb, 0x27, 0xb2, 0x75},
|
||||
{0x09, 0x83, 0x2c, 0x1a, 0x1b, 0x6e, 0x5a, 0xa0, 0x52, 0x3b, 0xd6, 0xb3,
|
||||
0x29, 0xe3, 0x2f, 0x84},
|
||||
{0x53, 0xd1, 0x00, 0xed, 0x20, 0xfc, 0xb1, 0x5b, 0x6a, 0xcb, 0xbe, 0x39,
|
||||
0x4a, 0x4c, 0x58, 0xcf},
|
||||
{0xd0, 0xef, 0xaa, 0xfb, 0x43, 0x4d, 0x33, 0x85, 0x45, 0xf9, 0x02, 0x7f,
|
||||
0x50, 0x3c, 0x9f, 0xa8},
|
||||
{0x51, 0xa3, 0x40, 0x8f, 0x92, 0x9d, 0x38, 0xf5, 0xbc, 0xb6, 0xda, 0x21,
|
||||
0x10, 0xff, 0xf3, 0xd2},
|
||||
{0xcd, 0x0c, 0x13, 0xec, 0x5f, 0x97, 0x44, 0x17, 0xc4, 0xa7, 0x7e, 0x3d,
|
||||
0x64, 0x5d, 0x19, 0x73},
|
||||
{0x60, 0x81, 0x4f, 0xdc, 0x22, 0x2a, 0x90, 0x88, 0x46, 0xee, 0xb8, 0x14,
|
||||
0xde, 0x5e, 0x0b, 0xdb},
|
||||
{0xe0, 0x32, 0x3a, 0x0a, 0x49, 0x06, 0x24, 0x5c, 0xc2, 0xd3, 0xac, 0x62,
|
||||
0x91, 0x95, 0xe4, 0x79},
|
||||
{0xe7, 0xc8, 0x37, 0x6d, 0x8d, 0xd5, 0x4e, 0xa9, 0x6c, 0x56, 0xf4, 0xea,
|
||||
0x65, 0x7a, 0xae, 0x08},
|
||||
{0xba, 0x78, 0x25, 0x2e, 0x1c, 0xa6, 0xb4, 0xc6, 0xe8, 0xdd, 0x74, 0x1f,
|
||||
0x4b, 0xbd, 0x8b, 0x8a},
|
||||
{0x70, 0x3e, 0xb5, 0x66, 0x48, 0x03, 0xf6, 0x0e, 0x61, 0x35, 0x57, 0xb9,
|
||||
0x86, 0xc1, 0x1d, 0x9e},
|
||||
{0xe1, 0xf8, 0x98, 0x11, 0x69, 0xd9, 0x8e, 0x94, 0x9b, 0x1e, 0x87, 0xe9,
|
||||
0xce, 0x55, 0x28, 0xdf},
|
||||
{0x8c, 0xa1, 0x89, 0x0d, 0xbf, 0xe6, 0x42, 0x68, 0x41, 0x99, 0x2d, 0x0f,
|
||||
0xb0, 0x54, 0xbb, 0x16}};
|
||||
|
||||
const unsigned char inv_sbox[16][16] = {
|
||||
{0x52, 0x09, 0x6a, 0xd5, 0x30, 0x36, 0xa5, 0x38, 0xbf, 0x40, 0xa3, 0x9e,
|
||||
0x81, 0xf3, 0xd7, 0xfb},
|
||||
{0x7c, 0xe3, 0x39, 0x82, 0x9b, 0x2f, 0xff, 0x87, 0x34, 0x8e, 0x43, 0x44,
|
||||
0xc4, 0xde, 0xe9, 0xcb},
|
||||
{0x54, 0x7b, 0x94, 0x32, 0xa6, 0xc2, 0x23, 0x3d, 0xee, 0x4c, 0x95, 0x0b,
|
||||
0x42, 0xfa, 0xc3, 0x4e},
|
||||
{0x08, 0x2e, 0xa1, 0x66, 0x28, 0xd9, 0x24, 0xb2, 0x76, 0x5b, 0xa2, 0x49,
|
||||
0x6d, 0x8b, 0xd1, 0x25},
|
||||
{0x72, 0xf8, 0xf6, 0x64, 0x86, 0x68, 0x98, 0x16, 0xd4, 0xa4, 0x5c, 0xcc,
|
||||
0x5d, 0x65, 0xb6, 0x92},
|
||||
{0x6c, 0x70, 0x48, 0x50, 0xfd, 0xed, 0xb9, 0xda, 0x5e, 0x15, 0x46, 0x57,
|
||||
0xa7, 0x8d, 0x9d, 0x84},
|
||||
{0x90, 0xd8, 0xab, 0x00, 0x8c, 0xbc, 0xd3, 0x0a, 0xf7, 0xe4, 0x58, 0x05,
|
||||
0xb8, 0xb3, 0x45, 0x06},
|
||||
{0xd0, 0x2c, 0x1e, 0x8f, 0xca, 0x3f, 0x0f, 0x02, 0xc1, 0xaf, 0xbd, 0x03,
|
||||
0x01, 0x13, 0x8a, 0x6b},
|
||||
{0x3a, 0x91, 0x11, 0x41, 0x4f, 0x67, 0xdc, 0xea, 0x97, 0xf2, 0xcf, 0xce,
|
||||
0xf0, 0xb4, 0xe6, 0x73},
|
||||
{0x96, 0xac, 0x74, 0x22, 0xe7, 0xad, 0x35, 0x85, 0xe2, 0xf9, 0x37, 0xe8,
|
||||
0x1c, 0x75, 0xdf, 0x6e},
|
||||
{0x47, 0xf1, 0x1a, 0x71, 0x1d, 0x29, 0xc5, 0x89, 0x6f, 0xb7, 0x62, 0x0e,
|
||||
0xaa, 0x18, 0xbe, 0x1b},
|
||||
{0xfc, 0x56, 0x3e, 0x4b, 0xc6, 0xd2, 0x79, 0x20, 0x9a, 0xdb, 0xc0, 0xfe,
|
||||
0x78, 0xcd, 0x5a, 0xf4},
|
||||
{0x1f, 0xdd, 0xa8, 0x33, 0x88, 0x07, 0xc7, 0x31, 0xb1, 0x12, 0x10, 0x59,
|
||||
0x27, 0x80, 0xec, 0x5f},
|
||||
{0x60, 0x51, 0x7f, 0xa9, 0x19, 0xb5, 0x4a, 0x0d, 0x2d, 0xe5, 0x7a, 0x9f,
|
||||
0x93, 0xc9, 0x9c, 0xef},
|
||||
{0xa0, 0xe0, 0x3b, 0x4d, 0xae, 0x2a, 0xf5, 0xb0, 0xc8, 0xeb, 0xbb, 0x3c,
|
||||
0x83, 0x53, 0x99, 0x61},
|
||||
{0x17, 0x2b, 0x04, 0x7e, 0xba, 0x77, 0xd6, 0x26, 0xe1, 0x69, 0x14, 0x63,
|
||||
0x55, 0x21, 0x0c, 0x7d}};
|
||||
|
||||
/// Galois Multiplication lookup tables
|
||||
static const unsigned char GF_MUL_TABLE[15][256] = {
|
||||
{},
|
||||
{},
|
||||
|
||||
// mul 2
|
||||
{0x00, 0x02, 0x04, 0x06, 0x08, 0x0a, 0x0c, 0x0e, 0x10, 0x12, 0x14, 0x16,
|
||||
0x18, 0x1a, 0x1c, 0x1e, 0x20, 0x22, 0x24, 0x26, 0x28, 0x2a, 0x2c, 0x2e,
|
||||
0x30, 0x32, 0x34, 0x36, 0x38, 0x3a, 0x3c, 0x3e, 0x40, 0x42, 0x44, 0x46,
|
||||
0x48, 0x4a, 0x4c, 0x4e, 0x50, 0x52, 0x54, 0x56, 0x58, 0x5a, 0x5c, 0x5e,
|
||||
0x60, 0x62, 0x64, 0x66, 0x68, 0x6a, 0x6c, 0x6e, 0x70, 0x72, 0x74, 0x76,
|
||||
0x78, 0x7a, 0x7c, 0x7e, 0x80, 0x82, 0x84, 0x86, 0x88, 0x8a, 0x8c, 0x8e,
|
||||
0x90, 0x92, 0x94, 0x96, 0x98, 0x9a, 0x9c, 0x9e, 0xa0, 0xa2, 0xa4, 0xa6,
|
||||
0xa8, 0xaa, 0xac, 0xae, 0xb0, 0xb2, 0xb4, 0xb6, 0xb8, 0xba, 0xbc, 0xbe,
|
||||
0xc0, 0xc2, 0xc4, 0xc6, 0xc8, 0xca, 0xcc, 0xce, 0xd0, 0xd2, 0xd4, 0xd6,
|
||||
0xd8, 0xda, 0xdc, 0xde, 0xe0, 0xe2, 0xe4, 0xe6, 0xe8, 0xea, 0xec, 0xee,
|
||||
0xf0, 0xf2, 0xf4, 0xf6, 0xf8, 0xfa, 0xfc, 0xfe, 0x1b, 0x19, 0x1f, 0x1d,
|
||||
0x13, 0x11, 0x17, 0x15, 0x0b, 0x09, 0x0f, 0x0d, 0x03, 0x01, 0x07, 0x05,
|
||||
0x3b, 0x39, 0x3f, 0x3d, 0x33, 0x31, 0x37, 0x35, 0x2b, 0x29, 0x2f, 0x2d,
|
||||
0x23, 0x21, 0x27, 0x25, 0x5b, 0x59, 0x5f, 0x5d, 0x53, 0x51, 0x57, 0x55,
|
||||
0x4b, 0x49, 0x4f, 0x4d, 0x43, 0x41, 0x47, 0x45, 0x7b, 0x79, 0x7f, 0x7d,
|
||||
0x73, 0x71, 0x77, 0x75, 0x6b, 0x69, 0x6f, 0x6d, 0x63, 0x61, 0x67, 0x65,
|
||||
0x9b, 0x99, 0x9f, 0x9d, 0x93, 0x91, 0x97, 0x95, 0x8b, 0x89, 0x8f, 0x8d,
|
||||
0x83, 0x81, 0x87, 0x85, 0xbb, 0xb9, 0xbf, 0xbd, 0xb3, 0xb1, 0xb7, 0xb5,
|
||||
0xab, 0xa9, 0xaf, 0xad, 0xa3, 0xa1, 0xa7, 0xa5, 0xdb, 0xd9, 0xdf, 0xdd,
|
||||
0xd3, 0xd1, 0xd7, 0xd5, 0xcb, 0xc9, 0xcf, 0xcd, 0xc3, 0xc1, 0xc7, 0xc5,
|
||||
0xfb, 0xf9, 0xff, 0xfd, 0xf3, 0xf1, 0xf7, 0xf5, 0xeb, 0xe9, 0xef, 0xed,
|
||||
0xe3, 0xe1, 0xe7, 0xe5},
|
||||
|
||||
// mul 3
|
||||
{0x00, 0x03, 0x06, 0x05, 0x0c, 0x0f, 0x0a, 0x09, 0x18, 0x1b, 0x1e, 0x1d,
|
||||
0x14, 0x17, 0x12, 0x11, 0x30, 0x33, 0x36, 0x35, 0x3c, 0x3f, 0x3a, 0x39,
|
||||
0x28, 0x2b, 0x2e, 0x2d, 0x24, 0x27, 0x22, 0x21, 0x60, 0x63, 0x66, 0x65,
|
||||
0x6c, 0x6f, 0x6a, 0x69, 0x78, 0x7b, 0x7e, 0x7d, 0x74, 0x77, 0x72, 0x71,
|
||||
0x50, 0x53, 0x56, 0x55, 0x5c, 0x5f, 0x5a, 0x59, 0x48, 0x4b, 0x4e, 0x4d,
|
||||
0x44, 0x47, 0x42, 0x41, 0xc0, 0xc3, 0xc6, 0xc5, 0xcc, 0xcf, 0xca, 0xc9,
|
||||
0xd8, 0xdb, 0xde, 0xdd, 0xd4, 0xd7, 0xd2, 0xd1, 0xf0, 0xf3, 0xf6, 0xf5,
|
||||
0xfc, 0xff, 0xfa, 0xf9, 0xe8, 0xeb, 0xee, 0xed, 0xe4, 0xe7, 0xe2, 0xe1,
|
||||
0xa0, 0xa3, 0xa6, 0xa5, 0xac, 0xaf, 0xaa, 0xa9, 0xb8, 0xbb, 0xbe, 0xbd,
|
||||
0xb4, 0xb7, 0xb2, 0xb1, 0x90, 0x93, 0x96, 0x95, 0x9c, 0x9f, 0x9a, 0x99,
|
||||
0x88, 0x8b, 0x8e, 0x8d, 0x84, 0x87, 0x82, 0x81, 0x9b, 0x98, 0x9d, 0x9e,
|
||||
0x97, 0x94, 0x91, 0x92, 0x83, 0x80, 0x85, 0x86, 0x8f, 0x8c, 0x89, 0x8a,
|
||||
0xab, 0xa8, 0xad, 0xae, 0xa7, 0xa4, 0xa1, 0xa2, 0xb3, 0xb0, 0xb5, 0xb6,
|
||||
0xbf, 0xbc, 0xb9, 0xba, 0xfb, 0xf8, 0xfd, 0xfe, 0xf7, 0xf4, 0xf1, 0xf2,
|
||||
0xe3, 0xe0, 0xe5, 0xe6, 0xef, 0xec, 0xe9, 0xea, 0xcb, 0xc8, 0xcd, 0xce,
|
||||
0xc7, 0xc4, 0xc1, 0xc2, 0xd3, 0xd0, 0xd5, 0xd6, 0xdf, 0xdc, 0xd9, 0xda,
|
||||
0x5b, 0x58, 0x5d, 0x5e, 0x57, 0x54, 0x51, 0x52, 0x43, 0x40, 0x45, 0x46,
|
||||
0x4f, 0x4c, 0x49, 0x4a, 0x6b, 0x68, 0x6d, 0x6e, 0x67, 0x64, 0x61, 0x62,
|
||||
0x73, 0x70, 0x75, 0x76, 0x7f, 0x7c, 0x79, 0x7a, 0x3b, 0x38, 0x3d, 0x3e,
|
||||
0x37, 0x34, 0x31, 0x32, 0x23, 0x20, 0x25, 0x26, 0x2f, 0x2c, 0x29, 0x2a,
|
||||
0x0b, 0x08, 0x0d, 0x0e, 0x07, 0x04, 0x01, 0x02, 0x13, 0x10, 0x15, 0x16,
|
||||
0x1f, 0x1c, 0x19, 0x1a},
|
||||
|
||||
{},
|
||||
{},
|
||||
{},
|
||||
{},
|
||||
{},
|
||||
|
||||
// mul 9
|
||||
{0x00, 0x09, 0x12, 0x1b, 0x24, 0x2d, 0x36, 0x3f, 0x48, 0x41, 0x5a, 0x53,
|
||||
0x6c, 0x65, 0x7e, 0x77, 0x90, 0x99, 0x82, 0x8b, 0xb4, 0xbd, 0xa6, 0xaf,
|
||||
0xd8, 0xd1, 0xca, 0xc3, 0xfc, 0xf5, 0xee, 0xe7, 0x3b, 0x32, 0x29, 0x20,
|
||||
0x1f, 0x16, 0x0d, 0x04, 0x73, 0x7a, 0x61, 0x68, 0x57, 0x5e, 0x45, 0x4c,
|
||||
0xab, 0xa2, 0xb9, 0xb0, 0x8f, 0x86, 0x9d, 0x94, 0xe3, 0xea, 0xf1, 0xf8,
|
||||
0xc7, 0xce, 0xd5, 0xdc, 0x76, 0x7f, 0x64, 0x6d, 0x52, 0x5b, 0x40, 0x49,
|
||||
0x3e, 0x37, 0x2c, 0x25, 0x1a, 0x13, 0x08, 0x01, 0xe6, 0xef, 0xf4, 0xfd,
|
||||
0xc2, 0xcb, 0xd0, 0xd9, 0xae, 0xa7, 0xbc, 0xb5, 0x8a, 0x83, 0x98, 0x91,
|
||||
0x4d, 0x44, 0x5f, 0x56, 0x69, 0x60, 0x7b, 0x72, 0x05, 0x0c, 0x17, 0x1e,
|
||||
0x21, 0x28, 0x33, 0x3a, 0xdd, 0xd4, 0xcf, 0xc6, 0xf9, 0xf0, 0xeb, 0xe2,
|
||||
0x95, 0x9c, 0x87, 0x8e, 0xb1, 0xb8, 0xa3, 0xaa, 0xec, 0xe5, 0xfe, 0xf7,
|
||||
0xc8, 0xc1, 0xda, 0xd3, 0xa4, 0xad, 0xb6, 0xbf, 0x80, 0x89, 0x92, 0x9b,
|
||||
0x7c, 0x75, 0x6e, 0x67, 0x58, 0x51, 0x4a, 0x43, 0x34, 0x3d, 0x26, 0x2f,
|
||||
0x10, 0x19, 0x02, 0x0b, 0xd7, 0xde, 0xc5, 0xcc, 0xf3, 0xfa, 0xe1, 0xe8,
|
||||
0x9f, 0x96, 0x8d, 0x84, 0xbb, 0xb2, 0xa9, 0xa0, 0x47, 0x4e, 0x55, 0x5c,
|
||||
0x63, 0x6a, 0x71, 0x78, 0x0f, 0x06, 0x1d, 0x14, 0x2b, 0x22, 0x39, 0x30,
|
||||
0x9a, 0x93, 0x88, 0x81, 0xbe, 0xb7, 0xac, 0xa5, 0xd2, 0xdb, 0xc0, 0xc9,
|
||||
0xf6, 0xff, 0xe4, 0xed, 0x0a, 0x03, 0x18, 0x11, 0x2e, 0x27, 0x3c, 0x35,
|
||||
0x42, 0x4b, 0x50, 0x59, 0x66, 0x6f, 0x74, 0x7d, 0xa1, 0xa8, 0xb3, 0xba,
|
||||
0x85, 0x8c, 0x97, 0x9e, 0xe9, 0xe0, 0xfb, 0xf2, 0xcd, 0xc4, 0xdf, 0xd6,
|
||||
0x31, 0x38, 0x23, 0x2a, 0x15, 0x1c, 0x07, 0x0e, 0x79, 0x70, 0x6b, 0x62,
|
||||
0x5d, 0x54, 0x4f, 0x46},
|
||||
|
||||
{},
|
||||
|
||||
// mul 11
|
||||
{0x00, 0x0b, 0x16, 0x1d, 0x2c, 0x27, 0x3a, 0x31, 0x58, 0x53, 0x4e, 0x45,
|
||||
0x74, 0x7f, 0x62, 0x69, 0xb0, 0xbb, 0xa6, 0xad, 0x9c, 0x97, 0x8a, 0x81,
|
||||
0xe8, 0xe3, 0xfe, 0xf5, 0xc4, 0xcf, 0xd2, 0xd9, 0x7b, 0x70, 0x6d, 0x66,
|
||||
0x57, 0x5c, 0x41, 0x4a, 0x23, 0x28, 0x35, 0x3e, 0x0f, 0x04, 0x19, 0x12,
|
||||
0xcb, 0xc0, 0xdd, 0xd6, 0xe7, 0xec, 0xf1, 0xfa, 0x93, 0x98, 0x85, 0x8e,
|
||||
0xbf, 0xb4, 0xa9, 0xa2, 0xf6, 0xfd, 0xe0, 0xeb, 0xda, 0xd1, 0xcc, 0xc7,
|
||||
0xae, 0xa5, 0xb8, 0xb3, 0x82, 0x89, 0x94, 0x9f, 0x46, 0x4d, 0x50, 0x5b,
|
||||
0x6a, 0x61, 0x7c, 0x77, 0x1e, 0x15, 0x08, 0x03, 0x32, 0x39, 0x24, 0x2f,
|
||||
0x8d, 0x86, 0x9b, 0x90, 0xa1, 0xaa, 0xb7, 0xbc, 0xd5, 0xde, 0xc3, 0xc8,
|
||||
0xf9, 0xf2, 0xef, 0xe4, 0x3d, 0x36, 0x2b, 0x20, 0x11, 0x1a, 0x07, 0x0c,
|
||||
0x65, 0x6e, 0x73, 0x78, 0x49, 0x42, 0x5f, 0x54, 0xf7, 0xfc, 0xe1, 0xea,
|
||||
0xdb, 0xd0, 0xcd, 0xc6, 0xaf, 0xa4, 0xb9, 0xb2, 0x83, 0x88, 0x95, 0x9e,
|
||||
0x47, 0x4c, 0x51, 0x5a, 0x6b, 0x60, 0x7d, 0x76, 0x1f, 0x14, 0x09, 0x02,
|
||||
0x33, 0x38, 0x25, 0x2e, 0x8c, 0x87, 0x9a, 0x91, 0xa0, 0xab, 0xb6, 0xbd,
|
||||
0xd4, 0xdf, 0xc2, 0xc9, 0xf8, 0xf3, 0xee, 0xe5, 0x3c, 0x37, 0x2a, 0x21,
|
||||
0x10, 0x1b, 0x06, 0x0d, 0x64, 0x6f, 0x72, 0x79, 0x48, 0x43, 0x5e, 0x55,
|
||||
0x01, 0x0a, 0x17, 0x1c, 0x2d, 0x26, 0x3b, 0x30, 0x59, 0x52, 0x4f, 0x44,
|
||||
0x75, 0x7e, 0x63, 0x68, 0xb1, 0xba, 0xa7, 0xac, 0x9d, 0x96, 0x8b, 0x80,
|
||||
0xe9, 0xe2, 0xff, 0xf4, 0xc5, 0xce, 0xd3, 0xd8, 0x7a, 0x71, 0x6c, 0x67,
|
||||
0x56, 0x5d, 0x40, 0x4b, 0x22, 0x29, 0x34, 0x3f, 0x0e, 0x05, 0x18, 0x13,
|
||||
0xca, 0xc1, 0xdc, 0xd7, 0xe6, 0xed, 0xf0, 0xfb, 0x92, 0x99, 0x84, 0x8f,
|
||||
0xbe, 0xb5, 0xa8, 0xa3},
|
||||
|
||||
{},
|
||||
|
||||
// mul 13
|
||||
{0x00, 0x0d, 0x1a, 0x17, 0x34, 0x39, 0x2e, 0x23, 0x68, 0x65, 0x72, 0x7f,
|
||||
0x5c, 0x51, 0x46, 0x4b, 0xd0, 0xdd, 0xca, 0xc7, 0xe4, 0xe9, 0xfe, 0xf3,
|
||||
0xb8, 0xb5, 0xa2, 0xaf, 0x8c, 0x81, 0x96, 0x9b, 0xbb, 0xb6, 0xa1, 0xac,
|
||||
0x8f, 0x82, 0x95, 0x98, 0xd3, 0xde, 0xc9, 0xc4, 0xe7, 0xea, 0xfd, 0xf0,
|
||||
0x6b, 0x66, 0x71, 0x7c, 0x5f, 0x52, 0x45, 0x48, 0x03, 0x0e, 0x19, 0x14,
|
||||
0x37, 0x3a, 0x2d, 0x20, 0x6d, 0x60, 0x77, 0x7a, 0x59, 0x54, 0x43, 0x4e,
|
||||
0x05, 0x08, 0x1f, 0x12, 0x31, 0x3c, 0x2b, 0x26, 0xbd, 0xb0, 0xa7, 0xaa,
|
||||
0x89, 0x84, 0x93, 0x9e, 0xd5, 0xd8, 0xcf, 0xc2, 0xe1, 0xec, 0xfb, 0xf6,
|
||||
0xd6, 0xdb, 0xcc, 0xc1, 0xe2, 0xef, 0xf8, 0xf5, 0xbe, 0xb3, 0xa4, 0xa9,
|
||||
0x8a, 0x87, 0x90, 0x9d, 0x06, 0x0b, 0x1c, 0x11, 0x32, 0x3f, 0x28, 0x25,
|
||||
0x6e, 0x63, 0x74, 0x79, 0x5a, 0x57, 0x40, 0x4d, 0xda, 0xd7, 0xc0, 0xcd,
|
||||
0xee, 0xe3, 0xf4, 0xf9, 0xb2, 0xbf, 0xa8, 0xa5, 0x86, 0x8b, 0x9c, 0x91,
|
||||
0x0a, 0x07, 0x10, 0x1d, 0x3e, 0x33, 0x24, 0x29, 0x62, 0x6f, 0x78, 0x75,
|
||||
0x56, 0x5b, 0x4c, 0x41, 0x61, 0x6c, 0x7b, 0x76, 0x55, 0x58, 0x4f, 0x42,
|
||||
0x09, 0x04, 0x13, 0x1e, 0x3d, 0x30, 0x27, 0x2a, 0xb1, 0xbc, 0xab, 0xa6,
|
||||
0x85, 0x88, 0x9f, 0x92, 0xd9, 0xd4, 0xc3, 0xce, 0xed, 0xe0, 0xf7, 0xfa,
|
||||
0xb7, 0xba, 0xad, 0xa0, 0x83, 0x8e, 0x99, 0x94, 0xdf, 0xd2, 0xc5, 0xc8,
|
||||
0xeb, 0xe6, 0xf1, 0xfc, 0x67, 0x6a, 0x7d, 0x70, 0x53, 0x5e, 0x49, 0x44,
|
||||
0x0f, 0x02, 0x15, 0x18, 0x3b, 0x36, 0x21, 0x2c, 0x0c, 0x01, 0x16, 0x1b,
|
||||
0x38, 0x35, 0x22, 0x2f, 0x64, 0x69, 0x7e, 0x73, 0x50, 0x5d, 0x4a, 0x47,
|
||||
0xdc, 0xd1, 0xc6, 0xcb, 0xe8, 0xe5, 0xf2, 0xff, 0xb4, 0xb9, 0xae, 0xa3,
|
||||
0x80, 0x8d, 0x9a, 0x97},
|
||||
|
||||
// mul 14
|
||||
{0x00, 0x0e, 0x1c, 0x12, 0x38, 0x36, 0x24, 0x2a, 0x70, 0x7e, 0x6c, 0x62,
|
||||
0x48, 0x46, 0x54, 0x5a, 0xe0, 0xee, 0xfc, 0xf2, 0xd8, 0xd6, 0xc4, 0xca,
|
||||
0x90, 0x9e, 0x8c, 0x82, 0xa8, 0xa6, 0xb4, 0xba, 0xdb, 0xd5, 0xc7, 0xc9,
|
||||
0xe3, 0xed, 0xff, 0xf1, 0xab, 0xa5, 0xb7, 0xb9, 0x93, 0x9d, 0x8f, 0x81,
|
||||
0x3b, 0x35, 0x27, 0x29, 0x03, 0x0d, 0x1f, 0x11, 0x4b, 0x45, 0x57, 0x59,
|
||||
0x73, 0x7d, 0x6f, 0x61, 0xad, 0xa3, 0xb1, 0xbf, 0x95, 0x9b, 0x89, 0x87,
|
||||
0xdd, 0xd3, 0xc1, 0xcf, 0xe5, 0xeb, 0xf9, 0xf7, 0x4d, 0x43, 0x51, 0x5f,
|
||||
0x75, 0x7b, 0x69, 0x67, 0x3d, 0x33, 0x21, 0x2f, 0x05, 0x0b, 0x19, 0x17,
|
||||
0x76, 0x78, 0x6a, 0x64, 0x4e, 0x40, 0x52, 0x5c, 0x06, 0x08, 0x1a, 0x14,
|
||||
0x3e, 0x30, 0x22, 0x2c, 0x96, 0x98, 0x8a, 0x84, 0xae, 0xa0, 0xb2, 0xbc,
|
||||
0xe6, 0xe8, 0xfa, 0xf4, 0xde, 0xd0, 0xc2, 0xcc, 0x41, 0x4f, 0x5d, 0x53,
|
||||
0x79, 0x77, 0x65, 0x6b, 0x31, 0x3f, 0x2d, 0x23, 0x09, 0x07, 0x15, 0x1b,
|
||||
0xa1, 0xaf, 0xbd, 0xb3, 0x99, 0x97, 0x85, 0x8b, 0xd1, 0xdf, 0xcd, 0xc3,
|
||||
0xe9, 0xe7, 0xf5, 0xfb, 0x9a, 0x94, 0x86, 0x88, 0xa2, 0xac, 0xbe, 0xb0,
|
||||
0xea, 0xe4, 0xf6, 0xf8, 0xd2, 0xdc, 0xce, 0xc0, 0x7a, 0x74, 0x66, 0x68,
|
||||
0x42, 0x4c, 0x5e, 0x50, 0x0a, 0x04, 0x16, 0x18, 0x32, 0x3c, 0x2e, 0x20,
|
||||
0xec, 0xe2, 0xf0, 0xfe, 0xd4, 0xda, 0xc8, 0xc6, 0x9c, 0x92, 0x80, 0x8e,
|
||||
0xa4, 0xaa, 0xb8, 0xb6, 0x0c, 0x02, 0x10, 0x1e, 0x34, 0x3a, 0x28, 0x26,
|
||||
0x7c, 0x72, 0x60, 0x6e, 0x44, 0x4a, 0x58, 0x56, 0x37, 0x39, 0x2b, 0x25,
|
||||
0x0f, 0x01, 0x13, 0x1d, 0x47, 0x49, 0x5b, 0x55, 0x7f, 0x71, 0x63, 0x6d,
|
||||
0xd7, 0xd9, 0xcb, 0xc5, 0xef, 0xe1, 0xf3, 0xfd, 0xa7, 0xa9, 0xbb, 0xb5,
|
||||
0x9f, 0x91, 0x83, 0x8d}};
|
||||
|
||||
/// circulant MDS matrix
|
||||
static const unsigned char CMDS[4][4] = {
|
||||
{2, 3, 1, 1}, {1, 2, 3, 1}, {1, 1, 2, 3}, {3, 1, 1, 2}};
|
||||
|
||||
/// Inverse circulant MDS matrix
|
||||
static const unsigned char INV_CMDS[4][4] = {
|
||||
{14, 11, 13, 9}, {9, 14, 11, 13}, {13, 9, 14, 11}, {11, 13, 9, 14}};
|
||||
|
||||
#endif
|
||||
@@ -1,41 +1,42 @@
|
||||
|
||||
#include <codecvt>
|
||||
#include "ByteStream.h"
|
||||
|
||||
#include <span>
|
||||
|
||||
ByteStream::ByteStream( const std::vector< uint8_t > &data )
|
||||
{
|
||||
this->data = data;
|
||||
this->write_position = 0;
|
||||
this->position = 0;
|
||||
}
|
||||
|
||||
ByteStream::ByteStream( const std::string &data )
|
||||
{
|
||||
this->data = std::vector< uint8_t >( data.begin(), data.end() );
|
||||
this->write_position = 0;
|
||||
this->position = 0;
|
||||
}
|
||||
|
||||
ByteStream::ByteStream( const uint8_t *data, size_t length )
|
||||
ByteStream::ByteStream( const uint8_t *data, uint32_t length )
|
||||
{
|
||||
this->data = std::vector< uint8_t >( data, data + length );
|
||||
this->write_position = 0;
|
||||
this->position = 0;
|
||||
}
|
||||
|
||||
ByteStream::ByteStream( size_t length )
|
||||
ByteStream::ByteStream( uint32_t length )
|
||||
{
|
||||
this->data = std::vector< uint8_t >( length, 0 );
|
||||
this->write_position = 0;
|
||||
this->position = 0;
|
||||
}
|
||||
|
||||
ByteStream::ByteStream()
|
||||
{
|
||||
this->write_position = 0;
|
||||
this->position = 0;
|
||||
}
|
||||
|
||||
ByteStream::~ByteStream()
|
||||
{
|
||||
}
|
||||
|
||||
void ByteStream::resize( size_t size )
|
||||
void ByteStream::resize( uint32_t size )
|
||||
{
|
||||
data.resize( size );
|
||||
}
|
||||
@@ -54,19 +55,22 @@ void ByteStream::write( T value )
|
||||
template < typename T >
|
||||
T ByteStream::read()
|
||||
{
|
||||
T value = *( T * )&data[ write_position ];
|
||||
write_position += sizeof( T );
|
||||
T value = *( T * )&data[ position ];
|
||||
position += sizeof( T );
|
||||
|
||||
return value;
|
||||
}
|
||||
|
||||
void ByteStream::write_utf8( const std::string &value )
|
||||
{
|
||||
write_u32( value.size() );
|
||||
write_bytes( std::vector< uint8_t >( value.begin(), value.end() ) );
|
||||
}
|
||||
|
||||
void ByteStream::write_utf16( const std::wstring &value )
|
||||
{
|
||||
write_u32( value.size() );
|
||||
|
||||
std::vector< uint8_t > utf16;
|
||||
for( auto c : value )
|
||||
{
|
||||
@@ -79,16 +83,50 @@ void ByteStream::write_utf16( const std::wstring &value )
|
||||
|
||||
void ByteStream::write_sz_utf8( const std::string &value )
|
||||
{
|
||||
write_utf8( value );
|
||||
write_bytes( std::vector< uint8_t >( value.begin(), value.end() ) );
|
||||
write< uint8_t >( 0 );
|
||||
}
|
||||
|
||||
void ByteStream::write_sz_utf16( const std::wstring &value )
|
||||
{
|
||||
write_utf16( value );
|
||||
std::vector< uint8_t > utf16;
|
||||
for( auto c : value )
|
||||
{
|
||||
utf16.push_back( c & 0xFF );
|
||||
utf16.push_back( ( c >> 8 ) & 0xFF );
|
||||
}
|
||||
|
||||
write_bytes( utf16 );
|
||||
write<uint16_t>( 0 );
|
||||
}
|
||||
|
||||
void ByteStream::write_encrypted_utf8( const std::string &value )
|
||||
{
|
||||
auto encrypted = RealmCrypt::encryptSymmetric( std::vector< uint8_t >( value.begin(), value.end() ) );
|
||||
|
||||
write_u32( encrypted.size() + 4 );
|
||||
write_u32( value.size() );
|
||||
|
||||
write_bytes( encrypted );
|
||||
}
|
||||
|
||||
void ByteStream::write_encrypted_utf16( const std::wstring &value )
|
||||
{
|
||||
std::vector< uint8_t > utf16;
|
||||
for( auto c : value )
|
||||
{
|
||||
utf16.push_back( c & 0xFF );
|
||||
utf16.push_back( ( c >> 8 ) & 0xFF );
|
||||
}
|
||||
|
||||
auto encrypted = RealmCrypt::encryptSymmetric( utf16 );
|
||||
|
||||
write_u32( encrypted.size() + 4 );
|
||||
write_u32( value.size() * 2 );
|
||||
|
||||
write_bytes( encrypted );
|
||||
}
|
||||
|
||||
uint8_t ByteStream::read_u8()
|
||||
{
|
||||
return read< uint8_t >();
|
||||
@@ -126,29 +164,29 @@ float_t ByteStream::read_f32()
|
||||
|
||||
std::string ByteStream::read_utf8()
|
||||
{
|
||||
uint32_t length = read_u32();
|
||||
auto length = read_u32();
|
||||
std::string value;
|
||||
for( size_t i = 0; i < length; i++ )
|
||||
{
|
||||
value.push_back( data[ write_position + i ] );
|
||||
value.push_back( data[ position + i ] );
|
||||
}
|
||||
|
||||
write_position += length;
|
||||
position += length;
|
||||
|
||||
return value;
|
||||
}
|
||||
|
||||
std::wstring ByteStream::read_utf16()
|
||||
{
|
||||
auto length = read_u32() * 2;
|
||||
std::wstring value;
|
||||
uint32_t length = read_u32() * 2;
|
||||
|
||||
for( size_t i = 0; i < length; i += 2 )
|
||||
{
|
||||
value.push_back( data[ write_position + i ] | ( data[ write_position + i + 1 ] << 8 ) );
|
||||
value.push_back( data[ position + i ] | ( data[ position + i + 1 ] << 8 ) );
|
||||
}
|
||||
|
||||
write_position += length;
|
||||
position += length;
|
||||
|
||||
return value;
|
||||
}
|
||||
@@ -156,13 +194,13 @@ std::wstring ByteStream::read_utf16()
|
||||
std::string ByteStream::read_sz_utf8()
|
||||
{
|
||||
std::string value;
|
||||
while( data[ write_position ] != 0 )
|
||||
while( data[ position ] != 0 )
|
||||
{
|
||||
value.push_back( data[ write_position ] );
|
||||
write_position++;
|
||||
value.push_back( data[ position ] );
|
||||
position++;
|
||||
}
|
||||
|
||||
write_position++;
|
||||
position++;
|
||||
|
||||
return value;
|
||||
}
|
||||
@@ -170,63 +208,151 @@ std::string ByteStream::read_sz_utf8()
|
||||
std::wstring ByteStream::read_sz_utf16()
|
||||
{
|
||||
std::wstring value;
|
||||
while( data[ write_position ] != 0 || data[ write_position + 1 ] != 0 )
|
||||
while( data[ position ] != 0 || data[ position + 1 ] != 0 )
|
||||
{
|
||||
value.push_back( data[ write_position ] | ( data[ write_position + 1 ] << 8 ) );
|
||||
write_position += 2;
|
||||
value.push_back( data[ position ] | ( data[ position + 1 ] << 8 ) );
|
||||
position += 2;
|
||||
}
|
||||
|
||||
write_position += 2;
|
||||
position += 2;
|
||||
|
||||
return value;
|
||||
}
|
||||
|
||||
std::string ByteStream::read_encrypted_utf8( bool hasBlockLength )
|
||||
{
|
||||
uint32_t encryptedLength = 0;
|
||||
uint32_t decryptedLength = 0;
|
||||
|
||||
if( hasBlockLength )
|
||||
{
|
||||
uint32_t blockLength = read_u32() * 2;
|
||||
decryptedLength = read_u32();
|
||||
encryptedLength = blockLength - 4;
|
||||
}
|
||||
else
|
||||
{
|
||||
decryptedLength = read_u32();
|
||||
encryptedLength = Math::round_up( decryptedLength, 16 );
|
||||
}
|
||||
|
||||
std::span< const uint8_t > encryptedBuffer( data.data() + position, encryptedLength );
|
||||
|
||||
position += encryptedLength;
|
||||
|
||||
if( decryptedLength == 0 )
|
||||
{
|
||||
return "";
|
||||
}
|
||||
|
||||
// Decrypt the buffer
|
||||
std::vector< uint8_t > decryptedBuffer = RealmCrypt::decryptSymmetric( encryptedBuffer );
|
||||
|
||||
std::string result( decryptedBuffer.begin(), decryptedBuffer.end() );
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
std::wstring ByteStream::read_encrypted_utf16( bool hasBlockLength )
|
||||
{
|
||||
uint32_t encryptedLength = 0;
|
||||
uint32_t decryptedLength = 0;
|
||||
|
||||
if( hasBlockLength )
|
||||
{
|
||||
uint32_t blockLength = read_u32() * 2;
|
||||
decryptedLength = read_u32(); // This length is already multiplied by sizeof(wchar_t)
|
||||
encryptedLength = blockLength - 4;
|
||||
}
|
||||
else
|
||||
{
|
||||
decryptedLength = read_u32();
|
||||
encryptedLength = Math::round_up( decryptedLength, 16 );
|
||||
}
|
||||
|
||||
std::span< const uint8_t > encryptedBuffer( data.data() + position, encryptedLength );
|
||||
|
||||
position += encryptedLength;
|
||||
|
||||
if( decryptedLength == 0 )
|
||||
{
|
||||
return L"";
|
||||
}
|
||||
|
||||
// Decrypt the buffer
|
||||
std::vector< uint8_t > decryptedBuffer = RealmCrypt::decryptSymmetric( encryptedBuffer );
|
||||
|
||||
std::wstring result( decryptedLength / 2, L'\0' );
|
||||
std::memcpy( result.data(), decryptedBuffer.data(), decryptedLength );
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
void ByteStream::write_bytes( const std::vector< uint8_t > &value )
|
||||
{
|
||||
std::copy( value.begin(), value.end(), std::back_inserter( data ) );
|
||||
write_position += value.size();
|
||||
position += value.size();
|
||||
}
|
||||
|
||||
void ByteStream::write_bytes( const uint8_t *value, size_t length )
|
||||
void ByteStream::write_bytes( const uint8_t *value, uint32_t length )
|
||||
{
|
||||
std::copy( value, value + length, std::back_inserter( data ) );
|
||||
write_position += length;
|
||||
position += length;
|
||||
}
|
||||
|
||||
std::vector<uint8_t> ByteStream::read_bytes( size_t length )
|
||||
void ByteStream::write_encrypted_bytes( const std::vector<uint8_t> &value )
|
||||
{
|
||||
auto encrypted = RealmCrypt::encryptSymmetric( value );
|
||||
|
||||
write_u32( encrypted.size() + 4 );
|
||||
write_u32( value.size() );
|
||||
|
||||
write_bytes( encrypted );
|
||||
}
|
||||
|
||||
std::vector<uint8_t> ByteStream::read_bytes( uint32_t length )
|
||||
{
|
||||
std::vector<uint8_t> value( length, 0 );
|
||||
|
||||
std::copy( data.begin() + write_position, data.begin() + write_position + length, value.begin() );
|
||||
std::copy( data.begin() + position, data.begin() + position + length, value.begin() );
|
||||
|
||||
write_position += length;
|
||||
position += length;
|
||||
|
||||
return value;
|
||||
}
|
||||
|
||||
std::vector<uint8_t> ByteStream::read_encrypted_bytes( uint32_t length )
|
||||
{
|
||||
std::vector< uint8_t > encrypted = read_bytes( length );
|
||||
|
||||
auto decrypted = RealmCrypt::decryptSymmetric( encrypted );
|
||||
|
||||
return decrypted;
|
||||
}
|
||||
|
||||
std::vector<uint8_t> ByteStream::get_data() const
|
||||
{
|
||||
return data;
|
||||
}
|
||||
|
||||
size_t ByteStream::get_length() const
|
||||
uint32_t ByteStream::get_length() const
|
||||
{
|
||||
return data.size();
|
||||
}
|
||||
|
||||
void ByteStream::set_write_position( size_t write_position )
|
||||
void ByteStream::set_position( uint32_t where )
|
||||
{
|
||||
if( write_position > data.size() )
|
||||
if( where > data.size() )
|
||||
{
|
||||
write_position = data.size();
|
||||
where = data.size();
|
||||
}
|
||||
|
||||
this->write_position = write_position;
|
||||
this->position = where;
|
||||
}
|
||||
|
||||
size_t ByteStream::get_write_position() const
|
||||
uint32_t ByteStream::get_position() const
|
||||
{
|
||||
return this->write_position;
|
||||
return this->position;
|
||||
}
|
||||
|
||||
void ByteStream::write_u8( uint8_t value )
|
||||
|
||||
@@ -5,18 +5,20 @@
|
||||
#include <memory>
|
||||
#include <iterator>
|
||||
|
||||
class ByteStream
|
||||
{
|
||||
#include "math.h"
|
||||
#include "RealmCrypt.h"
|
||||
|
||||
class ByteStream {
|
||||
public:
|
||||
ByteStream( const std::vector< uint8_t > &data );
|
||||
ByteStream( const std::string &data );
|
||||
ByteStream( const uint8_t *data, size_t length );
|
||||
ByteStream( size_t length );
|
||||
ByteStream( const uint8_t *data, uint32_t length );
|
||||
ByteStream( uint32_t length );
|
||||
ByteStream();
|
||||
|
||||
~ByteStream();
|
||||
|
||||
void resize( size_t size );
|
||||
void resize( uint32_t size );
|
||||
void shrink_to_fit();
|
||||
|
||||
template < typename T >
|
||||
@@ -37,6 +39,8 @@ public:
|
||||
void write_utf16( const std::wstring &value );
|
||||
void write_sz_utf8( const std::string &value );
|
||||
void write_sz_utf16( const std::wstring &value );
|
||||
void write_encrypted_utf8( const std::string &value );
|
||||
void write_encrypted_utf16( const std::wstring &value );
|
||||
|
||||
uint8_t read_u8();
|
||||
uint16_t read_u16();
|
||||
@@ -50,18 +54,24 @@ public:
|
||||
std::wstring read_utf16();
|
||||
std::string read_sz_utf8();
|
||||
std::wstring read_sz_utf16();
|
||||
std::string read_encrypted_utf8( bool hasBlockLength = true );
|
||||
std::wstring read_encrypted_utf16( bool hasBlockLength = true );
|
||||
|
||||
void write_bytes( const std::vector< uint8_t > &value );
|
||||
void write_bytes( const uint8_t *value, size_t length );
|
||||
void write_bytes( const uint8_t *value, uint32_t length );
|
||||
void write_encrypted_bytes( const std::vector< uint8_t > &value );
|
||||
|
||||
std::vector< uint8_t > read_bytes( uint32_t length );
|
||||
std::vector< uint8_t > read_encrypted_bytes( uint32_t length );
|
||||
|
||||
std::vector< uint8_t > read_bytes( size_t length );
|
||||
std::vector< uint8_t > get_data() const;
|
||||
|
||||
size_t get_length() const;
|
||||
|
||||
void set_write_position( size_t write_position );
|
||||
size_t get_write_position() const;
|
||||
uint32_t get_length() const;
|
||||
uint32_t get_position() const;
|
||||
void set_position( uint32_t pos );
|
||||
|
||||
std::vector< uint8_t > data;
|
||||
size_t write_position;
|
||||
};
|
||||
uint32_t position;
|
||||
};
|
||||
|
||||
typedef std::shared_ptr< ByteStream > sptr_byte_stream;
|
||||
@@ -1,153 +0,0 @@
|
||||
#include "Encryptor.h"
|
||||
#include <ctime>
|
||||
#include <array>
|
||||
|
||||
#include "AES.h"
|
||||
|
||||
bool Encryptor::ms_initialized = false;
|
||||
|
||||
Encryptor::Encryptor()
|
||||
{
|
||||
// Initialize the private key
|
||||
m_privateKey = ""; // Default initialization
|
||||
|
||||
// Initialize basic_str_b
|
||||
basic_str_b = "";
|
||||
|
||||
// Initialize the symmetric key
|
||||
m_symmetricKey.assign( default_sym_key );
|
||||
|
||||
// Static initialization logic
|
||||
if( !ms_initialized )
|
||||
{
|
||||
ms_initialized = true;
|
||||
std::srand( static_cast< unsigned >( std::time( nullptr ) ) );
|
||||
Encryptor::test();
|
||||
}
|
||||
}
|
||||
|
||||
std::string Encryptor::generateSymmetricKey( void )
|
||||
{
|
||||
constexpr size_t KEY_LENGTH = 32;
|
||||
|
||||
std::array<unsigned char, KEY_LENGTH> keyData{ 0 };
|
||||
|
||||
// Generate 32 random bytes
|
||||
for( size_t i = 0; i < KEY_LENGTH; ++i )
|
||||
{
|
||||
keyData[ i ] = static_cast< unsigned char >( rand() % 255 );
|
||||
}
|
||||
|
||||
// Replace the symmetric key with the generated key
|
||||
m_symmetricKey.assign( reinterpret_cast< char * >( keyData.data() ), KEY_LENGTH );
|
||||
|
||||
return m_symmetricKey;
|
||||
}
|
||||
|
||||
std::string Encryptor::generatePrivateSymKey( void )
|
||||
{
|
||||
constexpr size_t KEY_LENGTH = 32;
|
||||
|
||||
std::array<unsigned char, KEY_LENGTH> keyData{ 0 };
|
||||
|
||||
// Generate 32 random bytes
|
||||
for( size_t i = 0; i < KEY_LENGTH; ++i )
|
||||
{
|
||||
keyData[ i ] = static_cast< unsigned char >( rand() % 255 );
|
||||
}
|
||||
|
||||
// Replace the symmetric key with the generated key
|
||||
m_privateKey.assign( reinterpret_cast< char * >( keyData.data() ), KEY_LENGTH );
|
||||
|
||||
// Print the private key as bytes
|
||||
printf( "Private Sym Key: " );
|
||||
for( auto c : m_privateKey )
|
||||
{
|
||||
printf( "%02X", (uint8_t)c );
|
||||
}
|
||||
printf( "\n" );
|
||||
|
||||
// Encrypt the private key
|
||||
AES aes( AESKeyLength::AES_128 );
|
||||
|
||||
auto c = aes.EncryptECB(
|
||||
reinterpret_cast< const uint8_t * >( m_privateKey.c_str() ),
|
||||
m_privateKey.size(),
|
||||
reinterpret_cast< const uint8_t * >( default_public_key.c_str() ) );
|
||||
|
||||
m_encryptedPrivateKey = std::string( reinterpret_cast< const char * >( c ), m_privateKey.size() );
|
||||
|
||||
// Print the encrypted key as bytes
|
||||
printf( "Encrypted Sym Key: " );
|
||||
for( auto c : m_encryptedPrivateKey )
|
||||
{
|
||||
printf( "%02X", ( uint8_t )c );
|
||||
}
|
||||
printf( "\n" );
|
||||
|
||||
return m_encryptedPrivateKey;
|
||||
}
|
||||
|
||||
std::string Encryptor::encryptSymmetric( const std::string &input )
|
||||
{
|
||||
AES aes( AESKeyLength::AES_128 );
|
||||
|
||||
auto result = aes.EncryptECB( reinterpret_cast< const uint8_t * >( input.c_str() ), input.size(), reinterpret_cast< const uint8_t * >( m_privateKey.c_str() ) );
|
||||
|
||||
return std::string( reinterpret_cast< const char * >( result ), input.size() );
|
||||
}
|
||||
|
||||
std::string Encryptor::decryptSymmetric( const std::string &input )
|
||||
{
|
||||
AES aes( AESKeyLength::AES_128 );
|
||||
|
||||
auto result = aes.DecryptECB( reinterpret_cast< const uint8_t * >( input.c_str() ), input.size(), reinterpret_cast< const uint8_t * >( m_privateKey.c_str() ) );
|
||||
|
||||
return std::string( reinterpret_cast< const char * >( result ), input.size() );
|
||||
}
|
||||
|
||||
void Encryptor::test()
|
||||
{
|
||||
/*std::string inputStr = "HelloWorld"; // Input string to encrypt and decrypt
|
||||
std::string intermediateEncryptedStr; // Encrypted intermediate result
|
||||
std::string intermediateDecryptedStr; // Decrypted intermediate result
|
||||
|
||||
// Generate symmetric key
|
||||
std::string symmetricKey;
|
||||
generateSymmetricKey( symmetricKey );
|
||||
|
||||
// Encrypt the input string using the symmetric key
|
||||
encryptor.encryptSymmetric( intermediateEncryptedStr, inputStr );
|
||||
|
||||
// Log intermediate encryption result
|
||||
std::cout << "Encrypted string: " << intermediateEncryptedStr << std::endl;
|
||||
|
||||
// Decrypt the encrypted string using the symmetric key
|
||||
encryptor.decryptSymmetric( intermediateDecryptedStr, intermediateEncryptedStr );
|
||||
|
||||
// Log final decryption result
|
||||
std::cout << "Decrypted string: " << intermediateDecryptedStr << std::endl;
|
||||
|
||||
// Check if decryption matches the original input
|
||||
if( inputStr == intermediateDecryptedStr )
|
||||
{
|
||||
std::cout << "Test passed: Decryption matches original input." << std::endl;
|
||||
}
|
||||
else
|
||||
{
|
||||
std::cout << "Test failed: Decryption does not match original input." << std::endl;
|
||||
}*/
|
||||
}
|
||||
|
||||
int decryptBuffer( const uint8_t *input, int32_t dataSize, uint8_t *output, const uint8_t *symKey )
|
||||
{
|
||||
if( dataSize <= 0 )
|
||||
{
|
||||
return false; // No data to decrypt
|
||||
}
|
||||
|
||||
AES aes( AESKeyLength::AES_128 );
|
||||
output = aes.DecryptECB( input, dataSize, symKey );
|
||||
|
||||
return true;
|
||||
}
|
||||
@@ -1,39 +0,0 @@
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <string>
|
||||
|
||||
int decryptBuffer( const uint8_t *input, int64_t dataSize, uint8_t *output, const uint8_t *symKey );
|
||||
|
||||
class Encryptor
|
||||
{
|
||||
public:
|
||||
const static inline std::string default_public_key = "25B946EBC0B361734A63910E1FF3C9E1";
|
||||
const static inline std::string default_sym_key = "dlfk qs';r+t iqe4t9ueerjKDJ wdaj";
|
||||
|
||||
Encryptor(); // Constructor
|
||||
|
||||
std::string generateSymmetricKey( void );
|
||||
std::string generatePrivateSymKey( void );
|
||||
|
||||
std::string encryptSymmetric( const std::string &input );
|
||||
std::string decryptSymmetric( const std::string &input );
|
||||
|
||||
void setSymmetricKey( const std::string &key );
|
||||
std::string getSymmetricKey( void ) const;
|
||||
|
||||
void setPublicKey( const std::string &key );
|
||||
std::string getPublicKey( void ) const;
|
||||
|
||||
void setPrivateKey( const std::string &key );
|
||||
std::string getPrivateKey( void ) const;
|
||||
|
||||
static void test();
|
||||
|
||||
std::string m_privateKey, m_encryptedPrivateKey;
|
||||
std::string m_symmetricKey;
|
||||
std::string basic_str_b;
|
||||
|
||||
static bool ms_initialized;
|
||||
};
|
||||
|
||||
167
misc/RealmCrypt.cpp
Normal file
167
misc/RealmCrypt.cpp
Normal file
@@ -0,0 +1,167 @@
|
||||
|
||||
#include <ctime>
|
||||
#include <array>
|
||||
|
||||
#include "../misc/math.h"
|
||||
#include "../Crypto/NorrathCrypt.h"
|
||||
#include "RealmCrypt.h"
|
||||
|
||||
bool RealmCrypt::ms_initialized = false;
|
||||
|
||||
RealmCrypt::RealmCrypt()
|
||||
{
|
||||
if( !ms_initialized )
|
||||
{
|
||||
ms_initialized = true;
|
||||
std::srand( static_cast< unsigned >( std::time( nullptr ) ) );
|
||||
RealmCrypt::test();
|
||||
}
|
||||
}
|
||||
|
||||
std::vector< uint8_t > RealmCrypt::generateSymmetricKey( void )
|
||||
{
|
||||
constexpr size_t KEY_LENGTH = 32;
|
||||
|
||||
std::vector< uint8_t > keyData( KEY_LENGTH, 0 );
|
||||
|
||||
// Generate 32 random bytes
|
||||
for( size_t i = 0; i < KEY_LENGTH; ++i )
|
||||
{
|
||||
keyData[ i ] = static_cast< uint8_t >( rand() % 255 );
|
||||
}
|
||||
|
||||
return keyData;
|
||||
}
|
||||
|
||||
std::vector<uint8_t> RealmCrypt::getSymmetricKey( void )
|
||||
{
|
||||
return default_sym_key;
|
||||
}
|
||||
|
||||
std::string RealmCrypt::encryptString( std::string &input )
|
||||
{
|
||||
if( input.size() % 16 != 0 )
|
||||
{
|
||||
input.append( 16 - ( input.size() % 16 ), '\0' );
|
||||
}
|
||||
|
||||
rijndael aes( KeyLength::_256 );
|
||||
|
||||
auto result = aes.EncryptECB( reinterpret_cast< const uint8_t * >( input.c_str() ), input.size(), default_sym_key.data() );
|
||||
|
||||
return std::string( reinterpret_cast< const char * >( result ), input.size() );
|
||||
}
|
||||
|
||||
std::string RealmCrypt::decryptString( std::string &input )
|
||||
{
|
||||
if( input.size() % 16 != 0 )
|
||||
{
|
||||
input.append( 16 - ( input.size() % 16 ), '\0' );
|
||||
}
|
||||
|
||||
rijndael aes( KeyLength::_256 );
|
||||
|
||||
auto result = aes.DecryptECB( reinterpret_cast< const uint8_t * >( input.c_str() ), input.size(), default_sym_key.data() );
|
||||
|
||||
return std::string( reinterpret_cast< const char * >( result ), input.size() );
|
||||
}
|
||||
|
||||
std::wstring RealmCrypt::encryptString( std::wstring &input )
|
||||
{
|
||||
if( input.size() % 16 != 0 )
|
||||
{
|
||||
input.append( 16 - ( input.size() % 16 ), L'\0' );
|
||||
}
|
||||
|
||||
rijndael aes( KeyLength::_256 );
|
||||
|
||||
auto result = aes.EncryptECB( reinterpret_cast< const uint8_t * >( input.c_str() ), input.size(), default_sym_key.data() );
|
||||
|
||||
return std::wstring( reinterpret_cast< const wchar_t * >( result ), input.size() );
|
||||
}
|
||||
|
||||
std::wstring RealmCrypt::decryptString( std::wstring &input )
|
||||
{
|
||||
if( input.size() % 16 != 0 )
|
||||
{
|
||||
input.append( 16 - ( input.size() % 16 ), L'\0' );
|
||||
}
|
||||
|
||||
rijndael aes( KeyLength::_256 );
|
||||
|
||||
auto result = aes.DecryptECB( reinterpret_cast< const uint8_t * >( input.c_str() ), input.size(), default_sym_key.data() );
|
||||
|
||||
return std::wstring( reinterpret_cast< const wchar_t * >( result ), input.size() );
|
||||
}
|
||||
|
||||
std::vector< uint8_t > RealmCrypt::encryptSymmetric( std::vector< const uint8_t > &input )
|
||||
{
|
||||
return std::vector< uint8_t >();
|
||||
}
|
||||
|
||||
std::vector< uint8_t > RealmCrypt::decryptSymmetric( std::vector< const uint8_t > &input )
|
||||
{
|
||||
return std::vector< uint8_t >();
|
||||
}
|
||||
|
||||
std::vector< uint8_t > RealmCrypt::encryptSymmetric( std::span< const uint8_t > input )
|
||||
{
|
||||
if( input.size() % 16 != 0 )
|
||||
{
|
||||
std::vector< uint8_t > paddedInput( input.begin(), input.end() );
|
||||
paddedInput.resize( ( ( input.size() / 16 ) + 1 ) * 16, 0 );
|
||||
input = paddedInput;
|
||||
}
|
||||
|
||||
rijndael aes( KeyLength::_256 );
|
||||
|
||||
auto result = aes.EncryptECB( reinterpret_cast< const uint8_t * >( input.data() ), input.size(), default_sym_key.data() );
|
||||
|
||||
return std::vector< uint8_t >( result, result + input.size() );
|
||||
}
|
||||
|
||||
std::vector< uint8_t > RealmCrypt::decryptSymmetric( std::span< const uint8_t > input )
|
||||
{
|
||||
if( input.size() % 16 != 0 )
|
||||
{
|
||||
std::vector< uint8_t > paddedInput( input.begin(), input.end() );
|
||||
paddedInput.resize( ( ( input.size() / 16 ) + 1 ) * 16, 0 );
|
||||
input = paddedInput;
|
||||
}
|
||||
|
||||
rijndael aes( KeyLength::_256 );
|
||||
|
||||
auto result = aes.DecryptECB( reinterpret_cast< const uint8_t * >( input.data() ), input.size(), default_sym_key.data() );
|
||||
|
||||
return std::vector< uint8_t >( result, result + input.size() );
|
||||
}
|
||||
|
||||
void RealmCrypt::test()
|
||||
{
|
||||
std::string inputStr = "HelloWorldThisIsATest"; // Input string to encrypt and decrypt
|
||||
|
||||
// Generate symmetric key
|
||||
auto symmetricKey = generateSymmetricKey();
|
||||
|
||||
// Encrypt the input string using the symmetric key
|
||||
auto intermediateEncryptedStr = encryptString( inputStr );
|
||||
|
||||
// Log intermediate encryption result
|
||||
std::cout << "Encrypted string: " << intermediateEncryptedStr << std::endl;
|
||||
|
||||
// Decrypt the encrypted string using the symmetric key
|
||||
auto intermediateDecryptedStr = decryptString( intermediateEncryptedStr );
|
||||
|
||||
// Log final decryption result
|
||||
std::cout << "Decrypted string: " << intermediateDecryptedStr << std::endl;
|
||||
|
||||
// Check if decryption matches the original input
|
||||
if( inputStr == intermediateDecryptedStr )
|
||||
{
|
||||
std::cout << "Test passed: Decryption matches original input." << std::endl;
|
||||
}
|
||||
else
|
||||
{
|
||||
std::cout << "Test failed: Decryption does not match original input." << std::endl;
|
||||
}
|
||||
}
|
||||
88
misc/RealmCrypt.h
Normal file
88
misc/RealmCrypt.h
Normal file
@@ -0,0 +1,88 @@
|
||||
#pragma once
|
||||
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#include <span>
|
||||
|
||||
// This class is based on the games Encryptor class,
|
||||
// and is a wrapper around the rijndael ECB implementation.
|
||||
//
|
||||
// Normally CoN would generate a random symmetric key for each user,
|
||||
// but for the sake of simplicity we will just use the games default key,
|
||||
// since we have nothing to hide.
|
||||
|
||||
class RealmCrypt {
|
||||
private:
|
||||
// Byte array of dlfk qs';r+t iqe4t9ueerjKDJ wdaj
|
||||
const static inline std::vector< uint8_t > default_sym_key =
|
||||
{
|
||||
0x64, 0x6c, 0x66, 0x6b, 0x20, 0x71, 0x73, 0x27,
|
||||
0x3b, 0x72, 0x2b, 0x74, 0x20, 0x69, 0x71, 0x65,
|
||||
0x34, 0x74, 0x39, 0x75, 0x65, 0x65, 0x72, 0x6a,
|
||||
0x4b, 0x44, 0x4a, 0x20, 0x77, 0x64, 0x61, 0x6a
|
||||
};
|
||||
|
||||
public:
|
||||
RealmCrypt();
|
||||
|
||||
// Generate a new symmetric key for the user.
|
||||
static std::vector< uint8_t > generateSymmetricKey( void );
|
||||
static std::vector< uint8_t > getSymmetricKey( void );
|
||||
|
||||
// Encrypt and decrypt strings.
|
||||
static std::string encryptString( std::string &input );
|
||||
static std::string decryptString( std::string &input );
|
||||
static std::wstring encryptString( std::wstring &input );
|
||||
static std::wstring decryptString( std::wstring &input );
|
||||
|
||||
// Encrypt and decrypt byte arrays.
|
||||
static std::vector< uint8_t > encryptSymmetric( std::vector< const uint8_t > &input );
|
||||
static std::vector< uint8_t > decryptSymmetric( std::vector< const uint8_t > &input );
|
||||
static std::vector< uint8_t > encryptSymmetric( std::span< const uint8_t > input );
|
||||
static std::vector< uint8_t > decryptSymmetric( std::span< const uint8_t > input );
|
||||
|
||||
// Test to make sure the encryption and decryption works.
|
||||
void test();
|
||||
|
||||
// Initializer state for srand.
|
||||
static bool ms_initialized;
|
||||
};
|
||||
|
||||
|
||||
/*class Encryptor {
|
||||
private:
|
||||
// "dlfk qs';r+t iqe4t9ueerjKDJ wdaj";
|
||||
const static inline std::vector< uint8_t > default_sym_key =
|
||||
{
|
||||
0x64, 0x6c, 0x66, 0x6b, 0x20, 0x71, 0x73, 0x27,
|
||||
0x3b, 0x72, 0x2b, 0x74, 0x20, 0x69, 0x71, 0x65,
|
||||
0x34, 0x74, 0x39, 0x75, 0x65, 0x65, 0x72, 0x6a,
|
||||
0x4b, 0x44, 0x4a, 0x20, 0x77, 0x64, 0x61, 0x6a
|
||||
};
|
||||
|
||||
public:
|
||||
Encryptor();
|
||||
|
||||
std::vector< uint8_t > generateSymmetricKey( void );
|
||||
|
||||
std::string encryptString( std::string &input );
|
||||
std::string decryptString( std::string &input );
|
||||
std::wstring encryptString( std::wstring &input );
|
||||
std::wstring decryptString( std::wstring &input );
|
||||
|
||||
std::vector< uint8_t > encryptSymmetric( std::vector< const uint8_t > &input );
|
||||
std::vector< uint8_t > decryptSymmetric( std::vector< const uint8_t > &input );
|
||||
|
||||
std::vector< uint8_t > encryptSymmetric( std::span< const uint8_t > input );
|
||||
std::vector< uint8_t > decryptSymmetric( std::span< const uint8_t > input );
|
||||
|
||||
void setSymmetricKey( const std::vector< uint8_t > &input );
|
||||
std::vector< uint8_t > getSymmetricKey( void ) const;
|
||||
|
||||
void test();
|
||||
|
||||
std::vector< uint8_t > m_symKey;
|
||||
|
||||
static bool ms_initialized;
|
||||
};*/
|
||||
|
||||
99
misc/Timer.h
99
misc/Timer.h
@@ -1,58 +1,59 @@
|
||||
#pragma once
|
||||
|
||||
#include <Windows.h>
|
||||
#include <chrono>
|
||||
|
||||
class CTimer {
|
||||
int m_stopped;
|
||||
int m_inited;
|
||||
int m_usingQPF;
|
||||
|
||||
double m_lastElapsedTime;
|
||||
double m_baseTime;
|
||||
double m_stopTime;
|
||||
double m_currSysTime;
|
||||
double m_currElapsedTime;
|
||||
double m_baseMilliTime;
|
||||
double m_currSysMilliTime;
|
||||
double m_currElapsedMilliTime;
|
||||
|
||||
long long m_QPFTicksPerSec;
|
||||
long long m_QPFStopTime;
|
||||
long long m_QPFLastElapsedTime;
|
||||
long long m_QPFBaseTime;
|
||||
|
||||
LARGE_INTEGER m_QPFTime;
|
||||
class Timer {
|
||||
private:
|
||||
std::chrono::high_resolution_clock::time_point m_startTime;
|
||||
std::chrono::high_resolution_clock::time_point m_stopTime;
|
||||
bool m_running;
|
||||
|
||||
public:
|
||||
Timer() : m_running(false) {}
|
||||
|
||||
CTimer();
|
||||
~CTimer();
|
||||
void Start() {
|
||||
if (!m_running) {
|
||||
m_startTime = std::chrono::high_resolution_clock::now();
|
||||
m_running = true;
|
||||
}
|
||||
}
|
||||
|
||||
void Start();
|
||||
void Stop();
|
||||
void Advance();
|
||||
void Reset();
|
||||
double Tick();
|
||||
inline double GetAppTime()
|
||||
{
|
||||
return ( m_currSysTime - m_baseTime );
|
||||
}
|
||||
inline double GetElapsedTime()
|
||||
{
|
||||
return m_currElapsedTime;
|
||||
}
|
||||
inline double GetSysTime()
|
||||
{
|
||||
return m_currSysTime;
|
||||
}
|
||||
inline double GetAppMilliTime()
|
||||
{
|
||||
return ( m_currSysMilliTime - m_baseMilliTime );
|
||||
}
|
||||
inline double GetElapsedMilliTime()
|
||||
{
|
||||
return m_currElapsedMilliTime;
|
||||
}
|
||||
void Stop() {
|
||||
if (m_running) {
|
||||
m_stopTime = std::chrono::high_resolution_clock::now();
|
||||
m_running = false;
|
||||
}
|
||||
}
|
||||
|
||||
double GetAbsoluteTime();
|
||||
void Reset() {
|
||||
m_startTime = std::chrono::high_resolution_clock::now();
|
||||
m_stopTime = m_startTime;
|
||||
m_running = false;
|
||||
}
|
||||
|
||||
double GetElapsedTime() const
|
||||
{
|
||||
if (m_running) {
|
||||
auto currentTime = std::chrono::high_resolution_clock::now();
|
||||
return std::chrono::duration<double>(currentTime - m_startTime).count();
|
||||
}
|
||||
else {
|
||||
return std::chrono::duration<double>(m_stopTime - m_startTime).count();
|
||||
}
|
||||
}
|
||||
|
||||
long long GetElapsedTimeMilliseconds() const
|
||||
{
|
||||
if( m_running )
|
||||
{
|
||||
auto currentTime = std::chrono::high_resolution_clock::now();
|
||||
auto duration = std::chrono::duration_cast< std::chrono::milliseconds >( currentTime - m_startTime );
|
||||
return duration.count();
|
||||
}
|
||||
else
|
||||
{
|
||||
auto duration = std::chrono::duration_cast< std::chrono::milliseconds >( m_stopTime - m_startTime );
|
||||
return duration.count();
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
/*
|
||||
#include "../global_define.h"
|
||||
|
||||
PacketBuffer::PacketBuffer( uint16_t command, uint32_t event_seq, uint32_t hint_size )
|
||||
@@ -258,4 +259,5 @@ void process_networking()
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
*/
|
||||
@@ -1,5 +1,6 @@
|
||||
#pragma once
|
||||
|
||||
/*
|
||||
#include <memory>
|
||||
#include "socket.h"
|
||||
|
||||
@@ -77,9 +78,11 @@ public:
|
||||
uint32_t write_position;
|
||||
uint32_t read_position;
|
||||
};
|
||||
*/
|
||||
|
||||
typedef std::shared_ptr< PacketBuffer > sptr_packet;
|
||||
|
||||
//typedef std::shared_ptr< PacketBuffer > sptr_packet;
|
||||
/*
|
||||
sptr_packet make_packet( uint16_t command, uint32_t event_id, uint32_t hint_size );
|
||||
sptr_packet make_packet( sptr_packet request );
|
||||
void process_networking();
|
||||
void process_networking();
|
||||
*/
|
||||
@@ -1,4 +1,4 @@
|
||||
|
||||
/*
|
||||
#include "../global_define.h"
|
||||
|
||||
#include "protocol_broker.h"
|
||||
@@ -22,7 +22,7 @@ void Protocol::Broker::process_request( sptr_socket socket, sptr_packet request
|
||||
auto protocol_iter = BROKER_PROTOCOL.find( request->get_command() );
|
||||
if( protocol_iter == BROKER_PROTOCOL.end() )
|
||||
{
|
||||
logging.error( "UNDEFINED PROTOCOL: %02X", request->get_command() );
|
||||
Log::Error( "UNDEFINED PROTOCOL: %02X", request->get_command() );
|
||||
logging.packet( request->buffer, false );
|
||||
return;
|
||||
}
|
||||
@@ -45,4 +45,5 @@ void Protocol::Broker::process_notice( sptr_socket s, sptr_packet r )
|
||||
{
|
||||
} break;
|
||||
}
|
||||
}
|
||||
}
|
||||
*/
|
||||
@@ -7,11 +7,9 @@
|
||||
// ╠╩╗╠╦╝║ ║╠╩╗║╣ ╠╦╝ ╠═╝╠╦╝║ ║ ║ ║ ║║ ║ ║║
|
||||
// ╚═╝╩╚═╚═╝╩ ╩╚═╝╩╚═ ╩ ╩╚═╚═╝ ╩ ╚═╝╚═╝╚═╝╩═╝
|
||||
|
||||
#include "packet.h"
|
||||
#include "socket.h"
|
||||
|
||||
/*
|
||||
namespace Protocol::Broker
|
||||
{
|
||||
void process_request( sptr_socket socket, sptr_packet r ); // Process incoming packets
|
||||
void process_notice( sptr_socket socket, sptr_packet r ); // Process socket notices
|
||||
}
|
||||
}*/
|
||||
@@ -1,4 +1,4 @@
|
||||
|
||||
/*
|
||||
#include "../global_define.h"
|
||||
|
||||
using namespace Protocol::Game;
|
||||
@@ -38,7 +38,7 @@ void Protocol::Game::process_request( sptr_socket socket, sptr_packet request )
|
||||
auto protocol_iter = GAME_PROTOCOL.find( request->get_command() );
|
||||
if( protocol_iter == GAME_PROTOCOL.end() )
|
||||
{
|
||||
logging.error( "UNDEFINED PROTOCOL: %02X", request->get_command() );
|
||||
Log::Error( "UNDEFINED PROTOCOL: %02X", request->get_command() );
|
||||
logging.packet( request->buffer, false );
|
||||
return;
|
||||
}
|
||||
@@ -210,3 +210,4 @@ void Protocol::Game::ReqUpdateGameData( sptr_client client, sptr_packet request
|
||||
}
|
||||
client->socket->send( res );
|
||||
}
|
||||
*/
|
||||
@@ -7,9 +7,7 @@
|
||||
// ║ ╦╠═╣║║║║╣ ╠═╝╠╦╝║ ║ ║ ║ ║║ ║ ║║
|
||||
// ╚═╝╩ ╩╩ ╩╚═╝ ╩ ╩╚═╚═╝ ╩ ╚═╝╚═╝╚═╝╩═╝
|
||||
|
||||
#include "packet.h"
|
||||
#include "socket.h"
|
||||
|
||||
/*
|
||||
namespace Protocol::Game
|
||||
{
|
||||
void process_request( sptr_socket s, sptr_packet r ); // Process incoming packets
|
||||
@@ -25,4 +23,4 @@ namespace Protocol::Game
|
||||
void ReqGetEncryptionKey( sptr_client client, sptr_packet request ); // 2700
|
||||
void ReqGetRules( sptr_client client, sptr_packet request ); // 4200
|
||||
void ReqUpdateGameData( sptr_client client, sptr_packet request ); // 4400
|
||||
}
|
||||
}*/
|
||||
@@ -1,4 +1,4 @@
|
||||
|
||||
/*
|
||||
#include "../global_define.h"
|
||||
|
||||
#include "protocol_gateway.h"
|
||||
@@ -23,7 +23,7 @@ void Protocol::Gateway::process_request( sptr_socket socket, sptr_packet request
|
||||
auto protocol_iter = GATEWAY_PROTOCOL.find( request->get_command() );
|
||||
if( protocol_iter == GATEWAY_PROTOCOL.end() )
|
||||
{
|
||||
logging.error( "UNDEFINED PROTOCOL: %02X", request->get_command() );
|
||||
Log::Error( "UNDEFINED PROTOCOL: %02X", request->get_command() );
|
||||
logging.packet( request->buffer, false );
|
||||
return;
|
||||
}
|
||||
@@ -57,3 +57,4 @@ void Protocol::Gateway::ReqGetServerAddress( sptr_socket socket, sptr_packet req
|
||||
}
|
||||
socket->send( res );
|
||||
}
|
||||
*/
|
||||
@@ -7,13 +7,11 @@
|
||||
// ║ ╦╠═╣ ║ ║╣ ║║║╠═╣╚╦╝ ╠═╝╠╦╝║ ║ ║ ║ ║║ ║ ║║
|
||||
// ╚═╝╩ ╩ ╩ ╚═╝╚╩╝╩ ╩ ╩ ╩ ╩╚═╚═╝ ╩ ╚═╝╚═╝╚═╝╩═╝
|
||||
|
||||
#include "packet.h"
|
||||
#include "socket.h"
|
||||
|
||||
/*
|
||||
namespace Protocol::Gateway
|
||||
{
|
||||
void process_request( sptr_socket socket, sptr_packet r ); // Process incoming packets
|
||||
void process_notice( sptr_socket socket, sptr_packet r ); // Process socket notices
|
||||
|
||||
void ReqGetServerAddress( sptr_socket socket, sptr_packet r ); // 4300
|
||||
}
|
||||
}*/
|
||||
@@ -1,66 +0,0 @@
|
||||
|
||||
#include "../global_define.h"
|
||||
#include "socket.h"
|
||||
|
||||
CRealmSocket::CRealmSocket()
|
||||
{
|
||||
fd = INVALID_SOCKET;
|
||||
|
||||
memset( &local_address, 0, sizeof( local_address ) );
|
||||
memset( &remote_address, 0, sizeof( remote_address ) );
|
||||
port = 0;
|
||||
|
||||
type = RealmSocketType::TCP;
|
||||
channel = RealmChannelType::INVALID;
|
||||
|
||||
flag.disconnected = 0;
|
||||
flag.is_listener = 0;
|
||||
flag.want_more_read_data = 0;
|
||||
flag.want_more_write_data = 0;
|
||||
|
||||
last_write_position = 0;
|
||||
|
||||
latency = 0;
|
||||
last_recv_time = 0.0;
|
||||
last_send_time = 0.0;
|
||||
}
|
||||
|
||||
CRealmSocket::~CRealmSocket()
|
||||
{
|
||||
if( INVALID_SOCKET != fd )
|
||||
{
|
||||
closesocket( fd );
|
||||
}
|
||||
|
||||
fd = INVALID_SOCKET;
|
||||
|
||||
memset( &local_address, 0, sizeof( local_address ) );
|
||||
memset( &remote_address, 0, sizeof( remote_address ) );
|
||||
port = 0;
|
||||
|
||||
type = RealmSocketType::TCP;
|
||||
channel = RealmChannelType::INVALID;
|
||||
|
||||
flag.disconnected = 0;
|
||||
flag.is_listener = 0;
|
||||
flag.want_more_read_data = 0;
|
||||
flag.want_more_write_data = 0;
|
||||
|
||||
last_write_position = 0;
|
||||
|
||||
latency = 0;
|
||||
last_recv_time = 0.0;
|
||||
last_send_time = 0.0;
|
||||
}
|
||||
|
||||
void CRealmSocket::send( const sptr_packet p )
|
||||
{
|
||||
// TODO: UDP sockets probably need to be handled differently
|
||||
//
|
||||
// Swap the packet size to network byte order
|
||||
*( uint32_t * )&p->buffer[ 0 ] = Math::swap_endian( p->write_position );
|
||||
|
||||
logging.packet( p->buffer, true );
|
||||
std::lock_guard< std::mutex > lock( write_mutex );
|
||||
write_queue.push_back( p );
|
||||
}
|
||||
@@ -1,62 +0,0 @@
|
||||
#pragma once
|
||||
|
||||
#include <mutex>
|
||||
#include <vector>
|
||||
#include <memory>
|
||||
|
||||
#include "packet.h"
|
||||
|
||||
enum class RealmSocketType
|
||||
{
|
||||
TCP = 0,
|
||||
UDP,
|
||||
};
|
||||
|
||||
enum class RealmChannelType
|
||||
{
|
||||
INVALID = 0,
|
||||
GATEWAY,
|
||||
GAME,
|
||||
DISCOVERY,
|
||||
};
|
||||
|
||||
class CRealmSocket
|
||||
{
|
||||
public:
|
||||
CRealmSocket();
|
||||
~CRealmSocket();
|
||||
|
||||
void send( const sptr_packet p );
|
||||
|
||||
struct s_flag
|
||||
{
|
||||
bool disconnected;
|
||||
bool is_listener;
|
||||
bool want_more_read_data;
|
||||
bool want_more_write_data;
|
||||
} flag;
|
||||
|
||||
RealmSocketType type;
|
||||
RealmChannelType channel;
|
||||
|
||||
SOCKET fd;
|
||||
uint16_t port;
|
||||
sockaddr_in local_address;
|
||||
sockaddr_in remote_address;
|
||||
std::string peer_ip_address;
|
||||
|
||||
uint32_t last_write_position;
|
||||
|
||||
double_t latency;
|
||||
double_t last_recv_time;
|
||||
double_t last_send_time;
|
||||
|
||||
std::mutex write_mutex;
|
||||
std::mutex read_mutex;
|
||||
|
||||
std::vector< uint8_t > read_buffer;
|
||||
std::list< sptr_packet > read_queue;
|
||||
std::list< sptr_packet > write_queue;
|
||||
};
|
||||
|
||||
typedef std::shared_ptr< CRealmSocket > sptr_socket;
|
||||
@@ -1,4 +1,4 @@
|
||||
|
||||
/*
|
||||
#include "../global_define.h"
|
||||
#include "socket_manager.h"
|
||||
|
||||
@@ -67,7 +67,7 @@ bool CSocketManager::open_tcp_listener( std::string ip, uint16_t port, RealmChan
|
||||
|
||||
if( bind( socket->fd, ( LPSOCKADDR )&addr, sizeof( addr ) ) != 0 )
|
||||
{
|
||||
logging.error( "Could not open socket on %s:%d", ip.c_str(), port );
|
||||
Log::Error( "Could not open socket on %s:%d", ip.c_str(), port );
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -84,7 +84,7 @@ bool CSocketManager::open_tcp_listener( std::string ip, uint16_t port, RealmChan
|
||||
|
||||
socket_list.push_back( socket );
|
||||
|
||||
logging.information( "Open TCP Listener on %s:%d", ip.c_str(), port );
|
||||
Log::Info( "Open TCP Listener on %s:%d", ip.c_str(), port );
|
||||
|
||||
return true;
|
||||
}
|
||||
@@ -108,7 +108,7 @@ bool CSocketManager::open_udp_listener( std::string ip, uint16_t port, RealmChan
|
||||
|
||||
if( bind( socket->fd, ( LPSOCKADDR )&addr, sizeof( addr ) ) != 0 )
|
||||
{
|
||||
logging.error( "Could not open socket on %s:%d", ip.c_str(), port );
|
||||
Log::Error( "Could not open socket on %s:%d", ip.c_str(), port );
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -123,7 +123,7 @@ bool CSocketManager::open_udp_listener( std::string ip, uint16_t port, RealmChan
|
||||
|
||||
socket_list.push_back( socket );
|
||||
|
||||
logging.information( "Open UDP Listener on %s:%d", ip.c_str(), port );
|
||||
Log::Info( "Open UDP Listener on %s:%d", ip.c_str(), port );
|
||||
|
||||
return false;
|
||||
}
|
||||
@@ -166,11 +166,11 @@ void CSocketManager::accept_new_tcp( sptr_socket from_socket )
|
||||
|
||||
if( from_socket->channel == RealmChannelType::GATEWAY )
|
||||
{
|
||||
logging.information( "[GATEWAY] : New connection from %s", ( *new_socket ).peer_ip_address.c_str() );
|
||||
Log::Info( "[GATEWAY] : New connection from %s", ( *new_socket ).peer_ip_address.c_str() );
|
||||
}
|
||||
else if( from_socket->channel == RealmChannelType::GAME )
|
||||
{
|
||||
logging.information( "[GAME] : New connection from %s", ( *new_socket ).peer_ip_address.c_str() );
|
||||
Log::Info( "[GAME] : New connection from %s", ( *new_socket ).peer_ip_address.c_str() );
|
||||
}
|
||||
|
||||
new_socket->last_recv_time = net_time.GetAppMilliTime();
|
||||
@@ -306,7 +306,7 @@ void CSocketManager::TCP_TryReadData( sptr_socket socket )
|
||||
{
|
||||
case WSAECONNRESET:
|
||||
{
|
||||
logging.information( "Connection %s reset by peer.", socket->peer_ip_address.c_str() );
|
||||
Log::Info( "Connection %s reset by peer.", socket->peer_ip_address.c_str() );
|
||||
} break;
|
||||
}
|
||||
|
||||
@@ -465,3 +465,4 @@ void CSocketManager::UDP_TryWriteData( sptr_socket s )
|
||||
|
||||
s->write_mutex.unlock();
|
||||
}
|
||||
*/
|
||||
@@ -1,5 +1,5 @@
|
||||
#pragma once
|
||||
|
||||
/*
|
||||
#include <vector>
|
||||
#include <set>
|
||||
#include <thread>
|
||||
@@ -53,4 +53,4 @@ private:
|
||||
|
||||
void UDP_TryReadData( sptr_socket s );
|
||||
void UDP_TryWriteData( sptr_socket s );
|
||||
};
|
||||
};*/
|
||||
42
ui/logging.h
42
ui/logging.h
@@ -1,42 +0,0 @@
|
||||
#pragma once
|
||||
|
||||
#include <fstream>
|
||||
|
||||
class CLogManager {
|
||||
private:
|
||||
enum LOG_TYPE {
|
||||
log_generic = 0,
|
||||
log_debug,
|
||||
log_error,
|
||||
num_log_type
|
||||
};
|
||||
|
||||
uint8_t current_open_hour;
|
||||
std::fstream file_stream[ num_log_type ];
|
||||
|
||||
const char* get_time_stamp();
|
||||
void check_file_status( LOG_TYPE type );
|
||||
void write_log( LOG_TYPE type, std::string format );
|
||||
public:
|
||||
CLogManager()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
~CLogManager()
|
||||
{
|
||||
for( uint8_t i = 0; i < num_log_type; i++ )
|
||||
{
|
||||
if( file_stream[ i ].is_open() )
|
||||
{
|
||||
file_stream[ i ].close();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void information( std::string format, ... );
|
||||
void debug( std::string format, ... );
|
||||
void error( std::string format, ... );
|
||||
void packet( std::vector< uint8_t > p, bool send );
|
||||
void packet( std::vector< uint8_t > p, uint32_t size, bool send );
|
||||
};
|
||||
Reference in New Issue
Block a user