-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathudpclient.cpp
More file actions
176 lines (153 loc) · 4.27 KB
/
udpclient.cpp
File metadata and controls
176 lines (153 loc) · 4.27 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
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
/*---------------------------------------------------------------------------
Anime Tracker - udp client for http://anidb.net/
----------------------------------------------------------------------------*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <netdb.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <arpa/inet.h>
#include "log.h"
#include "udpclient.h"
udpclient::udpclient(int delay, log *rlog) {
udp_delay = delay;
s= -1;
udp_log = rlog;
connected = false;
retry = RETRY_TIMES;
udp_log->debug("UDP client loaded");
}
bool udpclient::udpconnect(const char *server, int port) {
struct hostent *hp;
struct in_addr iaddr;
struct sockaddr_in sock_in;
struct sockaddr_in local_in;
struct timeval resp_timeout = { 30, 0 };
int x = 1;
int rc;
udp_log->message("Connecting to %s ..", server);
if (connected) {
udp_log->warning("Already made a connection");
return false;
}
bzero((char *)&sock_in, sizeof(sock_in));
iaddr.s_addr = inet_addr(server);
if (iaddr.s_addr != INADDR_NONE) {
struct hostent *hp;
hp = gethostbyaddr((char *)&iaddr, sizeof(iaddr), AF_INET);
udp_log->error("Can not relolve ip %s: %s", server, hstrerror(h_errno));
return false;
} else {
hp = gethostbyname(server);
if (!hp) {
udp_log->error("Can not relolve %s: %s", server, hstrerror(h_errno));
return false;
}
if ((unsigned)hp->h_length > sizeof(sock_in.sin_addr) ||
hp->h_length < 0) {
udp_log->error("Connection: Illegal address");
return false;
}
}
sock_in.sin_family = hp->h_addrtype;
//sock_in.sin_len = sizeof(sock_in); Is not used anymore
memcpy(&sock_in.sin_addr, hp->h_addr_list[0],(unsigned)(hp->h_length));
sock_in.sin_port = htons(port);
s = socket(sock_in.sin_family, SOCK_DGRAM, IPPROTO_UDP);
if (s < 0) {
udp_log->error("Can not open socket");
return false;
}
rc = setsockopt(s, SOL_SOCKET, SO_RCVTIMEO, &resp_timeout, sizeof(resp_timeout));
if (rc < 0) {
udp_log->error("Can not set timeout");
return false;
}
rc = setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &x, sizeof(x));
if (rc < 0) {
udp_log->error("Can not set address");
return false;
}
rc = setsockopt(s, SOL_SOCKET, SO_KEEPALIVE, &x, sizeof(x));
if (rc < 0) {
udp_log->error("Can not set keep-alive");
return false;
}
bzero((char *)&local_in, sizeof(local_in));
//local_in.sin_len = sizeof(local_in); Is not used anymore
local_in.sin_family = sock_in.sin_family;
local_in.sin_port = htons(port);
rc = bind(s, (struct sockaddr *)(&local_in), sizeof(local_in));
if (rc < 0) {
udp_log->error("Can not bind local port");
return false;
}
if (connect(s, (struct sockaddr *)(&sock_in), sizeof(sock_in)) < 0) {
udp_log->error("Can not connect to server");
return false;
}
next_send = time(NULL);
connected = true;
udp_log->message("Connected");
return true;
}
void udpclient::udpdisconnect() {
int rc;
rc = shutdown(s, SHUT_RDWR);
connected = false;
udp_log->message("Disconnected");
}
bool udpclient::recieve() {
long len;
if (!connected) {
udp_log->error("Make a connection before sending/receiving");
return false;
}
bzero(gbuf, sizeof(gbuf));
len = read(s, gbuf, sizeof(gbuf));
if (len < 0) {
udp_log->error("Read Failed");
if (retry != 0) {
retry--;
udp_log->warning("Retry %d of %d for receiving.", (RETRY_TIMES - retry), RETRY_TIMES);
return this->recieve();
}
udp_log->error("Could not recieve message");
return false;
}
udp_log->debug("Packet Recieved: %s",this->get_recieved());
retry = RETRY_TIMES;
return true;
}
char* udpclient::get_recieved() {
return gbuf;
}
void udpclient::send(const char *buf){
time_t now;
long delay;
if (!connected) {
udp_log->error("Make a connection before sending/receiving");
return ;
}
now = time(NULL);
delay = next_send - now;
if (delay > 0) {
udp_log->debug("%i delay before send", delay);
sleep((unsigned int)delay);
}
if ( write (s, buf, strlen (buf)) != strlen (buf)) {
udp_log->error("Write Failed");
if (retry > 0) {
retry--;
udp_log->warning("Retry %d of %d for sending.", (RETRY_TIMES - retry), RETRY_TIMES);
next_send = time(NULL) + udp_delay;
return this->send(buf);
}
udp_log->error("Could not send message");
return ;
}
udp_log->debug("Sent Packet: %s", buf);
next_send = time(NULL) + udp_delay;
retry = RETRY_TIMES;
}