1 /*******************************************************************************
5 * Created : 2009-10-8 13:55:48
7 ******************************************************************************/
9 #ifndef __HLH_SOCK_INC_20091008_135548_HENRY__
10 #define __HLH_SOCK_INC_20091008_135548_HENRY__
13 /*******************************************************************************
14 * Desc : Includes Files
15 ******************************************************************************/
17 #include "utils/typedef.h"
18 #include "utils/HLH_Time.h"
19 #include "utils/HLH_Thread.h"
22 /*******************************************************************************
23 * Desc : Macro Definations
24 ******************************************************************************/
27 ////////////////////////////////////////////////////////////////////////////////
30 #define HLH_SOCK_POLL_READ 0x01
31 #define HLH_SOCK_POLL_WRITE 0x02
32 #define HLH_SOCK_POLL_ERROR 0x04
34 ////////////////////////////////////////////////////////////////////////////////
37 #define HLH_SOCK_ERR_FAILED (-1)
38 #define HLH_SOCK_ERR_ALREADY_CREATED (-2)
39 #define HLH_SOCK_ERR_CANT_CREATE (-3)
40 #define HLH_SOCK_ERR_NOT_CREATED (-4)
42 ////////////////////////////////////////////////////////////////////////////////
45 #define HLH_SOCK_DEFAULT_LISTEN_QUEUE_NUM 10
48 /*******************************************************************************
49 * Desc : Type Definations
50 ******************************************************************************/
52 ////////////////////////////////////////////////////////////////////////////////
55 typedef enum HLH_SockType
57 HLH_SOCK_TYPE_STREAM
= 0x01,
58 HLH_SOCK_TYPE_DGRAM
= 0x02
63 /*******************************************************************************
64 * Desc : Global Variables
65 ******************************************************************************/
68 /*******************************************************************************
70 ******************************************************************************/
75 ////////////////////////////////////////////////////////////////////////////////
76 // Socket Address : simple wrapper of sockaddr_t
80 friend class HLH_Sock
;
84 /******************************************************************************
85 * Desc : Constructor / Deconstructor
86 ******************************************************************************/
89 m_saAddr
.sin_addr
.s_addr
= htonl ( INADDR_ANY
);
90 m_saAddr
.sin_port
= htons ( rand() & 0xFFFF );
91 m_saAddr
.sin_family
= AF_INET
;
94 HLH_SockAddr (UINT32 unIP
, UINT16 usPort
, sa_family_t sfFamily
= AF_INET
) {
95 m_saAddr
.sin_addr
.s_addr
= htonl ( unIP
);
96 m_saAddr
.sin_port
= htons ( usPort
);
97 m_saAddr
.sin_family
= sfFamily
;
100 HLH_SockAddr (const char *pcIP
, UINT16 usPort
, sa_family_t sfFamily
= AF_INET
) {
101 m_saAddr
.sin_addr
.s_addr
= inet_addr ( pcIP
);
102 m_saAddr
.sin_port
= htons ( usPort
);
103 m_saAddr
.sin_family
= sfFamily
;
108 /******************************************************************************
110 ******************************************************************************/
112 struct sockaddr
* GetAddrPointer () const { return (struct sockaddr
*)(&m_saAddr
); }
114 UINT32
GetAddrLen () const { return sizeof (m_saAddr
); }
116 void SetAddr (struct sockaddr_in
&saAddr
) {
120 void SetAddr (UINT32 unIP
, UINT16 usPort
, sa_family_t sfFamily
= AF_INET
) {
121 m_saAddr
.sin_addr
.s_addr
= htonl ( unIP
);
122 m_saAddr
.sin_port
= htons ( usPort
);
123 m_saAddr
.sin_family
= sfFamily
;
126 void SetAddr (const char *pcIP
, UINT16 usPort
, sa_family_t sfFamily
= AF_INET
) {
127 m_saAddr
.sin_addr
.s_addr
= inet_addr (pcIP
);
128 m_saAddr
.sin_port
= htons ( usPort
);
129 m_saAddr
.sin_family
= sfFamily
;
132 void SetAddrFamily (sa_family_t sfFamily
) {
133 m_saAddr
.sin_family
= sfFamily
;
136 void SetAddrPort (UINT16 usPort
) {
137 m_saAddr
.sin_port
= htons ( usPort
);
140 void SetAddrIP (UINT32 unIP
) {
141 m_saAddr
.sin_addr
.s_addr
= htonl ( unIP
);
144 int SetAddrIP (const char *pcIP
) {
147 s_addr
= inet_addr (pcIP
);
148 if ( s_addr
== (in_addr_t
)(-1) ) {
151 m_saAddr
.sin_addr
.s_addr
= inet_addr (pcIP
);
156 bool operator == (const HLH_SockAddr
&zhsAddr
) const {
157 return ( m_saAddr
.sin_addr
.s_addr
== zhsAddr
.m_saAddr
.sin_addr
.s_addr
158 && m_saAddr
.sin_port
== zhsAddr
.m_saAddr
.sin_port
159 && m_saAddr
.sin_family
== zhsAddr
.m_saAddr
.sin_family
);
163 struct sockaddr_in m_saAddr
;
168 ////////////////////////////////////////////////////////////////////////////////
169 // Class of HLH_Sock: simple wrapper of socket funtions
175 /******************************************************************************
176 * Desc : Constructor / Deconstructor
177 ******************************************************************************/
182 /******************************************************************************
183 * Desc : Common Operations
184 ******************************************************************************/
186 // Initialize the socket and bind it
187 int Create (HLH_SockType zhsSockType
,
188 const HLH_SockAddr
&zhsSockAddr
, bool bNonblocking
= false);
190 // Initialize the socket from a initialized socket
191 int Create (int fdSock
, bool bNonblocking
= false);
193 // Whether this socket created
196 // Listen for socket connections and limit the queue of incoming connections
197 int Listen (UINT32 unQueue
= HLH_SOCK_DEFAULT_LISTEN_QUEUE_NUM
);
199 // Requests a connection to be made on a socket
200 int Connect (const HLH_SockAddr
&zhsSockAddr
);
202 // Extracts the first connection on the queue of pending connections,
203 // creates a new socket with the same socket type protocol and address family as the specified socket,
204 // and allocates a new file descriptor for that socket
205 int Accept (HLH_SockAddr
&zhsSockAddr
);
207 // Initiates transmission of a message from the specified socket to its peer
208 int Send (const void *pvBuf
, UINT32 unLen
, UINT32 unFlags
= 0);
210 // Receives a message from a connection-mode or connectionless-mode socket
211 int Recv (void *pvBuf
, UINT32 unLen
, UINT32 unFlags
= 0);
213 // Sends a message through a connection-mode or connectionless-mode socket
214 int SendTo (const void *pvBuf
, UINT32 unLen
,
215 const HLH_SockAddr
&zhsSockAddr
, UINT32 unFlags
);
217 // Receives a message from a connection-mode or connectionless-mode socket
218 int RecvFrom (void *pvBuf
, UINT32 unLen
,
219 HLH_SockAddr
&zhsSockAddr
, UINT32 unFlags
= 0);
221 // Destroy the socket (wait until send/pollwait operations finished)
227 /******************************************************************************
228 * Desc : Poll Operations
229 ******************************************************************************/
231 // Poll for specific events
232 int Poll (UINT32
&unPollType
);
234 // Poll for specific events until time eclipsed
235 int PollWait (UINT32
&unPollType
, HLH_Time
&zhtTime
);
239 /******************************************************************************
240 * Desc : Private Data
241 ******************************************************************************/
243 // socket file descriptor
246 // Whether this socket is created
250 // Master Lock of HLH_Sock
251 HLH_Mutex m_zhmMainMutex
;
254 HLH_Mutex m_zhmRecvMutex
;
257 HLH_Mutex m_zhmSendMutex
;
260 // Poll file discriptor set: initialized when created
272 #endif /* __HLH_SOCK_INC_20091008_135548_HENRY__ */