add HLH_Ref
[HLH_utils.git] / HLH_UDPSock.h
blobfafdd1e3115a22d50e76a1cde6e98ff8e729c568
1 /*******************************************************************************
2 * File : HLH_UDPSock.h
3 *
4 * Author : Henry He
5 * Created : 2009-10-10 16:35:35
6 * Description :
7 ******************************************************************************/
9 #ifndef __HLH_UDPSOCK_INC_20091010_163535_HENRY__
10 #define __HLH_UDPSOCK_INC_20091010_163535_HENRY__
13 /*******************************************************************************
14 * Desc : Includes Files
15 ******************************************************************************/
16 #include "HLH_utils/typedef.h"
17 #include "HLH_utils/HLH_Thread.h"
18 #include "HLH_utils/HLH_Sock.h"
21 /*******************************************************************************
22 * Desc : Macro Definations
23 ******************************************************************************/
25 ////////////////////////////////////////////////////////////////////////////////
26 // HLH_UDPSock Error
28 #define HLH_UDPSOCK_ERR_FAILED (-1)
29 #define HLH_UDPSOCK_ERR_CANT_CREATE (-2)
30 #define HLH_UDPSOCK_ERR_NOT_CREATED (-3)
31 #define HLH_UDPSOCK_ERR_ALREADY_CREATED (-4)
34 ////////////////////////////////////////////////////////////////////////////////
35 // Default Values
37 #define HLH_UDPSOCK_DEFAULT_POLLWAIT_SECONDS 1
40 ////////////////////////////////////////////////////////////////////////////////
41 // Max Values
43 #define HLH_UDPSOCK_MAX_RECEIVE_LENGTH 1536
46 /*******************************************************************************
47 * Desc : Type Definations
48 ******************************************************************************/
50 ////////////////////////////////////////////////////////////////////////////////
51 // Handler callback functions
53 typedef void (* OnRecvFunc) (void *pvOnRecvParam,
54 void *pvBuf, UINT32 unLen,
55 HLH_SockAddr &zhsSockAddr);
58 /*******************************************************************************
59 * Desc : Global Variables
60 ******************************************************************************/
63 /*******************************************************************************
64 * Desc : Classes
65 ******************************************************************************/
67 ////////////////////////////////////////////////////////////////////////////////
68 // Receive Event Handler
70 class RecvHandler
72 public:
73 bool operator == (const RecvHandler &zrhRecvHandler) const {
74 return ( m_zorOnRecv == zrhRecvHandler.m_zorOnRecv
75 && m_pvOnRecvParam == zrhRecvHandler.m_pvOnRecvParam );
78 public:
79 OnRecvFunc m_zorOnRecv;
80 void *m_pvOnRecvParam;
84 ////////////////////////////////////////////////////////////////////////////////
85 // class to process UDP transmit
87 class HLH_UDPSock : public HLH_Sock
90 public:
91 // Constructor
92 HLH_UDPSock ();
94 // Deconstructor
95 ~HLH_UDPSock ();
98 public:
99 // Whether this instance created
100 bool IsCreated ();
102 // Create UDP socket and bind it to zhsBindAddr,
103 // then create thread to handle receive event
104 int Create (const HLH_SockAddr &zhsBindAddr);
106 // Destroy this UDP socket
107 void Destroy ();
109 // Reload of HLH_Sock::Connect ()
110 int Connect (const HLH_SockAddr &zhsSockAddr);
112 // Get the address of peer if connected
113 int GetConnectAddr (HLH_SockAddr & zhsConnectAddr);
115 // Add receive handler to this socket
116 void AddRecvHandler (OnRecvFunc zorOnRecv, void *pvOnRecvParam);
118 // Delete receive handler from receive handler list
119 void DelRecvHandler (OnRecvFunc zorOnRecv, void *pvOnRecvParam);
121 // Clear the receive handler list
122 void ClearRecvHandlerList ();
124 private:
125 // Wrapper of __RecvThreadFunc
126 static void * RecvThreadFunc (HLH_Thread &zhtRecvThread, void *pvThis);
128 // Thread function that call receiver handler when data received
129 void __RecvThreadFunc (HLH_Thread &zhtRecvThread);
131 private:
132 // Mutex of this instance
133 HLH_Mutex m_zhmMutex;
135 // Whether this instance created
136 bool m_bCreated;
138 // Whether this UDP connected
139 bool m_bConnected;
140 HLH_SockAddr m_zhsConnectAddr;
142 // Receive event handler list
143 std::list <RecvHandler> m_slRecvHandlerList;
145 // Threads
146 HLH_Thread m_zhtRecvThread;
153 #endif /* __HLH_UDPSOCK_INC_20091010_163535_HENRY__ */