-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathHTTPSession.cpp
More file actions
120 lines (110 loc) · 1.93 KB
/
Copy pathHTTPSession.cpp
File metadata and controls
120 lines (110 loc) · 1.93 KB
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
#include "HTTPSession.h"
//************************************
// 文件名称:HttpClient.cpp
// 功能说明:
// QQ 群:181926437
// QQ 号:515199494
// 作 者:流年
// 日 期:2015/08/31
//************************************
HTTPSession::HTTPSession(void)
: m_sock(INVALID_SOCKET)
{
}
HTTPSession::~HTTPSession(void)
{
closeSession();
}
bool HTTPSession::connect(uint32_t ip, uint16_t port)
{
do
{
if (!initSocket())
{
break;
}
struct sockaddr_in dstAddr;
dstAddr.sin_family = AF_INET;
dstAddr.sin_addr.s_addr = ip;
dstAddr.sin_port = port;
if (::connect(m_sock, (struct sockaddr*)&dstAddr, sizeof(dstAddr)) == -1)
{
break;
}
return true;
} while (false);
return false;
}
void HTTPSession::closeSession()
{
if (m_sock != INVALID_SOCKET)
{
#if defined(_WIN32)
closesocket(m_sock);
#else
close(m_sock);
#endif
}
m_sock = INVALID_SOCKET;
}
int HTTPSession::send(const char* data, const int len)
{
return ::send(m_sock, data, len, 0);
}
int HTTPSession::read(char* data, const int len)
{
return ::recv(m_sock, data, len, 0);
}
int HTTPSession::readLine(std::string& line, const int len)
{
line.clear();
char ch = 0;
uint32_t nSize = 0;
do
{
if (len > 0 && nSize >= len)
{
break;
}
int ret = read(&ch, 1);
if (ret == 1)
{
nSize ++;
line.append(1, ch);
}
else if(ret == 0)
{
break;
}
else
{
nSize = (nSize == 0 ? -1 : nSize);
break;
}
} while (ch != '\n');
return nSize;
}
int HTTPSession::readLimit(std::string& limit, const int len)
{
limit.clear();
//len 1048
int size = len;
int nRead = size > 1024 ? 1024 : size;
char data[1024] = {0};
int ret = 0;
while(size > 0 && (ret = read(data, nRead)) > 0)
{
size -= ret;
nRead = size > 1024 ? 1024 : size;
limit.append(data, ret);
}
return len - size;
}
bool HTTPSession::initSocket()
{
if (m_sock == INVALID_SOCKET)
{
m_sock = socket(AF_INET, SOCK_STREAM, 0);
}
return m_sock != INVALID_SOCKET;
}