• R/O
  • SSH
  • HTTPS

wincs: Commit


Commit MetaInfo

Revisão13 (tree)
Hora2010-05-25 15:54:25
Autorsho1get

Mensagem de Log

(mensagem de log vazia)

Mudança Sumário

Diff

--- SocketX/Config.h (nonexistent)
+++ SocketX/Config.h (revision 13)
@@ -0,0 +1,31 @@
1+#pragma once
2+
3+//////////////////////////////////////////////////////////////////////////
4+
5+#define SM_EVENT (WM_APP + 100)
6+#define SM_GETHOST (WM_APP + 101)
7+
8+#define SX_KA_TIME 30000 // 30 seconds
9+#define SX_KA_INTERVAL 1000 // 1 seconds
10+#define SX_SEND_TIMEOUT 20000 // 20 seconds
11+#define SX_RECV_TIMEOUT 20000 // 20 seconds
12+#define SX_TCP_MAXBUFSIZE 262144 // 256 KByte
13+#define SX_TCP_MINBUFSIZE 8192 // 8 KByte
14+#define SX_UDP_BUFSIZE 4096 // 4 KByte
15+
16+#define FD_SERVER (FD_ACCEPT | FD_WRITE | FD_READ | FD_CLOSE)
17+#define FD_CLIENT (FD_WRITE | FD_READ | FD_CLOSE)
18+#define FD_PEER (FD_WRITE | FD_READ | FD_CLOSE)
19+
20+#define SF_KEY_RND1 0x0001
21+#define SF_KEY_RND2 0x0002
22+#define SF_KEY_RND3 0x0004
23+#define SF_SEC_MODE 0x0008
24+#define SF_SEC_HASH 0x0010
25+
26+//////////////////////////////////////////////////////////////////////////
27+
28+#define DATA_SEND(dt) const_cast<LPSTR>(reinterpret_cast<LPCSTR>(dt))
29+#define DATA_RECEIVE(dt) reinterpret_cast<LPSTR>(dt)
30+
31+//////////////////////////////////////////////////////////////////////////
\ No newline at end of file
--- SocketX/ServerSocket.cpp (revision 12)
+++ SocketX/ServerSocket.cpp (revision 13)
@@ -51,10 +51,9 @@
5151
5252 while (nRet != SOCKET_ERROR)
5353 {
54- nRet = recv(hSocket, reinterpret_cast<LPSTR>(&buf[0]), buf.GetSize(), 0);
54+ nRet = recv(hSocket, reinterpret_cast<LPSTR>(buf.GetData()), buf.GetSize(), 0);
5555 }
5656
57- closesocket(hSocket);
5857 TRACE0("***** ERROR: Receive(Socket not found) *****\n");
5958 return;
6059 }
--- SocketX/BaseSocket.cpp (revision 12)
+++ SocketX/BaseSocket.cpp (revision 13)
@@ -104,24 +104,11 @@
104104 int nErrorCode = WSAGETSELECTERROR(lParam);
105105 HANDLE hTask = (HANDLE)wParam;
106106
107- OnGetHostByName(hTask, nErrorCode);
107+ doGetHostByName(hTask, nErrorCode);
108108
109109 return 0;
110110 }
111111
112-void IBaseSocket::OnGetHostByName(HANDLE hTask, int nErrorCode)
113-{
114- LPHOSTENT lpHostEnt = reinterpret_cast<LPHOSTENT>(m_HostName.buf);
115-
116- if (m_HostName.hTask != hTask)
117- {
118- TRACE0("***** ERROR: GetHostByName(Handle) *****\n");
119- return;
120- }
121-
122- m_SockMsg.pTgtObj->OnGetHostByNameMessage(lpHostEnt);
123-}
124-
125112 BOOL IBaseSocket::CreateSocketWindow()
126113 {
127114 CString strClass;
@@ -342,6 +329,21 @@
342329
343330 //////////////////////////////////////////////////////////////////////////
344331
332+BOOL IBaseSocket::doGetHostByName(HANDLE hTask, int nErrorCode)
333+{
334+ LPHOSTENT lpHostEnt = reinterpret_cast<LPHOSTENT>(m_HostName.buf);
335+
336+ if (m_HostName.hTask != hTask)
337+ {
338+ TRACE0("***** ERROR: GetHostByName(Handle) *****\n");
339+ return FALSE;
340+ }
341+
342+ m_SockMsg.pTgtObj->OnGetHostByNameMessage(lpHostEnt);
343+
344+ return TRUE;
345+}
346+
345347 BOOL IBaseSocket::doCloseSocket(SOCKET hSocket)
346348 {
347349 if (closesocket(hSocket) != 0)
@@ -556,9 +558,9 @@
556558 BOOL IBaseSocket::doSend(SOCKET hSocket, const CByteArray &data)
557559 {
558560 CByteArray snd;
559- size_t dataSize;
560- size_t sendSize;
561- size_t bufPtr;
561+ HEADER header;
562+ size_t dataSize, hdrSize;
563+ size_t dataPtr, hdrPtr;
562564 LONG lEvent;
563565 BOOL fResult;
564566 int nRet;
@@ -572,18 +574,20 @@
572574 }
573575
574576 dataSize = data.GetSize();
575- sendSize = dataSize + sizeof(HEADER);
576- bufPtr = 0;
577+ hdrSize = sizeof(HEADER);
578+ dataPtr = 0;
579+ hdrPtr = 0;
577580 fResult = TRUE;
578581
579- snd.SetSize(sendSize);
580- ((LPHEADER)snd.GetData())->size = dataSize;
581- CopyMemory((snd.GetData() + sizeof(HEADER)), data.GetData(), dataSize);
582+ header.size = dataSize;
583+ snd.SetSize(hdrSize);
584+ CopyMemory(snd.GetData(), &header, hdrSize);
582585 snd.FreeExtra();
583586
584- do
587+ do
585588 {
586- nRet = send(hSocket, reinterpret_cast<const LPSTR>(snd.GetData() + bufPtr), sendSize, 0);
589+ // Send header
590+ nRet = send(hSocket, DATA_SEND(snd.GetData() + hdrPtr), hdrSize, 0);
587591
588592 if (nRet == SOCKET_ERROR)
589593 {
@@ -595,7 +599,7 @@
595599 }
596600 else
597601 {
598- TRACE1("***** ERROR: send(%d) *****\n", m_dwError);
602+ TRACE1("***** ERROR: send header(%d) *****\n", m_dwError);
599603 fResult = FALSE;
600604 break;
601605 }
@@ -602,11 +606,42 @@
602606 }
603607 else
604608 {
605- bufPtr += nRet;
606- sendSize -= nRet;
609+ hdrPtr += nRet;
610+ hdrSize -= nRet;
607611 }
612+ }
613+ while (hdrSize > 0);
614+
615+ if (fResult)
616+ {
617+ do
618+ {
619+ // Send data
620+ nRet = send(hSocket, DATA_SEND(data.GetData() + dataPtr), dataSize, 0);
621+
622+ if (nRet == SOCKET_ERROR)
623+ {
624+ m_dwError = GetLastError();
625+
626+ if (m_dwError == WSAEWOULDBLOCK)
627+ {
628+ continue;
629+ }
630+ else
631+ {
632+ TRACE1("***** ERROR: send data(%d) *****\n", m_dwError);
633+ fResult = FALSE;
634+ break;
635+ }
636+ }
637+ else
638+ {
639+ dataPtr += nRet;
640+ dataSize -= nRet;
641+ }
642+ }
643+ while (dataSize > 0);
608644 }
609- while (sendSize > 0);
610645
611646 if ((lEvent != 0) && (lEvent & FD_WRITE))
612647 {
@@ -636,7 +671,7 @@
636671 }
637672
638673 // Receive header
639- nRet = recv(hSocket, reinterpret_cast<LPSTR>(&header.size), sizeof(HEADER), MSG_PEEK);
674+ nRet = recv(hSocket, DATA_RECEIVE(&header), sizeof(HEADER), MSG_PEEK);
640675
641676 if (nRet == sizeof(HEADER))
642677 {
@@ -651,7 +686,7 @@
651686 do
652687 {
653688 // Receive data
654- nRet = recv(hSocket, reinterpret_cast<LPSTR>(data.GetData() + bufPtr), recvSize, 0);
689+ nRet = recv(hSocket, DATA_RECEIVE(data.GetData() + bufPtr), recvSize, 0);
655690
656691 if (nRet == SOCKET_ERROR)
657692 {
@@ -680,7 +715,6 @@
680715 if (data.GetSize() >= sizeof(HEADER))
681716 {
682717 data.RemoveAt(0, sizeof(HEADER));
683- data.FreeExtra();
684718 }
685719 }
686720 else
--- SocketX/SocketX.h (revision 12)
+++ SocketX/SocketX.h (revision 13)
@@ -35,30 +35,10 @@
3535 #include <WinSock2.h>
3636 #include <MSTcpIP.h> // For tcp_keepalive, promiscuous
3737 #include <memory> // For shard_ptr
38+#include "Config.h"
3839
3940 #pragma comment(lib, "ws2_32")
4041
41-#define SM_EVENT (WM_APP + 100)
42-#define SM_GETHOST (WM_APP + 101)
43-
44-#define SX_KA_TIME 30000 // 30 seconds
45-#define SX_KA_INTERVAL 1000 // 1 seconds
46-#define SX_SEND_TIMEOUT 20000 // 20 seconds
47-#define SX_RECV_TIMEOUT 20000 // 20 seconds
48-#define SX_TCP_MAXBUFSIZE 262144 // 256 KByte
49-#define SX_TCP_MINBUFSIZE 8192 // 8 KByte
50-#define SX_UDP_BUFSIZE 4096 // 4 KByte
51-
52-#define SX_EVENT_SEND 1000
53-#define SX_EVENT_RECEIVE 2000
54-#define SX_EVENT_CONNECT 3000
55-#define SX_EVENT_ACCEPT 4000
56-#define SX_EVENT_CLOSE 5000
57-
58-#define FD_SERVER (FD_ACCEPT | FD_WRITE | FD_READ | FD_CLOSE)
59-#define FD_CLIENT (FD_WRITE | FD_READ | FD_CLOSE)
60-#define FD_PEER (FD_WRITE | FD_READ | FD_CLOSE)
61-
6242 //////////////////////////////////////////////////////////////////////////
6343
6444 BOOL SocketXInitialize();
@@ -117,7 +97,7 @@
11797 {
11898 public:
11999 ISocketMessage() {}
120- ~ISocketMessage() {}
100+ virtual ~ISocketMessage() {}
121101
122102 // Message function
123103 virtual void OnSocketSendMessage(DWORD dwSocketID) {}
@@ -170,7 +150,6 @@
170150 virtual void OnClose(SOCKET hSocket, int nErrorCode) {}
171151 virtual void OnSend(SOCKET hSocket, int nErrorCode) {}
172152 virtual void OnReceive(SOCKET hSocket, int nErrorCode) {}
173- void OnGetHostByName(HANDLE hTask, int nErrorCode);
174153
175154 void SetTargetWnd(SOCKMSG SockMsg) { m_SockMsg = SockMsg; }
176155 void ConvertSockAddrToNetAddr(const LPSOCKADDR lpSockAddr, NETADDR &NetAddr);
@@ -221,6 +200,7 @@
221200 void MakeSockAddrIN(LPSOCKADDR_IN lpSockAddrIN, DWORD dwAddress, WORD wPort, UINT nAF = AF_INET);
222201
223202 BOOL doCloseSocket(SOCKET hSocket);
203+ BOOL doGetHostByName(HANDLE hTask, int nErrorCode);
224204 BOOL doSend(SOCKET hSocket, const CByteArray &data);
225205 BOOL doSendTo(SOCKET hSocket, const CByteArray &data, const LPSOCKADDR lpSockAddr, int nSockAddrLen);
226206 BOOL doReceive(SOCKET hSocket, CByteArray &data);
@@ -263,6 +243,8 @@
263243 typedef struct _HEADER
264244 {
265245 size_t size;
246+ size_t pad;
247+ WORD flags;
266248 }
267249 HEADER, *LPHEADER;
268250
Show on old repository browser