Use unix linefeeds for all files. Finally. It's about time.
This commit is contained in:
+84
-84
@@ -1,84 +1,84 @@
|
||||
/***************************************************************************
|
||||
* Copyright (C) 2007 by Lothar May *
|
||||
* *
|
||||
* This program is free software; you can redistribute it and/or modify *
|
||||
* it under the terms of the GNU General Public License as published by *
|
||||
* the Free Software Foundation; either version 2 of the License, or *
|
||||
* (at your option) any later version. *
|
||||
* *
|
||||
* This program is distributed in the hope that it will be useful, *
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
|
||||
* GNU General Public License for more details. *
|
||||
* *
|
||||
* You should have received a copy of the GNU General Public License *
|
||||
* along with this program; if not, write to the *
|
||||
* Free Software Foundation, Inc., *
|
||||
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
|
||||
***************************************************************************/
|
||||
/* A manager for avatar files, MD5sums and a cache. */
|
||||
|
||||
#ifndef _AVATARMANAGER_H_
|
||||
#define _AVATARMANAGER_H_
|
||||
|
||||
#include <playerdata.h>
|
||||
#include <net/netpacket.h>
|
||||
#include <core/crypthelper.h>
|
||||
#include <ctime>
|
||||
#include <map>
|
||||
#include <list>
|
||||
|
||||
#include <boost/thread.hpp>
|
||||
#include <boost/shared_ptr.hpp>
|
||||
|
||||
#define MIN_AVATAR_FILE_SIZE 32
|
||||
#define MAX_AVATAR_FILE_SIZE 30720
|
||||
|
||||
struct AvatarFileState;
|
||||
|
||||
class AvatarManager
|
||||
{
|
||||
public:
|
||||
|
||||
AvatarManager();
|
||||
~AvatarManager();
|
||||
|
||||
bool Init(const std::string &dataDir, const std::string &cacheDir);
|
||||
|
||||
void AddSingleAvatar(const std::string &fileName);
|
||||
|
||||
boost::shared_ptr<AvatarFileState> OpenAvatarFileForChunkRead(const std::string &fileName, unsigned &outFileSize, AvatarFileType &outFileType);
|
||||
unsigned ChunkReadAvatarFile(boost::shared_ptr<AvatarFileState> fileState, unsigned char *data, unsigned chunkSize);
|
||||
|
||||
int AvatarFileToNetPackets(const std::string &fileName, unsigned requestId, NetPacketList &packets);
|
||||
|
||||
bool GetHashForAvatar(const std::string &fileName, MD5Buf &md5buf) const;
|
||||
bool GetAvatarFileName(const MD5Buf &md5buf, std::string &fileName) const;
|
||||
bool HasAvatar(const MD5Buf &md5buf) const;
|
||||
bool StoreAvatarInCache(const MD5Buf &md5buf, AvatarFileType avatarFileType, const unsigned char *data, unsigned size);
|
||||
|
||||
static bool IsValidAvatarFileType(AvatarFileType avatarFileType, const unsigned char *fileHeader, unsigned fileHeaderSize);
|
||||
|
||||
void RemoveOldAvatarCacheEntries();
|
||||
|
||||
protected:
|
||||
typedef std::map<MD5Buf, std::string> AvatarMap;
|
||||
typedef std::list<MD5Buf> AvatarList;
|
||||
typedef std::map<std::time_t, MD5Buf> TimeAvatarMap;
|
||||
|
||||
bool InternalReadDirectory(const std::string &dir, AvatarMap &avatars);
|
||||
|
||||
private:
|
||||
mutable boost::mutex m_avatarsMutex;
|
||||
AvatarMap m_avatars;
|
||||
|
||||
mutable boost::mutex m_cachedAvatarsMutex;
|
||||
AvatarMap m_cachedAvatars;
|
||||
|
||||
mutable boost::mutex m_cacheDirMutex;
|
||||
std::string m_cacheDir;
|
||||
|
||||
mutable boost::mutex m_loadMutex;
|
||||
};
|
||||
|
||||
#endif
|
||||
/***************************************************************************
|
||||
* Copyright (C) 2007 by Lothar May *
|
||||
* *
|
||||
* This program is free software; you can redistribute it and/or modify *
|
||||
* it under the terms of the GNU General Public License as published by *
|
||||
* the Free Software Foundation; either version 2 of the License, or *
|
||||
* (at your option) any later version. *
|
||||
* *
|
||||
* This program is distributed in the hope that it will be useful, *
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
|
||||
* GNU General Public License for more details. *
|
||||
* *
|
||||
* You should have received a copy of the GNU General Public License *
|
||||
* along with this program; if not, write to the *
|
||||
* Free Software Foundation, Inc., *
|
||||
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
|
||||
***************************************************************************/
|
||||
/* A manager for avatar files, MD5sums and a cache. */
|
||||
|
||||
#ifndef _AVATARMANAGER_H_
|
||||
#define _AVATARMANAGER_H_
|
||||
|
||||
#include <playerdata.h>
|
||||
#include <net/netpacket.h>
|
||||
#include <core/crypthelper.h>
|
||||
#include <ctime>
|
||||
#include <map>
|
||||
#include <list>
|
||||
|
||||
#include <boost/thread.hpp>
|
||||
#include <boost/shared_ptr.hpp>
|
||||
|
||||
#define MIN_AVATAR_FILE_SIZE 32
|
||||
#define MAX_AVATAR_FILE_SIZE 30720
|
||||
|
||||
struct AvatarFileState;
|
||||
|
||||
class AvatarManager
|
||||
{
|
||||
public:
|
||||
|
||||
AvatarManager();
|
||||
~AvatarManager();
|
||||
|
||||
bool Init(const std::string &dataDir, const std::string &cacheDir);
|
||||
|
||||
void AddSingleAvatar(const std::string &fileName);
|
||||
|
||||
boost::shared_ptr<AvatarFileState> OpenAvatarFileForChunkRead(const std::string &fileName, unsigned &outFileSize, AvatarFileType &outFileType);
|
||||
unsigned ChunkReadAvatarFile(boost::shared_ptr<AvatarFileState> fileState, unsigned char *data, unsigned chunkSize);
|
||||
|
||||
int AvatarFileToNetPackets(const std::string &fileName, unsigned requestId, NetPacketList &packets);
|
||||
|
||||
bool GetHashForAvatar(const std::string &fileName, MD5Buf &md5buf) const;
|
||||
bool GetAvatarFileName(const MD5Buf &md5buf, std::string &fileName) const;
|
||||
bool HasAvatar(const MD5Buf &md5buf) const;
|
||||
bool StoreAvatarInCache(const MD5Buf &md5buf, AvatarFileType avatarFileType, const unsigned char *data, unsigned size);
|
||||
|
||||
static bool IsValidAvatarFileType(AvatarFileType avatarFileType, const unsigned char *fileHeader, unsigned fileHeaderSize);
|
||||
|
||||
void RemoveOldAvatarCacheEntries();
|
||||
|
||||
protected:
|
||||
typedef std::map<MD5Buf, std::string> AvatarMap;
|
||||
typedef std::list<MD5Buf> AvatarList;
|
||||
typedef std::map<std::time_t, MD5Buf> TimeAvatarMap;
|
||||
|
||||
bool InternalReadDirectory(const std::string &dir, AvatarMap &avatars);
|
||||
|
||||
private:
|
||||
mutable boost::mutex m_avatarsMutex;
|
||||
AvatarMap m_avatars;
|
||||
|
||||
mutable boost::mutex m_cachedAvatarsMutex;
|
||||
AvatarMap m_cachedAvatars;
|
||||
|
||||
mutable boost::mutex m_cacheDirMutex;
|
||||
std::string m_cacheDir;
|
||||
|
||||
mutable boost::mutex m_loadMutex;
|
||||
};
|
||||
|
||||
#endif
|
||||
|
||||
+553
-553
File diff suppressed because it is too large
Load Diff
+132
-132
@@ -1,132 +1,132 @@
|
||||
/***************************************************************************
|
||||
* Copyright (C) 2007 by Lothar May *
|
||||
* *
|
||||
* This program is free software; you can redistribute it and/or modify *
|
||||
* it under the terms of the GNU General Public License as published by *
|
||||
* the Free Software Foundation; either version 2 of the License, or *
|
||||
* (at your option) any later version. *
|
||||
* *
|
||||
* This program is distributed in the hope that it will be useful, *
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
|
||||
* GNU General Public License for more details. *
|
||||
* *
|
||||
* You should have received a copy of the GNU General Public License *
|
||||
* along with this program; if not, write to the *
|
||||
* Free Software Foundation, Inc., *
|
||||
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
|
||||
***************************************************************************/
|
||||
|
||||
#include "crypthelper.h"
|
||||
|
||||
|
||||
#include <openssl/md5.h>
|
||||
#include <cstring>
|
||||
#include <cstdio>
|
||||
|
||||
using namespace std;
|
||||
|
||||
// Helper function.
|
||||
static int
|
||||
fromHex(int ch)
|
||||
{
|
||||
int retVal = -1;
|
||||
|
||||
if (ch >= '0' && ch <= '9')
|
||||
retVal = ch - '0';
|
||||
else if (ch >= 'a' && ch <= 'f')
|
||||
retVal = ch - 'a' + 10;
|
||||
else if (ch >= 'A' && ch <= 'F')
|
||||
retVal = ch - 'A' + 10;
|
||||
|
||||
return retVal;
|
||||
}
|
||||
|
||||
|
||||
MD5Buf::MD5Buf()
|
||||
{
|
||||
memset(data, 0, sizeof(data));
|
||||
}
|
||||
|
||||
|
||||
std::string
|
||||
MD5Buf::ToString() const
|
||||
{
|
||||
// Create a hex-based string from the MD5 data.
|
||||
string retValue;
|
||||
char tmpBuf[2 + 1];
|
||||
tmpBuf[sizeof(tmpBuf) - 1] = 0;
|
||||
for (int i = 0; i < MD5_DATA_SIZE; i++)
|
||||
{
|
||||
sprintf(tmpBuf, "%02x", data[i]);
|
||||
retValue += tmpBuf;
|
||||
}
|
||||
return retValue;
|
||||
}
|
||||
|
||||
bool
|
||||
MD5Buf::FromString(const std::string &text)
|
||||
{
|
||||
// Convert hex-based string to MD5 data.
|
||||
bool retVal = false;
|
||||
if (text.size() == 2 * MD5_DATA_SIZE)
|
||||
{
|
||||
unsigned char *tmpData = data;
|
||||
const char *t = text.c_str();
|
||||
int i = 0;
|
||||
for (; i < MD5_DATA_SIZE; i++) {
|
||||
int part1 = fromHex(*t++);
|
||||
if (part1 == -1)
|
||||
break;
|
||||
int part2 = fromHex(*t++);
|
||||
if (part2 == -1)
|
||||
break;
|
||||
*tmpData++ = (part1<<4) + part2;
|
||||
}
|
||||
retVal = i == MD5_DATA_SIZE;
|
||||
}
|
||||
return retVal;
|
||||
}
|
||||
|
||||
bool
|
||||
MD5Buf::IsZero() const
|
||||
{
|
||||
return *this == MD5Buf();
|
||||
}
|
||||
|
||||
bool
|
||||
MD5Buf::operator==(const MD5Buf &other) const
|
||||
{
|
||||
return memcmp(data, other.data, MD5_DATA_SIZE) == 0;
|
||||
}
|
||||
|
||||
bool
|
||||
MD5Buf::operator<(const MD5Buf &other) const
|
||||
{
|
||||
return memcmp(data, other.data, MD5_DATA_SIZE) < 0;
|
||||
}
|
||||
|
||||
bool
|
||||
CryptHelper::MD5Sum(const std::string &fileName, MD5Buf &buf)
|
||||
{
|
||||
bool retVal = false;
|
||||
FILE *file = fopen(fileName.c_str(), "rb");
|
||||
|
||||
if (file)
|
||||
{
|
||||
// Calculate MD5 sum of file.
|
||||
unsigned char readBuf[8192];
|
||||
MD5_CTX context;
|
||||
int numBytes;
|
||||
|
||||
MD5_Init(&context);
|
||||
while ((numBytes = fread(readBuf, 1, sizeof(readBuf), file)) > 0)
|
||||
MD5_Update(&context, readBuf, numBytes);
|
||||
MD5_Final(buf.data, &context);
|
||||
retVal = ferror(file) == 0;
|
||||
|
||||
fclose(file);
|
||||
}
|
||||
return retVal;
|
||||
}
|
||||
|
||||
/***************************************************************************
|
||||
* Copyright (C) 2007 by Lothar May *
|
||||
* *
|
||||
* This program is free software; you can redistribute it and/or modify *
|
||||
* it under the terms of the GNU General Public License as published by *
|
||||
* the Free Software Foundation; either version 2 of the License, or *
|
||||
* (at your option) any later version. *
|
||||
* *
|
||||
* This program is distributed in the hope that it will be useful, *
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
|
||||
* GNU General Public License for more details. *
|
||||
* *
|
||||
* You should have received a copy of the GNU General Public License *
|
||||
* along with this program; if not, write to the *
|
||||
* Free Software Foundation, Inc., *
|
||||
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
|
||||
***************************************************************************/
|
||||
|
||||
#include "crypthelper.h"
|
||||
|
||||
|
||||
#include <openssl/md5.h>
|
||||
#include <cstring>
|
||||
#include <cstdio>
|
||||
|
||||
using namespace std;
|
||||
|
||||
// Helper function.
|
||||
static int
|
||||
fromHex(int ch)
|
||||
{
|
||||
int retVal = -1;
|
||||
|
||||
if (ch >= '0' && ch <= '9')
|
||||
retVal = ch - '0';
|
||||
else if (ch >= 'a' && ch <= 'f')
|
||||
retVal = ch - 'a' + 10;
|
||||
else if (ch >= 'A' && ch <= 'F')
|
||||
retVal = ch - 'A' + 10;
|
||||
|
||||
return retVal;
|
||||
}
|
||||
|
||||
|
||||
MD5Buf::MD5Buf()
|
||||
{
|
||||
memset(data, 0, sizeof(data));
|
||||
}
|
||||
|
||||
|
||||
std::string
|
||||
MD5Buf::ToString() const
|
||||
{
|
||||
// Create a hex-based string from the MD5 data.
|
||||
string retValue;
|
||||
char tmpBuf[2 + 1];
|
||||
tmpBuf[sizeof(tmpBuf) - 1] = 0;
|
||||
for (int i = 0; i < MD5_DATA_SIZE; i++)
|
||||
{
|
||||
sprintf(tmpBuf, "%02x", data[i]);
|
||||
retValue += tmpBuf;
|
||||
}
|
||||
return retValue;
|
||||
}
|
||||
|
||||
bool
|
||||
MD5Buf::FromString(const std::string &text)
|
||||
{
|
||||
// Convert hex-based string to MD5 data.
|
||||
bool retVal = false;
|
||||
if (text.size() == 2 * MD5_DATA_SIZE)
|
||||
{
|
||||
unsigned char *tmpData = data;
|
||||
const char *t = text.c_str();
|
||||
int i = 0;
|
||||
for (; i < MD5_DATA_SIZE; i++) {
|
||||
int part1 = fromHex(*t++);
|
||||
if (part1 == -1)
|
||||
break;
|
||||
int part2 = fromHex(*t++);
|
||||
if (part2 == -1)
|
||||
break;
|
||||
*tmpData++ = (part1<<4) + part2;
|
||||
}
|
||||
retVal = i == MD5_DATA_SIZE;
|
||||
}
|
||||
return retVal;
|
||||
}
|
||||
|
||||
bool
|
||||
MD5Buf::IsZero() const
|
||||
{
|
||||
return *this == MD5Buf();
|
||||
}
|
||||
|
||||
bool
|
||||
MD5Buf::operator==(const MD5Buf &other) const
|
||||
{
|
||||
return memcmp(data, other.data, MD5_DATA_SIZE) == 0;
|
||||
}
|
||||
|
||||
bool
|
||||
MD5Buf::operator<(const MD5Buf &other) const
|
||||
{
|
||||
return memcmp(data, other.data, MD5_DATA_SIZE) < 0;
|
||||
}
|
||||
|
||||
bool
|
||||
CryptHelper::MD5Sum(const std::string &fileName, MD5Buf &buf)
|
||||
{
|
||||
bool retVal = false;
|
||||
FILE *file = fopen(fileName.c_str(), "rb");
|
||||
|
||||
if (file)
|
||||
{
|
||||
// Calculate MD5 sum of file.
|
||||
unsigned char readBuf[8192];
|
||||
MD5_CTX context;
|
||||
int numBytes;
|
||||
|
||||
MD5_Init(&context);
|
||||
while ((numBytes = fread(readBuf, 1, sizeof(readBuf), file)) > 0)
|
||||
MD5_Update(&context, readBuf, numBytes);
|
||||
MD5_Final(buf.data, &context);
|
||||
retVal = ferror(file) == 0;
|
||||
|
||||
fclose(file);
|
||||
}
|
||||
return retVal;
|
||||
}
|
||||
|
||||
|
||||
+154
-154
@@ -1,154 +1,154 @@
|
||||
/***************************************************************************
|
||||
* Copyright (C) 2007 by Lothar May *
|
||||
* *
|
||||
* This program is free software; you can redistribute it and/or modify *
|
||||
* it under the terms of the GNU General Public License as published by *
|
||||
* the Free Software Foundation; either version 2 of the License, or *
|
||||
* (at your option) any later version. *
|
||||
* *
|
||||
* This program is distributed in the hope that it will be useful, *
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
|
||||
* GNU General Public License for more details. *
|
||||
* *
|
||||
* You should have received a copy of the GNU General Public License *
|
||||
* along with this program; if not, write to the *
|
||||
* Free Software Foundation, Inc., *
|
||||
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
|
||||
***************************************************************************/
|
||||
|
||||
#include "thread.h"
|
||||
|
||||
// This is ugly, but I can't help it.
|
||||
inline void ADD_MSEC_TO_XTIME(boost::xtime &xt, unsigned msec)
|
||||
{
|
||||
xt.sec += msec / 1000;
|
||||
xt.nsec += (msec % 1000) * 1000000;
|
||||
if (xt.nsec > NANOSECONDS_PER_SECOND)
|
||||
{
|
||||
xt.sec++;
|
||||
xt.nsec -= NANOSECONDS_PER_SECOND;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// Helper class for thread creation.
|
||||
class ThreadStarter
|
||||
{
|
||||
public:
|
||||
ThreadStarter(Thread &thread) : m_thread(thread) {}
|
||||
void operator()()
|
||||
{
|
||||
m_thread.MainWrapper();
|
||||
}
|
||||
|
||||
private:
|
||||
Thread &m_thread;
|
||||
};
|
||||
|
||||
Thread::Thread()
|
||||
{
|
||||
}
|
||||
|
||||
Thread::~Thread()
|
||||
{
|
||||
}
|
||||
|
||||
void
|
||||
Thread::Run()
|
||||
{
|
||||
boost::mutex::scoped_lock threadLock(m_threadObjMutex);
|
||||
|
||||
// Create the boost thread object.
|
||||
if (!m_threadObj.get())
|
||||
{
|
||||
// Initialise data structures within the context of the thread
|
||||
// who runs/terminates this thread.
|
||||
m_userReqTerminateLock.reset(new boost::timed_mutex::scoped_try_lock(m_shouldTerminateMutex));
|
||||
m_threadStartBarrier.reset(new boost::barrier(2));
|
||||
|
||||
m_threadObj.reset(new boost::thread(ThreadStarter(*this)));
|
||||
m_threadStartBarrier->wait();
|
||||
}
|
||||
}
|
||||
|
||||
void
|
||||
Thread::SignalTermination()
|
||||
{
|
||||
// Unlock the shouldTerminateMutex.
|
||||
if (m_userReqTerminateLock.get()) // cannot signal before calling Run
|
||||
m_userReqTerminateLock->unlock();
|
||||
}
|
||||
|
||||
bool
|
||||
Thread::Join(unsigned msecTimeout)
|
||||
{
|
||||
if (!IsRunning())
|
||||
return true;
|
||||
|
||||
bool tmpIsTerminated;
|
||||
if (msecTimeout == THREAD_WAIT_INFINITE)
|
||||
{
|
||||
// Wait infinitely.
|
||||
boost::timed_mutex::scoped_lock lock(m_isTerminatedMutex);
|
||||
tmpIsTerminated = true;
|
||||
}
|
||||
else
|
||||
{
|
||||
// Calculate time after timeout
|
||||
boost::xtime t;
|
||||
boost::xtime_get(&t, boost::TIME_UTC);
|
||||
ADD_MSEC_TO_XTIME(t, msecTimeout);
|
||||
|
||||
// Wait for the termination of the application code.
|
||||
boost::timed_mutex::scoped_timed_lock lock(m_isTerminatedMutex, t);
|
||||
tmpIsTerminated = lock.locked();
|
||||
}
|
||||
|
||||
if (tmpIsTerminated)
|
||||
{
|
||||
boost::mutex::scoped_lock lock(m_threadObjMutex);
|
||||
// Wait for "real" termination of the thread.
|
||||
if (m_threadObj.get())
|
||||
{
|
||||
m_threadObj->join();
|
||||
m_threadObj.reset();
|
||||
}
|
||||
}
|
||||
|
||||
return tmpIsTerminated;
|
||||
}
|
||||
|
||||
void
|
||||
Thread::Msleep(unsigned msecs)
|
||||
{
|
||||
boost::xtime t;
|
||||
boost::xtime_get(&t, boost::TIME_UTC);
|
||||
ADD_MSEC_TO_XTIME(t, msecs);
|
||||
|
||||
boost::thread::sleep(t);
|
||||
}
|
||||
|
||||
void
|
||||
Thread::MainWrapper()
|
||||
{
|
||||
boost::timed_mutex::scoped_lock lock(m_isTerminatedMutex);
|
||||
assert(m_threadStartBarrier.get());
|
||||
m_threadStartBarrier->wait();
|
||||
this->Main();
|
||||
}
|
||||
|
||||
bool
|
||||
Thread::ShouldTerminate() const
|
||||
{
|
||||
boost::timed_mutex::scoped_try_lock lock(m_shouldTerminateMutex);
|
||||
return lock.locked();
|
||||
}
|
||||
|
||||
bool
|
||||
Thread::IsRunning() const
|
||||
{
|
||||
boost::mutex::scoped_lock threadLock(m_threadObjMutex);
|
||||
return (m_threadObj.get() != NULL);
|
||||
}
|
||||
|
||||
/***************************************************************************
|
||||
* Copyright (C) 2007 by Lothar May *
|
||||
* *
|
||||
* This program is free software; you can redistribute it and/or modify *
|
||||
* it under the terms of the GNU General Public License as published by *
|
||||
* the Free Software Foundation; either version 2 of the License, or *
|
||||
* (at your option) any later version. *
|
||||
* *
|
||||
* This program is distributed in the hope that it will be useful, *
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
|
||||
* GNU General Public License for more details. *
|
||||
* *
|
||||
* You should have received a copy of the GNU General Public License *
|
||||
* along with this program; if not, write to the *
|
||||
* Free Software Foundation, Inc., *
|
||||
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
|
||||
***************************************************************************/
|
||||
|
||||
#include "thread.h"
|
||||
|
||||
// This is ugly, but I can't help it.
|
||||
inline void ADD_MSEC_TO_XTIME(boost::xtime &xt, unsigned msec)
|
||||
{
|
||||
xt.sec += msec / 1000;
|
||||
xt.nsec += (msec % 1000) * 1000000;
|
||||
if (xt.nsec > NANOSECONDS_PER_SECOND)
|
||||
{
|
||||
xt.sec++;
|
||||
xt.nsec -= NANOSECONDS_PER_SECOND;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// Helper class for thread creation.
|
||||
class ThreadStarter
|
||||
{
|
||||
public:
|
||||
ThreadStarter(Thread &thread) : m_thread(thread) {}
|
||||
void operator()()
|
||||
{
|
||||
m_thread.MainWrapper();
|
||||
}
|
||||
|
||||
private:
|
||||
Thread &m_thread;
|
||||
};
|
||||
|
||||
Thread::Thread()
|
||||
{
|
||||
}
|
||||
|
||||
Thread::~Thread()
|
||||
{
|
||||
}
|
||||
|
||||
void
|
||||
Thread::Run()
|
||||
{
|
||||
boost::mutex::scoped_lock threadLock(m_threadObjMutex);
|
||||
|
||||
// Create the boost thread object.
|
||||
if (!m_threadObj.get())
|
||||
{
|
||||
// Initialise data structures within the context of the thread
|
||||
// who runs/terminates this thread.
|
||||
m_userReqTerminateLock.reset(new boost::timed_mutex::scoped_try_lock(m_shouldTerminateMutex));
|
||||
m_threadStartBarrier.reset(new boost::barrier(2));
|
||||
|
||||
m_threadObj.reset(new boost::thread(ThreadStarter(*this)));
|
||||
m_threadStartBarrier->wait();
|
||||
}
|
||||
}
|
||||
|
||||
void
|
||||
Thread::SignalTermination()
|
||||
{
|
||||
// Unlock the shouldTerminateMutex.
|
||||
if (m_userReqTerminateLock.get()) // cannot signal before calling Run
|
||||
m_userReqTerminateLock->unlock();
|
||||
}
|
||||
|
||||
bool
|
||||
Thread::Join(unsigned msecTimeout)
|
||||
{
|
||||
if (!IsRunning())
|
||||
return true;
|
||||
|
||||
bool tmpIsTerminated;
|
||||
if (msecTimeout == THREAD_WAIT_INFINITE)
|
||||
{
|
||||
// Wait infinitely.
|
||||
boost::timed_mutex::scoped_lock lock(m_isTerminatedMutex);
|
||||
tmpIsTerminated = true;
|
||||
}
|
||||
else
|
||||
{
|
||||
// Calculate time after timeout
|
||||
boost::xtime t;
|
||||
boost::xtime_get(&t, boost::TIME_UTC);
|
||||
ADD_MSEC_TO_XTIME(t, msecTimeout);
|
||||
|
||||
// Wait for the termination of the application code.
|
||||
boost::timed_mutex::scoped_timed_lock lock(m_isTerminatedMutex, t);
|
||||
tmpIsTerminated = lock.locked();
|
||||
}
|
||||
|
||||
if (tmpIsTerminated)
|
||||
{
|
||||
boost::mutex::scoped_lock lock(m_threadObjMutex);
|
||||
// Wait for "real" termination of the thread.
|
||||
if (m_threadObj.get())
|
||||
{
|
||||
m_threadObj->join();
|
||||
m_threadObj.reset();
|
||||
}
|
||||
}
|
||||
|
||||
return tmpIsTerminated;
|
||||
}
|
||||
|
||||
void
|
||||
Thread::Msleep(unsigned msecs)
|
||||
{
|
||||
boost::xtime t;
|
||||
boost::xtime_get(&t, boost::TIME_UTC);
|
||||
ADD_MSEC_TO_XTIME(t, msecs);
|
||||
|
||||
boost::thread::sleep(t);
|
||||
}
|
||||
|
||||
void
|
||||
Thread::MainWrapper()
|
||||
{
|
||||
boost::timed_mutex::scoped_lock lock(m_isTerminatedMutex);
|
||||
assert(m_threadStartBarrier.get());
|
||||
m_threadStartBarrier->wait();
|
||||
this->Main();
|
||||
}
|
||||
|
||||
bool
|
||||
Thread::ShouldTerminate() const
|
||||
{
|
||||
boost::timed_mutex::scoped_try_lock lock(m_shouldTerminateMutex);
|
||||
return lock.locked();
|
||||
}
|
||||
|
||||
bool
|
||||
Thread::IsRunning() const
|
||||
{
|
||||
boost::mutex::scoped_lock threadLock(m_threadObjMutex);
|
||||
return (m_threadObj.get() != NULL);
|
||||
}
|
||||
|
||||
|
||||
+34
-34
@@ -1,34 +1,34 @@
|
||||
/***************************************************************************
|
||||
* Copyright (C) 2007 by Lothar May *
|
||||
* *
|
||||
* This program is free software; you can redistribute it and/or modify *
|
||||
* it under the terms of the GNU General Public License as published by *
|
||||
* the Free Software Foundation; either version 2 of the License, or *
|
||||
* (at your option) any later version. *
|
||||
* *
|
||||
* This program is distributed in the hope that it will be useful, *
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
|
||||
* GNU General Public License for more details. *
|
||||
* *
|
||||
* You should have received a copy of the GNU General Public License *
|
||||
* along with this program; if not, write to the *
|
||||
* Free Software Foundation, Inc., *
|
||||
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
|
||||
***************************************************************************/
|
||||
/* Helper class for character conversion functions. */
|
||||
|
||||
#ifndef _CRYPTHELPER_H_
|
||||
#define _CRYPTHELPER_H_
|
||||
|
||||
#include <string>
|
||||
|
||||
class ConvHelper
|
||||
{
|
||||
public:
|
||||
|
||||
static std::string NativeToUtf8(const std::string &inStr);
|
||||
static std::string Utf8ToNative(const std::string &inStr);
|
||||
};
|
||||
|
||||
#endif
|
||||
/***************************************************************************
|
||||
* Copyright (C) 2007 by Lothar May *
|
||||
* *
|
||||
* This program is free software; you can redistribute it and/or modify *
|
||||
* it under the terms of the GNU General Public License as published by *
|
||||
* the Free Software Foundation; either version 2 of the License, or *
|
||||
* (at your option) any later version. *
|
||||
* *
|
||||
* This program is distributed in the hope that it will be useful, *
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
|
||||
* GNU General Public License for more details. *
|
||||
* *
|
||||
* You should have received a copy of the GNU General Public License *
|
||||
* along with this program; if not, write to the *
|
||||
* Free Software Foundation, Inc., *
|
||||
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
|
||||
***************************************************************************/
|
||||
/* Helper class for character conversion functions. */
|
||||
|
||||
#ifndef _CRYPTHELPER_H_
|
||||
#define _CRYPTHELPER_H_
|
||||
|
||||
#include <string>
|
||||
|
||||
class ConvHelper
|
||||
{
|
||||
public:
|
||||
|
||||
static std::string NativeToUtf8(const std::string &inStr);
|
||||
static std::string Utf8ToNative(const std::string &inStr);
|
||||
};
|
||||
|
||||
#endif
|
||||
|
||||
+49
-49
@@ -1,49 +1,49 @@
|
||||
/***************************************************************************
|
||||
* Copyright (C) 2007 by Lothar May *
|
||||
* *
|
||||
* This program is free software; you can redistribute it and/or modify *
|
||||
* it under the terms of the GNU General Public License as published by *
|
||||
* the Free Software Foundation; either version 2 of the License, or *
|
||||
* (at your option) any later version. *
|
||||
* *
|
||||
* This program is distributed in the hope that it will be useful, *
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
|
||||
* GNU General Public License for more details. *
|
||||
* *
|
||||
* You should have received a copy of the GNU General Public License *
|
||||
* along with this program; if not, write to the *
|
||||
* Free Software Foundation, Inc., *
|
||||
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
|
||||
***************************************************************************/
|
||||
/* Helper class for crypt functions. */
|
||||
|
||||
#ifndef _CRYPTHELPER_H_
|
||||
#define _CRYPTHELPER_H_
|
||||
|
||||
#include <string>
|
||||
|
||||
#define MD5_DATA_SIZE 16
|
||||
|
||||
struct MD5Buf
|
||||
{
|
||||
MD5Buf();
|
||||
|
||||
std::string ToString() const;
|
||||
bool FromString(const std::string &text);
|
||||
bool IsZero() const;
|
||||
|
||||
bool operator==(const MD5Buf &other) const;
|
||||
bool operator<(const MD5Buf &other) const;
|
||||
|
||||
unsigned char data[MD5_DATA_SIZE];
|
||||
};
|
||||
|
||||
class CryptHelper
|
||||
{
|
||||
public:
|
||||
|
||||
static bool MD5Sum(const std::string &fileName, MD5Buf &buf);
|
||||
};
|
||||
|
||||
#endif
|
||||
/***************************************************************************
|
||||
* Copyright (C) 2007 by Lothar May *
|
||||
* *
|
||||
* This program is free software; you can redistribute it and/or modify *
|
||||
* it under the terms of the GNU General Public License as published by *
|
||||
* the Free Software Foundation; either version 2 of the License, or *
|
||||
* (at your option) any later version. *
|
||||
* *
|
||||
* This program is distributed in the hope that it will be useful, *
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
|
||||
* GNU General Public License for more details. *
|
||||
* *
|
||||
* You should have received a copy of the GNU General Public License *
|
||||
* along with this program; if not, write to the *
|
||||
* Free Software Foundation, Inc., *
|
||||
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
|
||||
***************************************************************************/
|
||||
/* Helper class for crypt functions. */
|
||||
|
||||
#ifndef _CRYPTHELPER_H_
|
||||
#define _CRYPTHELPER_H_
|
||||
|
||||
#include <string>
|
||||
|
||||
#define MD5_DATA_SIZE 16
|
||||
|
||||
struct MD5Buf
|
||||
{
|
||||
MD5Buf();
|
||||
|
||||
std::string ToString() const;
|
||||
bool FromString(const std::string &text);
|
||||
bool IsZero() const;
|
||||
|
||||
bool operator==(const MD5Buf &other) const;
|
||||
bool operator<(const MD5Buf &other) const;
|
||||
|
||||
unsigned char data[MD5_DATA_SIZE];
|
||||
};
|
||||
|
||||
class CryptHelper
|
||||
{
|
||||
public:
|
||||
|
||||
static bool MD5Sum(const std::string &fileName, MD5Buf &buf);
|
||||
};
|
||||
|
||||
#endif
|
||||
|
||||
+88
-88
@@ -1,88 +1,88 @@
|
||||
/***************************************************************************
|
||||
* Copyright (C) 2007 by Lothar May *
|
||||
* *
|
||||
* This program is free software; you can redistribute it and/or modify *
|
||||
* it under the terms of the GNU General Public License as published by *
|
||||
* the Free Software Foundation; either version 2 of the License, or *
|
||||
* (at your option) any later version. *
|
||||
* *
|
||||
* This program is distributed in the hope that it will be useful, *
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
|
||||
* GNU General Public License for more details. *
|
||||
* *
|
||||
* You should have received a copy of the GNU General Public License *
|
||||
* along with this program; if not, write to the *
|
||||
* Free Software Foundation, Inc., *
|
||||
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
|
||||
***************************************************************************/
|
||||
/* Base class for threads (used by network client/server). */
|
||||
|
||||
#ifndef _THREAD_H_
|
||||
#define _THREAD_H_
|
||||
|
||||
#include <boost/thread.hpp>
|
||||
#include <boost/thread/barrier.hpp>
|
||||
#include <boost/shared_ptr.hpp>
|
||||
|
||||
#ifndef NANOSECONDS_PER_SECOND
|
||||
#define NANOSECONDS_PER_SECOND 1000000000
|
||||
#endif
|
||||
|
||||
#define THREAD_WAIT_INFINITE 0xFFFFFFFF
|
||||
|
||||
class Thread
|
||||
{
|
||||
public:
|
||||
Thread();
|
||||
virtual ~Thread();
|
||||
|
||||
// Start the thread. Will do nothing if the
|
||||
// thread was already started.
|
||||
void Run();
|
||||
|
||||
// Signal that the thread should be terminated
|
||||
virtual void SignalTermination();
|
||||
|
||||
// Wait for the termination of the thread.
|
||||
// Only one Thread should wait for the termination!
|
||||
// You SHOULD always call join for a thread.
|
||||
bool Join(unsigned msecTimeout);
|
||||
|
||||
// Sleep the currently active thread.
|
||||
static void Msleep(unsigned msecs);
|
||||
|
||||
protected:
|
||||
|
||||
// Startup function.
|
||||
void MainWrapper();
|
||||
|
||||
// Main function of the thread.
|
||||
virtual void Main() = 0;
|
||||
|
||||
// Checks whether termination has been requested.
|
||||
bool ShouldTerminate() const;
|
||||
|
||||
// Checks whether the thread is running.
|
||||
bool IsRunning() const;
|
||||
|
||||
private:
|
||||
|
||||
// Flag specifying whether the application code within the
|
||||
// thread was terminated.
|
||||
mutable boost::timed_mutex m_isTerminatedMutex;
|
||||
|
||||
// Flag specifying whether the thread should be terminated.
|
||||
mutable boost::timed_mutex m_shouldTerminateMutex;
|
||||
mutable boost::shared_ptr<boost::timed_mutex::scoped_try_lock> m_userReqTerminateLock;
|
||||
|
||||
// The boost thread object.
|
||||
boost::shared_ptr<boost::thread> m_threadObj;
|
||||
mutable boost::mutex m_threadObjMutex;
|
||||
|
||||
mutable boost::shared_ptr<boost::barrier> m_threadStartBarrier;
|
||||
|
||||
friend class ThreadStarter;
|
||||
};
|
||||
|
||||
#endif
|
||||
/***************************************************************************
|
||||
* Copyright (C) 2007 by Lothar May *
|
||||
* *
|
||||
* This program is free software; you can redistribute it and/or modify *
|
||||
* it under the terms of the GNU General Public License as published by *
|
||||
* the Free Software Foundation; either version 2 of the License, or *
|
||||
* (at your option) any later version. *
|
||||
* *
|
||||
* This program is distributed in the hope that it will be useful, *
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
|
||||
* GNU General Public License for more details. *
|
||||
* *
|
||||
* You should have received a copy of the GNU General Public License *
|
||||
* along with this program; if not, write to the *
|
||||
* Free Software Foundation, Inc., *
|
||||
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
|
||||
***************************************************************************/
|
||||
/* Base class for threads (used by network client/server). */
|
||||
|
||||
#ifndef _THREAD_H_
|
||||
#define _THREAD_H_
|
||||
|
||||
#include <boost/thread.hpp>
|
||||
#include <boost/thread/barrier.hpp>
|
||||
#include <boost/shared_ptr.hpp>
|
||||
|
||||
#ifndef NANOSECONDS_PER_SECOND
|
||||
#define NANOSECONDS_PER_SECOND 1000000000
|
||||
#endif
|
||||
|
||||
#define THREAD_WAIT_INFINITE 0xFFFFFFFF
|
||||
|
||||
class Thread
|
||||
{
|
||||
public:
|
||||
Thread();
|
||||
virtual ~Thread();
|
||||
|
||||
// Start the thread. Will do nothing if the
|
||||
// thread was already started.
|
||||
void Run();
|
||||
|
||||
// Signal that the thread should be terminated
|
||||
virtual void SignalTermination();
|
||||
|
||||
// Wait for the termination of the thread.
|
||||
// Only one Thread should wait for the termination!
|
||||
// You SHOULD always call join for a thread.
|
||||
bool Join(unsigned msecTimeout);
|
||||
|
||||
// Sleep the currently active thread.
|
||||
static void Msleep(unsigned msecs);
|
||||
|
||||
protected:
|
||||
|
||||
// Startup function.
|
||||
void MainWrapper();
|
||||
|
||||
// Main function of the thread.
|
||||
virtual void Main() = 0;
|
||||
|
||||
// Checks whether termination has been requested.
|
||||
bool ShouldTerminate() const;
|
||||
|
||||
// Checks whether the thread is running.
|
||||
bool IsRunning() const;
|
||||
|
||||
private:
|
||||
|
||||
// Flag specifying whether the application code within the
|
||||
// thread was terminated.
|
||||
mutable boost::timed_mutex m_isTerminatedMutex;
|
||||
|
||||
// Flag specifying whether the thread should be terminated.
|
||||
mutable boost::timed_mutex m_shouldTerminateMutex;
|
||||
mutable boost::shared_ptr<boost::timed_mutex::scoped_try_lock> m_userReqTerminateLock;
|
||||
|
||||
// The boost thread object.
|
||||
boost::shared_ptr<boost::thread> m_threadObj;
|
||||
mutable boost::mutex m_threadObjMutex;
|
||||
|
||||
mutable boost::shared_ptr<boost::barrier> m_threadStartBarrier;
|
||||
|
||||
friend class ThreadStarter;
|
||||
};
|
||||
|
||||
#endif
|
||||
|
||||
@@ -1,76 +1,76 @@
|
||||
/***************************************************************************
|
||||
* Copyright (C) 2007 by Lothar May *
|
||||
* *
|
||||
* This program is free software; you can redistribute it and/or modify *
|
||||
* it under the terms of the GNU General Public License as published by *
|
||||
* the Free Software Foundation; either version 2 of the License, or *
|
||||
* (at your option) any later version. *
|
||||
* *
|
||||
* This program is distributed in the hope that it will be useful, *
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
|
||||
* GNU General Public License for more details. *
|
||||
* *
|
||||
* You should have received a copy of the GNU General Public License *
|
||||
* along with this program; if not, write to the *
|
||||
* Free Software Foundation, Inc., *
|
||||
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
|
||||
***************************************************************************/
|
||||
|
||||
#include "convhelper.h"
|
||||
|
||||
#ifndef _WIN32
|
||||
#error This file is Windows only.
|
||||
#endif
|
||||
|
||||
#include <windows.h>
|
||||
|
||||
using namespace std;
|
||||
|
||||
static string
|
||||
Convert(const std::string &inStr, int fromCP, int toCP)
|
||||
{
|
||||
// convert str from current Windows source to target charset
|
||||
string retStr(inStr);
|
||||
|
||||
if (!inStr.empty())
|
||||
{
|
||||
int len = (int)inStr.length() + 1;
|
||||
int reqLen = ::MultiByteToWideChar(fromCP, 0, inStr.c_str(), len, NULL, 0);
|
||||
|
||||
if (reqLen)
|
||||
{
|
||||
wchar_t *wstr = new wchar_t[reqLen];
|
||||
wstr[0] = L'\0';
|
||||
if (::MultiByteToWideChar(fromCP, 0, inStr.c_str(), len, wstr, reqLen) == (int)reqLen)
|
||||
{
|
||||
len = reqLen;
|
||||
reqLen = ::WideCharToMultiByte(toCP, 0, wstr, len, NULL, 0, NULL, NULL);
|
||||
|
||||
if (reqLen)
|
||||
{
|
||||
char *str = new char[reqLen];
|
||||
if (::WideCharToMultiByte(toCP, 0, wstr, len, str, reqLen, NULL, NULL) == (int)reqLen)
|
||||
retStr = str;
|
||||
delete[] str;
|
||||
}
|
||||
}
|
||||
|
||||
delete[] wstr;
|
||||
}
|
||||
}
|
||||
return retStr;
|
||||
}
|
||||
|
||||
string
|
||||
ConvHelper::NativeToUtf8(const std::string &inStr)
|
||||
{
|
||||
return Convert(inStr, CP_ACP, CP_UTF8);
|
||||
}
|
||||
|
||||
string
|
||||
ConvHelper::Utf8ToNative(const std::string &inStr)
|
||||
{
|
||||
return Convert(inStr, CP_UTF8, CP_ACP);
|
||||
}
|
||||
|
||||
/***************************************************************************
|
||||
* Copyright (C) 2007 by Lothar May *
|
||||
* *
|
||||
* This program is free software; you can redistribute it and/or modify *
|
||||
* it under the terms of the GNU General Public License as published by *
|
||||
* the Free Software Foundation; either version 2 of the License, or *
|
||||
* (at your option) any later version. *
|
||||
* *
|
||||
* This program is distributed in the hope that it will be useful, *
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
|
||||
* GNU General Public License for more details. *
|
||||
* *
|
||||
* You should have received a copy of the GNU General Public License *
|
||||
* along with this program; if not, write to the *
|
||||
* Free Software Foundation, Inc., *
|
||||
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
|
||||
***************************************************************************/
|
||||
|
||||
#include "convhelper.h"
|
||||
|
||||
#ifndef _WIN32
|
||||
#error This file is Windows only.
|
||||
#endif
|
||||
|
||||
#include <windows.h>
|
||||
|
||||
using namespace std;
|
||||
|
||||
static string
|
||||
Convert(const std::string &inStr, int fromCP, int toCP)
|
||||
{
|
||||
// convert str from current Windows source to target charset
|
||||
string retStr(inStr);
|
||||
|
||||
if (!inStr.empty())
|
||||
{
|
||||
int len = (int)inStr.length() + 1;
|
||||
int reqLen = ::MultiByteToWideChar(fromCP, 0, inStr.c_str(), len, NULL, 0);
|
||||
|
||||
if (reqLen)
|
||||
{
|
||||
wchar_t *wstr = new wchar_t[reqLen];
|
||||
wstr[0] = L'\0';
|
||||
if (::MultiByteToWideChar(fromCP, 0, inStr.c_str(), len, wstr, reqLen) == (int)reqLen)
|
||||
{
|
||||
len = reqLen;
|
||||
reqLen = ::WideCharToMultiByte(toCP, 0, wstr, len, NULL, 0, NULL, NULL);
|
||||
|
||||
if (reqLen)
|
||||
{
|
||||
char *str = new char[reqLen];
|
||||
if (::WideCharToMultiByte(toCP, 0, wstr, len, str, reqLen, NULL, NULL) == (int)reqLen)
|
||||
retStr = str;
|
||||
delete[] str;
|
||||
}
|
||||
}
|
||||
|
||||
delete[] wstr;
|
||||
}
|
||||
}
|
||||
return retStr;
|
||||
}
|
||||
|
||||
string
|
||||
ConvHelper::NativeToUtf8(const std::string &inStr)
|
||||
{
|
||||
return Convert(inStr, CP_ACP, CP_UTF8);
|
||||
}
|
||||
|
||||
string
|
||||
ConvHelper::Utf8ToNative(const std::string &inStr)
|
||||
{
|
||||
return Convert(inStr, CP_UTF8, CP_ACP);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user