Develop and Download Open Source Software

Browse Subversion Repository

Contents of /SocketX/ServerSocket.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 16 - (show annotations) (download) (as text)
Mon Jun 28 05:40:57 2010 UTC (13 years, 11 months ago) by sho1get
File MIME type: text/x-c++src
File size: 7791 byte(s)
Update_20100628
1 #pragma once
2
3 #include "stdafx.h"
4 #include "SocketX.h"
5
6 //////////////////////////////////////////////////////////////////////////
7
8 IMPLEMENT_DYNAMIC(CServerSocket, IBaseSocket)
9
10 CServerSocket::CServerSocket() :
11 m_fAccept(TRUE),
12 m_fInternet(TRUE)
13 {
14 }
15
16 CServerSocket::~CServerSocket()
17 {
18 CloseAllSocketConnection();
19 }
20
21 void CServerSocket::OnSend(SOCKET hSocket, int nErrorCode)
22 {
23 if (nErrorCode != 0)
24 {
25 TRACE1("***** ERROR: OnSend(%d) *****\n", nErrorCode);
26 return;
27 }
28
29 m_SockMsg.pCallback->OnSocketSendMessage(m_SockMsg.dwSocketID);
30 }
31
32 void CServerSocket::OnReceive(SOCKET hSocket, int nErrorCode)
33 {
34 CByteArray data, buf;
35 SOCKADDR sAddr;
36 NETADDR NetAddr;
37 int nRet, nIndex;
38
39 if (nErrorCode != 0)
40 {
41 TRACE1("***** ERROR: OnReceive(%d) *****\n", nErrorCode);
42 return;
43 }
44
45 nIndex = SearchSocketConnection(hSocket);
46
47 if (nIndex == -1)
48 {
49 nRet = 0;
50 buf.SetSize(SX_TCP_MINBUFSIZE);
51
52 while (nRet != SOCKET_ERROR)
53 {
54 nRet = recv(hSocket, reinterpret_cast<LPSTR>(buf.GetData()), buf.GetSize(), 0);
55 }
56
57 TRACE0("***** ERROR: Receive(Socket not found) *****\n");
58 return;
59 }
60
61 if (!doGetPeerName(hSocket, &sAddr) || !doReceive(hSocket, data))
62 {
63 return;
64 }
65
66 ConvertSockAddrToNetAddr(&sAddr, NetAddr);
67 m_SockMsg.pCallback->OnSocketReceiveMessage(m_SockMsg.dwSocketID, NetAddr, data);
68 }
69
70 void CServerSocket::OnAccept(SOCKET hSocket, int nErrorCode)
71 {
72 SOCKADDR sAddr;
73 NETADDR NetAddr;
74 int nSockLen = sizeof(SOCKADDR);
75
76 if (nErrorCode != 0)
77 {
78 TRACE1("***** ERROR: OnAccept(%d) *****\n", nErrorCode);
79 return;
80 }
81
82 if (!Accept(&sAddr, &nSockLen))
83 {
84 return;
85 }
86
87 if (!doSetKeepAlive(hSocket, SX_KA_TIME, SX_KA_INTERVAL))
88 {
89 doCloseSocket(hSocket);
90 return;
91 }
92
93 ConvertSockAddrToNetAddr(&sAddr, NetAddr);
94 m_SockMsg.pCallback->OnSocketAcceptMessage(m_SockMsg.dwSocketID, NetAddr);
95 }
96
97 void CServerSocket::OnClose(SOCKET hSocket, int nErrorCode)
98 {
99 NETADDR NetAddr;
100
101 // WinSock Error(10053) Keep-Alive timeout
102 if (nErrorCode != 10053 && nErrorCode != 0)
103 {
104 TRACE1("***** ERROR: OnClose(%d) *****\n", nErrorCode);
105 return;
106 }
107
108 if (!CloseSocketConnection(hSocket, &NetAddr))
109 {
110 TRACE("***** ERROR: OnClose(Socket not found) *****\n");
111 return;
112 }
113
114 m_SockMsg.pCallback->OnSocketCloseMessage(m_SockMsg.dwSocketID, NetAddr);
115 }
116
117 BOOL CServerSocket::CreateSocket(SOCKMSG SockMsg)
118 {
119 return (doCreateSocket(SockMsg, SOCK_STREAM, IPPROTO_TCP) && AsyncSelect(FD_SERVER));
120 }
121
122 BOOL CServerSocket::Initialize(SOCKMSG SockMsg, WORD wPort, DWORD dwBindAddress, int nBacklog)
123 {
124 BOOL flag = TRUE;
125 BOOL fSend, fReceive;
126
127 if (!CreateSocket(SockMsg))
128 {
129 return FALSE;
130 }
131
132 if (!(Bind(wPort, dwBindAddress) && SetReuseAddr(TRUE) && Listen(nBacklog)))
133 {
134 CloseSocket();
135 return FALSE;
136 }
137
138 // TCP Option(no delay)
139 if (!SetSockOpt(TCP_NODELAY, (LPSTR)&flag, sizeof(flag)))
140 {
141 CloseSocket();
142 return FALSE;
143 }
144
145 // buffer size
146 fSend = fReceive = FALSE;
147
148 for (UINT nBufSize = SX_TCP_MAXBUFSIZE; nBufSize > 0; nBufSize /= 2)
149 {
150 if (!fSend && SetSendBufferSize(nBufSize))
151 {
152 fSend = TRUE;
153 }
154
155 if (!fReceive && SetReceiveBufferSize(nBufSize))
156 {
157 fReceive = TRUE;
158 }
159
160 if (fSend && fReceive && TRUE)
161 {
162 break;
163 }
164 }
165
166 m_fInit = TRUE;
167
168 return TRUE;
169 }
170
171 BOOL CServerSocket::Listen(int nBacklog)
172 {
173 if (listen(m_hSocket, nBacklog) == SOCKET_ERROR)
174 {
175 TRACE1("***** ERROR: listen(%d) *****\n", GetLastError());
176 m_dwError = GetLastError();
177 return FALSE;
178 }
179
180 return TRUE;
181 }
182
183 BOOL CServerSocket::Accept(LPSOCKADDR lpsAddr, int *lpsAddrLen)
184 {
185 SOCKET hSocket;
186 BOOL flag = TRUE;
187 BOOL fSend, fReceive;
188
189 hSocket = accept(m_hSocket, lpsAddr, lpsAddrLen);
190
191 if (hSocket == INVALID_SOCKET)
192 {
193 TRACE1("***** ERROR: accept(%d) *****\n", GetLastError());
194 m_dwError = GetLastError();
195 return FALSE;
196 }
197
198 if (!m_fInternet && !IsLANConnection(lpsAddr))
199 {
200 doCloseSocket(hSocket);
201 return FALSE;
202 }
203
204 // TCP Option(no delay)
205 if (!doSetSockOpt(hSocket, SOL_SOCKET, TCP_NODELAY, (LPSTR)&flag, sizeof(flag)))
206 {
207 m_dwError = GetLastError();
208 doCloseSocket(hSocket);
209 return FALSE;
210 }
211
212 // buffer size
213 fSend = fReceive = FALSE;
214
215 for (UINT nBufSize = SX_TCP_MAXBUFSIZE; nBufSize > 0; nBufSize /= 2)
216 {
217 if (!fSend && doSetSendBufferSize(hSocket, nBufSize))
218 {
219 fSend = TRUE;
220 }
221
222 if (!fReceive && doSetReceiveBufferSize(hSocket, nBufSize))
223 {
224 fReceive = TRUE;
225 }
226
227 if (fSend && fReceive && TRUE)
228 {
229 break;
230 }
231 }
232
233 if (!doAsyncSelect(hSocket, SM_EVENT, FD_CLIENT))
234 {
235 m_dwError = GetLastError();
236 doCloseSocket(hSocket);
237 return FALSE;
238 }
239
240 AddConnection(hSocket, lpsAddr, *lpsAddrLen);
241
242 return TRUE;
243 }
244
245 void CServerSocket::AddConnection(SOCKET hSocket, const LPSOCKADDR lpsAddr, int nSockAddrLen)
246 {
247 SOCKETDATA sd;
248 ZeroMemory(&sd, sizeof(sd));
249 sd.hSocket = hSocket;
250 ConvertSockAddrToNetAddr(lpsAddr, sd.NetAddr);
251 CSingleLock cl(&m_sdList.cs, TRUE);
252 m_sdList.list.Add(sd);
253 }
254
255 BOOL CServerSocket::CloseSocketConnection(SOCKET hSocket, LPNETADDR lpNetAddr)
256 {
257 int nIndex = SearchSocketConnection(hSocket);
258
259 if (nIndex != -1)
260 {
261 CSingleLock sl(&m_sdList.cs, TRUE);
262 doCloseSocket(m_sdList.list[nIndex].hSocket);
263 *lpNetAddr = m_sdList.list[nIndex].NetAddr;
264 m_sdList.list.RemoveAt(nIndex);
265 return TRUE;
266 }
267
268 TRACE0("***** ERROR: CloseSocketConnection(Socket not found) *****\n");
269
270 return FALSE;
271 }
272
273 void CServerSocket::CloseAllSocketConnection()
274 {
275 UINT nSize;
276
277 CSingleLock sl(&m_sdList.cs, TRUE);
278 nSize = m_sdList.list.GetSize();
279
280 for (UINT i = 0; i < nSize; i++)
281 {
282 doShutdown(m_sdList.list[i].hSocket, SD_BOTH);
283 doCloseSocket(m_sdList.list[i].hSocket);
284 }
285
286 m_sdList.list.RemoveAll();
287 }
288
289 int CServerSocket::SearchSocketConnection(SOCKET hSocket)
290 {
291 CSingleLock sl(&m_sdList.cs, TRUE);
292 UINT nSize = m_sdList.list.GetSize();
293 int nRet = -1;
294
295 for (UINT i = 0; i < nSize; i++)
296 {
297 if (m_sdList.list[i].hSocket == hSocket)
298 {
299 nRet = i; break;
300 }
301 }
302
303 return nRet;
304 }
305
306 BOOL CServerSocket::Broadcast(const CByteArray &data)
307 {
308 CSingleLock sl(&m_sdList.cs, TRUE);
309
310 BOOL fRet;
311 BOOL fResult = TRUE;
312 UINT nDataSize = data.GetSize();
313 UINT nListSize = static_cast<UINT>(m_sdList.list.GetSize());
314
315 if (nListSize < 1)
316 {
317 return FALSE;
318 }
319
320 for (UINT i = 0; i < nListSize; i++)
321 {
322 fRet = doSend(m_sdList.list[i].hSocket, data);
323
324 if (!fRet)
325 {
326 fResult = FALSE;
327 TRACE1("***** ERROR: Broadcast(Dst: %s) *****\n",
328 DwToIPAddress(m_sdList.list[i].NetAddr.dwAddress));
329 }
330 }
331
332 return fResult;
333 }
334
335 SOCKET CServerSocket::GetSocketHandle(DWORD dwAddress, WORD wPort)
336 {
337 CSingleLock sl(&m_sdList.cs, TRUE);
338 SOCKET hSocket = INVALID_SOCKET;
339 UINT nListSize = static_cast<UINT>(m_sdList.list.GetSize());
340
341 for (UINT i = 0; i < nListSize; i++)
342 {
343 if ((m_sdList.list[i].NetAddr.dwAddress == dwAddress) &&
344 (m_sdList.list[i].NetAddr.wPort == wPort))
345 {
346 hSocket = m_sdList.list[i].hSocket;
347 break;
348 }
349 }
350
351 return hSocket;
352 }
353
354 BOOL CServerSocket::SendToClient(const CByteArray &data, DWORD dwAddress, WORD wPort)
355 {
356 SOCKET hSocket = GetSocketHandle(dwAddress, wPort);
357
358 if (hSocket == INVALID_SOCKET)
359 {
360 TRACE0("***** ERROR: SendToClient(Socket not found) *****\n");
361 return FALSE;
362 }
363
364 return doSend(hSocket, data);
365 }
366
367 void CServerSocket::SetAccept(BOOL fAccept)
368 {
369 if (m_fAccept != fAccept)
370 {
371 m_fAccept = fAccept;
372 m_lEvent = m_fAccept ? (m_lEvent | FD_ACCEPT) : (m_lEvent ^ FD_ACCEPT);
373 AsyncSelect(m_lEvent);
374 }
375 }
376
377 //////////////////////////////////////////////////////////////////////////

Back to OSDN">Back to OSDN
ViewVC Help
Powered by ViewVC 1.1.26