tweaks to polling

pull/5/head
Tom Early 7 years ago
parent 148ba8aeeb
commit ee0e62a2ca

@ -246,6 +246,7 @@ void CQnetITAP::Run(const char *cfgfile)
keep_running = true; keep_running = true;
unsigned poll_counter = 0; unsigned poll_counter = 0;
bool is_alive = false;
while (keep_running) { while (keep_running) {
fd_set readfds; fd_set readfds;
@ -265,14 +266,23 @@ void CQnetITAP::Run(const char *cfgfile)
printf("ERROR: Run: select returned err=%d, %s\n", errno, strerror(errno)); printf("ERROR: Run: select returned err=%d, %s\n", errno, strerror(errno));
break; break;
} }
if (ret == 0)
if (0 == ret) {
// nothing to read, so do the polling or pinging
unsigned char buf[3];
if (poll_counter++ < 18) {
unsigned char poll[3] = { 0xffu, 0xffu, 0xffu };
::memcpy(buf, poll, 3);
} else {
unsigned char ping[3] = { 0x02u, 0x02u, 0xffu };
::memcpy(buf, ping, 3);
}
SendTo((unsigned char)0x03U, buf);
continue; continue;
}
// there is something to read! // there is something to read!
unsigned char buf[100]; unsigned char buf[100];
sockaddr_in addr;
memset(&addr, 0, sizeof(sockaddr_in));
socklen_t size = sizeof(sockaddr);
ssize_t len; ssize_t len;
REPLY_TYPE rt = RT_NOTHING; REPLY_TYPE rt = RT_NOTHING;
@ -288,6 +298,9 @@ void CQnetITAP::Run(const char *cfgfile)
continue; continue;
} else if (FD_ISSET(gsock, &readfds)) { } else if (FD_ISSET(gsock, &readfds)) {
sockaddr_in addr;
memset(&addr, 0, sizeof(sockaddr_in));
socklen_t size = sizeof(sockaddr);
len = ::recvfrom(gsock, buf, 100, 0, (sockaddr *)&addr, &size); len = ::recvfrom(gsock, buf, 100, 0, (sockaddr *)&addr, &size);
if (len < 0) { if (len < 0) {
@ -298,18 +311,6 @@ void CQnetITAP::Run(const char *cfgfile)
if (ntohs(addr.sin_port) != G2_IN_PORT) if (ntohs(addr.sin_port) != G2_IN_PORT)
printf("DEBUG: Run: read from gsock but the port was %u, expected %u\n", ntohs(addr.sin_port), G2_IN_PORT); printf("DEBUG: Run: read from gsock but the port was %u, expected %u\n", ntohs(addr.sin_port), G2_IN_PORT);
} else {
// nothing to read, so do the polling or pinging
if (poll_counter < 18) {
unsigned char poll[3] = { 0xffu, 0xffu, 0xffu };
::memcpy(buf, poll, 3);
poll_counter++;
} else {
unsigned char ping[3] = { 0x02u, 0x02u, 0xffu };
::memcpy(buf, ping, 3);
}
SendTo((unsigned char)0x03U, buf);
continue;
} }
if (rt != RT_NOTHING) { if (rt != RT_NOTHING) {
@ -326,10 +327,13 @@ void CQnetITAP::Run(const char *cfgfile)
printf("DEBUG: Run: got data acknowledgement\n"); printf("DEBUG: Run: got data acknowledgement\n");
break; break;
case RT_PONG: case RT_PONG:
printf("DEBUG: Run: got pong\n"); if (! is_alive) {
printf("Icom Radio is connected.\n");
is_alive = true;
}
break; break;
case RT_TIMEOUT: case RT_TIMEOUT:
printf("DEBUG: Run: got timeout\n"); printf("DEBUG: Run: got a timeout.\n");
break; break;
default: default:
break; break;
@ -339,12 +343,6 @@ void CQnetITAP::Run(const char *cfgfile)
//printf("read %d bytes from QnetGateway\n", (int)len); //printf("read %d bytes from QnetGateway\n", (int)len);
if (ProcessGateway(len, buf)) if (ProcessGateway(len, buf))
break; break;
} else {
char title[5];
for (int i=0; i<4; i++)
title[i] = (buf[i]>=0x20u && buf[i]<0x7fu) ? buf[i] : '.';
title[4] = '\0';
printf("DEBUG: Run: received unknow packet '%s' len=%d\n", title, (int)len);
} }
} }

Loading…
Cancel
Save

Powered by TurnKey Linux.