-
Notifications
You must be signed in to change notification settings - Fork 0
/
SocketInterface.cpp
90 lines (84 loc) · 2.51 KB
/
SocketInterface.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
#include <string.h>
#include <netinet/in.h>
#include <sys/socket.h>
//#include<curl/curl.h>
#include<fcntl.h>
#include<arpa/inet.h>
#include "QueueFilter.h"
#include "Initialize.h"
#include "xmlParser.h"
#include "Inotify.h"
#include "SocketInterface.h"
SocketInterface::SocketInterface() : m_ip("127.0.0.1")
{
}
void SocketInterface::XmlParse(std::string config_file_name)
{
XMLNode xMainNode = XMLNode::openFileHelper(config_file_name.c_str(), "head");
int num = xMainNode.nChildNode("plugin");
if (num == 0)
{
perror("Error there is no plugin tag in config xml\n");
exit(1);
}
for (int i = 0; i < num; i++)
{
string name = xMainNode.getChildNode("plugin", i).getAttribute("name");
if (name == "socket")
{
XMLNode xNode = xMainNode.getChildNode("plugin", i);
m_watch = xNode.getChildNode("localpath").getAttribute("watch");
m_ip = xNode.getChildNode("localpath").getChildNode("deshost").getAttribute("ip");
m_port = atoi(xNode.getChildNode("localpath").getChildNode("deshost").getAttribute("port"));
cout << "监控路径: " << m_watch << " ip: " << m_ip << " 端口号: " << m_port << endl;
break;
}
}
}
int SocketInterface::Execute(Event e)
{
static int i = 1;
int sockfd = 0;
int numbytes = 0;
int flags = 0;
int cycletimes = 0;
char sendBuffer[2048] = "\0";
fd_set wset;
sprintf(sendBuffer, "%s", e->path.c_str());
struct sockaddr_in serv_addr;
serv_addr.sin_family = AF_INET;
serv_addr.sin_port = htons(m_port);
serv_addr.sin_addr.s_addr = inet_addr(m_ip.c_str());
bzero(&(serv_addr.sin_zero), 8);
if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1)
{
close(sockfd);
return 2;
}
flags = fcntl(sockfd, F_GETFL, 0);
fcntl(sockfd, F_SETFL, flags | O_NONBLOCK); //设置为非阻塞;
do
{
connect(sockfd, (struct sockaddr *) & serv_addr, sizeof (struct sockaddr));
FD_ZERO(&wset);
FD_SET(sockfd, &wset);
if (select(sockfd + 1, NULL, &wset, NULL, &timeout) <= 0 && cycletimes == 5)
{
close(sockfd);
return 5;
}
numbytes = send(sockfd, sendBuffer, strlen(sendBuffer), MSG_NOSIGNAL);
if (numbytes < 0)
{
usleep(20000);
}
cycletimes++;
} while (numbytes < 0 && cycletimes != 5);
if (numbytes < 0)
{
close(sockfd);
return 4;
}
close(sockfd);
return 0;
}