1 /*******************************************************************************
5 * Created : 2009-10-10 16:35:35
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 ////////////////////////////////////////////////////////////////////////////////
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 ////////////////////////////////////////////////////////////////////////////////
37 #define HLH_UDPSOCK_DEFAULT_POLLWAIT_SECONDS 1
40 ////////////////////////////////////////////////////////////////////////////////
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 /*******************************************************************************
65 ******************************************************************************/
67 ////////////////////////////////////////////////////////////////////////////////
68 // Receive Event Handler
73 bool operator == (const RecvHandler
&zrhRecvHandler
) const {
74 return ( m_zorOnRecv
== zrhRecvHandler
.m_zorOnRecv
75 && m_pvOnRecvParam
== zrhRecvHandler
.m_pvOnRecvParam
);
79 OnRecvFunc m_zorOnRecv
;
80 void *m_pvOnRecvParam
;
84 ////////////////////////////////////////////////////////////////////////////////
85 // class to process UDP transmit
87 class HLH_UDPSock
: public HLH_Sock
99 // Whether this instance created
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
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 ();
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
);
132 // Mutex of this instance
133 HLH_Mutex m_zhmMutex
;
135 // Whether this instance created
138 // Whether this UDP connected
140 HLH_SockAddr m_zhsConnectAddr
;
142 // Receive event handler list
143 std::list
<RecvHandler
> m_slRecvHandlerList
;
146 HLH_Thread m_zhtRecvThread
;
153 #endif /* __HLH_UDPSOCK_INC_20091010_163535_HENRY__ */