-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathSocket.cpp
133 lines (100 loc) · 2.24 KB
/
Socket.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
#include "Socket.h"
#include "type.h"
Socket::Socket(UINT mtu)
:m_Mtu(mtu)
{
//WSADATA wsaData;
//WORD wVersionRequested = MAKEWORD( 2, 2 );
//WSAStartup(wVersionRequested, &wsaData);
m_Socket = NULL;
memset(&m_BindAddr, 0, sizeof(m_BindAddr));
memset(&m_ConnectAddr, 0, sizeof(m_ConnectAddr));
m_isOpen = FALSE;
}
Socket::~Socket()
{
Close();
}
void Socket::Close()
{
if (m_Socket)close(m_Socket);
m_Socket = -1;
m_isOpen = FALSE;
}
int Socket::Read(BYTE* pBuffer, int readSize, UINT nTimeOut)
{
int selectState;
int recvSize;
if (!pBuffer || !m_isOpen)
return -1;
selectState = Select(SELECT_MODE_READY, nTimeOut);
if (SELECT_STATE_TIMEOUT == selectState)
return 0;
if (SELECT_STATE_READY == selectState)
{
recvSize = recv(m_Socket, (char*)pBuffer, readSize, 0);
if (recvSize <= 0)
return -1;
return recvSize;
}
return -1;
}
int Socket::Write(PBYTE pBuffer, int writeSize, UINT nTimeOut)
{
int selectState = 0;
int sendSize = 0;
if (!pBuffer || !m_isOpen)
return -1;
selectState = Select(SELECT_MODE_WRITE, nTimeOut);
if (selectState == SELECT_STATE_TIMEOUT)
return 0;
if (selectState == SELECT_STATE_READY)
{
sendSize = send(m_Socket, (char*)pBuffer, writeSize, 0);
if (sendSize <= 0)
return -1;
return sendSize;
}
return -1;
}
SOCKADDR_IN Socket::GetBindAddr()
{
return m_BindAddr;
}
SOCKADDR_IN Socket::GetConnectAddr()
{
return m_ConnectAddr;
}
UINT Socket::GetMTU()
{
return m_Mtu;
}
int Socket::Select( int iMode, int nTimeoutUsec )
{
fd_set fdset;
fd_set *readSet, *writeSet;
timeval selectTimeout;
int ret;
selectTimeout.tv_sec = 0;
selectTimeout.tv_usec = nTimeoutUsec*1000;
FD_ZERO (&fdset);
FD_SET (m_Socket, &fdset);
readSet = (iMode & SELECT_MODE_READY) ? &fdset : NULL;
writeSet = (iMode & SELECT_MODE_WRITE) ? &fdset : NULL;
ret = select ( (int)m_Socket + 1, readSet, writeSet, NULL, &selectTimeout);
if(-1 == ret)
{
printf("select error\n");
return SELECT_STATE_ERROR;
}
else if(0 == ret)
{
printf("select timeout\n");
return SELECT_STATE_TIMEOUT;
}
else
{
return SELECT_STATE_READY;
}
return SELECT_STATE_TIMEOUT;
}