Add run time options to RPi gateway

This commit is contained in:
Marcelo Aquino
2016-09-08 15:56:37 -03:00
parent b4f4e28545
commit 09103e9c04
13 changed files with 178 additions and 77 deletions
+7 -6
View File
@@ -29,6 +29,7 @@
#include <sys/time.h>
#include <netinet/tcp.h>
#include <errno.h>
#include "log.h"
#include "EthernetClient.h"
EthernetClient::EthernetClient() : _sock(-1) {
@@ -50,7 +51,7 @@ int EthernetClient::connect(const char* host, uint16_t port) {
sprintf(port_str, "%hu", port);
if ((rv = getaddrinfo(host, port_str, &hints, &servinfo)) != 0) {
fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(rv));
mys_log(LOG_ERR, "getaddrinfo: %s\n", gai_strerror(rv));
return -1;
}
@@ -58,13 +59,13 @@ int EthernetClient::connect(const char* host, uint16_t port) {
for (p = servinfo; p != NULL; p = p->ai_next) {
if ((sockfd = socket(p->ai_family, p->ai_socktype,
p->ai_protocol)) == -1) {
perror("socket");
mys_log(LOG_ERR, "socket: %s\n", strerror(errno));
continue;
}
if (::connect(sockfd, p->ai_addr, p->ai_addrlen) == -1) {
close(sockfd);
perror("connect");
mys_log(LOG_ERR, "connect: %s\n", strerror(errno));
continue;
}
@@ -72,7 +73,7 @@ int EthernetClient::connect(const char* host, uint16_t port) {
}
if (p == NULL) {
fprintf(stderr, "failed to connect\n");
mys_log(LOG_ERR, "failed to connect\n");
return -1;
}
@@ -80,7 +81,7 @@ int EthernetClient::connect(const char* host, uint16_t port) {
void *addr = &(((struct sockaddr_in*)p->ai_addr)->sin_addr);
inet_ntop(p->ai_family, addr, s, sizeof s);
ETHERNETCLIENT_DEBUG("connected to %s\n", s);
mys_log(LOG_DEBUG, "connected to %s\n", s);
freeaddrinfo(servinfo); // all done with this structure
@@ -106,7 +107,7 @@ size_t EthernetClient::write(const uint8_t *buf, size_t size) {
while (size > 0) {
rc = send(_sock, buf + bytes, size, MSG_NOSIGNAL | MSG_DONTWAIT);
if (rc == -1) {
perror("send");
mys_log(LOG_ERR, "send: %s\n", strerror(errno));
close(_sock);
_sock = -1;
break;