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
|
/*
* eUpstreamPumpThread.cpp
*
* Created on: 2013. 10. 30.
* Author: kos
*/
#include "eUpstreamSocket.h"
#include "ePreDefine.h"
#include <poll.h>
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
using namespace std;
#ifdef DEBUG_LOG
//#undef LOG
//#define LOG(X,...) { do{}while(0); }
#endif
eUpstreamSocket::eUpstreamSocket()
: mSockFd(-1)
{
}
//-------------------------------------------------------------------------------
eUpstreamSocket::~eUpstreamSocket()
{
}
//-------------------------------------------------------------------------------
bool eUpstreamSocket::Connect()
{
mSockFd = socket(PF_INET, SOCK_STREAM, 0);
mSockAddr.sin_family = AF_INET;
mSockAddr.sin_port = htons(80);
mSockAddr.sin_addr.s_addr = inet_addr("127.0.0.1");
if(connect(mSockFd, (struct sockaddr*)&mSockAddr, sizeof(struct sockaddr_in))) {
return false;
}
return true;
}
//-------------------------------------------------------------------------------
int eUpstreamSocket::Request(std::string aSendData, std::string& aRecvData)
{
#ifdef DEBUG_LOG
LOG("Send : %s", aSendData.c_str());
#endif
if(write(mSockFd, aSendData.c_str(), aSendData.length()) > 0) {
struct pollfd pollevt;
pollevt.fd = mSockFd;
pollevt.events = POLLIN | POLLERR;
pollevt.revents = 0;
while(true) {
char buffer[4096] = {0};
if(read(mSockFd, buffer, 4096) <= 0) {
break;
}
#ifdef DEBUG_LOG
LOG("Buffer : %s", buffer);
#endif
aRecvData += buffer;
if (poll((struct pollfd*)&pollevt, 1, 1000) == 0) {
break;
}
}
return aRecvData.length();
}
return -1;
}
//-------------------------------------------------------------------------------
|