add HLH_Ref
[HLH_utils.git] / HLH_Sock.h
blobe372c0574736e56b327b9477faec60ca39c5f41f
1 /*******************************************************************************
2 * File : HLH_Sock.h
3 *
4 * Author : Henry He
5 * Created : 2009-10-8 13:55:48
6 * Description :
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 "HLH_utils/typedef.h"
18 #include "HLH_utils/HLH_Time.h"
19 #include "HLH_utils/HLH_Thread.h"
22 /*******************************************************************************
23 * Desc : Macro Definations
24 ******************************************************************************/
27 ////////////////////////////////////////////////////////////////////////////////
28 // Poll Types
30 #define HLH_SOCK_POLL_READ 0x01
31 #define HLH_SOCK_POLL_WRITE 0x02
32 #define HLH_SOCK_POLL_ERROR 0x04
34 ////////////////////////////////////////////////////////////////////////////////
35 // Error Types
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 ////////////////////////////////////////////////////////////////////////////////
43 // Default Values
45 #define HLH_SOCK_DEFAULT_LISTEN_QUEUE_NUM 10
48 /*******************************************************************************
49 * Desc : Type Definations
50 ******************************************************************************/
52 ////////////////////////////////////////////////////////////////////////////////
53 // Socket type
55 typedef enum HLH_SockType
57 HLH_SOCK_TYPE_STREAM = 0x01,
58 HLH_SOCK_TYPE_DGRAM = 0x02
59 } HLH_SockType;
63 /*******************************************************************************
64 * Desc : Global Variables
65 ******************************************************************************/
68 /*******************************************************************************
69 * Desc : Classes
70 ******************************************************************************/
75 ////////////////////////////////////////////////////////////////////////////////
76 // Socket Address : simple wrapper of sockaddr_t
78 class HLH_SockAddr
80 friend class HLH_Sock;
82 public:
84 /******************************************************************************
85 * Desc : Constructor / Deconstructor
86 ******************************************************************************/
88 HLH_SockAddr () {
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;
106 ~HLH_SockAddr () { }
108 /******************************************************************************
109 * Desc : Operations
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) {
117 m_saAddr = 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) {
145 in_addr_t s_addr;
147 s_addr = inet_addr (pcIP);
148 if ( s_addr == (in_addr_t)(-1) ) {
149 return -1;
151 m_saAddr.sin_addr.s_addr = inet_addr (pcIP);
152 return 0;
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 );
162 private:
163 struct sockaddr_in m_saAddr;
168 ////////////////////////////////////////////////////////////////////////////////
169 // Class of HLH_Sock: simple wrapper of socket funtions
171 class HLH_Sock
174 public:
175 /******************************************************************************
176 * Desc : Constructor / Deconstructor
177 ******************************************************************************/
178 HLH_Sock ();
179 ~HLH_Sock ();
181 public:
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
194 bool IsCreated ();
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)
222 int Destroy ();
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);
238 private:
239 /******************************************************************************
240 * Desc : Private Data
241 ******************************************************************************/
243 // socket file descriptor
244 int m_fdSocket;
246 // Whether this socket is created
247 bool m_bCreated;
250 // Master Lock of HLH_Sock
251 HLH_Mutex m_zhmMainMutex;
253 // Read Lock
254 HLH_Mutex m_zhmRecvMutex;
256 // Write Lock
257 HLH_Mutex m_zhmSendMutex;
260 // Poll file discriptor set: initialized when created
261 fd_set m_fsReadSet;
262 fd_set m_fsWriteSet;
263 fd_set m_fsErrorSet;
272 #endif /* __HLH_SOCK_INC_20091008_135548_HENRY__ */