From 3d885beb0cfa4029a041c3e739349e8bd6684c4f Mon Sep 17 00:00:00 2001 From: henry Date: Sat, 14 Nov 2009 08:00:08 +0800 Subject: [PATCH] Create utils project. --- .gitignore | 4 + HLH_Cond.cpp | 264 +++++++++++++++++ HLH_Cond.h | 102 +++++++ HLH_Mutex.cpp | 152 ++++++++++ HLH_Mutex.h | 112 ++++++++ HLH_RoundU32.cpp | 45 +++ HLH_RoundU32.h | 184 ++++++++++++ HLH_Sock.cpp | 839 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ HLH_Sock.h | 272 ++++++++++++++++++ HLH_Thread.cpp | 330 ++++++++++++++++++++++ HLH_Thread.h | 119 ++++++++ HLH_Time.h | 509 +++++++++++++++++++++++++++++++++ HLH_UDPSock.cpp | 428 ++++++++++++++++++++++++++++ HLH_UDPSock.h | 153 ++++++++++ Makefile | 57 ++++ common.h | 52 ++++ config.h | 111 ++++++++ debug.cpp | 65 +++++ debug.h | 106 +++++++ typedef.h | 74 +++++ 20 files changed, 3978 insertions(+) create mode 100644 .gitignore create mode 100644 HLH_Cond.cpp create mode 100644 HLH_Cond.h create mode 100644 HLH_Mutex.cpp create mode 100644 HLH_Mutex.h create mode 100644 HLH_RoundU32.cpp create mode 100644 HLH_RoundU32.h create mode 100644 HLH_Sock.cpp create mode 100644 HLH_Sock.h create mode 100644 HLH_Thread.cpp create mode 100644 HLH_Thread.h create mode 100644 HLH_Time.h create mode 100644 HLH_UDPSock.cpp create mode 100644 HLH_UDPSock.h create mode 100644 Makefile create mode 100644 common.h create mode 100644 config.h create mode 100644 debug.cpp create mode 100644 debug.h create mode 100644 typedef.h diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..e1efd57 --- /dev/null +++ b/.gitignore @@ -0,0 +1,4 @@ +.*.swp +.*.d +*.o +ref/* diff --git a/HLH_Cond.cpp b/HLH_Cond.cpp new file mode 100644 index 0000000..c8e2cf8 --- /dev/null +++ b/HLH_Cond.cpp @@ -0,0 +1,264 @@ +/******************************************************************************* + * File Name : HLH_Cond.cpp + * + * Author : Henry He + * Created Time : 2009-10-8 11:46:29 + * Description : + ******************************************************************************/ + + +/******************************************************************************* + * Desc : Includes Files + ******************************************************************************/ +#include "utils/HLH_Cond.h" + + +/******************************************************************************* + * Desc : Macro Definations + ******************************************************************************/ + + +/******************************************************************************* + * Desc : Type Definations + ******************************************************************************/ + + +/******************************************************************************* + * Desc : Global Variables + ******************************************************************************/ + + +/******************************************************************************* + * Desc : File Variables + ******************************************************************************/ + + + + + + + +/****************************************************************************** + * Desc : Member Functions + ******************************************************************************/ + + + + +/****************************************************************************** + * Desc : Constructor / Deconstructor + ******************************************************************************/ + + + + +/****************************************************************************** + * Func : HLH_Cond + * Desc : Constructor of HLH_Cond + * Args : NONE + * Outs : NONE + ******************************************************************************/ +HLH_Cond::HLH_Cond () +{ + // Notify that this object not initialized + m_bInited = false; +} + + + +/****************************************************************************** + * Func : ~HLH_Cond + * Desc : Deconstructor of HLH_Cond + * Args : NONE + * Outs : NONE + ******************************************************************************/ +HLH_Cond::~ HLH_Cond () +{ + Destroy (); +} + + + + +/****************************************************************************** + * Desc : Operations + ******************************************************************************/ + + + +/****************************************************************************** + * Func : Init + * Desc : Initialize this condition + * Args : NONE + * Outs : if success, return 0, otherwise error code will be return + ******************************************************************************/ +int HLH_Cond::Init () +{ + + int ret; + + // Check if already initialized + if (m_bInited) { + HLH_DEBUG ( HLH_DEBUG_UTILS, ("already inited") ); + return HLH_COND_ERR_ALREADY_INITED; + } + + // Initialize this condition + ret = pthread_cond_init (&m_cCond, NULL); + if (ret < 0) { + HLH_DEBUG ( HLH_DEBUG_UTILS, ("can't init") ); + ret = HLH_COND_ERR_CANT_INIT; + goto fail; + } + + // Notify that this condition is initialized + m_bInited = true; + return 0; + +fail: + return ret; + +} + + + +/****************************************************************************** + * Func : HLH_Cond::Destory + * Desc : Destory this condition + * Args : NONE + * Outs : NONE + ******************************************************************************/ +void HLH_Cond::Destroy () +{ + + if (m_bInited) { + // Destory this condition + pthread_cond_destroy (&m_cCond); + // Notify that this condition not initialized + m_bInited = false; + } + +} + + +/****************************************************************************** + * Func : Wait + * Desc : Wait for this condition, called with zhmMutex locked + * Args : zhmMutex Mutex for this condition + * Outs : If success, return 0, otherwise return error code + ******************************************************************************/ +int HLH_Cond::Wait (HLH_Mutex &zhmMutex) +{ + int nRetVal; + + if (!m_bInited) + return HLH_COND_ERR_NOT_INIT; + + // Wait for this condition + nRetVal = pthread_cond_wait ( &m_cCond, &zhmMutex.m_mMutex ); + if (nRetVal < 0) { + HLH_DEBUG ( HLH_DEBUG_UTILS, ("pthread_cond_wait failed") ); + return HLH_COND_ERR_FAILED; + } + + return 0; +} + + +/****************************************************************************** + * Func : TimeWait + * Desc : Wait for condition until time reach, called with zhmMutex locked + * Args : zhmMutex Mutex for this condition + * zhtTime Max time will be waited + * Outs : If success, return 0; otherwise return error code + ******************************************************************************/ +int HLH_Cond::TimeWait (HLH_Mutex &zhmMutex, const HLH_Time &zhtTime) +{ + int nRetVal; + struct timespec tsWait; + HLH_Time zhtAbs; + + if (!m_bInited) { + HLH_DEBUG ( HLH_DEBUG_UTILS, ("not init") ); + return HLH_COND_ERR_NOT_INIT; + } + + // Set wait time + zhtAbs = HLH_Time::GetCurrentTime () + zhtTime; + tsWait.tv_sec = (time_t) zhtAbs.GetSeconds (); + tsWait.tv_nsec = (time_t) (zhtAbs.GetMicroSeconds () * 1000); + + // Wait until timeout + nRetVal = pthread_cond_timedwait (&m_cCond, &zhmMutex.m_mMutex, &tsWait); + if (nRetVal != 0) { + HLH_DEBUG ( HLH_DEBUG_UTILS, ("pthread_cond_timedwait failed") ); + return HLH_COND_ERR_FAILED; + } + + if (tsWait.tv_sec == 0 && tsWait.tv_nsec == 0) { + HLH_DEBUG ( HLH_DEBUG_UTILS, ("timeout") ); + } + + return 0; +} + + +/****************************************************************************** + * Func : Signal + * Desc : Signal one waiter that the condition has reached + * Args : NONE + * Outs : If success, return 0, otherwise return error code + ******************************************************************************/ +int HLH_Cond::Signal () +{ + int nRetVal; + + // Check if initialized + if (!m_bInited) { + HLH_DEBUG ( HLH_DEBUG_UTILS, ("not init") ); + return HLH_COND_ERR_NOT_INIT; + } + + // Signal this condition + nRetVal = pthread_cond_signal (&m_cCond); + if (nRetVal < 0) { + HLH_DEBUG ( HLH_DEBUG_UTILS, ("pthread_cond_signal failed") ); + return HLH_COND_ERR_FAILED; + } + + return 0; +} + +/****************************************************************************** + * Func : Broadcast + * Desc : Broadcast to all waiter that the condition has reached + * Args : NONE + * Outs : If success, return 0, otherwise return error code + ******************************************************************************/ +int HLH_Cond::Broadcast () +{ + int nRetVal; + + // Check if initialized + if (!m_bInited) { + HLH_DEBUG ( HLH_DEBUG_UTILS, ("not init") ); + return HLH_COND_ERR_NOT_INIT; + } + + // Broadcast this condition + nRetVal = pthread_cond_broadcast (&m_cCond); + if (nRetVal < 0) { + HLH_DEBUG ( HLH_DEBUG_UTILS, ("pthread_cond_broadcast failed") ); + return HLH_COND_ERR_FAILED; + } + + return 0; +} + + + + + + + + diff --git a/HLH_Cond.h b/HLH_Cond.h new file mode 100644 index 0000000..8438d7a --- /dev/null +++ b/HLH_Cond.h @@ -0,0 +1,102 @@ +/******************************************************************************* + * File : HLH_Cond.h + * + * Author : Henry He + * Created : 2009-10-8 11:46:20 + * Description : + ******************************************************************************/ + +#ifndef __HLH_COND_INC_20091008_114620_HENRY__ +#define __HLH_COND_INC_20091008_114620_HENRY__ + + +/******************************************************************************* + * Desc : Includes Files + ******************************************************************************/ +#include "utils/typedef.h" +#include "utils/HLH_RoundU32.h" +#include "utils/HLH_Time.h" +#include "utils/HLH_Mutex.h" + +/******************************************************************************* + * Desc : Macro Definations + ******************************************************************************/ + +//////////////////////////////////////////////////////////////////////////////// +// Error Codes + +#define HLH_COND_ERR_FAILED (-1) +#define HLH_COND_ERR_ALREADY_INITED (-2) +#define HLH_COND_ERR_NOT_INIT (-3) +#define HLH_COND_ERR_CANT_INIT (-4) + + +/******************************************************************************* + * Desc : Type Definations + ******************************************************************************/ + + +/******************************************************************************* + * Desc : Global Variables + ******************************************************************************/ + + + + + +/******************************************************************************* + * Desc : Classes + ******************************************************************************/ + +class HLH_Cond +{ + + public: + /****************************************************************************** + * Desc : Constructor / Deconstructor + ******************************************************************************/ + HLH_Cond (); + ~ HLH_Cond (); + + public: + /****************************************************************************** + * Desc : Operations + ******************************************************************************/ + + // Init this condition + int Init (); + + // Destroy this condition + void Destroy (); + + // Wait for this condition + int Wait (HLH_Mutex &zhmMutex); + + // Wait for this condition util time reach + int TimeWait (HLH_Mutex &zhmMutex, const HLH_Time &zhtTime); + + // Signal condition is reach + int Signal (); + + // Signal all the waiter condition is reach + int Broadcast (); + + private: + /****************************************************************************** + * Desc : Private Data + ******************************************************************************/ + + // Whether this instance initialized + bool m_bInited; + + // Internal condtion of HLH_Cond + pthread_cond_t m_cCond; + +}; + + + + + + +#endif /* __HLH_COND_INC_20091008_114620_HENRY__ */ diff --git a/HLH_Mutex.cpp b/HLH_Mutex.cpp new file mode 100644 index 0000000..3582afd --- /dev/null +++ b/HLH_Mutex.cpp @@ -0,0 +1,152 @@ +/******************************************************************************* + * File Name : HLH_Mutex.cpp + * + * Author : Henry He + * Created Time : 2009-10-8 11:10:40 + * Description : + ******************************************************************************/ + + +/******************************************************************************* + * Desc : Includes Files + ******************************************************************************/ +#include "utils/HLH_Mutex.h" +#include "utils/typedef.h" + + +/******************************************************************************* + * Desc : Macro Definations + ******************************************************************************/ + + +/******************************************************************************* + * Desc : Type Definations + ******************************************************************************/ + + +/******************************************************************************* + * Desc : Global Variables + ******************************************************************************/ + + +/******************************************************************************* + * Desc : File Variables + ******************************************************************************/ + + + + + +/****************************************************************************** + * Desc : Member Functions + ******************************************************************************/ + + + + + +/****************************************************************************** + * Func : HLH_Mutex::HLH_Mutex + * Desc : Constructor of HLH_Mutex + * Args : NONE + * Outs : NONE + ******************************************************************************/ +HLH_Mutex::HLH_Mutex () +{ + // Notify that this mutex is not initialized + m_bInited = false; +} + + +/****************************************************************************** + * Func : HLH_Mutex::~HLH_Mutex + * Desc : Deconstructor of HLH_Mutex + * Args : NONE + * Outs : NONE + ******************************************************************************/ +HLH_Mutex::~ HLH_Mutex () +{ + Destroy (); +} + + +/****************************************************************************** + * Func : HLH_Mutex::Destroy + * Desc : Destroy this mutex + * Args : NONE + * Outs : NONE + ******************************************************************************/ +void HLH_Mutex::Destroy () +{ + if (m_bInited) { + // Destroy this mutex + pthread_mutex_destroy(&m_mMutex); + // Notify that this mutex is not initialized + m_bInited = false; + m_bLocked = false; + } +} + +/****************************************************************************** + * Func : HLH_Mutex::Init + * Desc : Init this mutex + * Args : NONE + * Outs : if success return 0, otherwise return -1 + ******************************************************************************/ +int HLH_Mutex::Init () +{ + if (m_bInited) { + HLH_DEBUG ( HLH_DEBUG_UTILS, ("already inited") ); + return HLH_MUTEX_ERR_ALREADY_INITED; + } + + // Initialize this mutex + pthread_mutex_init (&m_mMutex, NULL); + + // Notify that this mutex is initialized + m_bInited = true; + m_bLocked = false; + + return 0; +} + + +/****************************************************************************** + * Func : HLH_Mutex::Lock + * Desc : Lock this mutex + * Args : NONE + * Outs : if success return 0, otherwise return -1 + ******************************************************************************/ +int HLH_Mutex::Lock () +{ + if (!m_bInited) { + HLH_DEBUG ( HLH_DEBUG_UTILS, ("not inited") ); + return HLH_MUTEX_ERR_NOT_INIT; + } + + pthread_mutex_lock ( &m_mMutex ); + m_bLocked = true; + + return 0; +} + + +/****************************************************************************** + * Func : HLH_Mutex::Unlock + * Desc : Unlock this mutex + * Args : NONE + * Outs : if success return 0, otherwise return -1 + ******************************************************************************/ +int HLH_Mutex::Unlock () +{ + if (!m_bInited) { + HLH_DEBUG ( HLH_DEBUG_UTILS, ("not inited") ); + return HLH_MUTEX_ERR_NOT_INIT; + } + + pthread_mutex_unlock ( &m_mMutex ); + m_bLocked = false; + + return 0; +} + diff --git a/HLH_Mutex.h b/HLH_Mutex.h new file mode 100644 index 0000000..5a4c623 --- /dev/null +++ b/HLH_Mutex.h @@ -0,0 +1,112 @@ +/******************************************************************************* + * File : HLH_Mutex.h + * + * Author : Henry He + * Created : 2009-10-8 11:08:38 + * Description : + ******************************************************************************/ + +#ifndef __HLH_MUTEX_INC_20091008_110838_HENRY__ +#define __HLH_MUTEX_INC_20091008_110838_HENRY__ + + +/******************************************************************************* + * Desc : Includes Files + ******************************************************************************/ +#include + + +/******************************************************************************* + * Desc : Macro Definations + ******************************************************************************/ +#define HLH_MUTEX_ERR_FAILED (-1) +#define HLH_MUTEX_ERR_ALREADY_INITED (-2) +#define HLH_MUTEX_ERR_NOT_INIT (-3) +#define HLH_MUTEX_ERR_CANT_INIT (-4) + + + +/******************************************************************************* + * Desc : Type Definations + ******************************************************************************/ + + +/******************************************************************************* + * Desc : Global Variables + ******************************************************************************/ + + +/******************************************************************************* + * Desc : Classes + ******************************************************************************/ +class HLH_Mutex +{ + + friend class HLH_Cond; + +public: + /****************************************************************************** + * Desc : Constructor / Deconstructor of HLH_Mutex + ******************************************************************************/ + + // Constructor of HLH_Mutex + HLH_Mutex(); + + // Deconstructor of HLH_Mutex + ~HLH_Mutex(); + +public: + /****************************************************************************** + * Desc : Operations + ******************************************************************************/ + + // Init this mutex + int Init (); + + // Destroy this mutex + void Destroy (); + + // Lock this mutex + int Lock (); + + // Unlock this mutex + int Unlock (); + + // Whether this mutex initialized + bool IsInited () { return m_bInited; } + + bool IsLocked () { return m_bLocked; } + +private: + /****************************************************************************** + * Desc : Private Data + ******************************************************************************/ + // Whether this mutex created + bool m_bInited; + + // Whether this mutex locked; + bool m_bLocked; + + // Internal mutex of HLH_Mutex + pthread_mutex_t m_mMutex; + +}; + + + +/****************************************************************************** + * Desc : Auto initialized HLH_Mutex + ******************************************************************************/ +class HLH_AutoMutex : public HLH_Mutex +{ + public: + HLH_AutoMutex () { HLH_Mutex::Init (); } + +}; + + + + + + +#endif /* __HLH_MUTEX_INC_20091008_110838_HENRY__ */ diff --git a/HLH_RoundU32.cpp b/HLH_RoundU32.cpp new file mode 100644 index 0000000..cb02046 --- /dev/null +++ b/HLH_RoundU32.cpp @@ -0,0 +1,45 @@ +/******************************************************************************* + * File Name : HLH_RoundU32.cpp + * + * Author : Henry He + * Created Time : Fri 06 Nov 2009 11:49:13 AM CST + * Description : + ******************************************************************************/ + + +/******************************************************************************* + * Desc : Includes Files + ******************************************************************************/ +#include "utils/HLH_RoundU32.h" + + +/******************************************************************************* + * Desc : Macro Definations + ******************************************************************************/ + + +/******************************************************************************* + * Desc : Type Definations + ******************************************************************************/ + + +/******************************************************************************* + * Desc : Global Variables + ******************************************************************************/ + + +/******************************************************************************* + * Desc : File Variables + ******************************************************************************/ + + +/****************************************************************************** + * Func : HLH_RoundU32::GetUINT32 + * Desc : Get UINT32 value of this instance not inline + * Args : NONE + * Outs : return UINT32 value of this instance + ******************************************************************************/ +UINT32 HLH_RoundU32::GetUINT32 () +{ + return m_unVal; +} diff --git a/HLH_RoundU32.h b/HLH_RoundU32.h new file mode 100644 index 0000000..b35a244 --- /dev/null +++ b/HLH_RoundU32.h @@ -0,0 +1,184 @@ +/******************************************************************************* + * File : ..\utils\HLH_RoundU32.h + * + * Author : Henry He + * Created : 2009-10-14 14:26:15 + * Description : + ******************************************************************************/ + +#ifndef __HLH_ROUNDU32_INC_20091014_142615_HENRY__ +#define __HLH_ROUNDU32_INC_20091014_142615_HENRY__ + + +/******************************************************************************* + * Desc : Includes Files + ******************************************************************************/ +#include "utils/typedef.h" + + +/******************************************************************************* + * Desc : Macro Definations + ******************************************************************************/ +#define HLH_ROUNDU32_RANGE ( ~ ( (UINT32) 0 ) ) +#define HLH_ROUNDU32_HALF ( HLH_ROUNDU32_RANGE / 2 ) + + +/******************************************************************************* + * Desc : Type Definations + ******************************************************************************/ + + +/******************************************************************************* + * Desc : Global Variables + ******************************************************************************/ + + +/******************************************************************************* + * Desc : Classes + ******************************************************************************/ + +//////////////////////////////////////////////////////////////////////////////// +// Class to handle round up counter + +class HLH_RoundU32 +{ + public: + /****************************************************************************** + * Desc : Constructor / Deconstructor + ******************************************************************************/ + HLH_RoundU32 (UINT32 unVal = 0) { m_unVal = unVal; } + + ~HLH_RoundU32 () {} + + public: + /****************************************************************************** + * Desc : Convertions + ******************************************************************************/ + UINT32 GetUINT32 (); + operator UINT32 () const { return m_unVal; } + operator float () const { return (float) m_unVal; } + operator double () const { return (double) m_unVal; } + + public: + /****************************************************************************** + * Desc : Operators + ******************************************************************************/ + // Preposed add + HLH_RoundU32 &operator ++ () { ++m_unVal; return *this; } + + // Preposed sub + HLH_RoundU32 &operator -- () { --m_unVal; return *this; } + + // Postpositioned add + HLH_RoundU32 operator ++ (int) { return HLH_RoundU32 (m_unVal++); } + + // Postpositioned sub + HLH_RoundU32 operator -- (int) { return HLH_RoundU32 (m_unVal--); } + + HLH_RoundU32 operator + (const HLH_RoundU32 &zhrVal) const { return HLH_RoundU32 (m_unVal + zhrVal.m_unVal); } + HLH_RoundU32 operator - (const HLH_RoundU32 &zhrVal) const { return HLH_RoundU32 (m_unVal - zhrVal.m_unVal); } + HLH_RoundU32 operator * (const HLH_RoundU32 &zhrVal) const { return HLH_RoundU32 (m_unVal * zhrVal.m_unVal); } + HLH_RoundU32 operator / (const HLH_RoundU32 &zhrVal) const { return HLH_RoundU32 (m_unVal / zhrVal.m_unVal); } + HLH_RoundU32 operator % (const HLH_RoundU32 &zhrVal) const { return HLH_RoundU32 (m_unVal % zhrVal.m_unVal); } + + HLH_RoundU32 &operator = (UINT32 unVal) { m_unVal = unVal; return *this; } + + HLH_RoundU32 &operator += (const HLH_RoundU32 &zhrVal) { m_unVal += zhrVal.m_unVal; return *this; } + HLH_RoundU32 &operator -= (const HLH_RoundU32 &zhrVal) { m_unVal -= zhrVal.m_unVal; return *this; } + HLH_RoundU32 &operator *= (const HLH_RoundU32 &zhrVal) { m_unVal *= zhrVal.m_unVal; return *this; } + HLH_RoundU32 &operator /= (const HLH_RoundU32 &zhrVal) { m_unVal /= zhrVal.m_unVal; return *this; } + HLH_RoundU32 &operator %= (const HLH_RoundU32 &zhrVal) { m_unVal %= zhrVal.m_unVal; return *this; } + + bool operator < (const HLH_RoundU32 &zhrVal) const; + bool operator > (const HLH_RoundU32 &zhrVal) const; + bool operator <= (const HLH_RoundU32 &zhrVal) const; + bool operator >= (const HLH_RoundU32 &zhrVal) const; + + bool operator == (const HLH_RoundU32 &zhrVal) const { return ( m_unVal == zhrVal.m_unVal ); } + bool operator != (const HLH_RoundU32 &zhrVal) const { return ( m_unVal != zhrVal.m_unVal ); } + + public: + UINT32 m_unVal; +}; + + + + +/****************************************************************************** + * Desc : Operators + ******************************************************************************/ + + +inline bool HLH_RoundU32::operator < (const HLH_RoundU32 &zhrVal) const +{ + + UINT32 unDivide; + UINT32 unVal; + + unVal = zhrVal.m_unVal; + unDivide = unVal + HLH_ROUNDU32_HALF; + + if (unVal < unDivide) { + return ( (m_unVal < unVal) || (m_unVal > unDivide) ); + } else { + return (m_unVal > unDivide && m_unVal < unVal); + } + +} + +inline bool HLH_RoundU32::operator > (const HLH_RoundU32 &zhrVal) const +{ + + UINT32 unDivide; + UINT32 unVal; + + unVal = zhrVal.m_unVal; + unDivide = unVal + HLH_ROUNDU32_HALF; + + if (unVal < unDivide) { + return ( m_unVal > unVal && m_unVal <= unDivide ); + } else { + return ( m_unVal <= unDivide || m_unVal > unVal ); + } + +} + +inline bool HLH_RoundU32::operator <= (const HLH_RoundU32 &zhrVal) const +{ + + UINT32 unDivide; + UINT32 unVal; + + unVal = zhrVal.m_unVal; + unDivide = unVal + HLH_ROUNDU32_HALF; + + if (unVal < unDivide) { + return ( (m_unVal <= unVal) || (m_unVal > unDivide) ); + } else { + return (m_unVal > unDivide && m_unVal <= unVal); + } + +} + +inline bool HLH_RoundU32::operator >= (const HLH_RoundU32 &zhrVal) const +{ + + UINT32 unDivide; + UINT32 unVal; + + unVal = zhrVal.m_unVal; + unDivide = unVal + HLH_ROUNDU32_HALF; + + if (unVal < unDivide) { + return ( m_unVal >= unVal && m_unVal <= unDivide ); + } else { + return ( m_unVal <= unDivide || m_unVal >= unVal); + } + +} + + + + + +#endif /* __HLH_ROUNDU32_INC_20091014_142615_HENRY__ */ diff --git a/HLH_Sock.cpp b/HLH_Sock.cpp new file mode 100644 index 0000000..791fb0d --- /dev/null +++ b/HLH_Sock.cpp @@ -0,0 +1,839 @@ +/******************************************************************************* + * File Name : HLH_Sock.cpp + * + * Author : Henry He + * Created Time : 2009-10-8 13:56:06 + * Description : + ******************************************************************************/ + + +/******************************************************************************* + * Desc : Includes Files + ******************************************************************************/ + + +#include "utils/typedef.h" +#include "utils/HLH_Sock.h" + + +/******************************************************************************* + * Desc : Macro Definations + ******************************************************************************/ + + +/******************************************************************************* + * Desc : Type Definations + ******************************************************************************/ + + +/******************************************************************************* + * Desc : Global Variables + ******************************************************************************/ + + +/******************************************************************************* + * Desc : File Variables + ******************************************************************************/ + + + + +/****************************************************************************** + * Desc : Member Functions + ******************************************************************************/ + + + + + +/****************************************************************************** + * Desc : Constructor / Deconstructor + ******************************************************************************/ + + + +/****************************************************************************** + * Func : HLH_Sock::HLH_Sock + * Desc : Constructor of HLH_Sock + * Args : NONE + * Outs : NONE + ******************************************************************************/ +HLH_Sock::HLH_Sock () +{ + + // Init member variables + m_fdSocket = -1; + m_bCreated = false; + + // Init Mutexs + m_zhmMainMutex.Init (); + m_zhmSendMutex.Init (); + m_zhmRecvMutex.Init (); +} + + + +/****************************************************************************** + * Func : HLH_Sock::~HLH_Sock + * Desc : Deconstructor of HLH_Sock + * Args : NONE + * Outs : NONE + ******************************************************************************/ +HLH_Sock::~HLH_Sock () +{ + Destroy (); +} + + + +/****************************************************************************** + * Desc : Common Operations + ******************************************************************************/ + + + + + +/****************************************************************************** + * Func : HLH_Sock::Create + * Desc : Initialize the socket and bind it + * Args : zhsSockType socket type + * zhsSockAddr address to be binded + * bNonblocking whether create this socket nonblocking + * (default is blocking) + * Outs : if success, return 0, otherwise return error code + ******************************************************************************/ +int HLH_Sock::Create (HLH_SockType zhsSockType, + const HLH_SockAddr &zhsSockAddr, bool bNonblocking) +{ + int nRetVal; + int nFlags; + + // Lock before process + m_zhmMainMutex.Lock (); + + if (m_bCreated) { + // Unlock after process + m_zhmMainMutex.Unlock (); + HLH_DEBUG ( HLH_DEBUG_MAIN, ("already created") ); + return HLH_SOCK_ERR_ALREADY_CREATED; + } + + // Create socket + switch (zhsSockType) { + case HLH_SOCK_TYPE_STREAM: + m_fdSocket = socket (AF_INET, SOCK_STREAM, 0); + break; + + case HLH_SOCK_TYPE_DGRAM: + m_fdSocket = socket (AF_INET, SOCK_DGRAM, 0); + break; + + default: + HLH_DEBUG ( HLH_DEBUG_UTILS, ("sock type not supported") ); + goto failed; + } + + if (m_fdSocket < 0) { + HLH_DEBUG ( HLH_DEBUG_MAIN, ("can't create socket") ); + goto failed; + } + + // Set Nonblocking if request + if (bNonblocking) { + nFlags = fcntl (m_fdSocket, F_GETFL, 0); + nRetVal = fcntl (m_fdSocket, F_SETFL, nFlags|O_NONBLOCK); + + if (nRetVal < 0) { + HLH_DEBUG ( HLH_DEBUG_MAIN, ("can't set nonblock") ); + goto failed; + } + } + + // Bind on specific address + nRetVal = bind ( m_fdSocket, zhsSockAddr.GetAddrPointer (), + zhsSockAddr.GetAddrLen () ); + + if (nRetVal < 0) { + HLH_DEBUG ( HLH_DEBUG_MAIN, ("can't bind on address") ); + goto failed; + } + + // Prepare fd_set for poll + FD_ZERO (&m_fsReadSet); + FD_ZERO (&m_fsWriteSet); + FD_ZERO (&m_fsErrorSet); + + FD_SET (m_fdSocket, &m_fsReadSet); + FD_SET (m_fdSocket, &m_fsWriteSet); + FD_SET (m_fdSocket, &m_fsErrorSet); + + // Notify that this socket has been create + m_bCreated = true; + + // Unlock after process + m_zhmMainMutex.Unlock (); + return 0; + +failed: + + // Close socket if failed after create + if (m_fdSocket >= 0) { + close (m_fdSocket); + m_fdSocket = -1; + } + + // Unlock after process + m_zhmMainMutex.Unlock (); + + return HLH_SOCK_ERR_CANT_CREATE; + +} + + +/****************************************************************************** + * Func : HLH_Sock::Create + * Desc : Initialize the socket from a initialized socket file descriptor + * Args : fdSock file descriptor to create from + * Outs : if success return 0, otherwise return error code + ******************************************************************************/ +int HLH_Sock::Create (int fdSock, bool bNonblocking) +{ + + int nRetVal; + int nFlags; + + // Lock before process + m_zhmMainMutex.Lock (); + + if (m_bCreated) { + // Unlock after process + m_zhmMainMutex.Unlock (); + HLH_DEBUG ( HLH_DEBUG_MAIN, ("already created") ); + return HLH_SOCK_ERR_ALREADY_CREATED; + } + + m_fdSocket = fdSock; + + // Set Nonblocking if request + if (bNonblocking) { + nFlags = fcntl (m_fdSocket, F_GETFL, 0); + nRetVal = fcntl (m_fdSocket, F_SETFL, nFlags|O_NONBLOCK); + + if (nRetVal < 0) { + HLH_DEBUG ( HLH_DEBUG_MAIN, ("can't set nonblock") ); + goto failed; + } + } + + + // Prepare fd_set for poll + FD_ZERO (&m_fsReadSet); + FD_ZERO (&m_fsWriteSet); + FD_ZERO (&m_fsErrorSet); + + FD_SET (m_fdSocket, &m_fsReadSet); + FD_SET (m_fdSocket, &m_fsWriteSet); + FD_SET (m_fdSocket, &m_fsErrorSet); + + // Notify that this socket has been created + m_bCreated = true; + + // Unlock after process + m_zhmMainMutex.Unlock (); + + return 0; + +failed: + + // Unlock after process + m_zhmMainMutex.Unlock (); + + return HLH_SOCK_ERR_CANT_CREATE; + +} + + +/****************************************************************************** + * Func : HLH_Sock::Destroy + * Desc : Destroy the socket, will wait for send/pollwait operations + * Args : NONE + * Outs : If success, return 0, otherwise return error code + ******************************************************************************/ +int HLH_Sock::Destroy () +{ + + // Wait for send operations + m_zhmSendMutex.Lock (); + + // Wait for operations other than read/write + m_zhmMainMutex.Lock (); + + // if not create, can't close + if (!m_bCreated) { + // Unlock operations other than read/write + m_zhmMainMutex.Unlock (); + + // Unlock send operations + m_zhmSendMutex.Unlock (); + + return HLH_SOCK_ERR_NOT_CREATED; + } + + + // Receive operations won't be waited + + // Close the socket now + close (m_fdSocket); + m_fdSocket = -1; + m_bCreated = false; + + + // Unlock operations other than read/write + m_zhmMainMutex.Unlock (); + + // Unlock send operations + m_zhmSendMutex.Unlock (); + + return 0; +} + + + + +/****************************************************************************** + * Func : HLH_Sock::IsCreated + * Desc : Whether this socket created + * Args : NONE + * Outs : If created return true, otherwise return false + ******************************************************************************/ +bool HLH_Sock::IsCreated () +{ + bool bCreated; + + m_zhmMainMutex.Lock (); + bCreated = m_bCreated; + m_zhmMainMutex.Unlock (); + + return bCreated; +} + + + + +/****************************************************************************** + * Func : HLH_Sock::Listen + * Desc : Listen for socket connections and limit the queue of incoming connections + * Args : unQueue length of listen queue + * Outs : if success, return 0, otherwise return error code + ******************************************************************************/ +int HLH_Sock::Listen (UINT32 unQueue) +{ + int nRetVal; + + // Lock before process + m_zhmMainMutex.Lock (); + + // Can't listen on socket which is not created + if (!m_bCreated) { + // Unlock after process + m_zhmMainMutex.Unlock (); + + HLH_DEBUG ( HLH_DEBUG_UTILS, ("not created") ); + return HLH_SOCK_ERR_NOT_CREATED; + } + + nRetVal = listen (m_fdSocket, unQueue); + if (nRetVal < 0) { + HLH_DEBUG ( HLH_DEBUG_UTILS, ("listen failed") ); + goto failed; + } + + // Unlock after process + m_zhmMainMutex.Unlock (); + + return 0; + +failed: + // Unlock after process + m_zhmMainMutex.Unlock (); + + return HLH_SOCK_ERR_FAILED; +} + + +/****************************************************************************** + * Func : HLH_Sock::Connect + * Desc : Requests a connection to be made on a socket + * Args : zhsPeekAddr address to connect + * Outs : if success, return 0, otherwise return error code + ******************************************************************************/ +int HLH_Sock::Connect (const HLH_SockAddr &zhsPeekAddr) +{ + int nRetVal; + + // Lock before process + m_zhmMainMutex.Lock (); + + // Can't Connect on socket which is not created + if (!m_bCreated) { + // Unlock after process + m_zhmMainMutex.Unlock (); + + HLH_DEBUG ( HLH_DEBUG_UTILS, ("not created") ); + return HLH_SOCK_ERR_NOT_CREATED; + } + + // Connect to specific address + nRetVal = connect ( m_fdSocket, zhsPeekAddr.GetAddrPointer (), + zhsPeekAddr.GetAddrLen () ); + if (nRetVal < 0) { + HLH_DEBUG ( HLH_DEBUG_UTILS, ("connect failed") ); + goto failed; + } + + // Unlock after process + m_zhmMainMutex.Unlock (); + + return 0; + +failed: + // Unlock after process + m_zhmMainMutex.Unlock (); + + return HLH_SOCK_ERR_FAILED; +} + + +/****************************************************************************** + * Func : HLH_Sock::Accept + * Desc : Extracts the first connection on the queue of pending connections, + * creates a new socket with the same socket type protocol and address family as the specified socket, + * and allocates a new file descriptor for that socket + * Args : zhsPeerAddr address of peer comming in + * Outs : if success, return 0, otherwise return error code + ******************************************************************************/ +int HLH_Sock::Accept (HLH_SockAddr &zhsPeerAddr) +{ + int nRetVal; + socklen_t slLen; + + // Lock before process + m_zhmMainMutex.Lock (); + + // Can't Accept on socket which is not created + if (!m_bCreated) { + // Unlock after process + m_zhmMainMutex.Unlock (); + + HLH_DEBUG ( HLH_DEBUG_UTILS, ("not created") ); + return HLH_SOCK_ERR_NOT_CREATED; + } + + // Wait and accept connections + slLen = zhsPeerAddr.GetAddrLen (); + nRetVal = accept ( m_fdSocket, zhsPeerAddr.GetAddrPointer(), &slLen ); + if (nRetVal < 0) { + HLH_DEBUG ( HLH_DEBUG_UTILS, ("accept failed") ); + goto failed; + } + + // Unlock after process + m_zhmMainMutex.Unlock (); + + return nRetVal; + +failed: + // Unlock after process + m_zhmMainMutex.Unlock (); + + return HLH_SOCK_ERR_FAILED; +} + + +/****************************************************************************** + * Func : HLH_Sock::Send + * Desc : Initiates transmission of a message from the specified socket to its peer + * Args : pvBuf pointer of buffer + * unLen length of buffer + * unFlags flags used by send () + * Outs : if success return the length of data send, otherwise error code + ******************************************************************************/ +int HLH_Sock::Send (const void *pvBuf, UINT32 unLen, UINT32 unFlags) +{ + int nRetVal; + + // Lock the send mutex + m_zhmSendMutex.Lock (); + + // Lock before process + m_zhmMainMutex.Lock (); + + // Can't send on socket not created + if (!m_bCreated) { + + // Unlock after process + m_zhmMainMutex.Unlock (); + + // Unlock send mutex + m_zhmSendMutex.Unlock (); + + HLH_DEBUG ( HLH_DEBUG_UTILS, ("not created") ); + return HLH_SOCK_ERR_NOT_CREATED; + } + + // Unlock after process + m_zhmMainMutex.Unlock (); + + // Send the data now + nRetVal = send (m_fdSocket, pvBuf, unLen, unFlags/*|MSG_NOSIGNAL*/); + + // Unlock send mutex after send + m_zhmSendMutex.Unlock (); + + if (nRetVal < 0) { + HLH_DEBUG ( HLH_DEBUG_UTILS, ("send failed") ); + goto failed; + } + + return nRetVal; +failed: + // Unlock send mutex + m_zhmSendMutex.Unlock (); + + return HLH_SOCK_ERR_FAILED; +} + + +/****************************************************************************** + * Func : HLH_Sock::Recv + * Desc : Receives a message from a connection-mode or connectionless-mode socket + * Args : pvBuf pointer to buffer + * unLen length of buffer + * unFlags flags used by recv () + * Outs : if success, return length of data received, otherwise return error code + ******************************************************************************/ +int HLH_Sock::Recv (void *pvBuf, UINT32 unLen, UINT32 unFlags) +{ + int nRetVal; + + // Lock before Receiving + m_zhmRecvMutex.Lock (); + + // Lock before process + m_zhmMainMutex.Lock (); + if (!m_bCreated) { + + // Unlock receive mutex + m_zhmRecvMutex.Unlock (); + + // Unlock after process + m_zhmMainMutex.Unlock (); + + HLH_DEBUG ( HLH_DEBUG_UTILS, ("not created") ); + return HLH_SOCK_ERR_NOT_CREATED; + } + + // Unlock after process + m_zhmMainMutex.Unlock (); + + nRetVal = recv (m_fdSocket, pvBuf, unLen, unFlags/*|MSG_NOSIGNAL*/); + + // Unlock after receiving + m_zhmRecvMutex.Unlock (); + + if (nRetVal < 0) { + HLH_DEBUG ( HLH_DEBUG_UTILS, ("receive failed") ); + goto failed; + } + + return nRetVal; + +failed: + return HLH_SOCK_ERR_FAILED; + +} + + +/****************************************************************************** + * Func : HLH_Sock::SendTo + * Desc : Sends a message through a connection-mode or connectionless-mode socket + * Args : pvBuf pointer of buffer + * unLen length of buffer + * zhsSockAddr address of peer + * unFlags flags used by send () + * Outs : if success return the length of data send, otherwise error code + ******************************************************************************/ +int HLH_Sock::SendTo (const void *pvBuf, UINT32 unLen, + const HLH_SockAddr &zhsSockAddr, UINT32 unFlags) +{ + int nRetVal; + + // Lock the send mutex + m_zhmSendMutex.Lock (); + + // Lock before process + m_zhmMainMutex.Lock (); + + if (!m_bCreated) { + // Unlock after process + m_zhmMainMutex.Unlock (); + + // Unlock send mutex + m_zhmSendMutex.Unlock (); + + HLH_DEBUG ( HLH_DEBUG_UTILS, ("not created") ); + return HLH_SOCK_ERR_NOT_CREATED; + } + + // Unlock after process + m_zhmMainMutex.Unlock (); + + // Send the data now + nRetVal = sendto ( m_fdSocket, pvBuf, unLen, unFlags, + zhsSockAddr.GetAddrPointer (), zhsSockAddr.GetAddrLen () ); + + // Unlock send mutex after send + m_zhmSendMutex.Unlock (); + + if (nRetVal < 0) { + HLH_DEBUG ( HLH_DEBUG_UTILS, ("sendto failed") ); + goto failed; + } + + return nRetVal; + +failed: + return HLH_SOCK_ERR_FAILED; + +} + + +/****************************************************************************** + * Func : HLH_Sock::RecvFrom + * Desc : Receives a message from a connection-mode or connectionless-mode socket + * Args : pvBuf pointer to buffer + * unLen length of buffer + * zhsPeerAddr used to store peer address + * unFlags flags used by recv () + * Outs : if success, return length of data received, otherwise return error code + ******************************************************************************/ +int HLH_Sock::RecvFrom (void *pvBuf, UINT32 unLen, + HLH_SockAddr &zhsPeekAddr, UINT32 unFlags) +{ + int nRetVal; + socklen_t slLen; + + // Lock before Receiving + m_zhmRecvMutex.Lock (); + + // Lock before process + m_zhmMainMutex.Lock (); + if (!m_bCreated) { + // Unlock after process + m_zhmMainMutex.Unlock (); + + // Unlock after Receiving + m_zhmRecvMutex.Unlock (); + + HLH_DEBUG ( HLH_DEBUG_UTILS, ("not created") ); + return HLH_SOCK_ERR_NOT_CREATED; + } + + // Unlock after process + m_zhmMainMutex.Unlock (); + + // Send the data now + slLen = zhsPeekAddr.GetAddrLen (); + nRetVal = recvfrom (m_fdSocket, pvBuf, unLen, unFlags, + zhsPeekAddr.GetAddrPointer (), &slLen); + + // Unlock after receiving + m_zhmRecvMutex.Unlock (); + + // Format return value + if (nRetVal < 0) { + HLH_DEBUG ( HLH_DEBUG_UTILS, ("recvfrom failed") ); + goto failed; + } + + return nRetVal; + +failed: + return HLH_SOCK_ERR_FAILED; + +} + + + + + +/****************************************************************************** + * Desc : Poll Operations + ******************************************************************************/ + + + + +/****************************************************************************** + * Func : HLH_Sock::Poll + * Desc : Poll for specific events + * Args : unPollType poll type + * Outs : if success return number of events, otherwise return error code + ******************************************************************************/ +int HLH_Sock::Poll (UINT32 &unPollType) +{ + fd_set fsReadSet; + fd_set fsWriteSet; + fd_set fsErrorSet; + + int nRetVal; + UINT32 unPollTypeNew; + + // Lock before process + m_zhmMainMutex.Lock (); + + if (!m_bCreated) { + // Unlock after process + m_zhmMainMutex.Unlock (); + + HLH_DEBUG ( HLH_DEBUG_UTILS, ("not created") ); + return HLH_SOCK_ERR_NOT_CREATED; + } + + memcpy ( &fsReadSet, &m_fsReadSet, sizeof (fd_set) ); + memcpy ( &fsWriteSet, &m_fsWriteSet, sizeof (fd_set) ); + memcpy ( &fsErrorSet, &m_fsErrorSet, sizeof (fd_set) ); + + // Unlock after process, + // Unlock before poll as we may want to close before poll successed + m_zhmMainMutex.Unlock (); + + // Poll for specific event + nRetVal = select ( m_fdSocket + 1, + unPollType & HLH_SOCK_POLL_READ ? &fsReadSet : NULL, + unPollType & HLH_SOCK_POLL_WRITE ? &fsWriteSet : NULL, + unPollType & HLH_SOCK_POLL_ERROR ? &fsErrorSet : NULL, + NULL + ); + + if (nRetVal < 0) { + HLH_DEBUG ( HLH_DEBUG_UTILS, ("select failed") ); + goto failed; + } + + // Set return value + + unPollTypeNew = 0; + + if ( ( unPollType & HLH_SOCK_POLL_READ ) + && FD_ISSET (m_fdSocket, &fsReadSet) ) { + unPollTypeNew |= HLH_SOCK_POLL_READ; + } + + if ( ( unPollType & HLH_SOCK_POLL_WRITE ) + && FD_ISSET (m_fdSocket, &fsWriteSet) ) { + unPollTypeNew |= HLH_SOCK_POLL_WRITE; + } + + if ( ( unPollType & HLH_SOCK_POLL_ERROR ) + && FD_ISSET (m_fdSocket, &fsErrorSet) ) { + unPollTypeNew |= HLH_SOCK_POLL_ERROR; + } + + unPollType = unPollTypeNew; + + return nRetVal; + +failed: + + return HLH_SOCK_ERR_FAILED; +} + + +/****************************************************************************** + * Func : HLH_Sock::PollWait + * Desc : Poll for specific events until time eclipsed + * Args : unPollType poll type + * zhtTime time to wait before poll success + * Outs : if success return number of events, otherwise return error code + * if failed, the time left will return in zhtTime + ******************************************************************************/ +int HLH_Sock::PollWait (UINT32 &unPollType, HLH_Time &zhtTime) +{ + fd_set fsReadSet; + fd_set fsWriteSet; + fd_set fsErrorSet; + + int nRetVal; + UINT32 unPollTypeNew; + + struct timeval tvTime; + + // Lock before process + m_zhmMainMutex.Lock (); + + if (!m_bCreated) { + // Unlock after process + m_zhmMainMutex.Unlock (); + + HLH_DEBUG ( HLH_DEBUG_UTILS, ("not created") ); + return HLH_SOCK_ERR_NOT_CREATED; + } + + memcpy ( &fsReadSet, &m_fsReadSet, sizeof (fd_set) ); + memcpy ( &fsWriteSet, &m_fsWriteSet, sizeof (fd_set) ); + memcpy ( &fsErrorSet, &m_fsErrorSet, sizeof (fd_set) ); + + // Poll for specific event + tvTime = zhtTime.GetTimeVal (); + + // Unlock after process + m_zhmMainMutex.Unlock (); + + nRetVal = select ( m_fdSocket + 1, + unPollType & HLH_SOCK_POLL_READ ? &fsReadSet : NULL, + unPollType & HLH_SOCK_POLL_WRITE ? &fsWriteSet : NULL, + unPollType & HLH_SOCK_POLL_ERROR ? &fsErrorSet : NULL, + &tvTime + ); + + if (nRetVal < 0) { + HLH_DEBUG ( HLH_DEBUG_UTILS, ("select failed") ); + goto failed; + } + + // Return the time left + zhtTime.SetTime (tvTime); + + // Set return value + + unPollTypeNew = 0; + + if ( ( unPollType & HLH_SOCK_POLL_READ ) + && FD_ISSET (m_fdSocket, &fsReadSet) ) { + unPollTypeNew |= HLH_SOCK_POLL_READ; + } + + if ( ( unPollType & HLH_SOCK_POLL_WRITE ) + && FD_ISSET (m_fdSocket, &fsWriteSet) ) { + unPollTypeNew |= HLH_SOCK_POLL_WRITE; + } + + if ( ( unPollType & HLH_SOCK_POLL_ERROR ) + && FD_ISSET (m_fdSocket, &fsErrorSet) ) { + unPollTypeNew |= HLH_SOCK_POLL_ERROR; + } + + unPollType = unPollTypeNew; + + return nRetVal; + +failed: + return HLH_SOCK_ERR_FAILED; + +} + + + + diff --git a/HLH_Sock.h b/HLH_Sock.h new file mode 100644 index 0000000..967a28f --- /dev/null +++ b/HLH_Sock.h @@ -0,0 +1,272 @@ +/******************************************************************************* + * File : HLH_Sock.h + * + * Author : Henry He + * Created : 2009-10-8 13:55:48 + * Description : + ******************************************************************************/ + +#ifndef __HLH_SOCK_INC_20091008_135548_HENRY__ +#define __HLH_SOCK_INC_20091008_135548_HENRY__ + + +/******************************************************************************* + * Desc : Includes Files + ******************************************************************************/ + +#include "utils/typedef.h" +#include "utils/HLH_Time.h" +#include "utils/HLH_Thread.h" + + +/******************************************************************************* + * Desc : Macro Definations + ******************************************************************************/ + + +//////////////////////////////////////////////////////////////////////////////// +// Poll Types + +#define HLH_SOCK_POLL_READ 0x01 +#define HLH_SOCK_POLL_WRITE 0x02 +#define HLH_SOCK_POLL_ERROR 0x04 + +//////////////////////////////////////////////////////////////////////////////// +// Error Types + +#define HLH_SOCK_ERR_FAILED (-1) +#define HLH_SOCK_ERR_ALREADY_CREATED (-2) +#define HLH_SOCK_ERR_CANT_CREATE (-3) +#define HLH_SOCK_ERR_NOT_CREATED (-4) + +//////////////////////////////////////////////////////////////////////////////// +// Default Values + +#define HLH_SOCK_DEFAULT_LISTEN_QUEUE_NUM 10 + + +/******************************************************************************* + * Desc : Type Definations + ******************************************************************************/ + +//////////////////////////////////////////////////////////////////////////////// +// Socket type + +typedef enum HLH_SockType +{ + HLH_SOCK_TYPE_STREAM = 0x01, + HLH_SOCK_TYPE_DGRAM = 0x02 +} HLH_SockType; + + + +/******************************************************************************* + * Desc : Global Variables + ******************************************************************************/ + + +/******************************************************************************* + * Desc : Classes + ******************************************************************************/ + + + + +//////////////////////////////////////////////////////////////////////////////// +// Socket Address : simple wrapper of sockaddr_t + +class HLH_SockAddr +{ + friend class HLH_Sock; + + public: + + /****************************************************************************** + * Desc : Constructor / Deconstructor + ******************************************************************************/ + + HLH_SockAddr () { + m_saAddr.sin_addr.s_addr = htonl ( INADDR_ANY ); + m_saAddr.sin_port = htons ( rand() & 0xFFFF ); + m_saAddr.sin_family = AF_INET; + } + + HLH_SockAddr (UINT32 unIP, UINT16 usPort, sa_family_t sfFamily = AF_INET) { + m_saAddr.sin_addr.s_addr = htonl ( unIP ); + m_saAddr.sin_port = htons ( usPort ); + m_saAddr.sin_family = sfFamily; + } + + HLH_SockAddr (const char *pcIP, UINT16 usPort, sa_family_t sfFamily = AF_INET) { + m_saAddr.sin_addr.s_addr = inet_addr ( pcIP ); + m_saAddr.sin_port = htons ( usPort ); + m_saAddr.sin_family = sfFamily; + } + + ~HLH_SockAddr () { } + + /****************************************************************************** + * Desc : Operations + ******************************************************************************/ + + struct sockaddr * GetAddrPointer () const { return (struct sockaddr *)(&m_saAddr); } + + UINT32 GetAddrLen () const { return sizeof (m_saAddr); } + + void SetAddr (struct sockaddr_in &saAddr) { + m_saAddr = saAddr; + } + + void SetAddr (UINT32 unIP, UINT16 usPort, sa_family_t sfFamily = AF_INET) { + m_saAddr.sin_addr.s_addr = htonl ( unIP ); + m_saAddr.sin_port = htons ( usPort ); + m_saAddr.sin_family = sfFamily; + } + + void SetAddr (const char *pcIP, UINT16 usPort, sa_family_t sfFamily = AF_INET) { + m_saAddr.sin_addr.s_addr = inet_addr (pcIP); + m_saAddr.sin_port = htons ( usPort ); + m_saAddr.sin_family = sfFamily; + } + + void SetAddrFamily (sa_family_t sfFamily) { + m_saAddr.sin_family = sfFamily; + } + + void SetAddrPort (UINT16 usPort) { + m_saAddr.sin_port = htons ( usPort ); + } + + void SetAddrIP (UINT32 unIP) { + m_saAddr.sin_addr.s_addr = htonl ( unIP ); + } + + int SetAddrIP (const char *pcIP) { + in_addr_t s_addr; + + s_addr = inet_addr (pcIP); + if ( s_addr == (in_addr_t)(-1) ) { + return -1; + } + m_saAddr.sin_addr.s_addr = inet_addr (pcIP); + return 0; + } + + + bool operator == (const HLH_SockAddr &zhsAddr) const { + return ( m_saAddr.sin_addr.s_addr == zhsAddr.m_saAddr.sin_addr.s_addr + && m_saAddr.sin_port == zhsAddr.m_saAddr.sin_port + && m_saAddr.sin_family == zhsAddr.m_saAddr.sin_family ); + } + + private: + struct sockaddr_in m_saAddr; +}; + + + +//////////////////////////////////////////////////////////////////////////////// +// Class of HLH_Sock: simple wrapper of socket funtions + +class HLH_Sock +{ + + public: + /****************************************************************************** + * Desc : Constructor / Deconstructor + ******************************************************************************/ + HLH_Sock (); + ~HLH_Sock (); + + public: + /****************************************************************************** + * Desc : Common Operations + ******************************************************************************/ + + // Initialize the socket and bind it + int Create (HLH_SockType zhsSockType, + const HLH_SockAddr &zhsSockAddr, bool bNonblocking = false); + + // Initialize the socket from a initialized socket + int Create (int fdSock, bool bNonblocking = false); + + // Whether this socket created + bool IsCreated (); + + // Listen for socket connections and limit the queue of incoming connections + int Listen (UINT32 unQueue = HLH_SOCK_DEFAULT_LISTEN_QUEUE_NUM); + + // Requests a connection to be made on a socket + int Connect (const HLH_SockAddr &zhsSockAddr); + + // Extracts the first connection on the queue of pending connections, + // creates a new socket with the same socket type protocol and address family as the specified socket, + // and allocates a new file descriptor for that socket + int Accept (HLH_SockAddr &zhsSockAddr); + + // Initiates transmission of a message from the specified socket to its peer + int Send (const void *pvBuf, UINT32 unLen, UINT32 unFlags = 0); + + // Receives a message from a connection-mode or connectionless-mode socket + int Recv (void *pvBuf, UINT32 unLen, UINT32 unFlags = 0); + + // Sends a message through a connection-mode or connectionless-mode socket + int SendTo (const void *pvBuf, UINT32 unLen, + const HLH_SockAddr &zhsSockAddr, UINT32 unFlags); + + // Receives a message from a connection-mode or connectionless-mode socket + int RecvFrom (void *pvBuf, UINT32 unLen, + HLH_SockAddr &zhsSockAddr, UINT32 unFlags = 0); + + // Destroy the socket (wait until send/pollwait operations finished) + int Destroy (); + + + + + /****************************************************************************** + * Desc : Poll Operations + ******************************************************************************/ + + // Poll for specific events + int Poll (UINT32 &unPollType); + + // Poll for specific events until time eclipsed + int PollWait (UINT32 &unPollType, HLH_Time &zhtTime); + + + private: + /****************************************************************************** + * Desc : Private Data + ******************************************************************************/ + + // socket file descriptor + int m_fdSocket; + + // Whether this socket is created + bool m_bCreated; + + + // Master Lock of HLH_Sock + HLH_Mutex m_zhmMainMutex; + + // Read Lock + HLH_Mutex m_zhmRecvMutex; + + // Write Lock + HLH_Mutex m_zhmSendMutex; + + + // Poll file discriptor set: initialized when created + fd_set m_fsReadSet; + fd_set m_fsWriteSet; + fd_set m_fsErrorSet; + +}; + + + + + + +#endif /* __HLH_SOCK_INC_20091008_135548_HENRY__ */ diff --git a/HLH_Thread.cpp b/HLH_Thread.cpp new file mode 100644 index 0000000..0275f68 --- /dev/null +++ b/HLH_Thread.cpp @@ -0,0 +1,330 @@ +/******************************************************************************* + * File Name : HLH_Thread.cpp + * + * Author : Henry He + * Created Time : 2009-10-8 11:18:18 + * Description : + ******************************************************************************/ + + +/******************************************************************************* + * Desc : Includes Files + ******************************************************************************/ + +#include +#include + +#include "utils/HLH_Thread.h" + + + +/******************************************************************************* + * Desc : Macro Definations + ******************************************************************************/ + + +/******************************************************************************* + * Desc : Type Definations + ******************************************************************************/ + + +/******************************************************************************* + * Desc : Global Variables + ******************************************************************************/ + + +/******************************************************************************* + * Desc : File Variables + ******************************************************************************/ + + + + + +/****************************************************************************** + * Desc : Member Functions + ******************************************************************************/ + + + + +/****************************************************************************** + * Func : HLH_Thread::HLH_Thread + * Desc : Constructor of HLH_Thread + * Args : NONE + * Outs : NONE + ******************************************************************************/ +HLH_Thread::HLH_Thread () +{ + m_zhmRunMutex.Init (); + m_zhmContinueMutex.Init (); + m_zhmContinueMutex2.Init (); + + m_pvRetval = NULL; + m_bRunning = false; + m_bStopping = false; +} + + +/****************************************************************************** + * Func : HLH_Thread::~HLH_Thread + * Desc : Deconstructor of HLH_Thread + * Args : NONE + * Outs : NONE + ******************************************************************************/ +HLH_Thread::~HLH_Thread () +{ + Kill(); +} + + +/****************************************************************************** + * Func : HLH_Thread::Start + * Desc : Start a thread calling \c ztfThreadFunc (pvThreadParam) + * Args : ztfThreadFunc Function called in this thread + * Outs : If success return 0, otherwise return error code. + ******************************************************************************/ +int HLH_Thread::Start (ThreadFunc ztfThreadFunc, void *pvThreadParam) +{ + int nRetval; + + // Force TheThread () to wait before call m_ztfThreadFunc (): + // TheThread () will set m_bRunning + // and wait Start () to confirm before it call m_ztfThreadFunc () + m_zhmContinueMutex.Lock (); + + // Check if already running + m_zhmRunMutex.Lock (); + if (m_bRunning) { + m_zhmRunMutex.Unlock (); + return HLH_THREAD_ERR_ALREADY_RUNNING; + } + m_zhmRunMutex.Unlock (); + + // Set Thread Function + m_ztfThreadFunc = ztfThreadFunc; + m_pvThreadParam = pvThreadParam; + + pthread_attr_t attr; + pthread_attr_init (&attr); + pthread_attr_setdetachstate (&attr,PTHREAD_CREATE_DETACHED); + + nRetval = pthread_create (&m_zpThreadId, &attr, TheThread, this); + pthread_attr_destroy (&attr); + if (nRetval != 0) { + m_zhmContinueMutex.Unlock (); + return HLH_THREAD_ERR_CANT_START_THREAD; + } + + /* Wait until 'm_bRunning' is set */ + + m_zhmRunMutex.Lock (); + while (!m_bRunning) + { + m_zhmRunMutex.Unlock (); + + struct timespec tsReq; + struct timespec tsRem; + + tsReq.tv_sec = 0; + tsReq.tv_nsec = 1000000; + nanosleep (&tsReq, &tsRem); + + m_zhmRunMutex.Lock(); + } + m_zhmRunMutex.Unlock(); + + // Unlock to let TheThread () continue and call m_ztfThreadFunc () + m_zhmContinueMutex.Unlock(); + + // Make sure that m_ztfThreadFunc () has call ThreadStarted () + m_zhmContinueMutex2.Lock(); + m_zhmContinueMutex2.Unlock(); + + return 0; +} + + +/****************************************************************************** + * Func : HLH_Thread::Stop + * Desc : Stop this thread + * Args : NONE + * Outs : If success return 0, otherwise return error code + ******************************************************************************/ +int HLH_Thread::Stop () +{ + struct timespec tsReq; + struct timespec tsRem; + + + m_zhmRunMutex.Lock (); + + // If the thread isn't running, abort + if (!m_bRunning) + { + m_zhmRunMutex.Unlock(); + return HLH_THREAD_ERR_NOT_RUNNING; + } + + if (m_bStopping) { + m_zhmRunMutex.Unlock(); + return HLH_THREAD_ERR_ALREADY_STOPPING; + } + + // Send stop request to m_ztfThreadFunc () + m_bStopping = true; + + m_zhmRunMutex.Unlock (); + + // Wait until 'm_bRunning' is unset + + m_zhmRunMutex.Lock (); + while (m_bRunning) + { + m_zhmRunMutex.Unlock (); + + tsReq.tv_sec = 0; + tsReq.tv_nsec = 1000000; + + // Wait for a while + nanosleep (&tsReq, &tsRem); + + m_zhmRunMutex.Lock (); + } + m_zhmRunMutex.Unlock (); + + return 0; +} + +/****************************************************************************** + * Func : HLH_Thread::Kill + * Desc : Kill this thread + * Args : NONE + * Outs : If success return 0, otherwise return error code. + ******************************************************************************/ +int HLH_Thread::Kill () +{ + m_zhmRunMutex.Lock (); + + if (!m_bRunning) + { + m_zhmRunMutex.Unlock(); + return HLH_THREAD_ERR_NOT_RUNNING; + } + + pthread_cancel (m_zpThreadId); + + m_bRunning = false; + m_bStopping = false; + m_pvRetval = NULL; + + m_zhmRunMutex.Unlock(); + + return 0; +} + +/****************************************************************************** + * Func : HLH_Thread::IsRunning + * Desc : Whether this thread is running + * Args : NONE + * Outs : If running return true, otherwise return false. + ******************************************************************************/ +bool HLH_Thread::IsRunning () +{ + bool bRunning; + + m_zhmRunMutex.Lock(); + bRunning = m_bRunning; + m_zhmRunMutex.Unlock(); + + return bRunning; +} + + +/****************************************************************************** + * Func : HLH_Thread::IsStopping + * Desc : Whether this thread is requested to stop + * Args : NONE + * Outs : If requested to stop return true, otherwise return false + ******************************************************************************/ +bool HLH_Thread::IsStopping () +{ + bool bStopping; + + m_zhmRunMutex.Lock(); + bStopping = m_bStopping; + m_zhmRunMutex.Unlock(); + + return bStopping; +} + + +/****************************************************************************** + * Func : HLH_Thread::GetReturnValue + * Desc : Get return value of this thread + * Args : NONE + * Outs : return return value of this thread in last run + ******************************************************************************/ +void * HLH_Thread::GetReturnValue () +{ + void *pvVal; + + m_zhmRunMutex.Lock (); + pvVal = m_pvRetval; + m_zhmRunMutex.Unlock (); + + return pvVal; +} + +/****************************************************************************** + * Func : HLH_Thread::TheThread + * Desc : Thread callback function of this thread + * Args : pvThis pointer to this instance + * Outs : Always return NULL. + ******************************************************************************/ +void * HLH_Thread::TheThread (void *pvThis) +{ + HLH_Thread *pzhThread; + void *pvRet; + + ASSERT (pvThis != NULL); + pzhThread = (HLH_Thread *)pvThis; + + pzhThread->m_zhmContinueMutex2.Lock (); + + // Identify that this thread is running + pzhThread->m_zhmRunMutex.Lock (); + pzhThread->m_bRunning = true; + pzhThread->m_bStopping = false; + pzhThread->m_zhmRunMutex.Unlock (); + + // Wait until Start () confirm that m_bRunning is set + pzhThread->m_zhmContinueMutex.Lock (); + pzhThread->m_zhmContinueMutex.Unlock (); + + // pzhThread->m_zhmContinueMutex2 will be unlock here + pvRet = pzhThread->m_ztfThreadFunc ( *pzhThread, pzhThread->m_pvThreadParam ); + + // Identify that this thread is stopped + pzhThread->m_zhmRunMutex.Lock (); + pzhThread->m_bRunning = false; + pzhThread->m_bStopping = false; + pzhThread->m_pvRetval = pvRet; + pzhThread->m_zhmRunMutex.Unlock (); + + return NULL; +} + +/****************************************************************************** + * Func : HLH_Thread::ThreadStarted + * Desc : Called by m_ztfThreadFunc () to notify \c Start () + * that \c m_ztfThreadFunc () started + * Args : NONE + * Outs : NONE + ******************************************************************************/ +void HLH_Thread::ThreadStarted () +{ + m_zhmContinueMutex2.Unlock (); +} + + diff --git a/HLH_Thread.h b/HLH_Thread.h new file mode 100644 index 0000000..b511303 --- /dev/null +++ b/HLH_Thread.h @@ -0,0 +1,119 @@ +/******************************************************************************* + * File : HLH_Thread.h + * + * Author : Henry He + * Created : 2009-10-8 11:14:55 + * Description : + ******************************************************************************/ + +#ifndef __HLH_THREAD_INC_20091008_111455_HENRY__ +#define __HLH_THREAD_INC_20091008_111455_HENRY__ + + +/******************************************************************************* + * Desc : Includes Files + ******************************************************************************/ +#include "utils/typedef.h" +#include "utils/HLH_Mutex.h" + + +/******************************************************************************* + * Desc : Macro Definations + ******************************************************************************/ +#define HLH_THREAD_ERR_FAILED (-1) +#define HLH_THREAD_ERR_CANT_START_THREAD (-2) +#define HLH_THREAD_ERR_NOT_RUNNING (-3) +#define HLH_THREAD_ERR_ALREADY_RUNNING (-4) +#define HLH_THREAD_ERR_ALREADY_STOPPING (-5) + + + +/******************************************************************************* + * Desc : Type Definations + ******************************************************************************/ +class HLH_Thread; +typedef void * (*ThreadFunc) (HLH_Thread &zhtThread, void *pvParam); + + +/******************************************************************************* + * Desc : Global Variables + ******************************************************************************/ + + +/******************************************************************************* + * Desc : Classes + ******************************************************************************/ + +class HLH_Thread +{ + +public: + /****************************************************************************** + * Desc : Constructor / Deconstructor + ******************************************************************************/ + HLH_Thread (); + virtual ~HLH_Thread (); + +public: + /****************************************************************************** + * Desc : Operations + ******************************************************************************/ + + // ztfThreadFunc () should call zhtThread.ThreadStarted () after thread prepared + // and should handle the stop request ( zhtThread.IsStopping() == ture ) + int Start (ThreadFunc ztfThreadFunc, void *pvThreadParam); + int Stop (); + int Kill (); + + + // Status + bool IsRunning (); + bool IsStopping (); + void *GetReturnValue (); + + + // Should be call by ThreadFunc () to notify that it has start + void ThreadStarted (); + +private: + /****************************************************************************** + * Desc : Private Operations + ******************************************************************************/ + static void * TheThread (void *pvThis); + +private: + /****************************************************************************** + * Desc : Private Data + ******************************************************************************/ + + // Thread info + pthread_t m_zpThreadId; + + + // Functions to execute during thread + ThreadFunc m_ztfThreadFunc; + + // Parameter for m_ztfThreadFunc () + void *m_pvThreadParam; + + + // Return value of \c m_ztfThreadFunc () + void *m_pvRetval; + + // Whether this thread is running + bool m_bRunning; + + // Whether stop is requested + bool m_bStopping; + + // Mutexs + HLH_Mutex m_zhmRunMutex; + HLH_Mutex m_zhmContinueMutex; + HLH_Mutex m_zhmContinueMutex2; + +}; + + + + +#endif /* __HLH_THREAD_INC_20091008_111455_HENRY__ */ diff --git a/HLH_Time.h b/HLH_Time.h new file mode 100644 index 0000000..f80af64 --- /dev/null +++ b/HLH_Time.h @@ -0,0 +1,509 @@ +/******************************************************************************* + * File : HLH_Time.h + * + * Author : Henry He + * Created : 2009-10-8 10:22:07 + * Description : + ******************************************************************************/ + +#ifndef __HLHTIME_INC_20091008_102207_HENRY__ +#define __HLHTIME_INC_20091008_102207_HENRY__ + + +/******************************************************************************* + * Desc : Includes Files + ******************************************************************************/ +#include "utils/typedef.h" +#include "utils/HLH_RoundU32.h" + + + + +/******************************************************************************* + * Desc : Macro Definations + ******************************************************************************/ +#define HLH_NTPTIMEOFFSET 2208988800UL + + +/******************************************************************************* + * Desc : Type Definations + ******************************************************************************/ + + +/******************************************************************************* + * Desc : Global Variables + ******************************************************************************/ + + +/******************************************************************************* + * Desc : Functions + ******************************************************************************/ + + + +/****************************************************************************** + * Desc : + * This is a simple wrapper for the most significant word (MSW) and least + * significant word (LSW) of an NTP timestamp. + ******************************************************************************/ +class HLH_NTPTime +{ + + public: + /** This constructor creates and instance with MSW \c m and LSW \c l. */ + HLH_NTPTime(UINT32 unMsw, UINT32 unLsw) { + m_unMsw = unMsw; + m_unLsw = unLsw; + } + + /** Returns the most significant word. */ + UINT32 GetMSW () const { + return m_unMsw; + } + + /** Returns the least significant word. */ + UINT32 GetLSW () const { + return m_unLsw; + } + + private: + UINT32 m_unMsw; + UINT32 m_unLsw; + +}; + + + +/****************************************************************************** + * Desc : + * This class is used to specify wallclock time, delay intervals etc. + * It stores a number of seconds and a number of microseconds. + ******************************************************************************/ +class HLH_Time +{ + + public: + /** Creates an HLH_Time instance representing \c dTime, + * which is expressed in units of seconds. + */ + HLH_Time (double dTime) { + SetTime (dTime); + } + + /** Creates an instance that corresponds to \c zhnTime. + * If the conversion cannot be made, both the seconds and the + * microseconds are set to zero. + */ + HLH_Time (const HLH_NTPTime &zhnTime) { + SetTime (zhnTime); + } + + /** Creates an instance corresponding to \c seconds and \c microseconds. */ + HLH_Time (UINT32 unSecs = 0, UINT32 unMicroSecs = 0) { + SetTime (unSecs, unMicroSecs); + } + + /** Creates an instance corresponding to \c tvTime. */ + HLH_Time (const struct timeval &tvTime) { + SetTime (tvTime); + } + + + public: + + // Set time to dTime + void SetTime (double dTime); + + // Set time to zhnTime + void SetTime (const HLH_NTPTime &zhnTime); + + // Set time to (unSecs, unMicroSecs) + void SetTime (UINT32 unSecs, UINT32 unMicroSecs) { + m_zhrSecs = unSecs; + m_unMicroSecs = unMicroSecs; + } + + // Set the time to tvTime + void SetTime (const struct timeval &tvTime); + + + public: + + /** Returns the number of seconds stored in this instance. */ + UINT32 GetSeconds () const { return (UINT32)m_zhrSecs; } + + /** Returns the number of microseconds stored in this instance. */ + UINT32 GetMicroSeconds () const { return m_unMicroSecs; } + + /** Returns the time stored in this instance, expressed in units of seconds. */ + double GetDouble () const { return ( ((double)m_zhrSecs) + (((double)m_unMicroSecs)/1000000.0) ); } + + /** Returns the NTP time corresponding to the time stored in this instance. */ + HLH_NTPTime GetNTPTime () const; + + /** Returns the timeval corresponding to the time stored in this instance. */ + struct timeval GetTimeVal () const; + + + public: + /** Returns an HLH_Time instance representing the current wallclock time. + * This is expressed as a number of seconds since 00:00:00 UTC, January 1, 1970. + */ + static HLH_Time GetCurrentTime (); + + /** This function waits the amount of time specified in \c delay. */ + static void WaitX (HLH_Time &zhtDelay); + static void Wait (HLH_Time zhtDelay) { WaitX (zhtDelay); } + + + + public: + + HLH_Time operator + (const HLH_Time &zhtTime) const; + HLH_Time operator - (const HLH_Time &zhtTime) const; + HLH_Time operator * (UINT32 unVal) const; + HLH_Time operator / (UINT32 unVal) const; + + HLH_Time &operator += (const HLH_Time &zhtTime); + HLH_Time &operator -= (const HLH_Time &zhtTime); + HLH_Time &operator *= (UINT32 unVal); + HLH_Time &operator /= (UINT32 unVal); + + bool operator < (const HLH_Time &zhtTime) const; + bool operator > (const HLH_Time &zhtTime) const; + bool operator <= (const HLH_Time &zhtTime) const; + bool operator >= (const HLH_Time &zhtTime) const; + + private: + HLH_RoundU32 m_zhrSecs; + UINT32 m_unMicroSecs; + +}; + + + + + +/****************************************************************************** + * Desc : Member functions + ******************************************************************************/ + + + + +/****************************************************************************** + * Func : HLH_Time::SetTime + * Desc : Set time to dTime + * Args : dTime Time to be set + * Outs : NONE + ******************************************************************************/ +inline void HLH_Time::SetTime (double dTime) +{ + double dMicroSecs; + + m_zhrSecs = (UINT32)dTime; + dMicroSecs = ( dTime - ((double)m_zhrSecs) ) * 1000000.0; + m_unMicroSecs = (UINT32) dMicroSecs; +} + + +/****************************************************************************** + * Func : HLH_Time::SetTime + * Desc : Set time to zhnTime + * Args : zhnTime Time to be set + * Outs : NONE + ******************************************************************************/ +inline void HLH_Time::SetTime (const HLH_NTPTime &zhnTime) +{ + double dMicroSecs; + + if (zhnTime.GetMSW() < HLH_NTPTIMEOFFSET) { + + m_zhrSecs = 0; + m_unMicroSecs = 0; + + } else { + + m_zhrSecs = zhnTime.GetMSW () - HLH_NTPTIMEOFFSET; + dMicroSecs = (double) zhnTime.GetLSW (); + dMicroSecs /= ( 65536.0 * 65536.0 ); + dMicroSecs *= 1000000.0; + m_unMicroSecs = (UINT32) dMicroSecs; + + } +} + + + +/****************************************************************************** + * Func : HLH_Time::SetTime + * Desc : Set the time to tvTime + * Args : tvTime Time to be set + * Outs : NONE + ******************************************************************************/ +inline void HLH_Time::SetTime (const struct timeval &tvTime) +{ + m_zhrSecs = tvTime.tv_sec; + m_unMicroSecs = tvTime.tv_usec; +} + + + +/****************************************************************************** + * Func : HLH_Time::GetNTPTime + * Desc : Get time in NTP format + * Args : NONE + * Outs : return time in NTP format + ******************************************************************************/ +inline HLH_NTPTime HLH_Time::GetNTPTime () const +{ + UINT32 unMsw; + UINT32 unLsw; + double dLsw; + + unMsw = (UINT32)m_zhrSecs + HLH_NTPTIMEOFFSET; + dLsw = m_unMicroSecs / 1000000.0; + dLsw *= (65536.0 * 65536.0); + unLsw = (UINT32) dLsw; + + return HLH_NTPTime (unMsw, unLsw); +} + + +/****************************************************************************** + * Func : HLH_Time::GetTimeVal + * Desc : Returns the timeval corresponding to the time stored in this instance. + * Args : NONE + * Outs : Returns the timeval corresponding to the time stored in this instance. + ******************************************************************************/ +inline struct timeval HLH_Time::GetTimeVal () const +{ + struct timeval tvTime; + + tvTime.tv_sec = (UINT32) m_zhrSecs; + tvTime.tv_usec = m_unMicroSecs; + + return tvTime; +} + + + + + + +/****************************************************************************** + * Func : GetCurrentTime + * Desc : Get current time + * Args : NONE + * Outs : Return current time + ******************************************************************************/ +inline HLH_Time HLH_Time::GetCurrentTime () +{ + struct timeval tvTime; + + gettimeofday (&tvTime, 0); + + return HLH_Time (tvTime); +} + +/****************************************************************************** + * Func : Wait + * Desc : Wait for 'zhtDelay' time + * Args : zhtDelay Time to wait + * Outs : NONE + ******************************************************************************/ +inline void HLH_Time::WaitX (HLH_Time &zhtDelay) +{ + struct timespec tsReq; + struct timespec tsRem; + + tsReq.tv_sec = (UINT32) zhtDelay.m_zhrSecs; + tsReq.tv_nsec = zhtDelay.m_unMicroSecs * 1000; + + // Sleep for tsReq + nanosleep ( &tsReq, &tsRem ); + + // Set remained time + zhtDelay.SetTime ( tsRem.tv_sec, tsRem.tv_nsec / 1000 ); +} + + + + + + + +/****************************************************************************** + * Desc : Operators + ******************************************************************************/ + + +inline HLH_Time HLH_Time::operator + (const HLH_Time &zhtTime) const +{ + HLH_Time zhtResult; + + zhtResult.m_zhrSecs = m_zhrSecs + zhtTime.m_zhrSecs; + zhtResult.m_unMicroSecs = m_unMicroSecs + zhtTime.m_unMicroSecs; + + if (zhtResult.m_unMicroSecs > 1000000) { + zhtResult.m_unMicroSecs -= 1000000; + zhtResult.m_zhrSecs ++; + } + + return zhtResult; +} + + +inline HLH_Time HLH_Time::operator - (const HLH_Time &zhtTime) const +{ + HLH_Time zhtResult; + + zhtResult.m_zhrSecs = m_zhrSecs - zhtTime.m_zhrSecs; + zhtResult.m_unMicroSecs = m_unMicroSecs; + + if (zhtResult.m_unMicroSecs < zhtTime.m_unMicroSecs) + { + zhtResult.m_zhrSecs --; + zhtResult.m_unMicroSecs += 1000000; + } + zhtResult.m_unMicroSecs -= zhtTime.m_unMicroSecs; + + return zhtResult; +} + +inline HLH_Time HLH_Time::operator * (UINT32 unVal) const +{ + HLH_Time zhtResult; + UINT32 unMicroSecs; + + unMicroSecs = m_unMicroSecs * unVal; + + zhtResult.m_zhrSecs = ((UINT32)m_zhrSecs) * unVal + unMicroSecs / 1000000; + zhtResult.m_unMicroSecs = unMicroSecs % 1000000; + + return zhtResult; +} + + +inline HLH_Time HLH_Time::operator / (UINT32 unVal) const +{ + HLH_Time zhtResult; + HLH_RoundU32 zhrSecs; + UINT32 unMicroSecs; + + zhtResult.m_zhrSecs = (UINT32)m_zhrSecs / unVal; + + unMicroSecs = ( (UINT32)m_zhrSecs % unVal ) * 1000000 + m_unMicroSecs; + zhtResult.m_unMicroSecs = unMicroSecs / unVal; + + return zhtResult; +} + + + +inline HLH_Time & HLH_Time::operator += (const HLH_Time &zhtTime) +{ + m_zhrSecs += zhtTime.m_zhrSecs; + m_unMicroSecs += zhtTime.m_unMicroSecs; + if (m_unMicroSecs >= 1000000) { + m_zhrSecs ++; + m_unMicroSecs -= 1000000; + } + + return *this; +} + + +inline HLH_Time & HLH_Time::operator -= (const HLH_Time &zhtTime) +{ + m_zhrSecs -= zhtTime.m_zhrSecs; + if (zhtTime.m_unMicroSecs > m_unMicroSecs) { + m_zhrSecs--; + m_unMicroSecs += 1000000; + } + m_unMicroSecs -= zhtTime.m_unMicroSecs; + + return *this; +} + + +inline HLH_Time & HLH_Time::operator *= (UINT32 unVal) +{ + UINT32 unMicroSecs; + + unMicroSecs = m_unMicroSecs * unVal; + m_zhrSecs = (UINT32)m_zhrSecs * unVal + unMicroSecs / 1000000; + m_unMicroSecs = unMicroSecs % 1000000; + + return *this; +} + + +inline HLH_Time & HLH_Time::operator /= (UINT32 unVal) +{ + HLH_RoundU32 zhrSecs; + UINT32 unMicroSecs; + + + m_zhrSecs /= unVal; + + unMicroSecs = ( (UINT32)m_zhrSecs % unVal) * 1000000 + m_unMicroSecs; + m_unMicroSecs = unMicroSecs / unVal; + + return *this; +} + + + + + +inline bool HLH_Time::operator < (const HLH_Time &zhtTime) const +{ + if (m_zhrSecs < zhtTime.m_zhrSecs) + return true; + if (m_zhrSecs > zhtTime.m_zhrSecs) + return false; + if (m_unMicroSecs < zhtTime.m_unMicroSecs) + return true; + return false; +} + +inline bool HLH_Time::operator > (const HLH_Time &zhtTime) const +{ + if (m_zhrSecs > zhtTime.m_zhrSecs) + return true; + if (m_zhrSecs < zhtTime.m_zhrSecs) + return false; + if (m_unMicroSecs > zhtTime.m_unMicroSecs) + return true; + return false; +} + +inline bool HLH_Time::operator <= (const HLH_Time &zhtTime) const +{ + if (m_zhrSecs < zhtTime.m_zhrSecs) + return true; + if (m_zhrSecs > zhtTime.m_zhrSecs) + return false; + if (m_unMicroSecs <= zhtTime.m_unMicroSecs) + return true; + return false; +} + + +inline bool HLH_Time::operator >= (const HLH_Time &zhtTime) const +{ + if (m_zhrSecs > zhtTime.m_zhrSecs) + return true; + if (m_zhrSecs < zhtTime.m_zhrSecs) + return false; + if (m_unMicroSecs >= zhtTime.m_unMicroSecs) + return true; + return false; +} + + + + + +#endif /* __HLHTIME_INC_20091008_102207_HENRY__ */ diff --git a/HLH_UDPSock.cpp b/HLH_UDPSock.cpp new file mode 100644 index 0000000..7da1f24 --- /dev/null +++ b/HLH_UDPSock.cpp @@ -0,0 +1,428 @@ +/******************************************************************************* + * File Name : HLH_UDPSock.cpp + * + * Author : Henry He + * Created Time : 2009-10-10 16:36:17 + * Description : + ******************************************************************************/ + + + + +/******************************************************************************* + * Desc : Includes Files + ******************************************************************************/ + +#include "utils/HLH_UDPSock.h" + + +/******************************************************************************* + * Desc : Macro Definations + ******************************************************************************/ + + +/******************************************************************************* + * Desc : Type Definations + ******************************************************************************/ + + +/******************************************************************************* + * Desc : Global Variables + ******************************************************************************/ + + +/******************************************************************************* + * Desc : File Variables + ******************************************************************************/ + + + + + + +/****************************************************************************** + * Desc : Member Functions + ******************************************************************************/ + + + + + +/****************************************************************************** + * Func : HLH_UDPSock::HLH_UDPSock + * Desc : Constuctor of HLH_UDPSock + * Args : NONE + * Outs : NONE + ******************************************************************************/ +HLH_UDPSock::HLH_UDPSock () +{ + m_zhmMutex.Init (); + m_bCreated = false; +} + + +/****************************************************************************** + * Func : HLH_UDPSock::~HLH_UDPSock + * Desc : Deconstructor of HLH_UDPSock + * Args : NONE + * Outs : NONE + ******************************************************************************/ +HLH_UDPSock::~HLH_UDPSock () +{ + Destroy (); +} + + + + + +/****************************************************************************** + * Desc : Operations + ******************************************************************************/ + + + + + +/****************************************************************************** + * Func : HLH_UDPSock::Create + * Desc : Create UDP socket and bind it to zhsBindAddr, + * then create thread to handle receive event + * Args : zhsBindAddr address of bindding + * Outs : If success return 0, otherwise return error code. + ******************************************************************************/ +int HLH_UDPSock::Create (const HLH_SockAddr &zhsBindAddr) +{ + + int nRetVal; + + // Lock before process + m_zhmMutex.Lock (); + + // Check if this instance created + if (m_bCreated) { + // Unlock this instance + m_zhmMutex.Unlock (); + + HLH_DEBUG ( HLH_DEBUG_MAIN, ("already created") ); + return HLH_UDPSOCK_ERR_ALREADY_CREATED; + } + + // Init supper class HLH_Sock first + nRetVal = HLH_Sock::Create (HLH_SOCK_TYPE_DGRAM, zhsBindAddr, false); + if (nRetVal < 0) { + HLH_DEBUG ( HLH_DEBUG_MAIN, ("create HLH_Sock failed") ); + goto fail; + } + + // Clear the receive handler list + m_slRecvHandlerList.clear (); + + // Start receive thread + nRetVal = m_zhtRecvThread.Start (RecvThreadFunc, this); + if (nRetVal < 0) { + HLH_DEBUG ( HLH_DEBUG_MAIN, ("start received thread failed") ); + goto fail; + } + + // Notify that this instance created + m_bCreated = true; + m_bConnected = false; + + // Unlock after process + m_zhmMutex.Unlock (); + + return 0; + +fail: + + // Close the socket if needed + if ( HLH_Sock::IsCreated () ) { + HLH_Sock::Destroy (); + } + + // Unlock after process + m_zhmMutex.Unlock (); + + return HLH_UDPSOCK_ERR_CANT_CREATE; +} + + +/****************************************************************************** + * Func : HLH_UDPSock::Destroy + * Desc : Destroy this UDP socket + * Args : NONE + * Outs : NONE + ******************************************************************************/ +void HLH_UDPSock::Destroy () +{ + // Stop receive thread + m_zhtRecvThread.Stop (); + + // Lock this instance + m_zhmMutex.Lock (); + + + // Check if this instance created + if (!m_bCreated) { + // Unlock this instance + m_zhmMutex.Unlock (); + + return; + } + + // Clear receiver list + m_slRecvHandlerList.clear (); + + // Destroy this socket + HLH_Sock::Destroy (); + + m_bCreated = false; + m_bConnected = false; + + // Unlock this instance + m_zhmMutex.Unlock (); +} + + +/****************************************************************************** + * Func : HLH_UDPSock::IsCreated + * Desc : Whether this instance created + * Args : NONE + * Outs : If success return 0, otherwise return error code. + ******************************************************************************/ +bool HLH_UDPSock::IsCreated () +{ + bool bCreated; + + m_zhmMutex.Lock (); + bCreated = m_bCreated; + m_zhmMutex.Unlock (); + + return bCreated; +} + + + + +/****************************************************************************** + * Func : HLH_UDPSock::AddRecvHandler + * Desc : Add receive handler to this socket + * Args : zorOnRecv Funtion to handle receive event + * pvOnRecvParam Parameter to \c zorOnRecv () + * Outs : if success, return 0, otherwise return error code + ******************************************************************************/ +void HLH_UDPSock::AddRecvHandler (OnRecvFunc zorOnRecv, void *pvOnRecvParam) +{ + + RecvHandler zrhRecvHandler; + + // Lock before process + m_zhmMutex.Lock (); + + // Put this handler into the list + zrhRecvHandler.m_zorOnRecv = zorOnRecv; + zrhRecvHandler.m_pvOnRecvParam = pvOnRecvParam; + + m_slRecvHandlerList.push_back (zrhRecvHandler); + + // Lock after process + m_zhmMutex.Unlock (); +} + + +/****************************************************************************** + * Func : HLH_UDPSock::DelRecvHandler + * Desc : Delete receive handler from receive handler list + * Args : zorOnRecv Funtion to handle receive event + * pvOnRecvParam Parameter to \c zorOnRecv () + * Outs : NONE + ******************************************************************************/ +void HLH_UDPSock::DelRecvHandler (OnRecvFunc zorOnRecv, void *pvOnRecvParam) +{ + + RecvHandler zrhRecvHandler; + + // Lock before process + m_zhmMutex.Lock (); + + // Remove this receiver handler + zrhRecvHandler.m_zorOnRecv = zorOnRecv; + zrhRecvHandler.m_pvOnRecvParam = pvOnRecvParam; + + m_slRecvHandlerList.remove (zrhRecvHandler); + + // Unlock after process + m_zhmMutex.Unlock (); +} + + +/****************************************************************************** + * Func : HLH_UDPSock::ClearRecvHandlerList + * Desc : Clear the receive handler list + * Args : NONE + * Outs : NONE + ******************************************************************************/ +void HLH_UDPSock::ClearRecvHandlerList () +{ + // Lock before process + m_zhmMutex.Lock (); + + // Clear the receive handler list + m_slRecvHandlerList.clear (); + + // Unlock after process + m_zhmMutex.Unlock (); +} + +/****************************************************************************** + * Func : HLH_UDPSock::__RecvThreadFunc + * Desc : Thread function to call receive handler functions + * Args : zhtRecvThread Thread that call \c RecvThreadFunc () + * Outs : + ******************************************************************************/ +void HLH_UDPSock::__RecvThreadFunc ( HLH_Thread &zhtRecvThread ) +{ + + int nRetVal; + + UINT32 unLen; + UINT32 unPollType; + HLH_Time zhtPollTime; + + UINT8 acRecvBuf [HLH_UDPSOCK_MAX_RECEIVE_LENGTH]; + HLH_SockAddr zhsPeerAddr; + HLH_SockAddr zhsConnAddr; + + std::list::iterator slIt; + + // Signal to zhtRecvThread that this thread functions runned + zhtRecvThread.ThreadStarted (); + + + + // Can't process socket not created + if ( !IsCreated () ) { + HLH_DEBUG ( HLH_DEBUG_MAIN, ("not created") ); + return; + } + + + do { + + // Poll for receive events + unPollType = HLH_SOCK_POLL_READ; + zhtPollTime.SetTime (HLH_UDPSOCK_DEFAULT_POLLWAIT_SECONDS, 0); + + nRetVal = PollWait (unPollType, zhtPollTime); + + // If there is any data received + if ( nRetVal > 0 ) { + + // Receive the data + nRetVal = RecvFrom (acRecvBuf, sizeof(acRecvBuf), zhsPeerAddr); + if (nRetVal < 0) { + // XXX: do nothing if receive error + goto check_stop; + } + unLen = nRetVal; + + // If connected, check the source ip, otherwise accept all + nRetVal = GetConnectAddr (zhsConnAddr); + if ( nRetVal < 0 || zhsConnAddr == zhsPeerAddr ) { + // Call receive handler call back function + for ( slIt = m_slRecvHandlerList.begin (); + slIt != m_slRecvHandlerList.end (); slIt ++ ) { + slIt->m_zorOnRecv (slIt->m_pvOnRecvParam, acRecvBuf, unLen, zhsPeerAddr); + } + } else { + HLH_DEBUG ( HLH_DEBUG_MAIN, ("ignore package") ); + } + + } + +check_stop: + // Check if stop request + if ( zhtRecvThread.IsStopping () ) { + return; + } + + } while (1); + +} + + +/****************************************************************************** + * Func : HLH_UDPSock::RecvThreadFunc + * Desc : Wrapper of __RecvThreadFunc + * Args : zhtRecvThread Thread that call this function + * Outs : Always return NULL. + ******************************************************************************/ +void * HLH_UDPSock::RecvThreadFunc (HLH_Thread &zhtRecvThread, void *pvThis) +{ + ASSERT (pvThis != NULL); + ( (HLH_UDPSock*) pvThis )->__RecvThreadFunc (zhtRecvThread); + return NULL; +} + + +/****************************************************************************** + * Func : HLH_UDPSock::Connect + * Desc : Reload of HLH_Sock::Connect () + * Args : zhsPeekAddr address to connect + * Outs : if success, return 0, otherwise return error code + ******************************************************************************/ +int HLH_UDPSock::Connect (const HLH_SockAddr &zhsSockAddr) +{ + int nRetVal; + + // Lock this instance + m_zhmMutex.Lock (); + + nRetVal = HLH_Sock::Connect (zhsSockAddr); + if (nRetVal < 0) { + goto failed; + } + + // Notify that this instance is created + m_bConnected = true; + m_zhsConnectAddr = zhsSockAddr; + + // Unlock this instance + m_zhmMutex.Unlock (); + + return 0; + +failed: + // Unlock this instance + m_zhmMutex.Unlock (); + + return HLH_UDPSOCK_ERR_FAILED; +} + +/****************************************************************************** + * Func : HLH_UDPSock::GetConnectAddr + * Desc : Get the address of peer if connected + * Args : zhsConnectAddr Space to save the address get + * Outs : If success return 0, otherwise return error code. + ******************************************************************************/ +int HLH_UDPSock::GetConnectAddr (HLH_SockAddr & zhsConnectAddr) +{ + // Lock this instance + m_zhmMutex.Lock (); + if (!m_bCreated || !m_bConnected) { + // Unlock this instance + m_zhmMutex.Unlock (); + HLH_DEBUG ( HLH_DEBUG_UTILS, ("not connected") ); + return HLH_UDPSOCK_ERR_NOT_CREATED; + } + + zhsConnectAddr = m_zhsConnectAddr; + + // Unlock this instance + m_zhmMutex.Unlock (); + + return 0; +} + + diff --git a/HLH_UDPSock.h b/HLH_UDPSock.h new file mode 100644 index 0000000..d551cc7 --- /dev/null +++ b/HLH_UDPSock.h @@ -0,0 +1,153 @@ +/******************************************************************************* + * File : HLH_UDPSock.h + * + * Author : Henry He + * Created : 2009-10-10 16:35:35 + * Description : + ******************************************************************************/ + +#ifndef __HLH_UDPSOCK_INC_20091010_163535_HENRY__ +#define __HLH_UDPSOCK_INC_20091010_163535_HENRY__ + + +/******************************************************************************* + * Desc : Includes Files + ******************************************************************************/ +#include "utils/typedef.h" +#include "utils/HLH_Thread.h" +#include "utils/HLH_Sock.h" + + +/******************************************************************************* + * Desc : Macro Definations + ******************************************************************************/ + +//////////////////////////////////////////////////////////////////////////////// +// HLH_UDPSock Error + +#define HLH_UDPSOCK_ERR_FAILED (-1) +#define HLH_UDPSOCK_ERR_CANT_CREATE (-2) +#define HLH_UDPSOCK_ERR_NOT_CREATED (-3) +#define HLH_UDPSOCK_ERR_ALREADY_CREATED (-4) + + +//////////////////////////////////////////////////////////////////////////////// +// Default Values + +#define HLH_UDPSOCK_DEFAULT_POLLWAIT_SECONDS 1 + + +//////////////////////////////////////////////////////////////////////////////// +// Max Values + +#define HLH_UDPSOCK_MAX_RECEIVE_LENGTH 1536 + + +/******************************************************************************* + * Desc : Type Definations + ******************************************************************************/ + +//////////////////////////////////////////////////////////////////////////////// +// Handler callback functions + +typedef void (* OnRecvFunc) (void *pvOnRecvParam, + void *pvBuf, UINT32 unLen, + HLH_SockAddr &zhsSockAddr); + + +/******************************************************************************* + * Desc : Global Variables + ******************************************************************************/ + + +/******************************************************************************* + * Desc : Classes + ******************************************************************************/ + +//////////////////////////////////////////////////////////////////////////////// +// Receive Event Handler + +class RecvHandler +{ + public: + bool operator == (const RecvHandler &zrhRecvHandler) const { + return ( m_zorOnRecv == zrhRecvHandler.m_zorOnRecv + && m_pvOnRecvParam == zrhRecvHandler.m_pvOnRecvParam ); + } + + public: + OnRecvFunc m_zorOnRecv; + void *m_pvOnRecvParam; +}; + + +//////////////////////////////////////////////////////////////////////////////// +// class to process UDP transmit + +class HLH_UDPSock : public HLH_Sock +{ + + public: + // Constructor + HLH_UDPSock (); + + // Deconstructor + ~HLH_UDPSock (); + + + public: + // Whether this instance created + bool IsCreated (); + + // Create UDP socket and bind it to zhsBindAddr, + // then create thread to handle receive event + int Create (const HLH_SockAddr &zhsBindAddr); + + // Destroy this UDP socket + void Destroy (); + + // Reload of HLH_Sock::Connect () + int Connect (const HLH_SockAddr &zhsSockAddr); + + // Get the address of peer if connected + int GetConnectAddr (HLH_SockAddr & zhsConnectAddr); + + // Add receive handler to this socket + void AddRecvHandler (OnRecvFunc zorOnRecv, void *pvOnRecvParam); + + // Delete receive handler from receive handler list + void DelRecvHandler (OnRecvFunc zorOnRecv, void *pvOnRecvParam); + + // Clear the receive handler list + void ClearRecvHandlerList (); + + private: + // Wrapper of __RecvThreadFunc + static void * RecvThreadFunc (HLH_Thread &zhtRecvThread, void *pvThis); + + // Thread function that call receiver handler when data received + void __RecvThreadFunc (HLH_Thread &zhtRecvThread); + + private: + // Mutex of this instance + HLH_Mutex m_zhmMutex; + + // Whether this instance created + bool m_bCreated; + + // Whether this UDP connected + bool m_bConnected; + HLH_SockAddr m_zhsConnectAddr; + + // Receive event handler list + std::list m_slRecvHandlerList; + + // Threads + HLH_Thread m_zhtRecvThread; + +}; + + + + +#endif /* __HLH_UDPSOCK_INC_20091010_163535_HENRY__ */ diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..a63a5fc --- /dev/null +++ b/Makefile @@ -0,0 +1,57 @@ + +################################################################################ +# Rules +################################################################################ +include $(TOP_DIR)/Rules + + +################################################################################ +# Source and Target +################################################################################ +TARGET = utils.o + + SRCS = + SRCS += + SRCS += debug.cpp + SRCS += HLH_RoundU32.cpp + SRCS += HLH_Mutex.cpp + SRCS += HLH_Cond.cpp + SRCS += HLH_Thread.cpp + SRCS += HLH_Sock.cpp + SRCS += HLH_UDPSock.cpp + +LIBS = + +OBJS = $(patsubst %.cpp,%.o,$(SRCS)) +DEPS = $(patsubst %.cpp,.%.d,$(SRCS)) + + +################################################################################ +# All rule +################################################################################ +.PHONY: all clean +all: $(TARGET) + + +################################################################################ +# Target rule +################################################################################ +$(TARGET): $(DEPS) $(OBJS) $(LIBS) + @echo " LD $@" + @$(LD) -i -o $@ $(OBJS) + + +################################################################################ +# Depend rule +################################################################################ +sinclude $(DEPS) + + +################################################################################ +# Clean rule +################################################################################ +clean: + @echo " CLEAN" + @rm $(DEPS) $(OBJS) $(TARGET) -f + + diff --git a/common.h b/common.h new file mode 100644 index 0000000..d530117 --- /dev/null +++ b/common.h @@ -0,0 +1,52 @@ +/******************************************************************************* + * File : utils\common.h + * + * Author : Henry He + * Created : 2009-10-21 13:15:05 + * Description : + ******************************************************************************/ + +#ifndef __COMMON_INC_20091021_131505_HENRY__ +#define __COMMON_INC_20091021_131505_HENRY__ + + +/******************************************************************************* + * Desc : Includes Files + ******************************************************************************/ + + + +#include "utils/typedef.h" + +#include "utils/HLH_RoundU32.h" +#include "utils/HLH_Time.h" + +#include "utils/HLH_Mutex.h" +#include "utils/HLH_Cond.h" +#include "utils/HLH_Thread.h" + +#include "utils/HLH_Sock.h" +#include "utils/HLH_UDPSock.h" + + +/******************************************************************************* + * Desc : Macro Definations + ******************************************************************************/ + + +/******************************************************************************* + * Desc : Type Definations + ******************************************************************************/ + + +/******************************************************************************* + * Desc : Global Variables + ******************************************************************************/ + + +/******************************************************************************* + * Desc : Functions + ******************************************************************************/ + + +#endif /* __COMMON_INC_20091021_131505_HENRY__ */ diff --git a/config.h b/config.h new file mode 100644 index 0000000..53ac37b --- /dev/null +++ b/config.h @@ -0,0 +1,111 @@ +/******************************************************************************* + * File : src/utils/config.h + * + * Author : Henry He + * Created : Tue 27 Oct 2009 01:45:02 PM CST + * Description : + ******************************************************************************/ + +#ifndef __CONFIG_INC_20091027_134502_HENRY__ +#define __CONFIG_INC_20091027_134502_HENRY__ + +/******************************************************************************* + * Desc : Debug Configurations + ******************************************************************************/ + + +//=========================== Global Debug Options =========================== + +// Whether use \c ASSERT +#define __USE_ASSERT__ 1 + +// Whether use \c HLH_DEBUG +#define __USE_HLH_DEBUG__ 1 + +// Whether use \c HLH_ENTER_FUNC and \c HLH_EXIT_FUNC +#define __TRACE_CALL__ 1 + + + + +//======================== Debug Options of Modules ========================== +// +// Set config to '1' if enable +// + +#define HLH_DEBUG_CONFIG_AUDIO 0 +#define HLH_DEBUG_CONFIG_LUDP 0 +#define HLH_DEBUG_CONFIG_JITTER 0 +#define HLH_DEBUG_CONFIG_UTILS 0 +#define HLH_DEBUG_CONFIG_VIDEO 0 +#define HLH_DEBUG_CONFIG_VPHONE 1 +#define HLH_DEBUG_CONFIG_FB 0 +#define HLH_DEBUG_CONFIG_PP 0 +#define HLH_DEBUG_CONFIG_MFC 0 + +#define HLH_DEBUG_CONFIG_TEST 0 +#define HLH_DEBUG_CONFIG_MAIN 1 + + +//======================== Debug Options Offsets ============================= + +#define HLH_DEBUG_BIT_OFFSET_AUDIO 1 +#define HLH_DEBUG_BIT_OFFSET_LUDP 2 +#define HLH_DEBUG_BIT_OFFSET_JITTER 3 +#define HLH_DEBUG_BIT_OFFSET_UTILS 4 +#define HLH_DEBUG_BIT_OFFSET_VIDEO 5 +#define HLH_DEBUG_BIT_OFFSET_VPHONE 6 +#define HLH_DEBUG_BIT_OFFSET_FB 7 +#define HLH_DEBUG_BIT_OFFSET_PP 8 +#define HLH_DEBUG_BIT_OFFSET_MFC 9 + +#define HLH_DEBUG_BIT_OFFSET_TEST 30 +#define HLH_DEBUG_BIT_OFFSET_MAIN 31 + + +//======================== Debug Options Flags =============================== + +#define HLH_DEBUG_AUDIO ( ( HLH_DEBUG_CONFIG_AUDIO & 0x01 ) << HLH_DEBUG_BIT_OFFSET_AUDIO ) +#define HLH_DEBUG_LUDP ( ( HLH_DEBUG_CONFIG_LUDP & 0x01 ) << HLH_DEBUG_BIT_OFFSET_LUDP ) +#define HLH_DEBUG_JITTER ( ( HLH_DEBUG_CONFIG_JITTER & 0x01 ) << HLH_DEBUG_BIT_OFFSET_JITTER ) +#define HLH_DEBUG_UTILS ( ( HLH_DEBUG_CONFIG_UTILS & 0x01 ) << HLH_DEBUG_BIT_OFFSET_UTILS ) +#define HLH_DEBUG_VIDEO ( ( HLH_DEBUG_CONFIG_VIDEO & 0x01 ) << HLH_DEBUG_BIT_OFFSET_VIDEO ) +#define HLH_DEBUG_VPHONE ( ( HLH_DEBUG_CONFIG_VPHONE & 0x01 ) << HLH_DEBUG_BIT_OFFSET_VPHONE ) +#define HLH_DEBUG_FB ( ( HLH_DEBUG_CONFIG_FB & 0x01 ) << HLH_DEBUG_BIT_OFFSET_FB ) +#define HLH_DEBUG_PP ( ( HLH_DEBUG_CONFIG_PP & 0x01 ) << HLH_DEBUG_BIT_OFFSET_PP ) +#define HLH_DEBUG_MFC ( ( HLH_DEBUG_CONFIG_MFC & 0x01 ) << HLH_DEBUG_BIT_OFFSET_MFC ) + +#define HLH_DEBUG_TEST ( ( HLH_DEBUG_CONFIG_TEST & 0x01 ) << HLH_DEBUG_BIT_OFFSET_TEST ) +#define HLH_DEBUG_MAIN ( ( HLH_DEBUG_CONFIG_MAIN & 0x01 ) << HLH_DEBUG_BIT_OFFSET_MAIN ) + + + + + + +/****************************************************************************** + * Desc : Combination of the Debug Configurations + ******************************************************************************/ +#define HLH_DEBUG_FLAGS \ + ( HLH_DEBUG_AUDIO | HLH_DEBUG_LUDP | HLH_DEBUG_JITTER | \ + HLH_DEBUG_UTILS | HLH_DEBUG_VIDEO | HLH_DEBUG_VPHONE | \ + HLH_DEBUG_FB | HLH_DEBUG_PP | HLH_DEBUG_MFC | \ + HLH_DEBUG_TEST | HLH_DEBUG_MAIN ) + + + +//=========================== Use fake audio ================================= +#define __USE_FAKE_AUDIO__ 0 + +#define __USE_FAKE_SWAP__ 1 +#if (__USE_FAKE_SWAP__ <= 0) +# define __USE_FAKE_AUDIO_IN__ 0 +# define __USE_FAKE_AUDIO_OUT__ 1 +#else +# define __USE_FAKE_AUDIO_IN__ 1 +# define __USE_FAKE_AUDIO_OUT__ 0 +#endif + + + +#endif /* __CONFIG_INC_20091027_134502_HENRY__ */ diff --git a/debug.cpp b/debug.cpp new file mode 100644 index 0000000..964b8d1 --- /dev/null +++ b/debug.cpp @@ -0,0 +1,65 @@ +/******************************************************************************* + * File Name : src/utils/debug.cpp + * + * Author : Henry He + * Created Time : Fri 23 Oct 2009 01:22:57 PM CST + * Description : + ******************************************************************************/ + + +/******************************************************************************* + * Desc : Includes Files + ******************************************************************************/ +#include + + +/******************************************************************************* + * Desc : Macro Definations + ******************************************************************************/ + + +/******************************************************************************* + * Desc : Type Definations + ******************************************************************************/ + + +/******************************************************************************* + * Desc : Global Variables + ******************************************************************************/ +HLH_AutoMutex g_zhmPrintMutex; + + +/******************************************************************************* + * Desc : File Variables + ******************************************************************************/ + + + +/****************************************************************************** + * Desc : Functions + ******************************************************************************/ + + + + +/****************************************************************************** + * Func : Printf + * Desc : wrapper of vprintf which is thread safe + * Args : pcFormat Format of message + * Outs : return value of vprintf + ******************************************************************************/ +int Printf (const char * pcFormat, ...) +{ + int nRetVal; + va_list vlList; + + va_start (vlList, pcFormat); + + nRetVal = vprintf (pcFormat, vlList); + + va_end (vlList); + + return nRetVal; +} + + diff --git a/debug.h b/debug.h new file mode 100644 index 0000000..f74627c --- /dev/null +++ b/debug.h @@ -0,0 +1,106 @@ +/******************************************************************************* + * File : debug.h + * + * Author : Henry He + * Created : Thu 22 Oct 2009 03:29:31 PM CST + * Description : + ******************************************************************************/ + +#ifndef __DEBUG_INC_20091022_152931_HENRY__ +#define __DEBUG_INC_20091022_152931_HENRY__ + + +/******************************************************************************* + * Desc : Includes Files + ******************************************************************************/ +#include + +#include "utils/typedef.h" +#include "utils/config.h" +#include "utils/HLH_Mutex.h" + + +/******************************************************************************* + * Desc : Macro Definations + ******************************************************************************/ + + +//=========================== Assertion ====================================== + +#if (__USE_ASSERT__ != 0) +# define ASSERT(cond) \ + do { \ + if ( !(cond) ) { \ + HLH_DEBUG (HLH_DEBUG_MAIN, ("assert [ %s ] failed", #cond)); \ + while (1); \ + } \ + } while (0) +#else +# define ASSERT(cond) +#endif + + +//=========================== Debug ========================================== + + +// 'info' must be surrounded by '()' + +#if (__USE_HLH_DEBUG__ != 0) + +# define HLH_DEBUG(flags, info) \ + do { \ + if ( (flags) & HLH_DEBUG_FLAGS ) { \ + Printf ("%s: %s: %d: ", __FILE__, __FUNCTION__, __LINE__); \ + Printf info; \ + Printf ("\n"); \ + } \ + } while (0) + +#else +# define HLH_DEBUG(flags, info) +#endif + +#if (__TRACE_CALL__ != 0) + +# define HLH_ENTER_FUNC(flags) \ + do { \ + HLH_DEBUG ( flags, ("Enter-----------------------------") ); \ + } while (0) + +# define HLH_EXIT_FUNC(flags) \ + do { \ + HLH_DEBUG ( flags, ("Exit -----------------------------") ); \ + } while (0) + +#else + +# define HLH_ENTER_FUNC(flags) +# define HLH_EXIT_FUNC(flags) + +#endif + + +/******************************************************************************* + * Desc : Type Definations + ******************************************************************************/ + + +/******************************************************************************* + * Desc : Classes + ******************************************************************************/ + + +/******************************************************************************* + * Desc : Extern Variables + ******************************************************************************/ +extern HLH_AutoMutex g_zhmPrintMutex; + + +/******************************************************************************* + * Desc : Functions + ******************************************************************************/ +extern int Printf (const char * pcFormat, ...); + + + +#endif /* __DEBUG_INC_20091022_152931_HENRY__ */ diff --git a/typedef.h b/typedef.h new file mode 100644 index 0000000..564e7c8 --- /dev/null +++ b/typedef.h @@ -0,0 +1,74 @@ +/************************************************************************* + * File : typedef.h + * + * Author : Henry He + * Created : 2009-9-17 16:04:46 + * Description : + ************************************************************************/ + +#ifndef __TYPEDEF_INC_20090917_160446_HENRY__ +#define __TYPEDEF_INC_20090917_160446_HENRY__ + +/****************************************************************************** + * Desc : basic types one + ******************************************************************************/ +typedef signed char I8; +typedef signed short I16; +typedef signed int I32; +typedef signed long int I64; + +typedef unsigned char U8; +typedef unsigned short U16; +typedef unsigned int U32; +typedef unsigned long int U64; + + +/****************************************************************************** + * Desc : basic types two + ******************************************************************************/ +typedef signed char INT8; +typedef signed short INT16; +typedef signed int INT32; +typedef signed long int INT64; + +typedef unsigned char UINT8; +typedef unsigned short UINT16; +typedef unsigned int UINT32; +typedef unsigned long int UINT64; + + +/****************************************************************************** + * Desc : Included Files + ******************************************************************************/ +#include +#include +#include +#include +#include +#include + +#include + +#include +#include + +#include +#include +#include + +#include + +#include +#include + + + +/****************************************************************************** + * Desc : Debug + ******************************************************************************/ +#include "utils/config.h" +#include "utils/debug.h" + + + +#endif /* __TYPEDEF_INC_20090917_160446_HENRY__ */ -- 2.11.4.GIT