From f44a291da8a735d00e311ae561dd2b1ba366e5ee Mon Sep 17 00:00:00 2001 From: madmaxoft Date: Fri, 11 Apr 2014 13:09:19 +0200 Subject: Fixed issues with 64-bit MSVC compilation. --- src/OSSupport/IsThread.cpp | 31 +++++++++++++++++++------------ src/OSSupport/IsThread.h | 2 +- src/OSSupport/Thread.cpp | 23 +++++++++++++++-------- 3 files changed, 35 insertions(+), 21 deletions(-) (limited to 'src/OSSupport') diff --git a/src/OSSupport/IsThread.cpp b/src/OSSupport/IsThread.cpp index 36205bcf1..04fc818e4 100644 --- a/src/OSSupport/IsThread.cpp +++ b/src/OSSupport/IsThread.cpp @@ -18,26 +18,33 @@ // Usage: SetThreadName (-1, "MainThread"); // -static void SetThreadName( DWORD dwThreadID, LPCSTR szThreadName) +// 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 { - struct - { - 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 - } 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 = szThreadName; + info.szName = threadName; info.dwThreadID = dwThreadID; info.dwFlags = 0; __try { - RaiseException(0x406D1388, 0, sizeof(info) / sizeof(DWORD), (DWORD *)&info); + RaiseException(MS_VC_EXCEPTION, 0, sizeof(info) / sizeof(ULONG_PTR), (ULONG_PTR *)&info); } - __except(EXCEPTION_CONTINUE_EXECUTION) + __except (EXCEPTION_EXECUTE_HANDLER) { } } diff --git a/src/OSSupport/IsThread.h b/src/OSSupport/IsThread.h index 42b8bfdda..57651a490 100644 --- a/src/OSSupport/IsThread.h +++ b/src/OSSupport/IsThread.h @@ -62,7 +62,7 @@ protected: HANDLE m_Handle; - static DWORD_PTR __stdcall thrExecute(LPVOID a_Param) + static DWORD __stdcall thrExecute(LPVOID a_Param) { // Create a window so that the thread can be identified by 3rd party tools: HWND IdentificationWnd = CreateWindow("STATIC", ((cIsThread *)a_Param)->m_ThreadName.c_str(), 0, 0, 0, 0, WS_OVERLAPPED, NULL, NULL, NULL, NULL); diff --git a/src/OSSupport/Thread.cpp b/src/OSSupport/Thread.cpp index 3df75f0e7..7a10ef8d2 100644 --- a/src/OSSupport/Thread.cpp +++ b/src/OSSupport/Thread.cpp @@ -10,27 +10,34 @@ // // 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 + 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) -void SetThreadName( DWORD dwThreadID, LPCSTR szThreadName) +static void SetThreadName(DWORD dwThreadID, const char * threadName) { THREADNAME_INFO info; info.dwType = 0x1000; - info.szName = szThreadName; + info.szName = threadName; info.dwThreadID = dwThreadID; info.dwFlags = 0; __try { - RaiseException( 0x406D1388, 0, sizeof(info)/sizeof(DWORD), (DWORD*)&info ); + RaiseException(MS_VC_EXCEPTION, 0, sizeof(info) / sizeof(ULONG_PTR), (ULONG_PTR *)&info); } - __except(EXCEPTION_CONTINUE_EXECUTION) + __except (EXCEPTION_EXECUTE_HANDLER) { } } -- cgit v1.2.3 From e47dab84f3f6606d6d6e95774b16d7d4e8d7daa6 Mon Sep 17 00:00:00 2001 From: archshift Date: Fri, 25 Apr 2014 09:56:15 -0700 Subject: Cmake generated projects for IDEs include headers in project files. --- src/OSSupport/CMakeLists.txt | 1 + 1 file changed, 1 insertion(+) (limited to 'src/OSSupport') diff --git a/src/OSSupport/CMakeLists.txt b/src/OSSupport/CMakeLists.txt index 497cd0ba3..dee60b450 100644 --- a/src/OSSupport/CMakeLists.txt +++ b/src/OSSupport/CMakeLists.txt @@ -5,6 +5,7 @@ project (MCServer) include_directories ("${PROJECT_SOURCE_DIR}/../") file(GLOB SOURCE "*.cpp" + "*.h" ) add_library(OSSupport ${SOURCE}) -- cgit v1.2.3 From 80b97fd9dd584c439ca25efaa7e5172da1509628 Mon Sep 17 00:00:00 2001 From: archshift Date: Fri, 25 Apr 2014 20:49:55 -0700 Subject: Fixed mobs that don't naturally spawn. --- src/OSSupport/File.cpp | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) (limited to 'src/OSSupport') diff --git a/src/OSSupport/File.cpp b/src/OSSupport/File.cpp index 7f0f0ad2f..f1b3bcf9e 100644 --- a/src/OSSupport/File.cpp +++ b/src/OSSupport/File.cpp @@ -67,11 +67,11 @@ bool cFile::Open(const AString & iFileName, eMode iMode) case fmRead: Mode = "rb"; break; case fmWrite: Mode = "wb"; break; case fmReadWrite: Mode = "rb+"; break; - default: - { - ASSERT(!"Unhandled file mode"); - return false; - } + } + if (Mode == NULL) + { + ASSERT(!"Unhandled file mode"); + return false; } #ifdef _WIN32 -- cgit v1.2.3 From a1765a2896714c956deb8eefcde93af074901f8e Mon Sep 17 00:00:00 2001 From: jfhumann Date: Sat, 26 Apr 2014 14:23:53 +0200 Subject: Fixed unitialized member in gZipFile (CID 43673) --- src/OSSupport/GZipFile.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/OSSupport') diff --git a/src/OSSupport/GZipFile.cpp b/src/OSSupport/GZipFile.cpp index 7a8433f4f..22d887783 100644 --- a/src/OSSupport/GZipFile.cpp +++ b/src/OSSupport/GZipFile.cpp @@ -11,7 +11,7 @@ cGZipFile::cGZipFile(void) : - m_File(NULL) + m_File(NULL), m_Mode(fmRead) { } -- cgit v1.2.3 From f301d052cfa70d00f76c31fcad4f961040736a80 Mon Sep 17 00:00:00 2001 From: madmaxoft Date: Sun, 27 Apr 2014 22:20:49 +0200 Subject: cSocket creates the socket in Connect if no socket is present yet. --- src/OSSupport/Socket.cpp | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) (limited to 'src/OSSupport') diff --git a/src/OSSupport/Socket.cpp b/src/OSSupport/Socket.cpp index c29e495c3..98f694a79 100644 --- a/src/OSSupport/Socket.cpp +++ b/src/OSSupport/Socket.cpp @@ -295,7 +295,7 @@ bool cSocket::ConnectToLocalhostIPv4(unsigned short a_Port) bool cSocket::ConnectIPv4(const AString & a_HostNameOrAddr, unsigned short a_Port) { - // First try IP Address string to hostent conversion, because it's faster + // First try IP Address string to hostent conversion, because it's faster and local: unsigned long addr = inet_addr(a_HostNameOrAddr.c_str()); if (addr == INADDR_NONE) { @@ -307,10 +307,16 @@ bool cSocket::ConnectIPv4(const AString & a_HostNameOrAddr, unsigned short a_Por CloseSocket(); return false; } - // Should be optimised to a single word copy memcpy(&addr, hp->h_addr, hp->h_length); } + // If the socket is not created yet, create one: + if (!IsValid()) + { + m_Socket = socket((int)IPv4, SOCK_STREAM, 0); + } + + // Connect the socket: sockaddr_in server; server.sin_addr.s_addr = addr; server.sin_family = AF_INET; -- cgit v1.2.3 From a42480cf8266a1e066209823aab42c9a3829adbc Mon Sep 17 00:00:00 2001 From: Tycho Date: Mon, 28 Apr 2014 10:31:07 -0700 Subject: Template Magic Removed need to allocate a fake meta block by using templates to provide a version of the code that does not use metas. Also changed the function to a template argument to make sure that the compilier is able to inline it. --- src/OSSupport/File.cpp | 4 ++-- src/OSSupport/File.h | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) (limited to 'src/OSSupport') diff --git a/src/OSSupport/File.cpp b/src/OSSupport/File.cpp index f1b3bcf9e..33b9cfc3f 100644 --- a/src/OSSupport/File.cpp +++ b/src/OSSupport/File.cpp @@ -143,7 +143,7 @@ bool cFile::IsEOF(void) const -int cFile::Read (void * iBuffer, int iNumBytes) +int cFile::Read (void * iBuffer, size_t iNumBytes) { ASSERT(IsOpen()); @@ -159,7 +159,7 @@ int cFile::Read (void * iBuffer, int iNumBytes) -int cFile::Write(const void * iBuffer, int iNumBytes) +int cFile::Write(const void * iBuffer, size_t iNumBytes) { ASSERT(IsOpen()); diff --git a/src/OSSupport/File.h b/src/OSSupport/File.h index b394c5cb9..2a7ecf0ed 100644 --- a/src/OSSupport/File.h +++ b/src/OSSupport/File.h @@ -80,10 +80,10 @@ public: bool IsEOF(void) const; /** Reads up to iNumBytes bytes into iBuffer, returns the number of bytes actually read, or -1 on failure; asserts if not open */ - int Read (void * iBuffer, int iNumBytes); + int Read (void * iBuffer, size_t iNumBytes); /** Writes up to iNumBytes bytes from iBuffer, returns the number of bytes actually written, or -1 on failure; asserts if not open */ - int Write(const void * iBuffer, int iNumBytes); + int Write(const void * iBuffer, size_t iNumBytes); /** Seeks to iPosition bytes from file start, returns old position or -1 for failure; asserts if not open */ int Seek (int iPosition); -- cgit v1.2.3 From 58929a994830329120c68bf43cf6fcb8a90be960 Mon Sep 17 00:00:00 2001 From: madmaxoft Date: Wed, 30 Apr 2014 17:06:31 +0200 Subject: Removed the unused cBlockingTCPLink class. --- src/OSSupport/BlockingTCPLink.cpp | 142 -------------------------------------- src/OSSupport/BlockingTCPLink.h | 28 -------- 2 files changed, 170 deletions(-) delete mode 100644 src/OSSupport/BlockingTCPLink.cpp delete mode 100644 src/OSSupport/BlockingTCPLink.h (limited to 'src/OSSupport') diff --git a/src/OSSupport/BlockingTCPLink.cpp b/src/OSSupport/BlockingTCPLink.cpp deleted file mode 100644 index 07f48b955..000000000 --- a/src/OSSupport/BlockingTCPLink.cpp +++ /dev/null @@ -1,142 +0,0 @@ - -#include "Globals.h" // NOTE: MSVC stupidness requires this to be the same across all modules - -#include "BlockingTCPLink.h" -#include "Errors.h" - - - - -cBlockingTCPLink::cBlockingTCPLink(void) -{ -} - - - - - -cBlockingTCPLink::~cBlockingTCPLink() -{ - CloseSocket(); -} - - - - - -void cBlockingTCPLink::CloseSocket() -{ - if (!m_Socket.IsValid()) - { - m_Socket.CloseSocket(); - } -} - - - - - -bool cBlockingTCPLink::Connect(const char * iAddress, unsigned int iPort) -{ - ASSERT(!m_Socket.IsValid()); - if (m_Socket.IsValid()) - { - LOGWARN("WARNING: cTCPLink Connect() called while still connected."); - m_Socket.CloseSocket(); - } - - struct hostent *hp; - unsigned int addr; - struct sockaddr_in server; - - m_Socket = socket(AF_INET, SOCK_STREAM, 0); - if (!m_Socket.IsValid()) - { - LOGERROR("cTCPLink: Cannot create a socket"); - return false; - } - - addr = inet_addr(iAddress); - hp = gethostbyaddr((char *)&addr, sizeof(addr), AF_INET); - if (hp == NULL) - { - //LOGWARN("cTCPLink: gethostbyaddr returned NULL"); - hp = gethostbyname(iAddress); - if (hp == NULL) - { - LOGWARN("cTCPLink: Could not resolve %s", iAddress); - CloseSocket(); - return false; - } - } - - memcpy(&server.sin_addr.s_addr,hp->h_addr, hp->h_length); - server.sin_family = AF_INET; - server.sin_port = htons( (unsigned short)iPort); - if (connect(m_Socket, (struct sockaddr *)&server, sizeof(server))) - { - LOGWARN("cTCPLink: Connection to \"%s:%d\" failed (%s)", iAddress, iPort,GetOSErrorString( cSocket::GetLastError() ).c_str() ); - CloseSocket(); - return false; - } - - return true; -} - - - - - -int cBlockingTCPLink::Send(char * a_Data, unsigned int a_Size, int a_Flags /* = 0 */ ) -{ - UNUSED(a_Flags); - - ASSERT(m_Socket.IsValid()); - if (!m_Socket.IsValid()) - { - LOGERROR("cBlockingTCPLink: Trying to send data without a valid connection!"); - return -1; - } - return m_Socket.Send(a_Data, a_Size); -} - - - - - -int cBlockingTCPLink::SendMessage( const char* a_Message, int a_Flags /* = 0 */ ) -{ - UNUSED(a_Flags); - - ASSERT(m_Socket.IsValid()); - if (!m_Socket.IsValid()) - { - LOGWARN("cBlockingTCPLink: Trying to send message without a valid connection!"); - return -1; - } - return m_Socket.Send(a_Message, strlen(a_Message)); -} - - - - - -void cBlockingTCPLink::ReceiveData(AString & oData) -{ - ASSERT(m_Socket.IsValid()); - if (!m_Socket.IsValid()) - { - return; - } - - int Received = 0; - char Buffer[256]; - while ((Received = recv(m_Socket, Buffer, sizeof(Buffer), 0)) > 0) - { - oData.append(Buffer, Received); - } -} - - - - diff --git a/src/OSSupport/BlockingTCPLink.h b/src/OSSupport/BlockingTCPLink.h deleted file mode 100644 index cb5f9e3f4..000000000 --- a/src/OSSupport/BlockingTCPLink.h +++ /dev/null @@ -1,28 +0,0 @@ - -#pragma once - -#include "Socket.h" - - - - - -class cBlockingTCPLink // tolua_export -{ // tolua_export -public: // tolua_export - cBlockingTCPLink(void); // tolua_export - ~cBlockingTCPLink(); // tolua_export - - bool Connect( const char* a_Address, unsigned int a_Port ); // tolua_export - int Send( char* a_Data, unsigned int a_Size, int a_Flags = 0 ); // tolua_export - int SendMessage( const char* a_Message, int a_Flags = 0 ); // tolua_export - void CloseSocket(); // tolua_export - void ReceiveData(AString & oData); // tolua_export -protected: - - cSocket m_Socket; -}; // tolua_export - - - - -- cgit v1.2.3 From 92c022c1405d32fa851a06958a55ea714c62eccf Mon Sep 17 00:00:00 2001 From: madmaxoft Date: Fri, 2 May 2014 20:55:50 +0200 Subject: Open files in shared mode on windows, so that other tools may read them. This was the behavior before fopen_s() was used for implementation; unlike fopen(), fopen_s() opens the file in exclusive mode. --- src/OSSupport/File.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'src/OSSupport') diff --git a/src/OSSupport/File.cpp b/src/OSSupport/File.cpp index 33b9cfc3f..8c24fa541 100644 --- a/src/OSSupport/File.cpp +++ b/src/OSSupport/File.cpp @@ -75,7 +75,7 @@ bool cFile::Open(const AString & iFileName, eMode iMode) } #ifdef _WIN32 - fopen_s(&m_File, (FILE_IO_PREFIX + iFileName).c_str(), Mode); + m_File = _fsopen((FILE_IO_PREFIX + iFileName).c_str(), Mode, _SH_DENYWR); #else m_File = fopen((FILE_IO_PREFIX + iFileName).c_str(), Mode); #endif // _WIN32 @@ -88,7 +88,7 @@ bool cFile::Open(const AString & iFileName, eMode iMode) // Simply re-open for read-writing, erasing existing contents: #ifdef _WIN32 - fopen_s(&m_File, (FILE_IO_PREFIX + iFileName).c_str(), "wb+"); + m_File = _fsopen((FILE_IO_PREFIX + iFileName).c_str(), "wb+", _SH_DENYWR); #else m_File = fopen((FILE_IO_PREFIX + iFileName).c_str(), "wb+"); #endif // _WIN32 -- cgit v1.2.3