commit
d00ebd7ee7
@ -6,6 +6,15 @@ compiler:
|
|||||||
before_install:
|
before_install:
|
||||||
- if [ "$TRAVIS_MCSERVER_BUILD_TYPE" == "COVERAGE" ]; then sudo pip install cpp_coveralls; fi
|
- if [ "$TRAVIS_MCSERVER_BUILD_TYPE" == "COVERAGE" ]; then sudo pip install cpp_coveralls; fi
|
||||||
|
|
||||||
|
# g++4.8
|
||||||
|
- sudo add-apt-repository -y ppa:ubuntu-toolchain-r/test
|
||||||
|
- sudo apt-get update -qq
|
||||||
|
|
||||||
|
install:
|
||||||
|
# g++4.8
|
||||||
|
- sudo apt-get install -qq g++-4.8
|
||||||
|
- export CXX="g++-4.8"
|
||||||
|
|
||||||
# Build MCServer
|
# Build MCServer
|
||||||
script: ./CIbuild.sh
|
script: ./CIbuild.sh
|
||||||
|
|
||||||
|
@ -64,18 +64,12 @@ macro(set_flags)
|
|||||||
execute_process(COMMAND ${CMAKE_C_COMPILER} -dumpversion
|
execute_process(COMMAND ${CMAKE_C_COMPILER} -dumpversion
|
||||||
OUTPUT_VARIABLE GCC_VERSION)
|
OUTPUT_VARIABLE GCC_VERSION)
|
||||||
endif()
|
endif()
|
||||||
|
|
||||||
if("${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU" AND (NOT GCC_VERSION VERSION_GREATER 4.6))
|
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
|
||||||
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++0x")
|
set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -std=c++11")
|
||||||
set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -std=c++0x")
|
set(CMAKE_CXX_FLAGS_COVERAGE "${CMAKE_CXX_FLAGS_COVERAGE} -std=c++11")
|
||||||
set(CMAKE_CXX_FLAGS_COVERAGE "${CMAKE_CXX_FLAGS_COVERAGE} -std=c++0x")
|
set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} -std=c++11")
|
||||||
set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} -std=c++0x")
|
|
||||||
else()
|
|
||||||
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
|
|
||||||
set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -std=c++11")
|
|
||||||
set(CMAKE_CXX_FLAGS_COVERAGE "${CMAKE_CXX_FLAGS_COVERAGE} -std=c++11")
|
|
||||||
set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} -std=c++11")
|
|
||||||
endif()
|
|
||||||
#on os x clang adds pthread for us but we need to add it for gcc
|
#on os x clang adds pthread for us but we need to add it for gcc
|
||||||
if ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang")
|
if ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang")
|
||||||
add_flags_cxx("-stdlib=libc++")
|
add_flags_cxx("-stdlib=libc++")
|
||||||
@ -94,18 +88,11 @@ macro(set_flags)
|
|||||||
execute_process(COMMAND ${CMAKE_C_COMPILER} -dumpversion
|
execute_process(COMMAND ${CMAKE_C_COMPILER} -dumpversion
|
||||||
OUTPUT_VARIABLE GCC_VERSION)
|
OUTPUT_VARIABLE GCC_VERSION)
|
||||||
endif()
|
endif()
|
||||||
|
|
||||||
if("${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU" AND (NOT GCC_VERSION VERSION_GREATER 4.6))
|
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
|
||||||
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++0x")
|
set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -std=c++11")
|
||||||
set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -std=c++0x")
|
set(CMAKE_CXX_FLAGS_COVERAGE "${CMAKE_CXX_FLAGS_COVERAGE} -std=c++11")
|
||||||
set(CMAKE_CXX_FLAGS_COVERAGE "${CMAKE_CXX_FLAGS_COVERAGE} -std=c++0x")
|
set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} -std=c++11")
|
||||||
set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} -std=c++0x")
|
|
||||||
else()
|
|
||||||
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
|
|
||||||
set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -std=c++11")
|
|
||||||
set(CMAKE_CXX_FLAGS_COVERAGE "${CMAKE_CXX_FLAGS_COVERAGE} -std=c++11")
|
|
||||||
set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} -std=c++11")
|
|
||||||
endif()
|
|
||||||
|
|
||||||
# We use a signed char (fixes #640 on RasPi)
|
# We use a signed char (fixes #640 on RasPi)
|
||||||
add_flags_cxx("-fsigned-char")
|
add_flags_cxx("-fsigned-char")
|
||||||
|
@ -53,7 +53,6 @@ set(SHARED_OSS_SRC
|
|||||||
../../src/OSSupport/File.cpp
|
../../src/OSSupport/File.cpp
|
||||||
../../src/OSSupport/GZipFile.cpp
|
../../src/OSSupport/GZipFile.cpp
|
||||||
../../src/OSSupport/IsThread.cpp
|
../../src/OSSupport/IsThread.cpp
|
||||||
../../src/OSSupport/Timer.cpp
|
|
||||||
)
|
)
|
||||||
|
|
||||||
set(SHARED_OSS_HDR
|
set(SHARED_OSS_HDR
|
||||||
@ -62,7 +61,6 @@ set(SHARED_OSS_HDR
|
|||||||
../../src/OSSupport/File.h
|
../../src/OSSupport/File.h
|
||||||
../../src/OSSupport/GZipFile.h
|
../../src/OSSupport/GZipFile.h
|
||||||
../../src/OSSupport/IsThread.h
|
../../src/OSSupport/IsThread.h
|
||||||
../../src/OSSupport/Timer.h
|
|
||||||
)
|
)
|
||||||
|
|
||||||
flatten_files(SHARED_SRC)
|
flatten_files(SHARED_SRC)
|
||||||
|
@ -56,14 +56,13 @@ set(SHARED_OSS_SRC
|
|||||||
../../src/OSSupport/File.cpp
|
../../src/OSSupport/File.cpp
|
||||||
../../src/OSSupport/IsThread.cpp
|
../../src/OSSupport/IsThread.cpp
|
||||||
../../src/OSSupport/StackTrace.cpp
|
../../src/OSSupport/StackTrace.cpp
|
||||||
../../src/OSSupport/Timer.cpp
|
|
||||||
)
|
)
|
||||||
|
|
||||||
set(SHARED_OSS_HDR
|
set(SHARED_OSS_HDR
|
||||||
../../src/OSSupport/CriticalSection.h
|
../../src/OSSupport/CriticalSection.h
|
||||||
../../src/OSSupport/File.h
|
../../src/OSSupport/File.h
|
||||||
../../src/OSSupport/IsThread.h
|
../../src/OSSupport/IsThread.h
|
||||||
../../src/OSSupport/StackTrace.h
|
../../src/OSSupport/StackTrace.h
|
||||||
../../src/OSSupport/Timer.h
|
|
||||||
)
|
)
|
||||||
|
|
||||||
if(WIN32)
|
if(WIN32)
|
||||||
|
@ -58,14 +58,12 @@ set(SHARED_OSS_SRC
|
|||||||
../../src/OSSupport/File.cpp
|
../../src/OSSupport/File.cpp
|
||||||
../../src/OSSupport/IsThread.cpp
|
../../src/OSSupport/IsThread.cpp
|
||||||
../../src/OSSupport/StackTrace.cpp
|
../../src/OSSupport/StackTrace.cpp
|
||||||
../../src/OSSupport/Timer.cpp
|
|
||||||
)
|
)
|
||||||
set(SHARED_OSS_HDR
|
set(SHARED_OSS_HDR
|
||||||
../../src/OSSupport/CriticalSection.h
|
../../src/OSSupport/CriticalSection.h
|
||||||
../../src/OSSupport/File.h
|
../../src/OSSupport/File.h
|
||||||
../../src/OSSupport/IsThread.h
|
../../src/OSSupport/IsThread.h
|
||||||
../../src/OSSupport/StackTrace.h
|
../../src/OSSupport/StackTrace.h
|
||||||
../../src/OSSupport/Timer.h
|
|
||||||
)
|
)
|
||||||
|
|
||||||
if(WIN32)
|
if(WIN32)
|
||||||
|
@ -189,7 +189,7 @@ cConnection::cConnection(SOCKET a_ClientSocket, cServer & a_Server) :
|
|||||||
m_Server(a_Server),
|
m_Server(a_Server),
|
||||||
m_ClientSocket(a_ClientSocket),
|
m_ClientSocket(a_ClientSocket),
|
||||||
m_ServerSocket(-1),
|
m_ServerSocket(-1),
|
||||||
m_BeginTick(m_Timer.GetNowTime()),
|
m_BeginTick(std::chrono::steady_clock::now()),
|
||||||
m_ClientState(csUnencrypted),
|
m_ClientState(csUnencrypted),
|
||||||
m_ServerState(csUnencrypted),
|
m_ServerState(csUnencrypted),
|
||||||
m_Nonce(0),
|
m_Nonce(0),
|
||||||
@ -436,7 +436,7 @@ bool cConnection::RelayFromClient(void)
|
|||||||
|
|
||||||
double cConnection::GetRelativeTime(void)
|
double cConnection::GetRelativeTime(void)
|
||||||
{
|
{
|
||||||
return (double)(m_Timer.GetNowTime() - m_BeginTick) / 1000;
|
return std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::steady_clock::now() - m_BeginTick).count() / 1000;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -10,7 +10,6 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include "ByteBuffer.h"
|
#include "ByteBuffer.h"
|
||||||
#include "OSSupport/Timer.h"
|
|
||||||
#include "PolarSSL++/AesCfb128Decryptor.h"
|
#include "PolarSSL++/AesCfb128Decryptor.h"
|
||||||
#include "PolarSSL++/AesCfb128Encryptor.h"
|
#include "PolarSSL++/AesCfb128Encryptor.h"
|
||||||
|
|
||||||
@ -37,8 +36,7 @@ class cConnection
|
|||||||
SOCKET m_ClientSocket;
|
SOCKET m_ClientSocket;
|
||||||
SOCKET m_ServerSocket;
|
SOCKET m_ServerSocket;
|
||||||
|
|
||||||
cTimer m_Timer;
|
std::chrono::steady_clock::time_point m_BeginTick; // Tick when the relative time was first retrieved (used for GetRelativeTime())
|
||||||
long long m_BeginTick; // Tick when the relative time was first retrieved (used for GetRelativeTime())
|
|
||||||
|
|
||||||
enum eConnectionState
|
enum eConnectionState
|
||||||
{
|
{
|
||||||
|
@ -111,26 +111,36 @@ public:
|
|||||||
|
|
||||||
#ifdef _DEBUG
|
#ifdef _DEBUG
|
||||||
|
|
||||||
/// Simple RAII class that uses one internal unsigned long for checking if two threads are using an object simultanously
|
/** Simple RAII class that is used for checking that no two threads are using an object simultanously.
|
||||||
class cSingleThreadAccessChecker
|
It requires the monitored object to provide the storage for a thread ID.
|
||||||
{
|
It uses that storage to check if the thread ID of consecutive calls is the same all the time. */
|
||||||
public:
|
class cSingleThreadAccessChecker
|
||||||
cSingleThreadAccessChecker(unsigned long * a_ThreadID) :
|
|
||||||
m_ThreadID(a_ThreadID)
|
|
||||||
{
|
{
|
||||||
ASSERT((*a_ThreadID == 0) || (*a_ThreadID == cIsThread::GetCurrentID()));
|
public:
|
||||||
}
|
cSingleThreadAccessChecker(std::thread::id * a_ThreadID) :
|
||||||
|
m_ThreadID(a_ThreadID)
|
||||||
~cSingleThreadAccessChecker()
|
{
|
||||||
{
|
ASSERT(
|
||||||
*m_ThreadID = 0;
|
(*a_ThreadID == std::this_thread::get_id()) || // Either the object is used by current thread...
|
||||||
}
|
(*a_ThreadID == std::thread::id()) // ... or by no thread at all
|
||||||
|
);
|
||||||
protected:
|
|
||||||
unsigned long * m_ThreadID;
|
|
||||||
} ;
|
|
||||||
|
|
||||||
#define CHECK_THREAD cSingleThreadAccessChecker Checker(const_cast<unsigned long *>(&m_ThreadID))
|
// Mark as being used by this thread:
|
||||||
|
*m_ThreadID = std::this_thread::get_id();
|
||||||
|
}
|
||||||
|
|
||||||
|
~cSingleThreadAccessChecker()
|
||||||
|
{
|
||||||
|
// Mark as not being used by any thread:
|
||||||
|
*m_ThreadID = std::thread::id();
|
||||||
|
}
|
||||||
|
|
||||||
|
protected:
|
||||||
|
/** Points to the storage used for ID of the thread using the object. */
|
||||||
|
std::thread::id * m_ThreadID;
|
||||||
|
};
|
||||||
|
|
||||||
|
#define CHECK_THREAD cSingleThreadAccessChecker Checker(&m_ThreadID);
|
||||||
|
|
||||||
#else
|
#else
|
||||||
#define CHECK_THREAD
|
#define CHECK_THREAD
|
||||||
@ -146,9 +156,6 @@ protected:
|
|||||||
cByteBuffer::cByteBuffer(size_t a_BufferSize) :
|
cByteBuffer::cByteBuffer(size_t a_BufferSize) :
|
||||||
m_Buffer(new char[a_BufferSize + 1]),
|
m_Buffer(new char[a_BufferSize + 1]),
|
||||||
m_BufferSize(a_BufferSize + 1),
|
m_BufferSize(a_BufferSize + 1),
|
||||||
#ifdef _DEBUG
|
|
||||||
m_ThreadID(0),
|
|
||||||
#endif // _DEBUG
|
|
||||||
m_DataStart(0),
|
m_DataStart(0),
|
||||||
m_WritePos(0),
|
m_WritePos(0),
|
||||||
m_ReadPos(0)
|
m_ReadPos(0)
|
||||||
@ -174,7 +181,7 @@ cByteBuffer::~cByteBuffer()
|
|||||||
|
|
||||||
bool cByteBuffer::Write(const void * a_Bytes, size_t a_Count)
|
bool cByteBuffer::Write(const void * a_Bytes, size_t a_Count)
|
||||||
{
|
{
|
||||||
CHECK_THREAD;
|
CHECK_THREAD
|
||||||
CheckValid();
|
CheckValid();
|
||||||
|
|
||||||
// Store the current free space for a check after writing:
|
// Store the current free space for a check after writing:
|
||||||
@ -221,7 +228,7 @@ bool cByteBuffer::Write(const void * a_Bytes, size_t a_Count)
|
|||||||
|
|
||||||
size_t cByteBuffer::GetFreeSpace(void) const
|
size_t cByteBuffer::GetFreeSpace(void) const
|
||||||
{
|
{
|
||||||
CHECK_THREAD;
|
CHECK_THREAD
|
||||||
CheckValid();
|
CheckValid();
|
||||||
if (m_WritePos >= m_DataStart)
|
if (m_WritePos >= m_DataStart)
|
||||||
{
|
{
|
||||||
@ -243,7 +250,7 @@ size_t cByteBuffer::GetFreeSpace(void) const
|
|||||||
/// Returns the number of bytes that are currently in the ringbuffer. Note GetReadableBytes()
|
/// Returns the number of bytes that are currently in the ringbuffer. Note GetReadableBytes()
|
||||||
size_t cByteBuffer::GetUsedSpace(void) const
|
size_t cByteBuffer::GetUsedSpace(void) const
|
||||||
{
|
{
|
||||||
CHECK_THREAD;
|
CHECK_THREAD
|
||||||
CheckValid();
|
CheckValid();
|
||||||
ASSERT(m_BufferSize >= GetFreeSpace());
|
ASSERT(m_BufferSize >= GetFreeSpace());
|
||||||
ASSERT((m_BufferSize - GetFreeSpace()) >= 1);
|
ASSERT((m_BufferSize - GetFreeSpace()) >= 1);
|
||||||
@ -257,7 +264,7 @@ size_t cByteBuffer::GetUsedSpace(void) const
|
|||||||
/// Returns the number of bytes that are currently available for reading (may be less than UsedSpace due to some data having been read already)
|
/// Returns the number of bytes that are currently available for reading (may be less than UsedSpace due to some data having been read already)
|
||||||
size_t cByteBuffer::GetReadableSpace(void) const
|
size_t cByteBuffer::GetReadableSpace(void) const
|
||||||
{
|
{
|
||||||
CHECK_THREAD;
|
CHECK_THREAD
|
||||||
CheckValid();
|
CheckValid();
|
||||||
if (m_ReadPos > m_WritePos)
|
if (m_ReadPos > m_WritePos)
|
||||||
{
|
{
|
||||||
@ -276,7 +283,7 @@ size_t cByteBuffer::GetReadableSpace(void) const
|
|||||||
|
|
||||||
bool cByteBuffer::CanReadBytes(size_t a_Count) const
|
bool cByteBuffer::CanReadBytes(size_t a_Count) const
|
||||||
{
|
{
|
||||||
CHECK_THREAD;
|
CHECK_THREAD
|
||||||
CheckValid();
|
CheckValid();
|
||||||
return (a_Count <= GetReadableSpace());
|
return (a_Count <= GetReadableSpace());
|
||||||
}
|
}
|
||||||
@ -287,7 +294,7 @@ bool cByteBuffer::CanReadBytes(size_t a_Count) const
|
|||||||
|
|
||||||
bool cByteBuffer::CanWriteBytes(size_t a_Count) const
|
bool cByteBuffer::CanWriteBytes(size_t a_Count) const
|
||||||
{
|
{
|
||||||
CHECK_THREAD;
|
CHECK_THREAD
|
||||||
CheckValid();
|
CheckValid();
|
||||||
return (a_Count <= GetFreeSpace());
|
return (a_Count <= GetFreeSpace());
|
||||||
}
|
}
|
||||||
@ -298,7 +305,7 @@ bool cByteBuffer::CanWriteBytes(size_t a_Count) const
|
|||||||
|
|
||||||
bool cByteBuffer::ReadChar(char & a_Value)
|
bool cByteBuffer::ReadChar(char & a_Value)
|
||||||
{
|
{
|
||||||
CHECK_THREAD;
|
CHECK_THREAD
|
||||||
CheckValid();
|
CheckValid();
|
||||||
NEEDBYTES(1);
|
NEEDBYTES(1);
|
||||||
ReadBuf(&a_Value, 1);
|
ReadBuf(&a_Value, 1);
|
||||||
@ -311,7 +318,7 @@ bool cByteBuffer::ReadChar(char & a_Value)
|
|||||||
|
|
||||||
bool cByteBuffer::ReadByte(unsigned char & a_Value)
|
bool cByteBuffer::ReadByte(unsigned char & a_Value)
|
||||||
{
|
{
|
||||||
CHECK_THREAD;
|
CHECK_THREAD
|
||||||
CheckValid();
|
CheckValid();
|
||||||
NEEDBYTES(1);
|
NEEDBYTES(1);
|
||||||
ReadBuf(&a_Value, 1);
|
ReadBuf(&a_Value, 1);
|
||||||
@ -324,7 +331,7 @@ bool cByteBuffer::ReadByte(unsigned char & a_Value)
|
|||||||
|
|
||||||
bool cByteBuffer::ReadBEShort(short & a_Value)
|
bool cByteBuffer::ReadBEShort(short & a_Value)
|
||||||
{
|
{
|
||||||
CHECK_THREAD;
|
CHECK_THREAD
|
||||||
CheckValid();
|
CheckValid();
|
||||||
NEEDBYTES(2);
|
NEEDBYTES(2);
|
||||||
ReadBuf(&a_Value, 2);
|
ReadBuf(&a_Value, 2);
|
||||||
@ -338,7 +345,7 @@ bool cByteBuffer::ReadBEShort(short & a_Value)
|
|||||||
|
|
||||||
bool cByteBuffer::ReadBEInt(int & a_Value)
|
bool cByteBuffer::ReadBEInt(int & a_Value)
|
||||||
{
|
{
|
||||||
CHECK_THREAD;
|
CHECK_THREAD
|
||||||
CheckValid();
|
CheckValid();
|
||||||
NEEDBYTES(4);
|
NEEDBYTES(4);
|
||||||
ReadBuf(&a_Value, 4);
|
ReadBuf(&a_Value, 4);
|
||||||
@ -352,7 +359,7 @@ bool cByteBuffer::ReadBEInt(int & a_Value)
|
|||||||
|
|
||||||
bool cByteBuffer::ReadBEInt64(Int64 & a_Value)
|
bool cByteBuffer::ReadBEInt64(Int64 & a_Value)
|
||||||
{
|
{
|
||||||
CHECK_THREAD;
|
CHECK_THREAD
|
||||||
CheckValid();
|
CheckValid();
|
||||||
NEEDBYTES(8);
|
NEEDBYTES(8);
|
||||||
ReadBuf(&a_Value, 8);
|
ReadBuf(&a_Value, 8);
|
||||||
@ -366,7 +373,7 @@ bool cByteBuffer::ReadBEInt64(Int64 & a_Value)
|
|||||||
|
|
||||||
bool cByteBuffer::ReadBEFloat(float & a_Value)
|
bool cByteBuffer::ReadBEFloat(float & a_Value)
|
||||||
{
|
{
|
||||||
CHECK_THREAD;
|
CHECK_THREAD
|
||||||
CheckValid();
|
CheckValid();
|
||||||
NEEDBYTES(4);
|
NEEDBYTES(4);
|
||||||
ReadBuf(&a_Value, 4);
|
ReadBuf(&a_Value, 4);
|
||||||
@ -380,7 +387,7 @@ bool cByteBuffer::ReadBEFloat(float & a_Value)
|
|||||||
|
|
||||||
bool cByteBuffer::ReadBEDouble(double & a_Value)
|
bool cByteBuffer::ReadBEDouble(double & a_Value)
|
||||||
{
|
{
|
||||||
CHECK_THREAD;
|
CHECK_THREAD
|
||||||
CheckValid();
|
CheckValid();
|
||||||
NEEDBYTES(8);
|
NEEDBYTES(8);
|
||||||
ReadBuf(&a_Value, 8);
|
ReadBuf(&a_Value, 8);
|
||||||
@ -394,7 +401,7 @@ bool cByteBuffer::ReadBEDouble(double & a_Value)
|
|||||||
|
|
||||||
bool cByteBuffer::ReadBool(bool & a_Value)
|
bool cByteBuffer::ReadBool(bool & a_Value)
|
||||||
{
|
{
|
||||||
CHECK_THREAD;
|
CHECK_THREAD
|
||||||
CheckValid();
|
CheckValid();
|
||||||
NEEDBYTES(1);
|
NEEDBYTES(1);
|
||||||
char Value = 0;
|
char Value = 0;
|
||||||
@ -409,7 +416,7 @@ bool cByteBuffer::ReadBool(bool & a_Value)
|
|||||||
|
|
||||||
bool cByteBuffer::ReadBEUTF16String16(AString & a_Value)
|
bool cByteBuffer::ReadBEUTF16String16(AString & a_Value)
|
||||||
{
|
{
|
||||||
CHECK_THREAD;
|
CHECK_THREAD
|
||||||
CheckValid();
|
CheckValid();
|
||||||
short Length;
|
short Length;
|
||||||
if (!ReadBEShort(Length))
|
if (!ReadBEShort(Length))
|
||||||
@ -430,7 +437,7 @@ bool cByteBuffer::ReadBEUTF16String16(AString & a_Value)
|
|||||||
|
|
||||||
bool cByteBuffer::ReadVarInt(UInt32 & a_Value)
|
bool cByteBuffer::ReadVarInt(UInt32 & a_Value)
|
||||||
{
|
{
|
||||||
CHECK_THREAD;
|
CHECK_THREAD
|
||||||
CheckValid();
|
CheckValid();
|
||||||
UInt32 Value = 0;
|
UInt32 Value = 0;
|
||||||
int Shift = 0;
|
int Shift = 0;
|
||||||
@ -452,7 +459,7 @@ bool cByteBuffer::ReadVarInt(UInt32 & a_Value)
|
|||||||
|
|
||||||
bool cByteBuffer::ReadVarUTF8String(AString & a_Value)
|
bool cByteBuffer::ReadVarUTF8String(AString & a_Value)
|
||||||
{
|
{
|
||||||
CHECK_THREAD;
|
CHECK_THREAD
|
||||||
CheckValid();
|
CheckValid();
|
||||||
UInt32 Size = 0;
|
UInt32 Size = 0;
|
||||||
if (!ReadVarInt(Size))
|
if (!ReadVarInt(Size))
|
||||||
@ -472,7 +479,7 @@ bool cByteBuffer::ReadVarUTF8String(AString & a_Value)
|
|||||||
|
|
||||||
bool cByteBuffer::ReadLEInt(int & a_Value)
|
bool cByteBuffer::ReadLEInt(int & a_Value)
|
||||||
{
|
{
|
||||||
CHECK_THREAD;
|
CHECK_THREAD
|
||||||
CheckValid();
|
CheckValid();
|
||||||
NEEDBYTES(4);
|
NEEDBYTES(4);
|
||||||
ReadBuf(&a_Value, 4);
|
ReadBuf(&a_Value, 4);
|
||||||
@ -491,6 +498,7 @@ bool cByteBuffer::ReadLEInt(int & a_Value)
|
|||||||
|
|
||||||
bool cByteBuffer::ReadPosition(int & a_BlockX, int & a_BlockY, int & a_BlockZ)
|
bool cByteBuffer::ReadPosition(int & a_BlockX, int & a_BlockY, int & a_BlockZ)
|
||||||
{
|
{
|
||||||
|
CHECK_THREAD
|
||||||
Int64 Value;
|
Int64 Value;
|
||||||
if (!ReadBEInt64(Value))
|
if (!ReadBEInt64(Value))
|
||||||
{
|
{
|
||||||
@ -515,7 +523,7 @@ bool cByteBuffer::ReadPosition(int & a_BlockX, int & a_BlockY, int & a_BlockZ)
|
|||||||
|
|
||||||
bool cByteBuffer::WriteChar(char a_Value)
|
bool cByteBuffer::WriteChar(char a_Value)
|
||||||
{
|
{
|
||||||
CHECK_THREAD;
|
CHECK_THREAD
|
||||||
CheckValid();
|
CheckValid();
|
||||||
PUTBYTES(1);
|
PUTBYTES(1);
|
||||||
return WriteBuf(&a_Value, 1);
|
return WriteBuf(&a_Value, 1);
|
||||||
@ -527,7 +535,7 @@ bool cByteBuffer::WriteChar(char a_Value)
|
|||||||
|
|
||||||
bool cByteBuffer::WriteByte(unsigned char a_Value)
|
bool cByteBuffer::WriteByte(unsigned char a_Value)
|
||||||
{
|
{
|
||||||
CHECK_THREAD;
|
CHECK_THREAD
|
||||||
CheckValid();
|
CheckValid();
|
||||||
PUTBYTES(1);
|
PUTBYTES(1);
|
||||||
return WriteBuf(&a_Value, 1);
|
return WriteBuf(&a_Value, 1);
|
||||||
@ -539,7 +547,7 @@ bool cByteBuffer::WriteByte(unsigned char a_Value)
|
|||||||
|
|
||||||
bool cByteBuffer::WriteBEShort(short a_Value)
|
bool cByteBuffer::WriteBEShort(short a_Value)
|
||||||
{
|
{
|
||||||
CHECK_THREAD;
|
CHECK_THREAD
|
||||||
CheckValid();
|
CheckValid();
|
||||||
PUTBYTES(2);
|
PUTBYTES(2);
|
||||||
u_short Converted = htons((u_short)a_Value);
|
u_short Converted = htons((u_short)a_Value);
|
||||||
@ -552,7 +560,7 @@ bool cByteBuffer::WriteBEShort(short a_Value)
|
|||||||
|
|
||||||
bool cByteBuffer::WriteBEUShort(unsigned short a_Value)
|
bool cByteBuffer::WriteBEUShort(unsigned short a_Value)
|
||||||
{
|
{
|
||||||
CHECK_THREAD;
|
CHECK_THREAD
|
||||||
CheckValid();
|
CheckValid();
|
||||||
PUTBYTES(2);
|
PUTBYTES(2);
|
||||||
u_short Converted = htons((u_short)a_Value);
|
u_short Converted = htons((u_short)a_Value);
|
||||||
@ -565,7 +573,7 @@ bool cByteBuffer::WriteBEUShort(unsigned short a_Value)
|
|||||||
|
|
||||||
bool cByteBuffer::WriteBEInt(int a_Value)
|
bool cByteBuffer::WriteBEInt(int a_Value)
|
||||||
{
|
{
|
||||||
CHECK_THREAD;
|
CHECK_THREAD
|
||||||
CheckValid();
|
CheckValid();
|
||||||
PUTBYTES(4);
|
PUTBYTES(4);
|
||||||
UInt32 Converted = HostToNetwork4(&a_Value);
|
UInt32 Converted = HostToNetwork4(&a_Value);
|
||||||
@ -578,7 +586,7 @@ bool cByteBuffer::WriteBEInt(int a_Value)
|
|||||||
|
|
||||||
bool cByteBuffer::WriteBEInt64(Int64 a_Value)
|
bool cByteBuffer::WriteBEInt64(Int64 a_Value)
|
||||||
{
|
{
|
||||||
CHECK_THREAD;
|
CHECK_THREAD
|
||||||
CheckValid();
|
CheckValid();
|
||||||
PUTBYTES(8);
|
PUTBYTES(8);
|
||||||
UInt64 Converted = HostToNetwork8(&a_Value);
|
UInt64 Converted = HostToNetwork8(&a_Value);
|
||||||
@ -591,7 +599,7 @@ bool cByteBuffer::WriteBEInt64(Int64 a_Value)
|
|||||||
|
|
||||||
bool cByteBuffer::WriteBEFloat(float a_Value)
|
bool cByteBuffer::WriteBEFloat(float a_Value)
|
||||||
{
|
{
|
||||||
CHECK_THREAD;
|
CHECK_THREAD
|
||||||
CheckValid();
|
CheckValid();
|
||||||
PUTBYTES(4);
|
PUTBYTES(4);
|
||||||
UInt32 Converted = HostToNetwork4(&a_Value);
|
UInt32 Converted = HostToNetwork4(&a_Value);
|
||||||
@ -604,7 +612,7 @@ bool cByteBuffer::WriteBEFloat(float a_Value)
|
|||||||
|
|
||||||
bool cByteBuffer::WriteBEDouble(double a_Value)
|
bool cByteBuffer::WriteBEDouble(double a_Value)
|
||||||
{
|
{
|
||||||
CHECK_THREAD;
|
CHECK_THREAD
|
||||||
CheckValid();
|
CheckValid();
|
||||||
PUTBYTES(8);
|
PUTBYTES(8);
|
||||||
UInt64 Converted = HostToNetwork8(&a_Value);
|
UInt64 Converted = HostToNetwork8(&a_Value);
|
||||||
@ -618,7 +626,7 @@ bool cByteBuffer::WriteBEDouble(double a_Value)
|
|||||||
|
|
||||||
bool cByteBuffer::WriteBool(bool a_Value)
|
bool cByteBuffer::WriteBool(bool a_Value)
|
||||||
{
|
{
|
||||||
CHECK_THREAD;
|
CHECK_THREAD
|
||||||
CheckValid();
|
CheckValid();
|
||||||
return WriteChar(a_Value ? 1 : 0);
|
return WriteChar(a_Value ? 1 : 0);
|
||||||
}
|
}
|
||||||
@ -629,7 +637,7 @@ bool cByteBuffer::WriteBool(bool a_Value)
|
|||||||
|
|
||||||
bool cByteBuffer::WriteVarInt(UInt32 a_Value)
|
bool cByteBuffer::WriteVarInt(UInt32 a_Value)
|
||||||
{
|
{
|
||||||
CHECK_THREAD;
|
CHECK_THREAD
|
||||||
CheckValid();
|
CheckValid();
|
||||||
|
|
||||||
// A 32-bit integer can be encoded by at most 5 bytes:
|
// A 32-bit integer can be encoded by at most 5 bytes:
|
||||||
@ -650,7 +658,7 @@ bool cByteBuffer::WriteVarInt(UInt32 a_Value)
|
|||||||
|
|
||||||
bool cByteBuffer::WriteVarUTF8String(const AString & a_Value)
|
bool cByteBuffer::WriteVarUTF8String(const AString & a_Value)
|
||||||
{
|
{
|
||||||
CHECK_THREAD;
|
CHECK_THREAD
|
||||||
CheckValid();
|
CheckValid();
|
||||||
PUTBYTES(a_Value.size() + 1); // This is a lower-bound on the bytes that will be actually written. Fail early.
|
PUTBYTES(a_Value.size() + 1); // This is a lower-bound on the bytes that will be actually written. Fail early.
|
||||||
bool res = WriteVarInt((UInt32)(a_Value.size()));
|
bool res = WriteVarInt((UInt32)(a_Value.size()));
|
||||||
@ -667,7 +675,7 @@ bool cByteBuffer::WriteVarUTF8String(const AString & a_Value)
|
|||||||
|
|
||||||
bool cByteBuffer::WriteLEInt(int a_Value)
|
bool cByteBuffer::WriteLEInt(int a_Value)
|
||||||
{
|
{
|
||||||
CHECK_THREAD;
|
CHECK_THREAD
|
||||||
CheckValid();
|
CheckValid();
|
||||||
#ifdef IS_LITTLE_ENDIAN
|
#ifdef IS_LITTLE_ENDIAN
|
||||||
return WriteBuf((const char *)&a_Value, 4);
|
return WriteBuf((const char *)&a_Value, 4);
|
||||||
@ -683,6 +691,7 @@ bool cByteBuffer::WriteLEInt(int a_Value)
|
|||||||
|
|
||||||
bool cByteBuffer::WritePosition(int a_BlockX, int a_BlockY, int a_BlockZ)
|
bool cByteBuffer::WritePosition(int a_BlockX, int a_BlockY, int a_BlockZ)
|
||||||
{
|
{
|
||||||
|
CHECK_THREAD
|
||||||
return WriteBEInt64(((Int64)a_BlockX & 0x3FFFFFF) << 38 | ((Int64)a_BlockY & 0xFFF) << 26 | ((Int64)a_BlockZ & 0x3FFFFFF));
|
return WriteBEInt64(((Int64)a_BlockX & 0x3FFFFFF) << 38 | ((Int64)a_BlockY & 0xFFF) << 26 | ((Int64)a_BlockZ & 0x3FFFFFF));
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -692,7 +701,7 @@ bool cByteBuffer::WritePosition(int a_BlockX, int a_BlockY, int a_BlockZ)
|
|||||||
|
|
||||||
bool cByteBuffer::ReadBuf(void * a_Buffer, size_t a_Count)
|
bool cByteBuffer::ReadBuf(void * a_Buffer, size_t a_Count)
|
||||||
{
|
{
|
||||||
CHECK_THREAD;
|
CHECK_THREAD
|
||||||
CheckValid();
|
CheckValid();
|
||||||
NEEDBYTES(a_Count);
|
NEEDBYTES(a_Count);
|
||||||
char * Dst = (char *)a_Buffer; // So that we can do byte math
|
char * Dst = (char *)a_Buffer; // So that we can do byte math
|
||||||
@ -725,7 +734,7 @@ bool cByteBuffer::ReadBuf(void * a_Buffer, size_t a_Count)
|
|||||||
|
|
||||||
bool cByteBuffer::WriteBuf(const void * a_Buffer, size_t a_Count)
|
bool cByteBuffer::WriteBuf(const void * a_Buffer, size_t a_Count)
|
||||||
{
|
{
|
||||||
CHECK_THREAD;
|
CHECK_THREAD
|
||||||
CheckValid();
|
CheckValid();
|
||||||
PUTBYTES(a_Count);
|
PUTBYTES(a_Count);
|
||||||
char * Src = (char *)a_Buffer; // So that we can do byte math
|
char * Src = (char *)a_Buffer; // So that we can do byte math
|
||||||
@ -755,7 +764,7 @@ bool cByteBuffer::WriteBuf(const void * a_Buffer, size_t a_Count)
|
|||||||
|
|
||||||
bool cByteBuffer::ReadString(AString & a_String, size_t a_Count)
|
bool cByteBuffer::ReadString(AString & a_String, size_t a_Count)
|
||||||
{
|
{
|
||||||
CHECK_THREAD;
|
CHECK_THREAD
|
||||||
CheckValid();
|
CheckValid();
|
||||||
NEEDBYTES(a_Count);
|
NEEDBYTES(a_Count);
|
||||||
a_String.clear();
|
a_String.clear();
|
||||||
@ -790,7 +799,7 @@ bool cByteBuffer::ReadString(AString & a_String, size_t a_Count)
|
|||||||
bool cByteBuffer::ReadUTF16String(AString & a_String, size_t a_NumChars)
|
bool cByteBuffer::ReadUTF16String(AString & a_String, size_t a_NumChars)
|
||||||
{
|
{
|
||||||
// Reads 2 * a_NumChars bytes and interprets it as a UTF16 string, converting it into UTF8 string a_String
|
// Reads 2 * a_NumChars bytes and interprets it as a UTF16 string, converting it into UTF8 string a_String
|
||||||
CHECK_THREAD;
|
CHECK_THREAD
|
||||||
CheckValid();
|
CheckValid();
|
||||||
AString RawData;
|
AString RawData;
|
||||||
if (!ReadString(RawData, a_NumChars * 2))
|
if (!ReadString(RawData, a_NumChars * 2))
|
||||||
@ -807,7 +816,7 @@ bool cByteBuffer::ReadUTF16String(AString & a_String, size_t a_NumChars)
|
|||||||
|
|
||||||
bool cByteBuffer::SkipRead(size_t a_Count)
|
bool cByteBuffer::SkipRead(size_t a_Count)
|
||||||
{
|
{
|
||||||
CHECK_THREAD;
|
CHECK_THREAD
|
||||||
CheckValid();
|
CheckValid();
|
||||||
if (!CanReadBytes(a_Count))
|
if (!CanReadBytes(a_Count))
|
||||||
{
|
{
|
||||||
@ -823,7 +832,7 @@ bool cByteBuffer::SkipRead(size_t a_Count)
|
|||||||
|
|
||||||
void cByteBuffer::ReadAll(AString & a_Data)
|
void cByteBuffer::ReadAll(AString & a_Data)
|
||||||
{
|
{
|
||||||
CHECK_THREAD;
|
CHECK_THREAD
|
||||||
CheckValid();
|
CheckValid();
|
||||||
ReadString(a_Data, GetReadableSpace());
|
ReadString(a_Data, GetReadableSpace());
|
||||||
}
|
}
|
||||||
@ -834,6 +843,7 @@ void cByteBuffer::ReadAll(AString & a_Data)
|
|||||||
|
|
||||||
bool cByteBuffer::ReadToByteBuffer(cByteBuffer & a_Dst, size_t a_NumBytes)
|
bool cByteBuffer::ReadToByteBuffer(cByteBuffer & a_Dst, size_t a_NumBytes)
|
||||||
{
|
{
|
||||||
|
CHECK_THREAD
|
||||||
if (!a_Dst.CanWriteBytes(a_NumBytes) || !CanReadBytes(a_NumBytes))
|
if (!a_Dst.CanWriteBytes(a_NumBytes) || !CanReadBytes(a_NumBytes))
|
||||||
{
|
{
|
||||||
// There's not enough source bytes or space in the dest BB
|
// There's not enough source bytes or space in the dest BB
|
||||||
@ -858,7 +868,7 @@ bool cByteBuffer::ReadToByteBuffer(cByteBuffer & a_Dst, size_t a_NumBytes)
|
|||||||
|
|
||||||
void cByteBuffer::CommitRead(void)
|
void cByteBuffer::CommitRead(void)
|
||||||
{
|
{
|
||||||
CHECK_THREAD;
|
CHECK_THREAD
|
||||||
CheckValid();
|
CheckValid();
|
||||||
m_DataStart = m_ReadPos;
|
m_DataStart = m_ReadPos;
|
||||||
}
|
}
|
||||||
@ -869,7 +879,7 @@ void cByteBuffer::CommitRead(void)
|
|||||||
|
|
||||||
void cByteBuffer::ResetRead(void)
|
void cByteBuffer::ResetRead(void)
|
||||||
{
|
{
|
||||||
CHECK_THREAD;
|
CHECK_THREAD
|
||||||
CheckValid();
|
CheckValid();
|
||||||
m_ReadPos = m_DataStart;
|
m_ReadPos = m_DataStart;
|
||||||
}
|
}
|
||||||
@ -882,7 +892,7 @@ void cByteBuffer::ReadAgain(AString & a_Out)
|
|||||||
{
|
{
|
||||||
// Return the data between m_DataStart and m_ReadPos (the data that has been read but not committed)
|
// Return the data between m_DataStart and m_ReadPos (the data that has been read but not committed)
|
||||||
// Used by ProtoProxy to repeat communication twice, once for parsing and the other time for the remote party
|
// Used by ProtoProxy to repeat communication twice, once for parsing and the other time for the remote party
|
||||||
CHECK_THREAD;
|
CHECK_THREAD
|
||||||
CheckValid();
|
CheckValid();
|
||||||
size_t DataStart = m_DataStart;
|
size_t DataStart = m_DataStart;
|
||||||
if (m_ReadPos < m_DataStart)
|
if (m_ReadPos < m_DataStart)
|
||||||
@ -902,7 +912,7 @@ void cByteBuffer::ReadAgain(AString & a_Out)
|
|||||||
|
|
||||||
void cByteBuffer::AdvanceReadPos(size_t a_Count)
|
void cByteBuffer::AdvanceReadPos(size_t a_Count)
|
||||||
{
|
{
|
||||||
CHECK_THREAD;
|
CHECK_THREAD
|
||||||
CheckValid();
|
CheckValid();
|
||||||
m_ReadPos += a_Count;
|
m_ReadPos += a_Count;
|
||||||
if (m_ReadPos >= m_BufferSize)
|
if (m_ReadPos >= m_BufferSize)
|
||||||
|
@ -130,13 +130,15 @@ protected:
|
|||||||
char * m_Buffer;
|
char * m_Buffer;
|
||||||
size_t m_BufferSize; // Total size of the ringbuffer
|
size_t m_BufferSize; // Total size of the ringbuffer
|
||||||
|
|
||||||
#ifdef _DEBUG
|
|
||||||
volatile unsigned long m_ThreadID; // Thread that is currently accessing the object, checked via cSingleThreadAccessChecker
|
|
||||||
#endif // _DEBUG
|
|
||||||
|
|
||||||
size_t m_DataStart; // Where the data starts in the ringbuffer
|
size_t m_DataStart; // Where the data starts in the ringbuffer
|
||||||
size_t m_WritePos; // Where the data ends in the ringbuffer
|
size_t m_WritePos; // Where the data ends in the ringbuffer
|
||||||
size_t m_ReadPos; // Where the next read will start in the ringbuffer
|
size_t m_ReadPos; // Where the next read will start in the ringbuffer
|
||||||
|
|
||||||
|
#ifdef _DEBUG
|
||||||
|
/** The ID of the thread currently accessing the object.
|
||||||
|
Used for checking that only one thread accesses the object at a time, via cSingleThreadAccessChecker. */
|
||||||
|
mutable std::thread::id m_ThreadID;
|
||||||
|
#endif
|
||||||
|
|
||||||
/** Advances the m_ReadPos by a_Count bytes */
|
/** Advances the m_ReadPos by a_Count bytes */
|
||||||
void AdvanceReadPos(size_t a_Count);
|
void AdvanceReadPos(size_t a_Count);
|
||||||
|
@ -113,7 +113,6 @@ SET (HDRS
|
|||||||
Map.h
|
Map.h
|
||||||
MapManager.h
|
MapManager.h
|
||||||
Matrix4.h
|
Matrix4.h
|
||||||
MersenneTwister.h
|
|
||||||
MobCensus.h
|
MobCensus.h
|
||||||
MobFamilyCollecter.h
|
MobFamilyCollecter.h
|
||||||
MobProximityCounter.h
|
MobProximityCounter.h
|
||||||
|
@ -135,11 +135,11 @@ local g_ViolationPatterns =
|
|||||||
|
|
||||||
-- Space after keywords:
|
-- Space after keywords:
|
||||||
{"[^_]if%(", "Needs a space after \"if\""},
|
{"[^_]if%(", "Needs a space after \"if\""},
|
||||||
{"for%(", "Needs a space after \"for\""},
|
{"%sfor%(", "Needs a space after \"for\""},
|
||||||
{"while%(", "Needs a space after \"while\""},
|
{"%swhile%(", "Needs a space after \"while\""},
|
||||||
{"switch%(", "Needs a space after \"switch\""},
|
{"%sswitch%(", "Needs a space after \"switch\""},
|
||||||
{"catch%(", "Needs a space after \"catch\""},
|
{"%scatch%(", "Needs a space after \"catch\""},
|
||||||
{"template<", "Needs a space after \"template\""},
|
{"%stemplate<", "Needs a space after \"template\""},
|
||||||
|
|
||||||
-- No space after keyword's parenthesis:
|
-- No space after keyword's parenthesis:
|
||||||
{"[^%a#]if %( ", "Remove the space after \"(\""},
|
{"[^%a#]if %( ", "Remove the space after \"(\""},
|
||||||
|
@ -28,7 +28,6 @@
|
|||||||
#include "Item.h"
|
#include "Item.h"
|
||||||
#include "Noise/Noise.h"
|
#include "Noise/Noise.h"
|
||||||
#include "Root.h"
|
#include "Root.h"
|
||||||
#include "MersenneTwister.h"
|
|
||||||
#include "Entities/Player.h"
|
#include "Entities/Player.h"
|
||||||
#include "BlockArea.h"
|
#include "BlockArea.h"
|
||||||
#include "Bindings/PluginManager.h"
|
#include "Bindings/PluginManager.h"
|
||||||
|
@ -16,7 +16,6 @@
|
|||||||
#include "Mobs/Monster.h"
|
#include "Mobs/Monster.h"
|
||||||
#include "ChatColor.h"
|
#include "ChatColor.h"
|
||||||
#include "OSSupport/Socket.h"
|
#include "OSSupport/Socket.h"
|
||||||
#include "OSSupport/Timer.h"
|
|
||||||
#include "Items/ItemHandler.h"
|
#include "Items/ItemHandler.h"
|
||||||
#include "Blocks/BlockHandler.h"
|
#include "Blocks/BlockHandler.h"
|
||||||
#include "Blocks/BlockSlab.h"
|
#include "Blocks/BlockSlab.h"
|
||||||
@ -25,8 +24,6 @@
|
|||||||
#include "Root.h"
|
#include "Root.h"
|
||||||
|
|
||||||
#include "Protocol/Authenticator.h"
|
#include "Protocol/Authenticator.h"
|
||||||
#include "MersenneTwister.h"
|
|
||||||
|
|
||||||
#include "Protocol/ProtocolRecognizer.h"
|
#include "Protocol/ProtocolRecognizer.h"
|
||||||
#include "CompositeChat.h"
|
#include "CompositeChat.h"
|
||||||
#include "Items/ItemSword.h"
|
#include "Items/ItemSword.h"
|
||||||
@ -45,16 +42,6 @@
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
#define RECI_RAND_MAX (1.f/RAND_MAX)
|
|
||||||
inline int fRadRand(MTRand & r1, int a_BlockCoord)
|
|
||||||
{
|
|
||||||
return a_BlockCoord * 32 + (int)(16 * ((float)r1.rand() * RECI_RAND_MAX) * 16 - 8);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
int cClientHandle::s_ClientCount = 0;
|
int cClientHandle::s_ClientCount = 0;
|
||||||
|
|
||||||
|
|
||||||
@ -76,8 +63,6 @@ cClientHandle::cClientHandle(const cSocket * a_Socket, int a_ViewDistance) :
|
|||||||
m_TimeSinceLastPacket(0),
|
m_TimeSinceLastPacket(0),
|
||||||
m_Ping(1000),
|
m_Ping(1000),
|
||||||
m_PingID(1),
|
m_PingID(1),
|
||||||
m_PingStartTime(0),
|
|
||||||
m_LastPingTime(1000),
|
|
||||||
m_BlockDigAnimStage(-1),
|
m_BlockDigAnimStage(-1),
|
||||||
m_BlockDigAnimSpeed(0),
|
m_BlockDigAnimSpeed(0),
|
||||||
m_BlockDigAnimX(0),
|
m_BlockDigAnimX(0),
|
||||||
@ -101,9 +86,7 @@ cClientHandle::cClientHandle(const cSocket * a_Socket, int a_ViewDistance) :
|
|||||||
|
|
||||||
s_ClientCount++; // Not protected by CS because clients are always constructed from the same thread
|
s_ClientCount++; // Not protected by CS because clients are always constructed from the same thread
|
||||||
m_UniqueID = s_ClientCount;
|
m_UniqueID = s_ClientCount;
|
||||||
|
m_LastPingTime = std::chrono::steady_clock::now();
|
||||||
cTimer t1;
|
|
||||||
m_LastPingTime = t1.GetNowTime();
|
|
||||||
|
|
||||||
LOGD("New ClientHandle created at %p", this);
|
LOGD("New ClientHandle created at %p", this);
|
||||||
}
|
}
|
||||||
@ -401,8 +384,7 @@ void cClientHandle::Authenticate(const AString & a_Name, const AString & a_UUID,
|
|||||||
|
|
||||||
// Delay the first ping until the client "settles down"
|
// Delay the first ping until the client "settles down"
|
||||||
// This should fix #889, "BadCast exception, cannot convert bit to fm" error in client
|
// This should fix #889, "BadCast exception, cannot convert bit to fm" error in client
|
||||||
cTimer t1;
|
m_LastPingTime = std::chrono::steady_clock::now() + std::chrono::seconds(3); // Send the first KeepAlive packet in 3 seconds
|
||||||
m_LastPingTime = t1.GetNowTime() + 3000; // Send the first KeepAlive packet in 3 seconds
|
|
||||||
|
|
||||||
cRoot::Get()->GetPluginManager()->CallHookPlayerSpawned(*m_Player);
|
cRoot::Get()->GetPluginManager()->CallHookPlayerSpawned(*m_Player);
|
||||||
}
|
}
|
||||||
@ -1783,8 +1765,7 @@ void cClientHandle::HandleKeepAlive(int a_KeepAliveID)
|
|||||||
{
|
{
|
||||||
if (a_KeepAliveID == m_PingID)
|
if (a_KeepAliveID == m_PingID)
|
||||||
{
|
{
|
||||||
cTimer t1;
|
m_Ping = std::chrono::steady_clock::now() - m_PingStartTime;
|
||||||
m_Ping = (short)((t1.GetNowTime() - m_PingStartTime) / 2);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -2009,11 +1990,10 @@ void cClientHandle::Tick(float a_Dt)
|
|||||||
// Send a ping packet:
|
// Send a ping packet:
|
||||||
if (m_State == csPlaying)
|
if (m_State == csPlaying)
|
||||||
{
|
{
|
||||||
cTimer t1;
|
if ((m_LastPingTime + cClientHandle::PING_TIME_MS <= std::chrono::steady_clock::now()))
|
||||||
if ((m_LastPingTime + cClientHandle::PING_TIME_MS <= t1.GetNowTime()))
|
|
||||||
{
|
{
|
||||||
m_PingID++;
|
m_PingID++;
|
||||||
m_PingStartTime = t1.GetNowTime();
|
m_PingStartTime = std::chrono::steady_clock::now();
|
||||||
m_Protocol->SendKeepAlive(m_PingID);
|
m_Protocol->SendKeepAlive(m_PingID);
|
||||||
m_LastPingTime = m_PingStartTime;
|
m_LastPingTime = m_PingStartTime;
|
||||||
}
|
}
|
||||||
|
@ -215,7 +215,7 @@ public:
|
|||||||
const AString & GetUsername(void) const;
|
const AString & GetUsername(void) const;
|
||||||
void SetUsername( const AString & a_Username);
|
void SetUsername( const AString & a_Username);
|
||||||
|
|
||||||
inline short GetPing(void) const { return m_Ping; }
|
inline short GetPing(void) const { return static_cast<short>(std::chrono::duration_cast<std::chrono::milliseconds>(m_Ping).count()); }
|
||||||
|
|
||||||
/** Sets the maximal view distance. */
|
/** Sets the maximal view distance. */
|
||||||
void SetViewDistance(int a_ViewDistance);
|
void SetViewDistance(int a_ViewDistance);
|
||||||
@ -378,11 +378,11 @@ private:
|
|||||||
/** Seconds since the last packet data was received (updated in Tick(), reset in DataReceived()) */
|
/** Seconds since the last packet data was received (updated in Tick(), reset in DataReceived()) */
|
||||||
float m_TimeSinceLastPacket;
|
float m_TimeSinceLastPacket;
|
||||||
|
|
||||||
short m_Ping;
|
std::chrono::steady_clock::duration m_Ping;
|
||||||
int m_PingID;
|
int m_PingID;
|
||||||
long long m_PingStartTime;
|
std::chrono::steady_clock::time_point m_PingStartTime;
|
||||||
long long m_LastPingTime;
|
std::chrono::steady_clock::time_point m_LastPingTime;
|
||||||
static const unsigned short PING_TIME_MS = 1000; // Vanilla sends 1 per 20 ticks (1 second or every 1000 ms)
|
std::chrono::milliseconds PING_TIME_MS = std::chrono::milliseconds(1000); // Vanilla sends 1 per 20 ticks (1 second or every 1000 ms)
|
||||||
|
|
||||||
// Values required for block dig animation
|
// Values required for block dig animation
|
||||||
int m_BlockDigAnimStage; // Current stage of the animation; -1 if not digging
|
int m_BlockDigAnimStage; // Current stage of the animation; -1 if not digging
|
||||||
|
@ -13,7 +13,7 @@
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
/// Number of milliseconds per cycle
|
/** Number of milliseconds per cycle */
|
||||||
const int CYCLE_MILLISECONDS = 100;
|
const int CYCLE_MILLISECONDS = 100;
|
||||||
|
|
||||||
|
|
||||||
@ -87,7 +87,7 @@ void cDeadlockDetect::Execute(void)
|
|||||||
} Checker(this);
|
} Checker(this);
|
||||||
cRoot::Get()->ForEachWorld(Checker);
|
cRoot::Get()->ForEachWorld(Checker);
|
||||||
|
|
||||||
cSleep::MilliSleep(CYCLE_MILLISECONDS);
|
std::this_thread::sleep_for(std::chrono::milliseconds(CYCLE_MILLISECONDS));
|
||||||
} // while (should run)
|
} // while (should run)
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -119,7 +119,7 @@ void cDeadlockDetect::CheckWorldAge(const AString & a_WorldName, Int64 a_Age)
|
|||||||
if (WorldAge.m_Age == a_Age)
|
if (WorldAge.m_Age == a_Age)
|
||||||
{
|
{
|
||||||
WorldAge.m_NumCyclesSame += 1;
|
WorldAge.m_NumCyclesSame += 1;
|
||||||
if (WorldAge.m_NumCyclesSame > (1000 * m_IntervalSec) / CYCLE_MILLISECONDS)
|
if (WorldAge.m_NumCyclesSame > (m_IntervalSec * 1000) / CYCLE_MILLISECONDS)
|
||||||
{
|
{
|
||||||
DeadlockDetected();
|
DeadlockDetected();
|
||||||
}
|
}
|
||||||
|
@ -28,7 +28,7 @@ class cDeadlockDetect :
|
|||||||
public:
|
public:
|
||||||
cDeadlockDetect(void);
|
cDeadlockDetect(void);
|
||||||
|
|
||||||
/// Starts the detection. Hides cIsThread's Start, because we need some initialization
|
/** Starts the detection. Hides cIsThread's Start, because we need some initialization */
|
||||||
bool Start(int a_IntervalSec);
|
bool Start(int a_IntervalSec);
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
|
@ -11,7 +11,6 @@
|
|||||||
#include "../BlockEntities/BlockEntity.h"
|
#include "../BlockEntities/BlockEntity.h"
|
||||||
#include "../BlockEntities/EnderChestEntity.h"
|
#include "../BlockEntities/EnderChestEntity.h"
|
||||||
#include "../Root.h"
|
#include "../Root.h"
|
||||||
#include "../OSSupport/Timer.h"
|
|
||||||
#include "../Chunk.h"
|
#include "../Chunk.h"
|
||||||
#include "../Items/ItemHandler.h"
|
#include "../Items/ItemHandler.h"
|
||||||
#include "../Vector3.h"
|
#include "../Vector3.h"
|
||||||
@ -27,7 +26,7 @@
|
|||||||
#define PLAYER_INVENTORY_SAVE_INTERVAL 6000
|
#define PLAYER_INVENTORY_SAVE_INTERVAL 6000
|
||||||
|
|
||||||
// 1000 = once per second
|
// 1000 = once per second
|
||||||
#define PLAYER_LIST_TIME_MS 1000
|
#define PLAYER_LIST_TIME_MS std::chrono::milliseconds(1000)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@ -91,9 +90,7 @@ cPlayer::cPlayer(cClientHandle* a_Client, const AString & a_PlayerName) :
|
|||||||
SetMaxHealth(MAX_HEALTH);
|
SetMaxHealth(MAX_HEALTH);
|
||||||
m_Health = MAX_HEALTH;
|
m_Health = MAX_HEALTH;
|
||||||
|
|
||||||
cTimer t1;
|
m_LastPlayerListTime = std::chrono::steady_clock::now();
|
||||||
m_LastPlayerListTime = t1.GetNowTime();
|
|
||||||
|
|
||||||
m_PlayerName = a_PlayerName;
|
m_PlayerName = a_PlayerName;
|
||||||
|
|
||||||
cWorld * World = nullptr;
|
cWorld * World = nullptr;
|
||||||
@ -268,11 +265,10 @@ void cPlayer::Tick(float a_Dt, cChunk & a_Chunk)
|
|||||||
m_Inventory.UpdateItems();
|
m_Inventory.UpdateItems();
|
||||||
|
|
||||||
// Send Player List (Once per m_LastPlayerListTime/1000 ms)
|
// Send Player List (Once per m_LastPlayerListTime/1000 ms)
|
||||||
cTimer t1;
|
if (m_LastPlayerListTime + PLAYER_LIST_TIME_MS <= std::chrono::steady_clock::now())
|
||||||
if (m_LastPlayerListTime + PLAYER_LIST_TIME_MS <= t1.GetNowTime())
|
|
||||||
{
|
{
|
||||||
m_World->BroadcastPlayerListUpdatePing(*this);
|
m_World->BroadcastPlayerListUpdatePing(*this);
|
||||||
m_LastPlayerListTime = t1.GetNowTime();
|
m_LastPlayerListTime = std::chrono::steady_clock::now();
|
||||||
}
|
}
|
||||||
|
|
||||||
if (IsFlying())
|
if (IsFlying())
|
||||||
@ -363,7 +359,7 @@ float cPlayer::GetXpPercentage()
|
|||||||
|
|
||||||
bool cPlayer::SetCurrentExperience(short int a_CurrentXp)
|
bool cPlayer::SetCurrentExperience(short int a_CurrentXp)
|
||||||
{
|
{
|
||||||
if (!(a_CurrentXp >= 0) || (a_CurrentXp > (SHRT_MAX - m_LifetimeTotalXp)))
|
if (!(a_CurrentXp >= 0) || (a_CurrentXp > (std::numeric_limits<short>().max() - m_LifetimeTotalXp)))
|
||||||
{
|
{
|
||||||
LOGWARNING("Tried to update experiece with an invalid Xp value: %d", a_CurrentXp);
|
LOGWARNING("Tried to update experiece with an invalid Xp value: %d", a_CurrentXp);
|
||||||
return false; // oops, they gave us a dodgey number
|
return false; // oops, they gave us a dodgey number
|
||||||
@ -383,18 +379,17 @@ bool cPlayer::SetCurrentExperience(short int a_CurrentXp)
|
|||||||
|
|
||||||
short cPlayer::DeltaExperience(short a_Xp_delta)
|
short cPlayer::DeltaExperience(short a_Xp_delta)
|
||||||
{
|
{
|
||||||
if (a_Xp_delta > (SHRT_MAX - m_CurrentXp))
|
if (a_Xp_delta > (std::numeric_limits<short>().max() - m_CurrentXp))
|
||||||
{
|
{
|
||||||
// Value was bad, abort and report
|
// Value was bad, abort and report
|
||||||
LOGWARNING("Attempt was made to increment Xp by %d, which overflowed the short datatype. Ignoring.",
|
LOGWARNING("Attempt was made to increment Xp by %d, which overflowed the short datatype. Ignoring.", a_Xp_delta);
|
||||||
a_Xp_delta);
|
|
||||||
return -1; // Should we instead just return the current Xp?
|
return -1; // Should we instead just return the current Xp?
|
||||||
}
|
}
|
||||||
|
|
||||||
m_CurrentXp += a_Xp_delta;
|
m_CurrentXp += a_Xp_delta;
|
||||||
|
|
||||||
// Make sure they didn't subtract too much
|
// Make sure they didn't subtract too much
|
||||||
m_CurrentXp = std::max<short int>(m_CurrentXp, 0);
|
m_CurrentXp = std::max<short>(m_CurrentXp, 0);
|
||||||
|
|
||||||
// Update total for score calculation
|
// Update total for score calculation
|
||||||
if (a_Xp_delta > 0)
|
if (a_Xp_delta > 0)
|
||||||
@ -402,8 +397,7 @@ short cPlayer::DeltaExperience(short a_Xp_delta)
|
|||||||
m_LifetimeTotalXp += a_Xp_delta;
|
m_LifetimeTotalXp += a_Xp_delta;
|
||||||
}
|
}
|
||||||
|
|
||||||
LOGD("Player \"%s\" gained/lost %d experience, total is now: %d",
|
LOGD("Player \"%s\" gained/lost %d experience, total is now: %d", GetName().c_str(), a_Xp_delta, m_CurrentXp);
|
||||||
GetName().c_str(), a_Xp_delta, m_CurrentXp);
|
|
||||||
|
|
||||||
// Set experience to be updated
|
// Set experience to be updated
|
||||||
m_bDirtyExperience = true;
|
m_bDirtyExperience = true;
|
||||||
|
@ -516,7 +516,7 @@ protected:
|
|||||||
/** The item being dragged by the cursor while in a UI window */
|
/** The item being dragged by the cursor while in a UI window */
|
||||||
cItem m_DraggingItem;
|
cItem m_DraggingItem;
|
||||||
|
|
||||||
long long m_LastPlayerListTime;
|
std::chrono::steady_clock::time_point m_LastPlayerListTime;
|
||||||
|
|
||||||
cClientHandle * m_ClientHandle;
|
cClientHandle * m_ClientHandle;
|
||||||
|
|
||||||
|
@ -4,13 +4,15 @@
|
|||||||
// Implements the cFastRandom class representing a fast random number generator
|
// Implements the cFastRandom class representing a fast random number generator
|
||||||
|
|
||||||
#include "Globals.h"
|
#include "Globals.h"
|
||||||
#include <time.h>
|
|
||||||
#include "FastRandom.h"
|
#include "FastRandom.h"
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
////////////////////////////////////////////////////////////////////////////////
|
||||||
|
// cFastRandom:
|
||||||
|
|
||||||
#if 0 && defined(_DEBUG)
|
#if 0 && defined(_DEBUG)
|
||||||
// Self-test
|
// Self-test
|
||||||
// Both ints and floats are quick-tested to see if the random is calculated correctly, checking the range in ASSERTs,
|
// Both ints and floats are quick-tested to see if the random is calculated correctly, checking the range in ASSERTs,
|
||||||
@ -83,16 +85,8 @@ public:
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
int cFastRandom::m_SeedCounter = 0;
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
cFastRandom::cFastRandom(void) :
|
cFastRandom::cFastRandom(void) :
|
||||||
m_Seed(m_SeedCounter++),
|
m_LinearRand(static_cast<unsigned>(std::chrono::system_clock::now().time_since_epoch().count()))
|
||||||
m_Counter(0)
|
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -102,82 +96,96 @@ cFastRandom::cFastRandom(void) :
|
|||||||
|
|
||||||
int cFastRandom::NextInt(int a_Range)
|
int cFastRandom::NextInt(int a_Range)
|
||||||
{
|
{
|
||||||
ASSERT(a_Range <= 1000000); // The random is not sufficiently linearly distributed with bigger ranges
|
m_IntDistribution = std::uniform_int_distribution<>(0, a_Range - 1);
|
||||||
ASSERT(a_Range > 0);
|
return m_IntDistribution(m_LinearRand);
|
||||||
|
|
||||||
// Make the m_Counter operations as minimal as possible, to emulate atomicity
|
|
||||||
int Counter = m_Counter++;
|
|
||||||
|
|
||||||
// Use a_Range, m_Counter and m_Seed as inputs to the pseudorandom function:
|
|
||||||
int n = a_Range + Counter * 57 + m_Seed * 57 * 57;
|
|
||||||
n = (n << 13) ^ n;
|
|
||||||
n = ((n * (n * n * 15731 + 789221) + 1376312589) & 0x7fffffff);
|
|
||||||
return ((n / 11) % a_Range);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
int cFastRandom::NextInt(int a_Range, int a_Salt)
|
int cFastRandom::NextInt(int a_Range, int a_Salt)
|
||||||
{
|
{
|
||||||
ASSERT(a_Range <= 1000000); // The random is not sufficiently linearly distributed with bigger ranges
|
m_LinearRand.seed(a_Salt);
|
||||||
ASSERT(a_Range > 0);
|
m_IntDistribution = std::uniform_int_distribution<>(0, a_Range - 1);
|
||||||
|
return m_IntDistribution(m_LinearRand);
|
||||||
// Make the m_Counter operations as minimal as possible, to emulate atomicity
|
|
||||||
int Counter = m_Counter++;
|
|
||||||
|
|
||||||
// Use a_Range, a_Salt, m_Counter and m_Seed as inputs to the pseudorandom function:
|
|
||||||
int n = a_Range + Counter * 57 + m_Seed * 57 * 57 + a_Salt * 57 * 57 * 57;
|
|
||||||
n = (n << 13) ^ n;
|
|
||||||
n = ((n * (n * n * 15731 + 789221) + 1376312589) & 0x7fffffff);
|
|
||||||
return ((n / 11) % a_Range);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
float cFastRandom::NextFloat(float a_Range)
|
float cFastRandom::NextFloat(float a_Range)
|
||||||
{
|
{
|
||||||
// Make the m_Counter operations as minimal as possible, to emulate atomicity
|
m_FloatDistribution = std::uniform_real_distribution<float>(0, a_Range - 1);
|
||||||
int Counter = m_Counter++;
|
return m_FloatDistribution(m_LinearRand);
|
||||||
|
|
||||||
// Use a_Range, a_Salt, m_Counter and m_Seed as inputs to the pseudorandom function:
|
|
||||||
int n = (int)a_Range + Counter * 57 + m_Seed * 57 * 57;
|
|
||||||
n = (n << 13) ^ n;
|
|
||||||
n = ((n * (n * n * 15731 + 789221) + 1376312589) & 0x7fffffff);
|
|
||||||
|
|
||||||
// Convert the integer into float with the specified range:
|
|
||||||
return (((float)n / (float)0x7fffffff) * a_Range);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
float cFastRandom::NextFloat(float a_Range, int a_Salt)
|
float cFastRandom::NextFloat(float a_Range, int a_Salt)
|
||||||
{
|
{
|
||||||
// Make the m_Counter operations as minimal as possible, to emulate atomicity
|
m_LinearRand.seed(a_Salt);
|
||||||
int Counter = m_Counter++;
|
m_FloatDistribution = std::uniform_real_distribution<float>(0, a_Range - 1);
|
||||||
|
return m_FloatDistribution(m_LinearRand);
|
||||||
// Use a_Range, a_Salt, m_Counter and m_Seed as inputs to the pseudorandom function:
|
|
||||||
int n = (int)a_Range + Counter * 57 + m_Seed * 57 * 57 + a_Salt * 57 * 57 * 57;
|
|
||||||
n = (n << 13) ^ n;
|
|
||||||
n = ((n * (n * n * 15731 + 789221) + 1376312589) & 0x7fffffff);
|
|
||||||
|
|
||||||
// Convert the integer into float with the specified range:
|
|
||||||
return (((float)n / (float)0x7fffffff) * a_Range);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
int cFastRandom::GenerateRandomInteger(int a_Begin, int a_End)
|
int cFastRandom::GenerateRandomInteger(int a_Begin, int a_End)
|
||||||
{
|
{
|
||||||
cFastRandom Random;
|
m_IntDistribution = std::uniform_int_distribution<>(a_Begin, a_End - 1);
|
||||||
return Random.NextInt(a_End - a_Begin + 1) + a_Begin;
|
return m_IntDistribution(m_LinearRand);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
////////////////////////////////////////////////////////////////////////////////
|
||||||
|
// MTRand:
|
||||||
|
|
||||||
|
MTRand::MTRand() :
|
||||||
|
m_MersenneRand(static_cast<unsigned>(std::chrono::system_clock::now().time_since_epoch().count()))
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
int MTRand::randInt(int a_Range)
|
||||||
|
{
|
||||||
|
m_IntDistribution = std::uniform_int_distribution<>(0, a_Range);
|
||||||
|
return m_IntDistribution(m_MersenneRand);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
int MTRand::randInt()
|
||||||
|
{
|
||||||
|
m_IntDistribution = std::uniform_int_distribution<>(0, std::numeric_limits<int>::max());
|
||||||
|
return m_IntDistribution(m_MersenneRand);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
double MTRand::rand(double a_Range)
|
||||||
|
{
|
||||||
|
m_DoubleDistribution = std::uniform_real_distribution<>(0, a_Range);
|
||||||
|
return m_DoubleDistribution(m_MersenneRand);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -22,6 +22,7 @@ salts, the values they get will be different.
|
|||||||
|
|
||||||
|
|
||||||
#pragma once
|
#pragma once
|
||||||
|
#include <random>
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@ -30,6 +31,7 @@ salts, the values they get will be different.
|
|||||||
class cFastRandom
|
class cFastRandom
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
|
|
||||||
cFastRandom(void);
|
cFastRandom(void);
|
||||||
|
|
||||||
/// Returns a random int in the range [0 .. a_Range - 1]; a_Range must be less than 1M
|
/// Returns a random int in the range [0 .. a_Range - 1]; a_Range must be less than 1M
|
||||||
@ -49,15 +51,33 @@ public:
|
|||||||
|
|
||||||
/** Returns a random int in the range [a_Begin .. a_End] */
|
/** Returns a random int in the range [a_Begin .. a_End] */
|
||||||
int GenerateRandomInteger(int a_Begin, int a_End);
|
int GenerateRandomInteger(int a_Begin, int a_End);
|
||||||
|
|
||||||
protected:
|
private:
|
||||||
int m_Seed;
|
|
||||||
int m_Counter;
|
std::minstd_rand m_LinearRand;
|
||||||
|
std::uniform_int_distribution<> m_IntDistribution;
|
||||||
/// Counter that is used to initialize the seed, incremented for each object created
|
std::uniform_real_distribution<float> m_FloatDistribution;
|
||||||
static int m_SeedCounter;
|
};
|
||||||
} ;
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
class MTRand
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
|
||||||
|
MTRand(void);
|
||||||
|
|
||||||
|
int randInt(int a_Range);
|
||||||
|
|
||||||
|
int randInt(void);
|
||||||
|
|
||||||
|
double rand(double a_Range);
|
||||||
|
|
||||||
|
private:
|
||||||
|
|
||||||
|
std::mt19937 m_MersenneRand;
|
||||||
|
std::uniform_int_distribution<> m_IntDistribution;
|
||||||
|
std::uniform_real_distribution<> m_DoubleDistribution;
|
||||||
|
};
|
||||||
|
@ -6,7 +6,7 @@
|
|||||||
#include "ChunkDesc.h"
|
#include "ChunkDesc.h"
|
||||||
#include "ComposableGenerator.h"
|
#include "ComposableGenerator.h"
|
||||||
#include "Noise3DGenerator.h"
|
#include "Noise3DGenerator.h"
|
||||||
#include "../MersenneTwister.h"
|
#include "FastRandom.h"
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
@ -6,7 +6,6 @@
|
|||||||
#include "Globals.h"
|
#include "Globals.h"
|
||||||
#include "Noise3DGenerator.h"
|
#include "Noise3DGenerator.h"
|
||||||
#include "../OSSupport/File.h"
|
#include "../OSSupport/File.h"
|
||||||
#include "../OSSupport/Timer.h"
|
|
||||||
#include "../IniFile.h"
|
#include "../IniFile.h"
|
||||||
#include "../LinearInterpolation.h"
|
#include "../LinearInterpolation.h"
|
||||||
#include "../LinearUpscale.h"
|
#include "../LinearUpscale.h"
|
||||||
|
@ -184,9 +184,9 @@ template class SizeChecker<UInt16, 2>;
|
|||||||
|
|
||||||
// OS-dependent stuff:
|
// OS-dependent stuff:
|
||||||
#ifdef _WIN32
|
#ifdef _WIN32
|
||||||
#define WIN32_LEAN_AND_MEAN
|
|
||||||
|
|
||||||
#define _WIN32_WINNT 0x501 // We want to target WinXP and higher
|
#define WIN32_LEAN_AND_MEAN
|
||||||
|
#define _WIN32_WINNT _WIN32_WINNT_WS03 // We want to target Windows XP with Service Pack 2 & Windows Server 2003 with Service Pack 1 and higher
|
||||||
|
|
||||||
#include <Windows.h>
|
#include <Windows.h>
|
||||||
#include <winsock2.h>
|
#include <winsock2.h>
|
||||||
@ -241,6 +241,7 @@ template class SizeChecker<UInt16, 2>;
|
|||||||
|
|
||||||
|
|
||||||
// STL stuff:
|
// STL stuff:
|
||||||
|
#include <chrono>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
#include <list>
|
#include <list>
|
||||||
#include <deque>
|
#include <deque>
|
||||||
@ -257,11 +258,9 @@ template class SizeChecker<UInt16, 2>;
|
|||||||
#ifndef TEST_GLOBALS
|
#ifndef TEST_GLOBALS
|
||||||
// Common headers (part 1, without macros):
|
// Common headers (part 1, without macros):
|
||||||
#include "StringUtils.h"
|
#include "StringUtils.h"
|
||||||
#include "OSSupport/Sleep.h"
|
|
||||||
#include "OSSupport/CriticalSection.h"
|
#include "OSSupport/CriticalSection.h"
|
||||||
#include "OSSupport/Semaphore.h"
|
#include "OSSupport/Semaphore.h"
|
||||||
#include "OSSupport/Event.h"
|
#include "OSSupport/Event.h"
|
||||||
#include "OSSupport/Thread.h"
|
|
||||||
#include "OSSupport/File.h"
|
#include "OSSupport/File.h"
|
||||||
#include "Logger.h"
|
#include "Logger.h"
|
||||||
#include "OSSupport/StackTrace.h"
|
#include "OSSupport/StackTrace.h"
|
||||||
|
@ -45,7 +45,7 @@ void cLogger::LogSimple(AString a_Message, eLogLevel a_LogLevel)
|
|||||||
|
|
||||||
AString Line;
|
AString Line;
|
||||||
#ifdef _DEBUG
|
#ifdef _DEBUG
|
||||||
Printf(Line, "[%04lx|%02d:%02d:%02d] %s\n", cIsThread::GetCurrentID(), timeinfo->tm_hour, timeinfo->tm_min, timeinfo->tm_sec, a_Message.c_str());
|
Printf(Line, "[%04lx|%02d:%02d:%02d] %s\n", std::hash<std::thread::id>()(std::this_thread::get_id()), timeinfo->tm_hour, timeinfo->tm_min, timeinfo->tm_sec, a_Message.c_str());
|
||||||
#else
|
#else
|
||||||
Printf(Line, "[%02d:%02d:%02d] %s\n", timeinfo->tm_hour, timeinfo->tm_min, timeinfo->tm_sec, a_Message.c_str());
|
Printf(Line, "[%02d:%02d:%02d] %s\n", timeinfo->tm_hour, timeinfo->tm_min, timeinfo->tm_sec, a_Message.c_str());
|
||||||
#endif
|
#endif
|
||||||
|
@ -1,456 +0,0 @@
|
|||||||
// MersenneTwister.h
|
|
||||||
// Mersenne Twister random number generator -- a C++ class MTRand
|
|
||||||
// Based on code by Makoto Matsumoto, Takuji Nishimura, and Shawn Cokus
|
|
||||||
// Richard J. Wagner v1.1 28 September 2009 wagnerr@umich.edu
|
|
||||||
|
|
||||||
// The Mersenne Twister is an algorithm for generating random numbers. It
|
|
||||||
// was designed with consideration of the flaws in various other generators.
|
|
||||||
// The period, 2^19937-1, and the order of equidistribution, 623 dimensions,
|
|
||||||
// are far greater. The generator is also fast; it avoids multiplication and
|
|
||||||
// division, and it benefits from caches and pipelines. For more information
|
|
||||||
// see the inventors' web page at
|
|
||||||
// http://www.math.sci.hiroshima-u.ac.jp/~m-mat/MT/emt.html
|
|
||||||
|
|
||||||
// Reference
|
|
||||||
// M. Matsumoto and T. Nishimura, "Mersenne Twister: A 623-Dimensionally
|
|
||||||
// Equidistributed Uniform Pseudo-Random Number Generator", ACM Transactions on
|
|
||||||
// Modeling and Computer Simulation, Vol. 8, No. 1, January 1998, pp 3-30.
|
|
||||||
|
|
||||||
// Copyright (C) 1997 - 2002, Makoto Matsumoto and Takuji Nishimura,
|
|
||||||
// Copyright (C) 2000 - 2009, Richard J. Wagner
|
|
||||||
// All rights reserved.
|
|
||||||
//
|
|
||||||
// Redistribution and use in source and binary forms, with or without
|
|
||||||
// modification, are permitted provided that the following conditions
|
|
||||||
// are met:
|
|
||||||
//
|
|
||||||
// 1. Redistributions of source code must retain the above copyright
|
|
||||||
// notice, this list of conditions and the following disclaimer.
|
|
||||||
//
|
|
||||||
// 2. Redistributions in binary form must reproduce the above copyright
|
|
||||||
// notice, this list of conditions and the following disclaimer in the
|
|
||||||
// documentation and/or other materials provided with the distribution.
|
|
||||||
//
|
|
||||||
// 3. The names of its contributors may not be used to endorse or promote
|
|
||||||
// products derived from this software without specific prior written
|
|
||||||
// permission.
|
|
||||||
//
|
|
||||||
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
||||||
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
||||||
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
||||||
// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
|
|
||||||
// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
|
||||||
// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
|
||||||
// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
||||||
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
|
||||||
// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
|
||||||
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
||||||
// POSSIBILITY OF SUCH DAMAGE.
|
|
||||||
|
|
||||||
#ifndef MERSENNETWISTER_H
|
|
||||||
#define MERSENNETWISTER_H
|
|
||||||
|
|
||||||
// Not thread safe (unless auto-initialization is avoided and each thread has
|
|
||||||
// its own MTRand object)
|
|
||||||
|
|
||||||
#include <iostream>
|
|
||||||
#include <climits>
|
|
||||||
#include <cstdio>
|
|
||||||
#include <ctime>
|
|
||||||
#include <cmath>
|
|
||||||
|
|
||||||
class MTRand {
|
|
||||||
// Data
|
|
||||||
public:
|
|
||||||
typedef UInt32 uint32; // unsigned integer type, at least 32 bits
|
|
||||||
|
|
||||||
enum { N = 624 }; // length of state vector
|
|
||||||
enum { SAVE = N + 1 }; // length of array for save()
|
|
||||||
|
|
||||||
protected:
|
|
||||||
enum { M = 397 }; // period parameter
|
|
||||||
|
|
||||||
uint32 state[N]; // internal state
|
|
||||||
uint32 *pNext; // next value to get from state
|
|
||||||
uint32 left; // number of values left before reload needed
|
|
||||||
|
|
||||||
// Methods
|
|
||||||
public:
|
|
||||||
MTRand( const uint32 oneSeed ); // initialize with a simple uint32
|
|
||||||
MTRand( uint32 *const bigSeed, uint32 const seedLength = N ); // or array
|
|
||||||
MTRand(); // auto-initialize with /dev/urandom or time() and clock()
|
|
||||||
MTRand( const MTRand& o ); // copy
|
|
||||||
|
|
||||||
// Do NOT use for CRYPTOGRAPHY without securely hashing several returned
|
|
||||||
// values together, otherwise the generator state can be learned after
|
|
||||||
// reading 624 consecutive values.
|
|
||||||
|
|
||||||
// Access to 32-bit random numbers
|
|
||||||
uint32 randInt(); // integer in [0,2^32-1]
|
|
||||||
uint32 randInt( const uint32 n ); // integer in [0,n] for n < 2^32
|
|
||||||
double rand(); // real number in [0,1]
|
|
||||||
double rand( const double n ); // real number in [0,n]
|
|
||||||
double randExc(); // real number in [0,1)
|
|
||||||
double randExc( const double n ); // real number in [0,n)
|
|
||||||
double randDblExc(); // real number in (0,1)
|
|
||||||
double randDblExc( const double n ); // real number in (0,n)
|
|
||||||
double operator()(); // same as rand()
|
|
||||||
|
|
||||||
// Access to 53-bit random numbers (capacity of IEEE double precision)
|
|
||||||
double rand53(); // real number in [0,1)
|
|
||||||
|
|
||||||
// Access to nonuniform random number distributions
|
|
||||||
double randNorm( const double mean = 0.0, const double stddev = 1.0 );
|
|
||||||
|
|
||||||
// Re-seeding functions with same behavior as initializers
|
|
||||||
void seed( const uint32 oneSeed );
|
|
||||||
void seed( uint32 *const bigSeed, const uint32 seedLength = N );
|
|
||||||
void seed();
|
|
||||||
|
|
||||||
// Saving and loading generator state
|
|
||||||
void save( uint32* saveArray ) const; // to array of size SAVE
|
|
||||||
void load( uint32 *const loadArray ); // from such array
|
|
||||||
friend std::ostream& operator<<( std::ostream& os, const MTRand& mtrand );
|
|
||||||
friend std::istream& operator>>( std::istream& is, MTRand& mtrand );
|
|
||||||
MTRand& operator=( const MTRand& o );
|
|
||||||
|
|
||||||
protected:
|
|
||||||
void initialize( const uint32 oneSeed );
|
|
||||||
void reload();
|
|
||||||
uint32 hiBit( const uint32 u ) const { return u & 0x80000000UL; }
|
|
||||||
uint32 loBit( const uint32 u ) const { return u & 0x00000001UL; }
|
|
||||||
uint32 loBits( const uint32 u ) const { return u & 0x7fffffffUL; }
|
|
||||||
uint32 mixBits( const uint32 u, const uint32 v ) const
|
|
||||||
{ return hiBit(u) | loBits(v); }
|
|
||||||
uint32 magic( const uint32 u ) const
|
|
||||||
{ return loBit(u) ? 0x9908b0dfUL : 0x0UL; }
|
|
||||||
uint32 twist( const uint32 m, const uint32 s0, const uint32 s1 ) const
|
|
||||||
{ return m ^ (mixBits(s0,s1)>>1) ^ magic(s1); }
|
|
||||||
static uint32 hash( time_t t, clock_t c );
|
|
||||||
};
|
|
||||||
|
|
||||||
// Functions are defined in order of usage to assist inlining
|
|
||||||
|
|
||||||
inline MTRand::uint32 MTRand::hash( time_t t, clock_t c )
|
|
||||||
{
|
|
||||||
// Get a uint32 from t and c
|
|
||||||
// Better than uint32(x) in case x is floating point in [0,1]
|
|
||||||
// Based on code by Lawrence Kirby (fred@genesis.demon.co.uk)
|
|
||||||
|
|
||||||
static uint32 differ = 0; // guarantee time-based seeds will change
|
|
||||||
|
|
||||||
uint32 h1 = 0;
|
|
||||||
unsigned char *p = (unsigned char *) &t;
|
|
||||||
for( size_t i = 0; i < sizeof(t); ++i )
|
|
||||||
{
|
|
||||||
h1 *= UCHAR_MAX + 2U;
|
|
||||||
h1 += p[i];
|
|
||||||
}
|
|
||||||
uint32 h2 = 0;
|
|
||||||
p = (unsigned char *) &c;
|
|
||||||
for( size_t j = 0; j < sizeof(c); ++j )
|
|
||||||
{
|
|
||||||
h2 *= UCHAR_MAX + 2U;
|
|
||||||
h2 += p[j];
|
|
||||||
}
|
|
||||||
return ( h1 + differ++ ) ^ h2;
|
|
||||||
}
|
|
||||||
|
|
||||||
inline void MTRand::initialize( const uint32 seed )
|
|
||||||
{
|
|
||||||
// Initialize generator state with seed
|
|
||||||
// See Knuth TAOCP Vol 2, 3rd Ed, p.106 for multiplier.
|
|
||||||
// In previous versions, most significant bits (MSBs) of the seed affect
|
|
||||||
// only MSBs of the state array. Modified 9 Jan 2002 by Makoto Matsumoto.
|
|
||||||
uint32 *s = state;
|
|
||||||
uint32 *r = state;
|
|
||||||
uint32 i = 1;
|
|
||||||
*s++ = seed & 0xffffffffUL;
|
|
||||||
for( ; i < N; ++i )
|
|
||||||
{
|
|
||||||
*s++ = ( 1812433253UL * ( *r ^ (*r >> 30) ) + i ) & 0xffffffffUL;
|
|
||||||
r++;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
inline void MTRand::reload()
|
|
||||||
{
|
|
||||||
// Generate N new values in state
|
|
||||||
// Made clearer and faster by Matthew Bellew (matthew.bellew@home.com)
|
|
||||||
static const int MmN = int(M) - int(N); // in case enums are unsigned
|
|
||||||
uint32 *p = state;
|
|
||||||
int i;
|
|
||||||
for( i = N - M; i--; ++p )
|
|
||||||
*p = twist( p[M], p[0], p[1] );
|
|
||||||
for( i = M; --i; ++p )
|
|
||||||
*p = twist( p[MmN], p[0], p[1] );
|
|
||||||
*p = twist( p[MmN], p[0], state[0] );
|
|
||||||
|
|
||||||
left = N, pNext = state;
|
|
||||||
}
|
|
||||||
|
|
||||||
inline void MTRand::seed( const uint32 oneSeed )
|
|
||||||
{
|
|
||||||
// Seed the generator with a simple uint32
|
|
||||||
initialize(oneSeed);
|
|
||||||
reload();
|
|
||||||
}
|
|
||||||
|
|
||||||
inline void MTRand::seed( uint32 *const bigSeed, const uint32 seedLength )
|
|
||||||
{
|
|
||||||
// Seed the generator with an array of uint32's
|
|
||||||
// There are 2^19937-1 possible initial states. This function allows
|
|
||||||
// all of those to be accessed by providing at least 19937 bits (with a
|
|
||||||
// default seed length of N = 624 uint32's). Any bits above the lower 32
|
|
||||||
// in each element are discarded.
|
|
||||||
// Just call seed() if you want to get array from /dev/urandom
|
|
||||||
initialize(19650218UL);
|
|
||||||
uint32 i = 1;
|
|
||||||
uint32 j = 0;
|
|
||||||
uint32 k = ( static_cast<uint32>(N) > seedLength ? static_cast<uint32>(N) : seedLength );
|
|
||||||
for( ; k; --k )
|
|
||||||
{
|
|
||||||
state[i] =
|
|
||||||
state[i] ^ ( (state[i-1] ^ (state[i-1] >> 30)) * 1664525UL );
|
|
||||||
state[i] += ( bigSeed[j] & 0xffffffffUL ) + j;
|
|
||||||
state[i] &= 0xffffffffUL;
|
|
||||||
++i; ++j;
|
|
||||||
if( i >= N ) { state[0] = state[N-1]; i = 1; }
|
|
||||||
if( j >= seedLength ) j = 0;
|
|
||||||
}
|
|
||||||
for( k = N - 1; k; --k )
|
|
||||||
{
|
|
||||||
state[i] =
|
|
||||||
state[i] ^ ( (state[i-1] ^ (state[i-1] >> 30)) * 1566083941UL );
|
|
||||||
state[i] -= i;
|
|
||||||
state[i] &= 0xffffffffUL;
|
|
||||||
++i;
|
|
||||||
if( i >= N ) { state[0] = state[N-1]; i = 1; }
|
|
||||||
}
|
|
||||||
state[0] = 0x80000000UL; // MSB is 1, assuring non-zero initial array
|
|
||||||
reload();
|
|
||||||
}
|
|
||||||
|
|
||||||
inline void MTRand::seed()
|
|
||||||
{
|
|
||||||
// Seed the generator with an array from /dev/urandom if available
|
|
||||||
// Otherwise use a hash of time() and clock() values
|
|
||||||
|
|
||||||
// First try getting an array from /dev/urandom
|
|
||||||
|
|
||||||
/* // Commented out by FakeTruth because doing this 200 times a tick is SUUUUPEERRR SLOW!!~~!\D5Ne
|
|
||||||
FILE* urandom = fopen( "/dev/urandom", "rb" );
|
|
||||||
if( urandom )
|
|
||||||
{
|
|
||||||
uint32 bigSeed[N];
|
|
||||||
register uint32 *s = bigSeed;
|
|
||||||
register int i = N;
|
|
||||||
register bool success = true;
|
|
||||||
while( success && i-- )
|
|
||||||
success = fread( s++, sizeof(uint32), 1, urandom );
|
|
||||||
fclose(urandom);
|
|
||||||
if( success ) { seed( bigSeed, N ); return; }
|
|
||||||
}
|
|
||||||
*/
|
|
||||||
|
|
||||||
// Was not successful, so use time() and clock() instead
|
|
||||||
seed( hash( time(NULL), clock() ) );
|
|
||||||
}
|
|
||||||
|
|
||||||
inline MTRand::MTRand( const uint32 oneSeed )
|
|
||||||
{ seed(oneSeed); }
|
|
||||||
|
|
||||||
inline MTRand::MTRand( uint32 *const bigSeed, const uint32 seedLength )
|
|
||||||
{ seed(bigSeed,seedLength); }
|
|
||||||
|
|
||||||
inline MTRand::MTRand()
|
|
||||||
{ seed(); }
|
|
||||||
|
|
||||||
inline MTRand::MTRand( const MTRand& o )
|
|
||||||
{
|
|
||||||
const uint32 *t = o.state;
|
|
||||||
uint32 *s = state;
|
|
||||||
int i = N;
|
|
||||||
for( ; i--; *s++ = *t++ ) {}
|
|
||||||
left = o.left;
|
|
||||||
pNext = &state[N-left];
|
|
||||||
}
|
|
||||||
|
|
||||||
inline MTRand::uint32 MTRand::randInt()
|
|
||||||
{
|
|
||||||
// Pull a 32-bit integer from the generator state
|
|
||||||
// Every other access function simply transforms the numbers extracted here
|
|
||||||
|
|
||||||
if( left == 0 ) reload();
|
|
||||||
--left;
|
|
||||||
|
|
||||||
uint32 s1;
|
|
||||||
s1 = *pNext++;
|
|
||||||
s1 ^= (s1 >> 11);
|
|
||||||
s1 ^= (s1 << 7) & 0x9d2c5680UL;
|
|
||||||
s1 ^= (s1 << 15) & 0xefc60000UL;
|
|
||||||
return ( s1 ^ (s1 >> 18) );
|
|
||||||
}
|
|
||||||
|
|
||||||
inline MTRand::uint32 MTRand::randInt( const uint32 n )
|
|
||||||
{
|
|
||||||
// Find which bits are used in n
|
|
||||||
// Optimized by Magnus Jonsson (magnus@smartelectronix.com)
|
|
||||||
uint32 used = n;
|
|
||||||
used |= used >> 1;
|
|
||||||
used |= used >> 2;
|
|
||||||
used |= used >> 4;
|
|
||||||
used |= used >> 8;
|
|
||||||
used |= used >> 16;
|
|
||||||
|
|
||||||
// Draw numbers until one is found in [0,n]
|
|
||||||
uint32 i;
|
|
||||||
do
|
|
||||||
i = randInt() & used; // toss unused bits to shorten search
|
|
||||||
while( i > n );
|
|
||||||
return i;
|
|
||||||
}
|
|
||||||
|
|
||||||
inline double MTRand::rand()
|
|
||||||
{ return double(randInt()) * (1.0/4294967295.0); }
|
|
||||||
|
|
||||||
inline double MTRand::rand( const double n )
|
|
||||||
{ return rand() * n; }
|
|
||||||
|
|
||||||
inline double MTRand::randExc()
|
|
||||||
{ return double(randInt()) * (1.0/4294967296.0); }
|
|
||||||
|
|
||||||
inline double MTRand::randExc( const double n )
|
|
||||||
{ return randExc() * n; }
|
|
||||||
|
|
||||||
inline double MTRand::randDblExc()
|
|
||||||
{ return ( double(randInt()) + 0.5 ) * (1.0/4294967296.0); }
|
|
||||||
|
|
||||||
inline double MTRand::randDblExc( const double n )
|
|
||||||
{ return randDblExc() * n; }
|
|
||||||
|
|
||||||
inline double MTRand::rand53()
|
|
||||||
{
|
|
||||||
uint32 a = randInt() >> 5, b = randInt() >> 6;
|
|
||||||
return ( a * 67108864.0 + b ) * (1.0/9007199254740992.0); // by Isaku Wada
|
|
||||||
}
|
|
||||||
|
|
||||||
inline double MTRand::randNorm( const double mean, const double stddev )
|
|
||||||
{
|
|
||||||
// Return a real number from a normal (Gaussian) distribution with given
|
|
||||||
// mean and standard deviation by polar form of Box-Muller transformation
|
|
||||||
double x, y, r;
|
|
||||||
do
|
|
||||||
{
|
|
||||||
x = 2.0 * rand() - 1.0;
|
|
||||||
y = 2.0 * rand() - 1.0;
|
|
||||||
r = x * x + y * y;
|
|
||||||
}
|
|
||||||
while ( r >= 1.0 || r == 0.0 );
|
|
||||||
double s = sqrt( -2.0 * log(r) / r );
|
|
||||||
return mean + x * s * stddev;
|
|
||||||
}
|
|
||||||
|
|
||||||
inline double MTRand::operator()()
|
|
||||||
{
|
|
||||||
return rand();
|
|
||||||
}
|
|
||||||
|
|
||||||
inline void MTRand::save( uint32* saveArray ) const
|
|
||||||
{
|
|
||||||
const uint32 *s = state;
|
|
||||||
uint32 *sa = saveArray;
|
|
||||||
int i = N;
|
|
||||||
for( ; i--; *sa++ = *s++ ) {}
|
|
||||||
*sa = left;
|
|
||||||
}
|
|
||||||
|
|
||||||
inline void MTRand::load( uint32 *const loadArray )
|
|
||||||
{
|
|
||||||
uint32 *s = state;
|
|
||||||
uint32 *la = loadArray;
|
|
||||||
int i = N;
|
|
||||||
for( ; i--; *s++ = *la++ ) {}
|
|
||||||
left = *la;
|
|
||||||
pNext = &state[N-left];
|
|
||||||
}
|
|
||||||
|
|
||||||
inline std::ostream& operator<<( std::ostream& os, const MTRand& mtrand )
|
|
||||||
{
|
|
||||||
const MTRand::uint32 *s = mtrand.state;
|
|
||||||
int i = mtrand.N;
|
|
||||||
for( ; i--; os << *s++ << "\t" ) {}
|
|
||||||
return os << mtrand.left;
|
|
||||||
}
|
|
||||||
|
|
||||||
inline std::istream& operator>>( std::istream& is, MTRand& mtrand )
|
|
||||||
{
|
|
||||||
MTRand::uint32 *s = mtrand.state;
|
|
||||||
int i = mtrand.N;
|
|
||||||
for( ; i--; is >> *s++ ) {}
|
|
||||||
is >> mtrand.left;
|
|
||||||
mtrand.pNext = &mtrand.state[mtrand.N-mtrand.left];
|
|
||||||
return is;
|
|
||||||
}
|
|
||||||
|
|
||||||
inline MTRand& MTRand::operator=( const MTRand& o )
|
|
||||||
{
|
|
||||||
if( this == &o ) return (*this);
|
|
||||||
const uint32 *t = o.state;
|
|
||||||
uint32 *s = state;
|
|
||||||
int i = N;
|
|
||||||
for( ; i--; *s++ = *t++ ) {}
|
|
||||||
left = o.left;
|
|
||||||
pNext = &state[N-left];
|
|
||||||
return (*this);
|
|
||||||
}
|
|
||||||
|
|
||||||
#endif // MERSENNETWISTER_H
|
|
||||||
|
|
||||||
// Change log:
|
|
||||||
//
|
|
||||||
// v0.1 - First release on 15 May 2000
|
|
||||||
// - Based on code by Makoto Matsumoto, Takuji Nishimura, and Shawn Cokus
|
|
||||||
// - Translated from C to C++
|
|
||||||
// - Made completely ANSI compliant
|
|
||||||
// - Designed convenient interface for initialization, seeding, and
|
|
||||||
// obtaining numbers in default or user-defined ranges
|
|
||||||
// - Added automatic seeding from /dev/urandom or time() and clock()
|
|
||||||
// - Provided functions for saving and loading generator state
|
|
||||||
//
|
|
||||||
// v0.2 - Fixed bug which reloaded generator one step too late
|
|
||||||
//
|
|
||||||
// v0.3 - Switched to clearer, faster reload() code from Matthew Bellew
|
|
||||||
//
|
|
||||||
// v0.4 - Removed trailing newline in saved generator format to be consistent
|
|
||||||
// with output format of built-in types
|
|
||||||
//
|
|
||||||
// v0.5 - Improved portability by replacing static const int's with enum's and
|
|
||||||
// clarifying return values in seed(); suggested by Eric Heimburg
|
|
||||||
// - Removed MAXINT constant; use 0xffffffffUL instead
|
|
||||||
//
|
|
||||||
// v0.6 - Eliminated seed overflow when uint32 is larger than 32 bits
|
|
||||||
// - Changed integer [0,n] generator to give better uniformity
|
|
||||||
//
|
|
||||||
// v0.7 - Fixed operator precedence ambiguity in reload()
|
|
||||||
// - Added access for real numbers in (0,1) and (0,n)
|
|
||||||
//
|
|
||||||
// v0.8 - Included time.h header to properly support time_t and clock_t
|
|
||||||
//
|
|
||||||
// v1.0 - Revised seeding to match 26 Jan 2002 update of Nishimura and Matsumoto
|
|
||||||
// - Allowed for seeding with arrays of any length
|
|
||||||
// - Added access for real numbers in [0,1) with 53-bit resolution
|
|
||||||
// - Added access for real numbers from normal (Gaussian) distributions
|
|
||||||
// - Increased overall speed by optimizing twist()
|
|
||||||
// - Doubled speed of integer [0,n] generation
|
|
||||||
// - Fixed out-of-range number generation on 64-bit machines
|
|
||||||
// - Improved portability by substituting literal constants for long enum's
|
|
||||||
// - Changed license from GNU LGPL to BSD
|
|
||||||
//
|
|
||||||
// v1.1 - Corrected parameter label in randNorm from "variance" to "stddev"
|
|
||||||
// - Changed randNorm algorithm from basic to polar form for efficiency
|
|
||||||
// - Updated includes from deprecated <xxxx.h> to standard <cxxxx> forms
|
|
||||||
// - Cleaned declarations and definitions to please Intel compiler
|
|
||||||
// - Revised twist() operator to work on ones'-complement machines
|
|
||||||
// - Fixed reload() function to work when N and M are unsigned
|
|
||||||
// - Added copy constructor and copy operator from Salvador Espana
|
|
@ -9,7 +9,6 @@
|
|||||||
#include "../Entities/Player.h"
|
#include "../Entities/Player.h"
|
||||||
#include "../Entities/ExpOrb.h"
|
#include "../Entities/ExpOrb.h"
|
||||||
#include "../MonsterConfig.h"
|
#include "../MonsterConfig.h"
|
||||||
#include "../MersenneTwister.h"
|
|
||||||
|
|
||||||
#include "../Chunk.h"
|
#include "../Chunk.h"
|
||||||
#include "../FastRandom.h"
|
#include "../FastRandom.h"
|
||||||
|
@ -2,6 +2,7 @@
|
|||||||
#include "Globals.h" // NOTE: MSVC stupidness requires this to be the same across all modules
|
#include "Globals.h" // NOTE: MSVC stupidness requires this to be the same across all modules
|
||||||
|
|
||||||
#include "Witch.h"
|
#include "Witch.h"
|
||||||
|
#include "FastRandom.h"
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
@ -2,7 +2,6 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include "AggressiveMonster.h"
|
#include "AggressiveMonster.h"
|
||||||
#include "../MersenneTwister.h"
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
@ -13,12 +13,9 @@ SET (SRCS
|
|||||||
IsThread.cpp
|
IsThread.cpp
|
||||||
ListenThread.cpp
|
ListenThread.cpp
|
||||||
Semaphore.cpp
|
Semaphore.cpp
|
||||||
Sleep.cpp
|
|
||||||
Socket.cpp
|
Socket.cpp
|
||||||
SocketThreads.cpp
|
SocketThreads.cpp
|
||||||
StackTrace.cpp
|
StackTrace.cpp
|
||||||
Thread.cpp
|
|
||||||
Timer.cpp
|
|
||||||
)
|
)
|
||||||
|
|
||||||
SET (HDRS
|
SET (HDRS
|
||||||
@ -31,12 +28,9 @@ SET (HDRS
|
|||||||
ListenThread.h
|
ListenThread.h
|
||||||
Queue.h
|
Queue.h
|
||||||
Semaphore.h
|
Semaphore.h
|
||||||
Sleep.h
|
|
||||||
Socket.h
|
Socket.h
|
||||||
SocketThreads.h
|
SocketThreads.h
|
||||||
StackTrace.h
|
StackTrace.h
|
||||||
Thread.h
|
|
||||||
Timer.h
|
|
||||||
)
|
)
|
||||||
|
|
||||||
if(NOT MSVC)
|
if(NOT MSVC)
|
||||||
|
@ -1,6 +1,5 @@
|
|||||||
|
|
||||||
#include "Globals.h" // NOTE: MSVC stupidness requires this to be the same across all modules
|
#include "Globals.h" // NOTE: MSVC stupidness requires this to be the same across all modules
|
||||||
#include "IsThread.h"
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@ -9,41 +8,12 @@
|
|||||||
////////////////////////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////////////////////////
|
||||||
// cCriticalSection:
|
// cCriticalSection:
|
||||||
|
|
||||||
|
#ifdef _DEBUG
|
||||||
cCriticalSection::cCriticalSection()
|
cCriticalSection::cCriticalSection()
|
||||||
{
|
{
|
||||||
#ifdef _WIN32
|
m_IsLocked = 0;
|
||||||
InitializeCriticalSection(&m_CriticalSection);
|
|
||||||
#else
|
|
||||||
pthread_mutexattr_init(&m_Attributes);
|
|
||||||
pthread_mutexattr_settype(&m_Attributes, PTHREAD_MUTEX_RECURSIVE);
|
|
||||||
|
|
||||||
if (pthread_mutex_init(&m_CriticalSection, &m_Attributes) != 0)
|
|
||||||
{
|
|
||||||
LOGERROR("Could not initialize Critical Section!");
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#ifdef _DEBUG
|
|
||||||
m_IsLocked = 0;
|
|
||||||
#endif // _DEBUG
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
cCriticalSection::~cCriticalSection()
|
|
||||||
{
|
|
||||||
#ifdef _WIN32
|
|
||||||
DeleteCriticalSection(&m_CriticalSection);
|
|
||||||
#else
|
|
||||||
if (pthread_mutex_destroy(&m_CriticalSection) != 0)
|
|
||||||
{
|
|
||||||
LOGWARNING("Could not destroy Critical Section!");
|
|
||||||
}
|
|
||||||
pthread_mutexattr_destroy(&m_Attributes);
|
|
||||||
#endif
|
|
||||||
}
|
}
|
||||||
|
#endif // _DEBUG
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@ -51,15 +21,11 @@ cCriticalSection::~cCriticalSection()
|
|||||||
|
|
||||||
void cCriticalSection::Lock()
|
void cCriticalSection::Lock()
|
||||||
{
|
{
|
||||||
#ifdef _WIN32
|
m_Mutex.lock();
|
||||||
EnterCriticalSection(&m_CriticalSection);
|
|
||||||
#else
|
|
||||||
pthread_mutex_lock(&m_CriticalSection);
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#ifdef _DEBUG
|
#ifdef _DEBUG
|
||||||
m_IsLocked += 1;
|
m_IsLocked += 1;
|
||||||
m_OwningThreadID = cIsThread::GetCurrentID();
|
m_OwningThreadID = std::this_thread::get_id();
|
||||||
#endif // _DEBUG
|
#endif // _DEBUG
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -74,11 +40,7 @@ void cCriticalSection::Unlock()
|
|||||||
m_IsLocked -= 1;
|
m_IsLocked -= 1;
|
||||||
#endif // _DEBUG
|
#endif // _DEBUG
|
||||||
|
|
||||||
#ifdef _WIN32
|
m_Mutex.unlock();
|
||||||
LeaveCriticalSection(&m_CriticalSection);
|
|
||||||
#else
|
|
||||||
pthread_mutex_unlock(&m_CriticalSection);
|
|
||||||
#endif
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -97,7 +59,7 @@ bool cCriticalSection::IsLocked(void)
|
|||||||
|
|
||||||
bool cCriticalSection::IsLockedByCurrentThread(void)
|
bool cCriticalSection::IsLockedByCurrentThread(void)
|
||||||
{
|
{
|
||||||
return ((m_IsLocked > 0) && (m_OwningThreadID == cIsThread::GetCurrentID()));
|
return ((m_IsLocked > 0) && (m_OwningThreadID == std::this_thread::get_id()));
|
||||||
}
|
}
|
||||||
#endif // _DEBUG
|
#endif // _DEBUG
|
||||||
|
|
||||||
|
@ -1,5 +1,7 @@
|
|||||||
|
|
||||||
#pragma once
|
#pragma once
|
||||||
|
#include <mutex>
|
||||||
|
#include <thread>
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@ -8,8 +10,6 @@
|
|||||||
class cCriticalSection
|
class cCriticalSection
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
cCriticalSection(void);
|
|
||||||
~cCriticalSection();
|
|
||||||
|
|
||||||
void Lock(void);
|
void Lock(void);
|
||||||
void Unlock(void);
|
void Unlock(void);
|
||||||
@ -17,6 +17,7 @@ public:
|
|||||||
// IsLocked/IsLockedByCurrentThread are only used in ASSERT statements, but because of the changes with ASSERT they must always be defined
|
// IsLocked/IsLockedByCurrentThread are only used in ASSERT statements, but because of the changes with ASSERT they must always be defined
|
||||||
// The fake versions (in Release) will not effect the program in any way
|
// The fake versions (in Release) will not effect the program in any way
|
||||||
#ifdef _DEBUG
|
#ifdef _DEBUG
|
||||||
|
cCriticalSection(void);
|
||||||
bool IsLocked(void);
|
bool IsLocked(void);
|
||||||
bool IsLockedByCurrentThread(void);
|
bool IsLockedByCurrentThread(void);
|
||||||
#else
|
#else
|
||||||
@ -27,15 +28,10 @@ public:
|
|||||||
private:
|
private:
|
||||||
#ifdef _DEBUG
|
#ifdef _DEBUG
|
||||||
int m_IsLocked; // Number of times this CS is locked
|
int m_IsLocked; // Number of times this CS is locked
|
||||||
unsigned long m_OwningThreadID;
|
std::thread::id m_OwningThreadID;
|
||||||
#endif // _DEBUG
|
#endif // _DEBUG
|
||||||
|
|
||||||
#ifdef _WIN32
|
std::recursive_mutex m_Mutex;
|
||||||
CRITICAL_SECTION m_CriticalSection;
|
|
||||||
#else // _WIN32
|
|
||||||
pthread_mutex_t m_CriticalSection;
|
|
||||||
pthread_mutexattr_t m_Attributes;
|
|
||||||
#endif // else _WIN32
|
|
||||||
} ALIGN_8;
|
} ALIGN_8;
|
||||||
|
|
||||||
|
|
||||||
|
@ -5,49 +5,40 @@
|
|||||||
// This class will eventually suupersede the old cThread class
|
// This class will eventually suupersede the old cThread class
|
||||||
|
|
||||||
#include "Globals.h"
|
#include "Globals.h"
|
||||||
|
|
||||||
#include "IsThread.h"
|
#include "IsThread.h"
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
// When in MSVC, the debugger provides "thread naming" by catching special exceptions. Interface here:
|
|
||||||
#if defined(_MSC_VER) && defined(_DEBUG)
|
#if defined(_MSC_VER) && defined(_DEBUG)
|
||||||
//
|
// Code adapted from MSDN: http://msdn.microsoft.com/en-us/library/xcb2z8hs.aspx
|
||||||
// Usage: SetThreadName (-1, "MainThread");
|
|
||||||
//
|
|
||||||
|
|
||||||
// Code adapted from MSDN: http://msdn.microsoft.com/en-us/library/xcb2z8hs.aspx
|
const DWORD MS_VC_EXCEPTION = 0x406D1388;
|
||||||
|
#pragma pack(push, 8)
|
||||||
|
struct THREADNAME_INFO
|
||||||
|
{
|
||||||
|
DWORD dwType; // Must be 0x1000.
|
||||||
|
LPCSTR szName; // Pointer to name (in user addr space).
|
||||||
|
DWORD dwThreadID; // Thread ID (-1 = caller thread).
|
||||||
|
DWORD dwFlags; // Reserved for future use, must be zero.
|
||||||
|
};
|
||||||
|
#pragma pack(pop)
|
||||||
|
|
||||||
const DWORD MS_VC_EXCEPTION = 0x406D1388;
|
/** Sets the name of a thread with the specified ID
|
||||||
|
(When in MSVC, the debugger provides "thread naming" by catching special exceptions)
|
||||||
#pragma pack(push, 8)
|
*/
|
||||||
typedef struct tagTHREADNAME_INFO
|
static void SetThreadName(std::thread * a_Thread, const char * a_ThreadName)
|
||||||
{
|
|
||||||
DWORD dwType; // Must be 0x1000.
|
|
||||||
LPCSTR szName; // Pointer to name (in user addr space).
|
|
||||||
DWORD dwThreadID; // Thread ID (-1 = caller thread).
|
|
||||||
DWORD dwFlags; // Reserved for future use, must be zero.
|
|
||||||
} THREADNAME_INFO;
|
|
||||||
#pragma pack(pop)
|
|
||||||
|
|
||||||
static void SetThreadName(DWORD dwThreadID, const char * threadName)
|
|
||||||
{
|
|
||||||
THREADNAME_INFO info;
|
|
||||||
info.dwType = 0x1000;
|
|
||||||
info.szName = threadName;
|
|
||||||
info.dwThreadID = dwThreadID;
|
|
||||||
info.dwFlags = 0;
|
|
||||||
|
|
||||||
__try
|
|
||||||
{
|
{
|
||||||
RaiseException(MS_VC_EXCEPTION, 0, sizeof(info) / sizeof(ULONG_PTR), (ULONG_PTR *)&info);
|
THREADNAME_INFO info { 0x1000, a_ThreadName, GetThreadId(a_Thread->native_handle()), 0 };
|
||||||
|
__try
|
||||||
|
{
|
||||||
|
RaiseException(MS_VC_EXCEPTION, 0, sizeof(info) / sizeof(ULONG_PTR), (ULONG_PTR *)&info);
|
||||||
|
}
|
||||||
|
__except (EXCEPTION_EXECUTE_HANDLER)
|
||||||
|
{
|
||||||
|
}
|
||||||
}
|
}
|
||||||
__except (EXCEPTION_EXECUTE_HANDLER)
|
|
||||||
{
|
|
||||||
}
|
|
||||||
}
|
|
||||||
#endif // _MSC_VER && _DEBUG
|
#endif // _MSC_VER && _DEBUG
|
||||||
|
|
||||||
|
|
||||||
@ -57,13 +48,9 @@ static void SetThreadName(DWORD dwThreadID, const char * threadName)
|
|||||||
////////////////////////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////////////////////////
|
||||||
// cIsThread:
|
// cIsThread:
|
||||||
|
|
||||||
cIsThread::cIsThread(const AString & iThreadName) :
|
cIsThread::cIsThread(const AString & a_ThreadName) :
|
||||||
m_ShouldTerminate(false),
|
m_ShouldTerminate(false),
|
||||||
m_ThreadName(iThreadName),
|
m_ThreadName(a_ThreadName)
|
||||||
#ifdef _WIN32
|
|
||||||
m_ThreadID(0),
|
|
||||||
#endif
|
|
||||||
m_Handle(NULL_HANDLE)
|
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -83,35 +70,24 @@ cIsThread::~cIsThread()
|
|||||||
|
|
||||||
bool cIsThread::Start(void)
|
bool cIsThread::Start(void)
|
||||||
{
|
{
|
||||||
ASSERT(m_Handle == NULL_HANDLE); // Has already started one thread?
|
try
|
||||||
#ifdef _WIN32
|
{
|
||||||
// Create the thread suspended, so that the mHandle variable is valid in the thread procedure
|
m_Thread = std::thread(&cIsThread::Execute, this);
|
||||||
m_ThreadID = 0;
|
|
||||||
m_Handle = CreateThread(NULL, 0, thrExecute, this, CREATE_SUSPENDED, &m_ThreadID);
|
|
||||||
if (m_Handle == NULL)
|
|
||||||
{
|
|
||||||
LOGERROR("ERROR: Could not create thread \"%s\", GLE = %u!", m_ThreadName.c_str(), (unsigned)GetLastError());
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
ResumeThread(m_Handle);
|
|
||||||
|
|
||||||
#if defined(_DEBUG) && defined(_MSC_VER)
|
#if defined (_MSC_VER) && defined(_DEBUG)
|
||||||
// Thread naming is available only in MSVC
|
if (!m_ThreadName.empty())
|
||||||
if (!m_ThreadName.empty())
|
|
||||||
{
|
|
||||||
SetThreadName(m_ThreadID, m_ThreadName.c_str());
|
|
||||||
}
|
|
||||||
#endif // _DEBUG and _MSC_VER
|
|
||||||
|
|
||||||
#else // _WIN32
|
|
||||||
if (pthread_create(&m_Handle, NULL, thrExecute, this))
|
|
||||||
{
|
{
|
||||||
LOGERROR("ERROR: Could not create thread \"%s\", !", m_ThreadName.c_str());
|
SetThreadName(&m_Thread, m_ThreadName.c_str());
|
||||||
return false;
|
|
||||||
}
|
}
|
||||||
#endif // else _WIN32
|
#endif
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
|
}
|
||||||
|
catch (std::system_error & a_Exception)
|
||||||
|
{
|
||||||
|
LOGERROR("cIsThread::Wait (std::thread) error %i: could not construct thread %s; %s", a_Exception.code().value(), m_ThreadName.c_str(), a_Exception.what());
|
||||||
|
return false;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -120,10 +96,6 @@ bool cIsThread::Start(void)
|
|||||||
|
|
||||||
void cIsThread::Stop(void)
|
void cIsThread::Stop(void)
|
||||||
{
|
{
|
||||||
if (m_Handle == NULL_HANDLE)
|
|
||||||
{
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
m_ShouldTerminate = true;
|
m_ShouldTerminate = true;
|
||||||
Wait();
|
Wait();
|
||||||
}
|
}
|
||||||
@ -134,59 +106,29 @@ void cIsThread::Stop(void)
|
|||||||
|
|
||||||
bool cIsThread::Wait(void)
|
bool cIsThread::Wait(void)
|
||||||
{
|
{
|
||||||
if (m_Handle == NULL_HANDLE)
|
|
||||||
{
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
#ifdef LOGD // ProtoProxy doesn't have LOGD
|
#ifdef LOGD // ProtoProxy doesn't have LOGD
|
||||||
LOGD("Waiting for thread %s to finish", m_ThreadName.c_str());
|
LOGD("Waiting for thread %s to finish", m_ThreadName.c_str());
|
||||||
#endif // LOGD
|
#endif // LOGD
|
||||||
|
|
||||||
#ifdef _WIN32
|
|
||||||
int res = WaitForSingleObject(m_Handle, INFINITE);
|
|
||||||
m_Handle = NULL;
|
|
||||||
|
|
||||||
#ifdef LOGD // ProtoProxy doesn't have LOGD
|
|
||||||
LOGD("Thread %s finished", m_ThreadName.c_str());
|
|
||||||
#endif // LOGD
|
|
||||||
|
|
||||||
return (res == WAIT_OBJECT_0);
|
|
||||||
#else // _WIN32
|
|
||||||
int res = pthread_join(m_Handle, NULL);
|
|
||||||
m_Handle = NULL_HANDLE;
|
|
||||||
|
|
||||||
#ifdef LOGD // ProtoProxy doesn't have LOGD
|
|
||||||
LOGD("Thread %s finished", m_ThreadName.c_str());
|
|
||||||
#endif // LOGD
|
|
||||||
|
|
||||||
return (res == 0);
|
|
||||||
#endif // else _WIN32
|
|
||||||
}
|
|
||||||
|
|
||||||
|
if (m_Thread.joinable())
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
m_Thread.join();
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
catch (std::system_error & a_Exception)
|
||||||
|
{
|
||||||
|
LOGERROR("cIsThread::Wait (std::thread) error %i: could not join thread %s; %s", a_Exception.code().value(), m_ThreadName.c_str(), a_Exception.what());
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#ifdef LOGD // ProtoProxy doesn't have LOGD
|
||||||
|
LOGD("Thread %s finished", m_ThreadName.c_str());
|
||||||
|
|
||||||
unsigned long cIsThread::GetCurrentID(void)
|
|
||||||
{
|
|
||||||
#ifdef _WIN32
|
|
||||||
return (unsigned long) GetCurrentThreadId();
|
|
||||||
#else
|
|
||||||
return (unsigned long) pthread_self();
|
|
||||||
#endif
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
bool cIsThread::IsCurrentThread(void) const
|
|
||||||
{
|
|
||||||
#ifdef _WIN32
|
|
||||||
return (GetCurrentThreadId() == m_ThreadID);
|
|
||||||
#else
|
|
||||||
return (m_Handle == pthread_self());
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -16,8 +16,7 @@ In the descending class' constructor call the Start() method to start the thread
|
|||||||
|
|
||||||
|
|
||||||
#pragma once
|
#pragma once
|
||||||
#ifndef CISTHREAD_H_INCLUDED
|
#include <thread>
|
||||||
#define CISTHREAD_H_INCLUDED
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@ -33,7 +32,7 @@ protected:
|
|||||||
volatile bool m_ShouldTerminate;
|
volatile bool m_ShouldTerminate;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
cIsThread(const AString & iThreadName);
|
cIsThread(const AString & a_ThreadName);
|
||||||
virtual ~cIsThread();
|
virtual ~cIsThread();
|
||||||
|
|
||||||
/// Starts the thread; returns without waiting for the actual start
|
/// Starts the thread; returns without waiting for the actual start
|
||||||
@ -45,56 +44,14 @@ public:
|
|||||||
/// Waits for the thread to finish. Doesn't signalize the ShouldTerminate flag
|
/// Waits for the thread to finish. Doesn't signalize the ShouldTerminate flag
|
||||||
bool Wait(void);
|
bool Wait(void);
|
||||||
|
|
||||||
/// Returns the OS-dependent thread ID for the caller's thread
|
|
||||||
static unsigned long GetCurrentID(void);
|
|
||||||
|
|
||||||
/** Returns true if the thread calling this function is the thread contained within this object. */
|
/** Returns true if the thread calling this function is the thread contained within this object. */
|
||||||
bool IsCurrentThread(void) const;
|
bool IsCurrentThread(void) const { return std::this_thread::get_id() == m_Thread.get_id(); }
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
AString m_ThreadName;
|
AString m_ThreadName;
|
||||||
|
std::thread m_Thread;
|
||||||
// Value used for "no handle":
|
|
||||||
#ifdef _WIN32
|
|
||||||
#define NULL_HANDLE NULL
|
|
||||||
#else
|
|
||||||
#define NULL_HANDLE 0
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#ifdef _WIN32
|
|
||||||
|
|
||||||
DWORD m_ThreadID;
|
|
||||||
HANDLE m_Handle;
|
|
||||||
|
|
||||||
static DWORD __stdcall thrExecute(LPVOID a_Param)
|
|
||||||
{
|
|
||||||
// Create a window so that the thread can be identified by 3rd party tools:
|
|
||||||
HWND IdentificationWnd = CreateWindowA("STATIC", ((cIsThread *)a_Param)->m_ThreadName.c_str(), 0, 0, 0, 0, WS_OVERLAPPED, NULL, NULL, NULL, NULL);
|
|
||||||
|
|
||||||
// Run the thread:
|
|
||||||
((cIsThread *)a_Param)->Execute();
|
|
||||||
|
|
||||||
// Destroy the identification window:
|
|
||||||
DestroyWindow(IdentificationWnd);
|
|
||||||
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
#else // _WIN32
|
|
||||||
|
|
||||||
pthread_t m_Handle;
|
|
||||||
|
|
||||||
static void * thrExecute(void * a_Param)
|
|
||||||
{
|
|
||||||
(static_cast<cIsThread *>(a_Param))->Execute();
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
#endif // else _WIN32
|
|
||||||
} ;
|
} ;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
#endif // CISTHREAD_H_INCLUDED
|
|
||||||
|
@ -1,19 +0,0 @@
|
|||||||
|
|
||||||
#include "Globals.h" // NOTE: MSVC stupidness requires this to be the same across all modules
|
|
||||||
|
|
||||||
#ifndef _WIN32
|
|
||||||
#include <unistd.h>
|
|
||||||
#endif
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
void cSleep::MilliSleep( unsigned int a_MilliSeconds)
|
|
||||||
{
|
|
||||||
#ifdef _WIN32
|
|
||||||
Sleep(a_MilliSeconds); // Don't tick too much
|
|
||||||
#else
|
|
||||||
usleep(a_MilliSeconds*1000);
|
|
||||||
#endif
|
|
||||||
}
|
|
@ -1,7 +0,0 @@
|
|||||||
#pragma once
|
|
||||||
|
|
||||||
class cSleep
|
|
||||||
{
|
|
||||||
public:
|
|
||||||
static void MilliSleep( unsigned int a_MilliSeconds);
|
|
||||||
};
|
|
@ -1,146 +0,0 @@
|
|||||||
|
|
||||||
#include "Globals.h" // NOTE: MSVC stupidness requires this to be the same across all modules
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
// When in MSVC, the debugger provides "thread naming" by catching special exceptions. Interface here:
|
|
||||||
#ifdef _MSC_VER
|
|
||||||
//
|
|
||||||
// Usage: SetThreadName (-1, "MainThread");
|
|
||||||
//
|
|
||||||
|
|
||||||
// Code adapted from MSDN: http://msdn.microsoft.com/en-us/library/xcb2z8hs.aspx
|
|
||||||
|
|
||||||
const DWORD MS_VC_EXCEPTION = 0x406D1388;
|
|
||||||
|
|
||||||
#pragma pack(push, 8)
|
|
||||||
typedef struct tagTHREADNAME_INFO
|
|
||||||
{
|
|
||||||
DWORD dwType; // Must be 0x1000.
|
|
||||||
LPCSTR szName; // Pointer to name (in user addr space).
|
|
||||||
DWORD dwThreadID; // Thread ID (-1 = caller thread).
|
|
||||||
DWORD dwFlags; // Reserved for future use, must be zero.
|
|
||||||
} THREADNAME_INFO;
|
|
||||||
#pragma pack(pop)
|
|
||||||
|
|
||||||
static void SetThreadName(DWORD dwThreadID, const char * threadName)
|
|
||||||
{
|
|
||||||
THREADNAME_INFO info;
|
|
||||||
info.dwType = 0x1000;
|
|
||||||
info.szName = threadName;
|
|
||||||
info.dwThreadID = dwThreadID;
|
|
||||||
info.dwFlags = 0;
|
|
||||||
|
|
||||||
__try
|
|
||||||
{
|
|
||||||
RaiseException(MS_VC_EXCEPTION, 0, sizeof(info) / sizeof(ULONG_PTR), (ULONG_PTR *)&info);
|
|
||||||
}
|
|
||||||
__except (EXCEPTION_EXECUTE_HANDLER)
|
|
||||||
{
|
|
||||||
}
|
|
||||||
}
|
|
||||||
#endif // _MSC_VER
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
cThread::cThread( ThreadFunc a_ThreadFunction, void* a_Param, const char* a_ThreadName /* = 0 */)
|
|
||||||
: m_ThreadFunction( a_ThreadFunction)
|
|
||||||
, m_Param( a_Param)
|
|
||||||
, m_Event( new cEvent())
|
|
||||||
, m_StopEvent( 0)
|
|
||||||
{
|
|
||||||
if (a_ThreadName)
|
|
||||||
{
|
|
||||||
m_ThreadName.assign(a_ThreadName);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
cThread::~cThread()
|
|
||||||
{
|
|
||||||
delete m_Event;
|
|
||||||
m_Event = NULL;
|
|
||||||
|
|
||||||
if (m_StopEvent)
|
|
||||||
{
|
|
||||||
m_StopEvent->Wait();
|
|
||||||
delete m_StopEvent;
|
|
||||||
m_StopEvent = NULL;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
void cThread::Start( bool a_bWaitOnDelete /* = true */)
|
|
||||||
{
|
|
||||||
if (a_bWaitOnDelete)
|
|
||||||
{
|
|
||||||
m_StopEvent = new cEvent();
|
|
||||||
}
|
|
||||||
|
|
||||||
#ifndef _WIN32
|
|
||||||
pthread_t SndThread;
|
|
||||||
if (pthread_create( &SndThread, NULL, MyThread, this))
|
|
||||||
{
|
|
||||||
LOGERROR("ERROR: Could not create thread!");
|
|
||||||
}
|
|
||||||
#else
|
|
||||||
DWORD ThreadID = 0;
|
|
||||||
HANDLE hThread = CreateThread(NULL // security
|
|
||||||
, 0 // stack size
|
|
||||||
, (LPTHREAD_START_ROUTINE) MyThread // function name
|
|
||||||
, this // parameters
|
|
||||||
, 0 // flags
|
|
||||||
, &ThreadID); // thread id
|
|
||||||
CloseHandle( hThread);
|
|
||||||
|
|
||||||
#ifdef _MSC_VER
|
|
||||||
if (!m_ThreadName.empty())
|
|
||||||
{
|
|
||||||
SetThreadName(ThreadID, m_ThreadName.c_str());
|
|
||||||
}
|
|
||||||
#endif // _MSC_VER
|
|
||||||
#endif
|
|
||||||
|
|
||||||
// Wait until thread has actually been created
|
|
||||||
m_Event->Wait();
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
#ifdef _WIN32
|
|
||||||
unsigned long cThread::MyThread(void* a_Param)
|
|
||||||
#else
|
|
||||||
void *cThread::MyThread( void *a_Param)
|
|
||||||
#endif
|
|
||||||
{
|
|
||||||
cThread* self = (cThread*)a_Param;
|
|
||||||
cEvent* StopEvent = self->m_StopEvent;
|
|
||||||
|
|
||||||
ThreadFunc* ThreadFunction = self->m_ThreadFunction;
|
|
||||||
void* ThreadParam = self->m_Param;
|
|
||||||
|
|
||||||
// Set event to let other thread know this thread has been created and it's safe to delete the cThread object
|
|
||||||
self->m_Event->Set();
|
|
||||||
|
|
||||||
ThreadFunction( ThreadParam);
|
|
||||||
|
|
||||||
// If the thread was marked as wait-on-delete, signal the event being waited on:
|
|
||||||
if (StopEvent != nullptr)
|
|
||||||
{
|
|
||||||
StopEvent->Set();
|
|
||||||
}
|
|
||||||
|
|
||||||
return 0;
|
|
||||||
}
|
|
@ -1,26 +0,0 @@
|
|||||||
#pragma once
|
|
||||||
|
|
||||||
class cThread
|
|
||||||
{
|
|
||||||
public:
|
|
||||||
typedef void (ThreadFunc)(void*);
|
|
||||||
cThread( ThreadFunc a_ThreadFunction, void* a_Param, const char* a_ThreadName = 0);
|
|
||||||
~cThread();
|
|
||||||
|
|
||||||
void Start( bool a_bWaitOnDelete = true);
|
|
||||||
void WaitForThread();
|
|
||||||
private:
|
|
||||||
ThreadFunc* m_ThreadFunction;
|
|
||||||
|
|
||||||
#ifdef _WIN32
|
|
||||||
static unsigned long MyThread(void* a_Param);
|
|
||||||
#else
|
|
||||||
static void *MyThread( void *lpParam);
|
|
||||||
#endif
|
|
||||||
|
|
||||||
void* m_Param;
|
|
||||||
cEvent* m_Event;
|
|
||||||
cEvent* m_StopEvent;
|
|
||||||
|
|
||||||
AString m_ThreadName;
|
|
||||||
};
|
|
@ -1,37 +0,0 @@
|
|||||||
|
|
||||||
#include "Globals.h" // NOTE: MSVC stupidness requires this to be the same across all modules
|
|
||||||
|
|
||||||
#include "Timer.h"
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
cTimer::cTimer(void)
|
|
||||||
{
|
|
||||||
#ifdef _WIN32
|
|
||||||
QueryPerformanceFrequency(&m_TicksPerSecond);
|
|
||||||
#endif
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
long long cTimer::GetNowTime(void)
|
|
||||||
{
|
|
||||||
#ifdef _WIN32
|
|
||||||
LARGE_INTEGER now;
|
|
||||||
QueryPerformanceCounter(&now);
|
|
||||||
return ((now.QuadPart * 1000) / m_TicksPerSecond.QuadPart);
|
|
||||||
#else
|
|
||||||
struct timeval now;
|
|
||||||
gettimeofday(&now, NULL);
|
|
||||||
return (long long)now.tv_sec * 1000 + (long long)now.tv_usec / 1000;
|
|
||||||
#endif
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -1,32 +0,0 @@
|
|||||||
|
|
||||||
// Timer.h
|
|
||||||
|
|
||||||
// Declares the cTimer class representing an OS-independent of retrieving current time with msec accuracy
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
#pragma once
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
class cTimer
|
|
||||||
{
|
|
||||||
public:
|
|
||||||
cTimer(void);
|
|
||||||
|
|
||||||
// Returns the current time expressed in milliseconds
|
|
||||||
long long GetNowTime(void);
|
|
||||||
private:
|
|
||||||
|
|
||||||
#ifdef _WIN32
|
|
||||||
LARGE_INTEGER m_TicksPerSecond;
|
|
||||||
#endif
|
|
||||||
} ;
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -5,7 +5,7 @@
|
|||||||
|
|
||||||
#include "Globals.h"
|
#include "Globals.h"
|
||||||
#include "ProbabDistrib.h"
|
#include "ProbabDistrib.h"
|
||||||
#include "MersenneTwister.h"
|
#include "FastRandom.h"
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
38
src/Root.cpp
38
src/Root.cpp
@ -16,7 +16,6 @@
|
|||||||
#include "Protocol/ProtocolRecognizer.h" // for protocol version constants
|
#include "Protocol/ProtocolRecognizer.h" // for protocol version constants
|
||||||
#include "CommandOutput.h"
|
#include "CommandOutput.h"
|
||||||
#include "DeadlockDetect.h"
|
#include "DeadlockDetect.h"
|
||||||
#include "OSSupport/Timer.h"
|
|
||||||
#include "LoggerListeners.h"
|
#include "LoggerListeners.h"
|
||||||
#include "BuildInfo.h"
|
#include "BuildInfo.h"
|
||||||
#include "IniFile.h"
|
#include "IniFile.h"
|
||||||
@ -42,7 +41,6 @@ cRoot* cRoot::s_Root = nullptr;
|
|||||||
|
|
||||||
cRoot::cRoot(void) :
|
cRoot::cRoot(void) :
|
||||||
m_pDefaultWorld(nullptr),
|
m_pDefaultWorld(nullptr),
|
||||||
m_InputThread(nullptr),
|
|
||||||
m_Server(nullptr),
|
m_Server(nullptr),
|
||||||
m_MonsterConfig(nullptr),
|
m_MonsterConfig(nullptr),
|
||||||
m_CraftingRecipes(nullptr),
|
m_CraftingRecipes(nullptr),
|
||||||
@ -68,26 +66,24 @@ cRoot::~cRoot()
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
void cRoot::InputThread(void * a_Params)
|
void cRoot::InputThread(cRoot & a_Params)
|
||||||
{
|
{
|
||||||
cRoot & self = *(cRoot*)a_Params;
|
|
||||||
|
|
||||||
cLogCommandOutputCallback Output;
|
cLogCommandOutputCallback Output;
|
||||||
|
|
||||||
while (!self.m_bStop && !self.m_bRestart && !m_TerminateEventRaised && std::cin.good())
|
while (!a_Params.m_bStop && !a_Params.m_bRestart && !m_TerminateEventRaised && std::cin.good())
|
||||||
{
|
{
|
||||||
AString Command;
|
AString Command;
|
||||||
std::getline(std::cin, Command);
|
std::getline(std::cin, Command);
|
||||||
if (!Command.empty())
|
if (!Command.empty())
|
||||||
{
|
{
|
||||||
self.ExecuteConsoleCommand(TrimString(Command), Output);
|
a_Params.ExecuteConsoleCommand(TrimString(Command), Output);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (m_TerminateEventRaised || !std::cin.good())
|
if (m_TerminateEventRaised || !std::cin.good())
|
||||||
{
|
{
|
||||||
// We have come here because the std::cin has received an EOF / a terminate signal has been sent, and the server is still running; stop the server:
|
// We have come here because the std::cin has received an EOF / a terminate signal has been sent, and the server is still running; stop the server:
|
||||||
self.m_bStop = true;
|
a_Params.m_bStop = true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -120,9 +116,7 @@ void cRoot::Start(void)
|
|||||||
m_bStop = false;
|
m_bStop = false;
|
||||||
while (!m_bStop)
|
while (!m_bStop)
|
||||||
{
|
{
|
||||||
cTimer Time;
|
auto BeginTime = std::chrono::steady_clock::now();
|
||||||
long long mseconds = Time.GetNowTime();
|
|
||||||
|
|
||||||
m_bRestart = false;
|
m_bRestart = false;
|
||||||
|
|
||||||
LoadGlobalSettings();
|
LoadGlobalSettings();
|
||||||
@ -191,21 +185,25 @@ void cRoot::Start(void)
|
|||||||
|
|
||||||
#if !defined(ANDROID_NDK)
|
#if !defined(ANDROID_NDK)
|
||||||
LOGD("Starting InputThread...");
|
LOGD("Starting InputThread...");
|
||||||
m_InputThread = new cThread( InputThread, this, "cRoot::InputThread");
|
try
|
||||||
m_InputThread->Start( false); // We should NOT wait? Otherwise we can't stop the server from other threads than the input thread
|
{
|
||||||
|
m_InputThread = std::thread(InputThread, std::ref(*this));
|
||||||
|
m_InputThread.detach();
|
||||||
|
}
|
||||||
|
catch (std::system_error & a_Exception)
|
||||||
|
{
|
||||||
|
LOGERROR("cRoot::Start (std::thread) error %i: could not construct input thread; %s", a_Exception.code().value(), a_Exception.what());
|
||||||
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
long long finishmseconds = Time.GetNowTime();
|
LOG("Startup complete, took %lld ms!", std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::steady_clock::now() - BeginTime).count());
|
||||||
finishmseconds -= mseconds;
|
|
||||||
|
|
||||||
LOG("Startup complete, took %lld ms!", finishmseconds);
|
|
||||||
#ifdef _WIN32
|
#ifdef _WIN32
|
||||||
EnableMenuItem(hmenu, SC_CLOSE, MF_ENABLED); // Re-enable close button
|
EnableMenuItem(hmenu, SC_CLOSE, MF_ENABLED); // Re-enable close button
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
while (!m_bStop && !m_bRestart && !m_TerminateEventRaised) // These are modified by external threads
|
while (!m_bStop && !m_bRestart && !m_TerminateEventRaised) // These are modified by external threads
|
||||||
{
|
{
|
||||||
cSleep::MilliSleep(1000);
|
std::this_thread::sleep_for(std::chrono::seconds(1));
|
||||||
}
|
}
|
||||||
|
|
||||||
if (m_TerminateEventRaised)
|
if (m_TerminateEventRaised)
|
||||||
@ -213,10 +211,6 @@ void cRoot::Start(void)
|
|||||||
m_bStop = true;
|
m_bStop = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
#if !defined(ANDROID_NDK)
|
|
||||||
delete m_InputThread; m_InputThread = nullptr;
|
|
||||||
#endif
|
|
||||||
|
|
||||||
// Stop the server:
|
// Stop the server:
|
||||||
m_WebAdmin->Stop();
|
m_WebAdmin->Stop();
|
||||||
LOG("Shutting down server...");
|
LOG("Shutting down server...");
|
||||||
|
@ -6,6 +6,7 @@
|
|||||||
#include "HTTPServer/HTTPServer.h"
|
#include "HTTPServer/HTTPServer.h"
|
||||||
#include "Defines.h"
|
#include "Defines.h"
|
||||||
#include "RankManager.h"
|
#include "RankManager.h"
|
||||||
|
#include <thread>
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@ -177,7 +178,7 @@ private:
|
|||||||
cCriticalSection m_CSPendingCommands;
|
cCriticalSection m_CSPendingCommands;
|
||||||
cCommandQueue m_PendingCommands;
|
cCommandQueue m_PendingCommands;
|
||||||
|
|
||||||
cThread * m_InputThread;
|
std::thread m_InputThread;
|
||||||
|
|
||||||
cServer * m_Server;
|
cServer * m_Server;
|
||||||
cMonsterConfig * m_MonsterConfig;
|
cMonsterConfig * m_MonsterConfig;
|
||||||
@ -212,10 +213,10 @@ private:
|
|||||||
|
|
||||||
/// Does the actual work of executing a command
|
/// Does the actual work of executing a command
|
||||||
void DoExecuteConsoleCommand(const AString & a_Cmd);
|
void DoExecuteConsoleCommand(const AString & a_Cmd);
|
||||||
|
|
||||||
static void InputThread(void* a_Params);
|
|
||||||
|
|
||||||
static cRoot* s_Root;
|
static cRoot* s_Root;
|
||||||
|
|
||||||
|
static void InputThread(cRoot & a_Params);
|
||||||
}; // tolua_export
|
}; // tolua_export
|
||||||
|
|
||||||
|
|
||||||
|
@ -4,7 +4,6 @@
|
|||||||
|
|
||||||
#include "Server.h"
|
#include "Server.h"
|
||||||
#include "ClientHandle.h"
|
#include "ClientHandle.h"
|
||||||
#include "OSSupport/Timer.h"
|
|
||||||
#include "Mobs/Monster.h"
|
#include "Mobs/Monster.h"
|
||||||
#include "OSSupport/Socket.h"
|
#include "OSSupport/Socket.h"
|
||||||
#include "Root.h"
|
#include "Root.h"
|
||||||
@ -20,8 +19,6 @@
|
|||||||
#include "Protocol/ProtocolRecognizer.h"
|
#include "Protocol/ProtocolRecognizer.h"
|
||||||
#include "CommandOutput.h"
|
#include "CommandOutput.h"
|
||||||
|
|
||||||
#include "MersenneTwister.h"
|
|
||||||
|
|
||||||
#include "IniFile.h"
|
#include "IniFile.h"
|
||||||
#include "Vector3.h"
|
#include "Vector3.h"
|
||||||
|
|
||||||
@ -75,22 +72,19 @@ cServer::cTickThread::cTickThread(cServer & a_Server) :
|
|||||||
|
|
||||||
void cServer::cTickThread::Execute(void)
|
void cServer::cTickThread::Execute(void)
|
||||||
{
|
{
|
||||||
cTimer Timer;
|
auto LastTime = std::chrono::steady_clock::now();
|
||||||
|
static const auto msPerTick = std::chrono::milliseconds(50);
|
||||||
long long msPerTick = 50;
|
|
||||||
long long LastTime = Timer.GetNowTime();
|
|
||||||
|
|
||||||
while (!m_ShouldTerminate)
|
while (!m_ShouldTerminate)
|
||||||
{
|
{
|
||||||
long long NowTime = Timer.GetNowTime();
|
auto NowTime = std::chrono::steady_clock::now();
|
||||||
float DeltaTime = (float)(NowTime-LastTime);
|
m_ShouldTerminate = !m_Server.Tick(static_cast<float>(std::chrono::duration_cast<std::chrono::milliseconds>(NowTime - LastTime).count()));
|
||||||
m_ShouldTerminate = !m_Server.Tick(DeltaTime);
|
auto TickTime = std::chrono::steady_clock::now() - NowTime;
|
||||||
long long TickTime = Timer.GetNowTime() - NowTime;
|
|
||||||
|
|
||||||
if (TickTime < msPerTick)
|
if (TickTime < msPerTick)
|
||||||
{
|
{
|
||||||
// Stretch tick time until it's at least msPerTick
|
// Stretch tick time until it's at least msPerTick
|
||||||
cSleep::MilliSleep((unsigned int)(msPerTick - TickTime));
|
std::this_thread::sleep_for(msPerTick - TickTime);
|
||||||
}
|
}
|
||||||
|
|
||||||
LastTime = NowTime;
|
LastTime = NowTime;
|
||||||
|
@ -2322,7 +2322,7 @@ cItem * cSlotAreaTemporary::GetPlayerSlots(cPlayer & a_Player)
|
|||||||
{
|
{
|
||||||
return nullptr;
|
return nullptr;
|
||||||
}
|
}
|
||||||
return &(itr->second[0]);
|
return itr->second.data();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -11,7 +11,6 @@
|
|||||||
#include "IniFile.h"
|
#include "IniFile.h"
|
||||||
#include "ChunkMap.h"
|
#include "ChunkMap.h"
|
||||||
#include "Generating/ChunkDesc.h"
|
#include "Generating/ChunkDesc.h"
|
||||||
#include "OSSupport/Timer.h"
|
|
||||||
#include "SetChunkData.h"
|
#include "SetChunkData.h"
|
||||||
|
|
||||||
// Serializers
|
// Serializers
|
||||||
@ -45,7 +44,6 @@
|
|||||||
#include "MobCensus.h"
|
#include "MobCensus.h"
|
||||||
#include "MobSpawner.h"
|
#include "MobSpawner.h"
|
||||||
|
|
||||||
#include "MersenneTwister.h"
|
|
||||||
#include "Generating/Trees.h"
|
#include "Generating/Trees.h"
|
||||||
#include "Bindings/PluginManager.h"
|
#include "Bindings/PluginManager.h"
|
||||||
#include "Blocks/BlockHandler.h"
|
#include "Blocks/BlockHandler.h"
|
||||||
@ -92,7 +90,6 @@ public:
|
|||||||
m_PrepareDistance(a_PrepareDistance),
|
m_PrepareDistance(a_PrepareDistance),
|
||||||
m_MaxIdx(a_PrepareDistance * a_PrepareDistance),
|
m_MaxIdx(a_PrepareDistance * a_PrepareDistance),
|
||||||
m_NumPrepared(0),
|
m_NumPrepared(0),
|
||||||
m_LastReportTime(0),
|
|
||||||
m_LastReportChunkCount(0)
|
m_LastReportChunkCount(0)
|
||||||
{
|
{
|
||||||
// Start the thread:
|
// Start the thread:
|
||||||
@ -113,7 +110,7 @@ public:
|
|||||||
m_MaxIdx = m_PrepareDistance * m_PrepareDistance;
|
m_MaxIdx = m_PrepareDistance * m_PrepareDistance;
|
||||||
int maxQueue = std::min(m_MaxIdx - 1, 100); // Number of chunks to queue at once
|
int maxQueue = std::min(m_MaxIdx - 1, 100); // Number of chunks to queue at once
|
||||||
m_NextIdx = maxQueue;
|
m_NextIdx = maxQueue;
|
||||||
m_LastReportTime = m_Timer.GetNowTime();
|
m_LastReportTime = std::chrono::steady_clock::now();
|
||||||
for (int i = 0; i < maxQueue; i++)
|
for (int i = 0; i < maxQueue; i++)
|
||||||
{
|
{
|
||||||
int chunkX, chunkZ;
|
int chunkX, chunkZ;
|
||||||
@ -146,16 +143,12 @@ protected:
|
|||||||
/** Event used to signal that the preparation is finished. */
|
/** Event used to signal that the preparation is finished. */
|
||||||
cEvent m_EvtFinished;
|
cEvent m_EvtFinished;
|
||||||
|
|
||||||
/** The timer used to report progress every second. */
|
|
||||||
cTimer m_Timer;
|
|
||||||
|
|
||||||
/** The timestamp of the last progress report emitted. */
|
/** The timestamp of the last progress report emitted. */
|
||||||
long long m_LastReportTime;
|
std::chrono::steady_clock::time_point m_LastReportTime;
|
||||||
|
|
||||||
/** Number of chunks prepared when the last progress report was emitted. */
|
/** Number of chunks prepared when the last progress report was emitted. */
|
||||||
int m_LastReportChunkCount;
|
int m_LastReportChunkCount;
|
||||||
|
|
||||||
|
|
||||||
// cChunkCoordCallback override:
|
// cChunkCoordCallback override:
|
||||||
virtual void Call(int a_ChunkX, int a_ChunkZ)
|
virtual void Call(int a_ChunkX, int a_ChunkZ)
|
||||||
{
|
{
|
||||||
@ -178,15 +171,15 @@ protected:
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Report progress every 1 second:
|
// Report progress every 1 second:
|
||||||
long long now = m_Timer.GetNowTime();
|
auto Now = std::chrono::steady_clock::now();
|
||||||
if (now - m_LastReportTime > 1000)
|
if (Now - m_LastReportTime > std::chrono::seconds(1))
|
||||||
{
|
{
|
||||||
float percentDone = static_cast<float>(m_NumPrepared * 100) / m_MaxIdx;
|
float PercentDone = static_cast<float>(m_NumPrepared * 100) / m_MaxIdx;
|
||||||
float chunkSpeed = static_cast<float>((m_NumPrepared - m_LastReportChunkCount) * 1000) / (now - m_LastReportTime);
|
float ChunkSpeed = static_cast<float>((m_NumPrepared - m_LastReportChunkCount) * 1000) / std::chrono::duration_cast<std::chrono::milliseconds>(Now - m_LastReportTime).count();
|
||||||
LOG("Preparing spawn (%s): %.02f%% done (%d chunks out of %d; %.02f chunks / sec)",
|
LOG("Preparing spawn (%s): %.02f%% (%d/%d; %.02f chunks/s)",
|
||||||
m_World.GetName().c_str(), percentDone, m_NumPrepared, m_MaxIdx, chunkSpeed
|
m_World.GetName().c_str(), PercentDone, m_NumPrepared, m_MaxIdx, ChunkSpeed
|
||||||
);
|
);
|
||||||
m_LastReportTime = now;
|
m_LastReportTime = Now;
|
||||||
m_LastReportChunkCount = m_NumPrepared;
|
m_LastReportChunkCount = m_NumPrepared;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -239,23 +232,20 @@ cWorld::cTickThread::cTickThread(cWorld & a_World) :
|
|||||||
|
|
||||||
void cWorld::cTickThread::Execute(void)
|
void cWorld::cTickThread::Execute(void)
|
||||||
{
|
{
|
||||||
cTimer Timer;
|
auto LastTime = std::chrono::steady_clock::now();
|
||||||
|
static const auto msPerTick = std::chrono::milliseconds(50);
|
||||||
|
auto TickTime = std::chrono::steady_clock::duration(50);
|
||||||
|
|
||||||
const Int64 msPerTick = 50;
|
|
||||||
Int64 LastTime = Timer.GetNowTime();
|
|
||||||
|
|
||||||
Int64 TickDuration = 50;
|
|
||||||
while (!m_ShouldTerminate)
|
while (!m_ShouldTerminate)
|
||||||
{
|
{
|
||||||
Int64 NowTime = Timer.GetNowTime();
|
auto NowTime = std::chrono::steady_clock::now();
|
||||||
float DeltaTime = (float)(NowTime - LastTime);
|
m_World.Tick(static_cast<float>(std::chrono::duration_cast<std::chrono::milliseconds>(NowTime - LastTime).count()), std::chrono::duration_cast<std::chrono::duration<int>>(TickTime).count());
|
||||||
m_World.Tick(DeltaTime, (int)TickDuration);
|
TickTime = std::chrono::steady_clock::now() - NowTime;
|
||||||
TickDuration = Timer.GetNowTime() - NowTime;
|
|
||||||
|
|
||||||
if (TickDuration < msPerTick)
|
if (TickTime < msPerTick)
|
||||||
{
|
{
|
||||||
// Stretch tick time until it's at least msPerTick
|
// Stretch tick time until it's at least msPerTick
|
||||||
cSleep::MilliSleep((unsigned int)(msPerTick - TickDuration));
|
std::this_thread::sleep_for(msPerTick - TickTime);
|
||||||
}
|
}
|
||||||
|
|
||||||
LastTime = NowTime;
|
LastTime = NowTime;
|
||||||
|
@ -10,7 +10,6 @@
|
|||||||
#define MAX_PLAYERS 65535
|
#define MAX_PLAYERS 65535
|
||||||
|
|
||||||
#include "Simulator/SimulatorManager.h"
|
#include "Simulator/SimulatorManager.h"
|
||||||
#include "MersenneTwister.h"
|
|
||||||
#include "ChunkMap.h"
|
#include "ChunkMap.h"
|
||||||
#include "WorldStorage/WorldStorage.h"
|
#include "WorldStorage/WorldStorage.h"
|
||||||
#include "Generating/ChunkGenerator.h"
|
#include "Generating/ChunkGenerator.h"
|
||||||
@ -26,6 +25,7 @@
|
|||||||
#include "MapManager.h"
|
#include "MapManager.h"
|
||||||
#include "Blocks/WorldInterface.h"
|
#include "Blocks/WorldInterface.h"
|
||||||
#include "Blocks/BroadcastInterface.h"
|
#include "Blocks/BroadcastInterface.h"
|
||||||
|
#include "FastRandom.h"
|
||||||
#include "ClientHandle.h"
|
#include "ClientHandle.h"
|
||||||
|
|
||||||
|
|
||||||
|
@ -22,11 +22,11 @@ void cFireworkItem::WriteToNBTCompound(const cFireworkItem & a_FireworkItem, cFa
|
|||||||
a_Writer.AddByte("Type", a_FireworkItem.m_Type);
|
a_Writer.AddByte("Type", a_FireworkItem.m_Type);
|
||||||
if (!a_FireworkItem.m_Colours.empty())
|
if (!a_FireworkItem.m_Colours.empty())
|
||||||
{
|
{
|
||||||
a_Writer.AddIntArray("Colors", &(a_FireworkItem.m_Colours[0]), a_FireworkItem.m_Colours.size());
|
a_Writer.AddIntArray("Colors", a_FireworkItem.m_Colours.data(), a_FireworkItem.m_Colours.size());
|
||||||
}
|
}
|
||||||
if (!a_FireworkItem.m_FadeColours.empty())
|
if (!a_FireworkItem.m_FadeColours.empty())
|
||||||
{
|
{
|
||||||
a_Writer.AddIntArray("FadeColors", &(a_FireworkItem.m_FadeColours[0]), a_FireworkItem.m_FadeColours.size());
|
a_Writer.AddIntArray("FadeColors", a_FireworkItem.m_FadeColours.data(), a_FireworkItem.m_FadeColours.size());
|
||||||
}
|
}
|
||||||
a_Writer.EndCompound();
|
a_Writer.EndCompound();
|
||||||
a_Writer.EndList();
|
a_Writer.EndList();
|
||||||
@ -41,11 +41,11 @@ void cFireworkItem::WriteToNBTCompound(const cFireworkItem & a_FireworkItem, cFa
|
|||||||
a_Writer.AddByte("Type", a_FireworkItem.m_Type);
|
a_Writer.AddByte("Type", a_FireworkItem.m_Type);
|
||||||
if (!a_FireworkItem.m_Colours.empty())
|
if (!a_FireworkItem.m_Colours.empty())
|
||||||
{
|
{
|
||||||
a_Writer.AddIntArray("Colors", &(a_FireworkItem.m_Colours[0]), a_FireworkItem.m_Colours.size());
|
a_Writer.AddIntArray("Colors", a_FireworkItem.m_Colours.data(), a_FireworkItem.m_Colours.size());
|
||||||
}
|
}
|
||||||
if (!a_FireworkItem.m_FadeColours.empty())
|
if (!a_FireworkItem.m_FadeColours.empty())
|
||||||
{
|
{
|
||||||
a_Writer.AddIntArray("FadeColors", &(a_FireworkItem.m_FadeColours[0]), a_FireworkItem.m_FadeColours.size());
|
a_Writer.AddIntArray("FadeColors", a_FireworkItem.m_FadeColours.data(), a_FireworkItem.m_FadeColours.size());
|
||||||
}
|
}
|
||||||
a_Writer.EndCompound();
|
a_Writer.EndCompound();
|
||||||
break;
|
break;
|
||||||
|
@ -112,7 +112,7 @@ void cMapSerializer::SaveMapToNBT(cFastNBTWriter & a_Writer)
|
|||||||
a_Writer.AddInt("zCenter", m_Map->GetCenterZ());
|
a_Writer.AddInt("zCenter", m_Map->GetCenterZ());
|
||||||
|
|
||||||
const cMap::cColorList & Data = m_Map->GetData();
|
const cMap::cColorList & Data = m_Map->GetData();
|
||||||
a_Writer.AddByteArray("colors", (char *) &Data[0], Data.size());
|
a_Writer.AddByteArray("colors", (char *)Data.data(), Data.size());
|
||||||
|
|
||||||
a_Writer.EndCompound();
|
a_Writer.EndCompound();
|
||||||
}
|
}
|
||||||
@ -190,7 +190,7 @@ bool cMapSerializer::LoadMapFromNBT(const cParsedNBT & a_NBT)
|
|||||||
CurrLine = a_NBT.FindChildByName(Data, "colors");
|
CurrLine = a_NBT.FindChildByName(Data, "colors");
|
||||||
if ((CurrLine >= 0) && (a_NBT.GetType(CurrLine) == TAG_ByteArray))
|
if ((CurrLine >= 0) && (a_NBT.GetType(CurrLine) == TAG_ByteArray))
|
||||||
{
|
{
|
||||||
memcpy(&m_Map->m_Data[0], a_NBT.GetData(CurrLine), NumPixels);
|
memcpy(m_Map->m_Data.data(), a_NBT.GetData(CurrLine), NumPixels);
|
||||||
}
|
}
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
|
@ -163,9 +163,9 @@ BOOL CtrlHandler(DWORD fdwCtrlType)
|
|||||||
cRoot::m_TerminateEventRaised = true;
|
cRoot::m_TerminateEventRaised = true;
|
||||||
LOGD("Terminate event raised from the Windows CtrlHandler");
|
LOGD("Terminate event raised from the Windows CtrlHandler");
|
||||||
|
|
||||||
if (fdwCtrlType == CTRL_CLOSE_EVENT) // Console window closed via 'x' button, Windows will try to close immediately, therefore...
|
while (!g_ServerTerminated)
|
||||||
{
|
{
|
||||||
while (!g_ServerTerminated) { cSleep::MilliSleep(100); } // Delay as much as possible to try to get the server to shut down cleanly
|
std::this_thread::sleep_for(std::chrono::milliseconds(50)); // Delay as much as possible to try to get the server to shut down cleanly
|
||||||
}
|
}
|
||||||
|
|
||||||
return TRUE;
|
return TRUE;
|
||||||
|
Loading…
Reference in New Issue
Block a user