summaryrefslogtreecommitdiffstats
path: root/src/OSSupport
diff options
context:
space:
mode:
authorHowaner <franzi.moos@googlemail.com>2014-11-18 14:56:32 +0100
committerHowaner <franzi.moos@googlemail.com>2014-11-18 14:56:32 +0100
commit42120e2ea5db0cdb9920ff1c5efef33e0f496d48 (patch)
tree20ba1ae0a53f757cb8814b6cd6a466fe5acf1308 /src/OSSupport
parentFixed compile errors. (diff)
parentMerge pull request #1598 from mc-server/SignEditor (diff)
downloadcuberite-42120e2ea5db0cdb9920ff1c5efef33e0f496d48.tar
cuberite-42120e2ea5db0cdb9920ff1c5efef33e0f496d48.tar.gz
cuberite-42120e2ea5db0cdb9920ff1c5efef33e0f496d48.tar.bz2
cuberite-42120e2ea5db0cdb9920ff1c5efef33e0f496d48.tar.lz
cuberite-42120e2ea5db0cdb9920ff1c5efef33e0f496d48.tar.xz
cuberite-42120e2ea5db0cdb9920ff1c5efef33e0f496d48.tar.zst
cuberite-42120e2ea5db0cdb9920ff1c5efef33e0f496d48.zip
Diffstat (limited to '')
-rw-r--r--src/OSSupport/CMakeLists.txt4
-rw-r--r--src/OSSupport/Errors.cpp4
-rw-r--r--src/OSSupport/Event.cpp53
-rw-r--r--src/OSSupport/Event.h4
-rw-r--r--src/OSSupport/File.cpp22
-rw-r--r--src/OSSupport/GZipFile.cpp14
-rw-r--r--src/OSSupport/IsThread.h36
-rw-r--r--src/OSSupport/ListenThread.cpp2
-rw-r--r--src/OSSupport/Semaphore.cpp4
-rw-r--r--src/OSSupport/Socket.cpp2
-rw-r--r--src/OSSupport/SocketThreads.cpp14
-rw-r--r--src/OSSupport/SocketThreads.h2
12 files changed, 106 insertions, 55 deletions
diff --git a/src/OSSupport/CMakeLists.txt b/src/OSSupport/CMakeLists.txt
index a42fcbed4..c3eabeef6 100644
--- a/src/OSSupport/CMakeLists.txt
+++ b/src/OSSupport/CMakeLists.txt
@@ -39,6 +39,10 @@ if(NOT MSVC)
add_library(OSSupport ${SRCS} ${HDRS})
if(UNIX)
+ if(NOT APPLE)
+ target_link_libraries(OSSupport rt)
+ endif()
+
target_link_libraries(OSSupport pthread)
endif()
endif()
diff --git a/src/OSSupport/Errors.cpp b/src/OSSupport/Errors.cpp
index 9401ec257..a5361e1a6 100644
--- a/src/OSSupport/Errors.cpp
+++ b/src/OSSupport/Errors.cpp
@@ -10,7 +10,7 @@ AString GetOSErrorString( int a_ErrNo)
#ifdef _WIN32
- FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM, NULL, a_ErrNo, 0, buffer, ARRAYCOUNT(buffer), NULL);
+ FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM, nullptr, a_ErrNo, 0, buffer, ARRAYCOUNT(buffer), nullptr);
Printf(Out, "%d: %s", a_ErrNo, buffer);
if (!Out.empty() && (Out[Out.length() - 1] == '\n'))
{
@@ -25,7 +25,7 @@ AString GetOSErrorString( int a_ErrNo)
#if !defined(__APPLE__) && ( _GNU_SOURCE) && !defined(ANDROID_NDK) // GNU version of strerror_r()
char * res = strerror_r( errno, buffer, ARRAYCOUNT(buffer));
- if (res != NULL)
+ if (res != nullptr)
{
Printf(Out, "%d: %s", a_ErrNo, res);
return Out;
diff --git a/src/OSSupport/Event.cpp b/src/OSSupport/Event.cpp
index 74f823216..87bc110ce 100644
--- a/src/OSSupport/Event.cpp
+++ b/src/OSSupport/Event.cpp
@@ -15,8 +15,8 @@
cEvent::cEvent(void)
{
#ifdef _WIN32
- m_Event = CreateEvent(NULL, FALSE, FALSE, NULL);
- if (m_Event == NULL)
+ m_Event = CreateEvent(nullptr, FALSE, FALSE, nullptr);
+ if (m_Event == nullptr)
{
LOGERROR("cEvent: cannot create event, GLE = %u. Aborting server.", (unsigned)GetLastError());
abort();
@@ -71,7 +71,7 @@ cEvent::~cEvent()
{
sem_destroy(m_Event);
delete m_Event;
- m_Event = NULL;
+ m_Event = nullptr;
}
#endif
}
@@ -102,6 +102,53 @@ void cEvent::Wait(void)
+bool cEvent::Wait(int a_TimeoutMSec)
+{
+ #ifdef _WIN32
+ DWORD res = WaitForSingleObject(m_Event, (DWORD)a_TimeoutMSec);
+ switch (res)
+ {
+ case WAIT_OBJECT_0: return true; // Regular event signalled
+ case WAIT_TIMEOUT: return false; // Regular event timeout
+ default:
+ {
+ LOGWARN("cEvent: waiting for the event failed: %u, GLE = %u. Continuing, but server may be unstable.", (unsigned)res, (unsigned)GetLastError());
+ return false;
+ }
+ }
+ #else
+ // Get the current time:
+ timespec timeout;
+ if (clock_gettime(CLOCK_REALTIME, &timeout) == -1)
+ {
+ LOGWARN("cEvent: Getting current time failed: %i, err = %s. Continuing, but the server may be unstable.", errno, GetOSErrorString(errno).c_str());
+ return false;
+ }
+
+ // Add the specified timeout:
+ timeout.tv_sec += a_TimeoutMSec / 1000;
+ timeout.tv_nsec += (a_TimeoutMSec % 1000) * 1000000; // 1 msec = 1000000 usec
+
+ // Wait with timeout:
+ int res = sem_timedwait(m_Event, &timeout);
+ switch (res)
+ {
+ case 0: return true; // Regular event signalled
+ case ETIMEDOUT: return false; // Regular even timeout
+ default:
+ {
+ AString error = GetOSErrorString(errno);
+ LOGWARN("cEvent: waiting for the event failed: %i, err = %s. Continuing, but server may be unstable.", res, error.c_str());
+ return false;
+ }
+ }
+ #endif
+}
+
+
+
+
+
void cEvent::Set(void)
{
#ifdef _WIN32
diff --git a/src/OSSupport/Event.h b/src/OSSupport/Event.h
index 71f418c0c..e2fa65a05 100644
--- a/src/OSSupport/Event.h
+++ b/src/OSSupport/Event.h
@@ -24,6 +24,10 @@ public:
void Wait(void);
void Set (void);
+
+ /** Waits for the event until either it is signalled, or the (relative) timeout is passed.
+ Returns true if the event was signalled, false if the timeout was hit or there was an error. */
+ bool Wait(int a_TimeoutMSec);
private:
diff --git a/src/OSSupport/File.cpp b/src/OSSupport/File.cpp
index cb6031da6..8957dfaef 100644
--- a/src/OSSupport/File.cpp
+++ b/src/OSSupport/File.cpp
@@ -17,7 +17,7 @@
cFile::cFile(void) :
#ifdef USE_STDIO_FILE
- m_File(NULL)
+ m_File(nullptr)
#else
m_File(INVALID_HANDLE_VALUE)
#endif // USE_STDIO_FILE
@@ -31,7 +31,7 @@ cFile::cFile(void) :
cFile::cFile(const AString & iFileName, eMode iMode) :
#ifdef USE_STDIO_FILE
- m_File(NULL)
+ m_File(nullptr)
#else
m_File(INVALID_HANDLE_VALUE)
#endif // USE_STDIO_FILE
@@ -64,7 +64,7 @@ bool cFile::Open(const AString & iFileName, eMode iMode)
Close();
}
- const char * Mode = NULL;
+ const char * Mode = nullptr;
switch (iMode)
{
case fmRead: Mode = "rb"; break;
@@ -72,7 +72,7 @@ bool cFile::Open(const AString & iFileName, eMode iMode)
case fmReadWrite: Mode = "rb+"; break;
case fmAppend: Mode = "a+"; break;
}
- if (Mode == NULL)
+ if (Mode == nullptr)
{
ASSERT(!"Unhandled file mode");
return false;
@@ -84,7 +84,7 @@ bool cFile::Open(const AString & iFileName, eMode iMode)
m_File = fopen((FILE_IO_PREFIX + iFileName).c_str(), Mode);
#endif // _WIN32
- if ((m_File == NULL) && (iMode == fmReadWrite))
+ if ((m_File == nullptr) && (iMode == fmReadWrite))
{
// Fix for MS not following C spec, opening "a" mode files for writing at the end only
// The file open operation has been tried with "read update", fails if file not found
@@ -98,7 +98,7 @@ bool cFile::Open(const AString & iFileName, eMode iMode)
#endif // _WIN32
}
- return (m_File != NULL);
+ return (m_File != nullptr);
}
@@ -114,7 +114,7 @@ void cFile::Close(void)
}
fclose(m_File);
- m_File = NULL;
+ m_File = nullptr;
}
@@ -123,7 +123,7 @@ void cFile::Close(void)
bool cFile::IsOpen(void) const
{
- return (m_File != NULL);
+ return (m_File != nullptr);
}
@@ -366,7 +366,7 @@ int cFile::GetSize(const AString & a_FileName)
bool cFile::CreateFolder(const AString & a_FolderPath)
{
#ifdef _WIN32
- return (CreateDirectoryA(a_FolderPath.c_str(), NULL) != 0);
+ return (CreateDirectoryA(a_FolderPath.c_str(), nullptr) != 0);
#else
return (mkdir(a_FolderPath.c_str(), S_IRWXU | S_IRWXG | S_IRWXO) == 0);
#endif
@@ -415,13 +415,13 @@ AStringVector cFile::GetFolderContents(const AString & a_Folder)
{
Folder = ".";
}
- if ((dp = opendir(Folder.c_str())) == NULL)
+ if ((dp = opendir(Folder.c_str())) == nullptr)
{
LOGERROR("Error (%i) opening directory \"%s\"\n", errno, Folder.c_str());
}
else
{
- while ((dirp = readdir(dp)) != NULL)
+ while ((dirp = readdir(dp)) != nullptr)
{
AllFiles.push_back(dirp->d_name);
}
diff --git a/src/OSSupport/GZipFile.cpp b/src/OSSupport/GZipFile.cpp
index 22d887783..32d84a0d5 100644
--- a/src/OSSupport/GZipFile.cpp
+++ b/src/OSSupport/GZipFile.cpp
@@ -11,7 +11,7 @@
cGZipFile::cGZipFile(void) :
- m_File(NULL), m_Mode(fmRead)
+ m_File(nullptr), m_Mode(fmRead)
{
}
@@ -30,14 +30,14 @@ cGZipFile::~cGZipFile()
bool cGZipFile::Open(const AString & a_FileName, eMode a_Mode)
{
- if (m_File != NULL)
+ if (m_File != nullptr)
{
ASSERT(!"A file is already open in this object");
return false;
}
m_File = gzopen(a_FileName.c_str(), (a_Mode == fmRead) ? "r" : "w");
m_Mode = a_Mode;
- return (m_File != NULL);
+ return (m_File != nullptr);
}
@@ -46,10 +46,10 @@ bool cGZipFile::Open(const AString & a_FileName, eMode a_Mode)
void cGZipFile::Close(void)
{
- if (m_File != NULL)
+ if (m_File != nullptr)
{
gzclose(m_File);
- m_File = NULL;
+ m_File = nullptr;
}
}
@@ -59,7 +59,7 @@ void cGZipFile::Close(void)
int cGZipFile::ReadRestOfFile(AString & a_Contents)
{
- if (m_File == NULL)
+ if (m_File == nullptr)
{
ASSERT(!"No file has been opened");
return -1;
@@ -90,7 +90,7 @@ int cGZipFile::ReadRestOfFile(AString & a_Contents)
bool cGZipFile::Write(const char * a_Contents, int a_Size)
{
- if (m_File == NULL)
+ if (m_File == nullptr)
{
ASSERT(!"No file has been opened");
return false;
diff --git a/src/OSSupport/IsThread.h b/src/OSSupport/IsThread.h
index 5de5f31c4..5c8d28d2d 100644
--- a/src/OSSupport/IsThread.h
+++ b/src/OSSupport/IsThread.h
@@ -31,20 +31,20 @@ protected:
/// The overriden Execute() method should check this value periodically and terminate if this is true
volatile bool m_ShouldTerminate;
-
+
public:
cIsThread(const AString & iThreadName);
virtual ~cIsThread();
-
+
/// Starts the thread; returns without waiting for the actual start
bool Start(void);
-
+
/// Signals the thread to terminate and waits until it's finished
void Stop(void);
-
+
/// Waits for the thread to finish. Doesn't signalize the ShouldTerminate flag
bool Wait(void);
-
+
/// Returns the OS-dependent thread ID for the caller's thread
static unsigned long GetCurrentID(void);
@@ -53,7 +53,7 @@ public:
protected:
AString m_ThreadName;
-
+
// Value used for "no handle":
#ifdef _WIN32
#define NULL_HANDLE NULL
@@ -62,34 +62,34 @@ protected:
#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)
{
- ((cIsThread *)a_Param)->Execute();
+ (static_cast<cIsThread *>(a_Param))->Execute();
return NULL;
}
-
+
#endif // else _WIN32
} ;
@@ -98,7 +98,3 @@ protected:
#endif // CISTHREAD_H_INCLUDED
-
-
-
-
diff --git a/src/OSSupport/ListenThread.cpp b/src/OSSupport/ListenThread.cpp
index 02e98acfc..b029634e9 100644
--- a/src/OSSupport/ListenThread.cpp
+++ b/src/OSSupport/ListenThread.cpp
@@ -214,7 +214,7 @@ void cListenThread::Execute(void)
timeval tv; // On Linux select() doesn't seem to wake up when socket is closed, so let's kinda busy-wait:
tv.tv_sec = 1;
tv.tv_usec = 0;
- if (select((int)Highest + 1, &fdRead, NULL, NULL, &tv) == -1)
+ if (select((int)Highest + 1, &fdRead, nullptr, nullptr, &tv) == -1)
{
LOG("select(R) call failed in cListenThread: \"%s\"", cSocket::GetLastErrorString().c_str());
continue;
diff --git a/src/OSSupport/Semaphore.cpp b/src/OSSupport/Semaphore.cpp
index c1fc7d9c7..6a2d57901 100644
--- a/src/OSSupport/Semaphore.cpp
+++ b/src/OSSupport/Semaphore.cpp
@@ -36,7 +36,7 @@ cSemaphore::cSemaphore( unsigned int a_MaxCount, unsigned int a_InitialCount /*
}
#else
m_Handle = CreateSemaphore(
- NULL, // security attribute
+ nullptr, // security attribute
a_InitialCount, // initial count
a_MaxCount, // maximum count
0 // name (optional)
@@ -98,7 +98,7 @@ void cSemaphore::Signal()
LOG("ERROR: Could not signal cSemaphore. (%i)", errno);
}
#else
- ReleaseSemaphore( m_Handle, 1, NULL);
+ ReleaseSemaphore( m_Handle, 1, nullptr);
#endif
}
diff --git a/src/OSSupport/Socket.cpp b/src/OSSupport/Socket.cpp
index c07d31c8b..5025a09ad 100644
--- a/src/OSSupport/Socket.cpp
+++ b/src/OSSupport/Socket.cpp
@@ -292,7 +292,7 @@ bool cSocket::ConnectIPv4(const AString & a_HostNameOrAddr, unsigned short a_Por
{
// It is not an IP Address string, but rather a regular hostname, resolve:
hostent * hp = gethostbyname(a_HostNameOrAddr.c_str());
- if (hp == NULL)
+ if (hp == nullptr)
{
LOGWARNING("%s: Could not resolve hostname \"%s\"", __FUNCTION__, a_HostNameOrAddr.c_str());
CloseSocket();
diff --git a/src/OSSupport/SocketThreads.cpp b/src/OSSupport/SocketThreads.cpp
index f436318a5..7a3ef4274 100644
--- a/src/OSSupport/SocketThreads.cpp
+++ b/src/OSSupport/SocketThreads.cpp
@@ -61,7 +61,7 @@ bool cSocketThreads::AddClient(const cSocket & a_Socket, cCallback * a_Client)
// There was an error launching the thread (but it was already logged along with the reason)
LOGERROR("A new cSocketThread failed to start");
delete Thread;
- Thread = NULL;
+ Thread = nullptr;
return false;
}
Thread->AddClient(a_Socket, a_Client);
@@ -233,7 +233,7 @@ bool cSocketThreads::cSocketThread::RemoveClient(const cCallback * a_Client)
// More data to send, shut down reading and wait for the rest to get sent:
m_Slots[i].m_State = sSlot::ssWritingRestOut;
}
- m_Slots[i].m_Client = NULL;
+ m_Slots[i].m_Client = nullptr;
}
// Notify the thread of the change:
@@ -407,7 +407,7 @@ void cSocketThreads::cSocketThread::Execute(void)
timeval Timeout;
Timeout.tv_sec = 5;
Timeout.tv_usec = 0;
- if (select((int)Highest + 1, &fdRead, &fdWrite, NULL, &Timeout) == -1)
+ if (select((int)Highest + 1, &fdRead, &fdWrite, nullptr, &Timeout) == -1)
{
LOG("select() call failed in cSocketThread: \"%s\"", cSocket::GetLastErrorString().c_str());
continue;
@@ -519,7 +519,7 @@ void cSocketThreads::cSocketThread::ReadFromSockets(fd_set * a_Read)
}
else
{
- if (m_Slots[i].m_Client != NULL)
+ if (m_Slots[i].m_Client != nullptr)
{
m_Slots[i].m_Client->DataReceived(Buffer, Received);
}
@@ -545,7 +545,7 @@ void cSocketThreads::cSocketThread::WriteToSockets(fd_set * a_Write)
if (m_Slots[i].m_Outgoing.empty())
{
// Request another chunk of outgoing data:
- if (m_Slots[i].m_Client != NULL)
+ if (m_Slots[i].m_Client != nullptr)
{
AString Data;
m_Slots[i].m_Client->GetOutgoingData(Data);
@@ -573,7 +573,7 @@ void cSocketThreads::cSocketThread::WriteToSockets(fd_set * a_Write)
int Err = cSocket::GetLastError();
LOGWARNING("Error %d while writing to client \"%s\", disconnecting. \"%s\"", Err, m_Slots[i].m_Socket.GetIPString().c_str(), GetOSErrorString(Err).c_str());
m_Slots[i].m_Socket.CloseSocket();
- if (m_Slots[i].m_Client != NULL)
+ if (m_Slots[i].m_Client != nullptr)
{
m_Slots[i].m_Client->SocketClosed();
}
@@ -668,7 +668,7 @@ void cSocketThreads::cSocketThread::QueueOutgoingData(void)
cCSLock Lock(m_Parent->m_CS);
for (int i = 0; i < m_NumSlots; i++)
{
- if (m_Slots[i].m_Client != NULL)
+ if (m_Slots[i].m_Client != nullptr)
{
AString Data;
m_Slots[i].m_Client->GetOutgoingData(Data);
diff --git a/src/OSSupport/SocketThreads.h b/src/OSSupport/SocketThreads.h
index 944f5f3bc..df819468d 100644
--- a/src/OSSupport/SocketThreads.h
+++ b/src/OSSupport/SocketThreads.h
@@ -137,7 +137,7 @@ private:
/** The socket is primarily owned by this object */
cSocket m_Socket;
- /** The callback to call for events. May be NULL */
+ /** The callback to call for events. May be nullptr */
cCallback * m_Client;
/** If sending writes only partial data, the rest is stored here for another send.