-
Notifications
You must be signed in to change notification settings - Fork 40
/
Copy pathBcecrSocket.cpp
299 lines (262 loc) · 5.81 KB
/
BcecrSocket.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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
#include "stdafx.h"
#include "BcecrSocket.h"
#include <stdio.h>
#include <iostream>
#ifndef USING_LOG
#define MY_LOG(pStr) std::cout << pStr << std::endl
#else
#include "../log.h"
#endif
#include <assert.h>
/**
* @brief 默认的构造函数
*/
CBcecrSocket::CBcecrSocket()
{
m_bInit = false;
m_bConneted = false;
m_bRegistered = false;
m_nType = 0;
m_chToport = 0;
m_Socket = INVALID_SOCKET;
memset(m_chLocalIp, 0, sizeof(m_chLocalIp));
memset(m_chToIp, 0, sizeof(m_chToIp));
memset(&m_in, 0, sizeof(m_in));
}
/**
* @brief ~CBcecrSocket
*/
CBcecrSocket::~CBcecrSocket()
{
unInit();
}
// 从域名获取IP地址
inline std::string GetIPAddress(const char *hostName)
{
struct hostent *host = gethostbyname(hostName);
#ifdef _DEBUG
printf("此域名的IP类型为: %s.\n", host->h_addrtype == AF_INET ? "IPV4" : "IPV6");
for (int i = 0; host->h_addr_list[i]; ++i)
printf("获取的第%d个IP: %s\n", i+1, inet_ntoa(*(struct in_addr*)host->h_addr_list[i]));
#endif
return host ? inet_ntoa(*(struct in_addr*)host->h_addr_list[0]) : "";
}
/**
* @brief 初始化一个 socket
* @param[in] *pIp 服务端IP
* @param[in] nPort 通信端口
* @param[in] nType socket类型,0:server 1:client
* @retval int
* @return 错误码,可通过GetErrorMsg()获取错误信息
*/
int CBcecrSocket::init(const char *pIp, int nPort, int nType)
{
int nRet = _NO__ERROR;
/// 创建socket,并建立连接
do
{
m_nType = nType;
assert(nType == 1);
{
if (!m_bInit)
{
m_Socket = ::socket(AF_INET, SOCK_STREAM, IPPROTO_IP);
if (INVALID_SOCKET == m_Socket)
{
nRet = ERROR_SOCKET;
break;
}
/// 发送缓冲区
const int nSendBuf = 1024 * 1024 * 2;
::setsockopt(m_Socket, SOL_SOCKET, SO_SNDBUF, (const char*)&nSendBuf, sizeof(int));
/// 接收缓冲区
const int nRecvBuf = 1024 * 1024 * 2;
::setsockopt(m_Socket, SOL_SOCKET, SO_RCVBUF, (const char*)&nRecvBuf, sizeof(int));
bool bConditionalAccept = true;
::setsockopt(m_Socket, SOL_SOCKET, SO_CONDITIONAL_ACCEPT, (const char *)&bConditionalAccept, sizeof(bool));
m_chToport = nPort;
std::string server = ('0' <= pIp[0] && pIp[0] <= '9') ? pIp : GetIPAddress(pIp);
memcpy(m_chToIp, server.c_str(), server.length());
m_in.sin_family = AF_INET;
m_in.sin_port = htons(nPort);
m_in.sin_addr.s_addr = inet_addr(m_chToIp);
}
if (_NO__ERROR == nRet)
m_bInit = true;
clock_t t = clock();
nRet = Connect();
TRACE("======> Connect use time = %d\n", clock() - t);
}
} while (false);
return nRet;
}
int CBcecrSocket::init(SOCKET s, const char *ip)
{
if (INVALID_SOCKET == s)
return -1;
m_bInit = true;
m_bConneted = true;
m_bRegistered = true;
m_Socket = s;
strcpy_s(m_chToIp, ip);
return _NO__ERROR;
}
int CBcecrSocket::Connect()
{
int nRet = _NO__ERROR;
do
{
if (0 == m_nType || !m_bInit)
break;
/// 和服务端建立连接
nRet = ::connect(m_Socket, (const sockaddr *)&m_in, sizeof(sockaddr_in));
if (SOCKET_ERROR == nRet)
{
nRet = ERROR_CONNECT;
break;
}
//socket转换为非阻塞模式,当与某个视频服务器传输中断时,防止传输阻塞
ULONG ul = 1;
if (SOCKET_ERROR == ioctlsocket(m_Socket, FIONBIO, &ul))
{
nRet = ERROR_IOCTLSOCKET;
break;
}
m_bConneted = true;
} while (false);
return nRet;
}
void CBcecrSocket::unInit()
{
m_bInit = false;
m_bConneted = false;
m_bRegistered = false;
if (INVALID_SOCKET != m_Socket)
{
closesocket(m_Socket);
m_Socket = INVALID_SOCKET;
}
}
/**
* @brief 收取数据
* @param[in] *pBuf 缓存区
* @param[in] nReadLen 数据长度
* @param[in] nTimeOut 超时时间
* @return 返回操作代码(大于等于0为成功)
* @retval int
*/
int CBcecrSocket::recvData(char *pBuf, int nReadLen, int nTimeOut)
{
if ((INVALID_SOCKET == m_Socket) || (NULL == pBuf) || (0 == nReadLen))
{
return -1;
}
const struct timeval time = { nTimeOut/1000, (nTimeOut%1000) * 1000 };
fd_set fd;
FD_ZERO(&fd);
FD_SET(m_Socket, &fd);
int ret = ::select(m_Socket+1, &fd, NULL, NULL, &time);
if ( ret )
{
if ( FD_ISSET(m_Socket, &fd) )
{
ret = ::recv(m_Socket, pBuf, nReadLen, 0);
ret = (ret <= 0) ? -1 : ret;
}
}
else if(ret < 0)
{
ret = -1;
}
return ret;
}
/**
* @brief 收取数据
* @param[in] *pData 缓存区
* @param[in] nSendLen 数据长度
* @return 返回操作代码
* @retval int
*/
int CBcecrSocket::sendData(const char *pData, int nSendLen)
{
if ((INVALID_SOCKET == m_Socket) || (NULL == pData) || (0 == nSendLen))
{
return -1;
}
int nRet = 0;
int ret = 0;
const struct timeval time = { 2, 0 };
fd_set fdSend;
int nLen = nSendLen;
const char *pTmp = pData;
while (nLen > 0)
{
FD_ZERO(&fdSend);
FD_SET(m_Socket, &fdSend);
ret = ::select(m_Socket+1, NULL, &fdSend, NULL, &time);
if ( 1== ret )
{
if ( FD_ISSET(m_Socket, &fdSend) )
{
ret = ::send(m_Socket, pTmp, nLen, 0);
if (ret <= 0)
{
nRet = -1;
break;
}
nLen -= ret;
pTmp += ret;
}
}
else if ( ret < 0)
{
nRet = ret;
break;
}
else if (0 == ret)
{
nRet = 0;
break;
}
}
return nRet;
}
/** 输出socket错误信息
* @brief 根据错误码,获取socket错误信息
* @param[in] nRet 错误码
* @return 返回错误信息字符串
* @retval string
*/
std::string CBcecrSocket::GetErrorMsg(const int nRet) const
{
char Msg[100];
memset(Msg, 0, 100);
switch (nRet)
{
case _NO__ERROR:
sprintf(Msg, "Success!\n");
break;
case ERROR_WSASTARTUP:
sprintf(Msg, "WSAStartup() error:%d.\n", WSAGetLastError());
break;
case ERROR_SOCKET:
sprintf(Msg, "socket() error:%d.\n", WSAGetLastError());
break;
case ERROR_CONNECT:
sprintf(Msg, "connect() error:%d.\n", WSAGetLastError());
break;
case ERROR_BIND:
sprintf(Msg, "bind() error:%d.\n", WSAGetLastError());
break;
case ERROR_LISTEN:
sprintf(Msg, "listen() error:%d.\n", WSAGetLastError());
break;
case ERROR_IOCTLSOCKET:
sprintf(Msg, "ioctlsocket() error:%d.\n", WSAGetLastError());
break;
default:
sprintf(Msg, "Unknow error!\n");
break;
}
return Msg;
}