-
Notifications
You must be signed in to change notification settings - Fork 0
/
udpinterface.cpp
92 lines (69 loc) · 1.81 KB
/
udpinterface.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
#include <iostream>
#include "udpinterface.h"
#include "const.h"
UDPInterface::UDPInterface(TypeParams _params):
IInterface(_params)
{
#ifdef _WIN32
WSADATA wsdata;
WSAStartup(0x0101,&wsdata);
#endif
params = std::dynamic_pointer_cast<ParamsUDP>(_params);
if(params)
{
std::cout << "constructor UDP Interface devPath=" << params->getDevPath() << std::endl;
}
else
{
std::cout << "Fatal Error get TypeParamsUDP. Exit program" << std::endl;
exit(0);
}
}
bool UDPInterface::open()
{
int result = -1;
sockfd = ::socket(AF_INET, SOCK_DGRAM, 0);
if(sockfd > 0)
{
memset(&sock_addr, 0, sizeof(struct sockaddr_in));
sock_addr.sin_family = AF_INET;
sock_addr.sin_port = htons(params->getPortUDP());
sock_addr.sin_addr.s_addr = inet_addr(params->getDevPath().c_str());
result = 0;
}
return (result == 0)?true:false;
}
bool UDPInterface::bind()
{
int res = ::bind(sockfd, (struct sockaddr *)&sock_addr, sizeof(sock_addr) );
return (res == 0) ? true : false;
}
bool UDPInterface::close()
{
#ifdef _WIN32
WSACleanup();
#endif
return ::close(sockfd);
}
int UDPInterface::read(char *data, int size, int timeout)
{
if(check_sock(sockfd, timeout))
return recvfrom(sockfd, data, size, /*MSG_DONTWAIT*/ 0, NULL, NULL);
else
return 0;
}
int UDPInterface::write(const char *data, int size)
{
return sendto(sockfd, data, size, 0,
(struct sockaddr *)&sock_addr, sizeof(sock_addr));
}
bool UDPInterface::check_sock(int sock, int timeout)
{
fd_set rset;
struct timeval tv;
FD_ZERO(&rset);
FD_SET(sock, &rset);
tv.tv_sec = timeout/1000000;
tv.tv_usec = timeout - tv.tv_sec*1000000;
return (select(sock + 1, &rset, NULL, NULL, &tv) > 0);
}