Update tcpsockets classes to use log framework.

This commit is contained in:
Peter Johnson
2015-07-31 23:34:54 -07:00
parent 6c0103639c
commit ecbf76e94b
2 changed files with 11 additions and 8 deletions

View File

@@ -29,6 +29,8 @@
#include <netinet/in.h>
#include <unistd.h>
#include "Log.h"
TCPAcceptor::TCPAcceptor(int port, const char* address)
: m_lsd(0),
m_port(port),
@@ -63,13 +65,13 @@ int TCPAcceptor::start() {
int result = bind(m_lsd, (struct sockaddr*)&address, sizeof(address));
if (result != 0) {
perror("bind() failed");
ERROR("bind() failed: " << strerror(errno));
return result;
}
result = listen(m_lsd, 5);
if (result != 0) {
perror("listen() failed");
ERROR("listen() failed: " << strerror(errno));
return result;
}
m_listening = true;
@@ -89,7 +91,7 @@ std::unique_ptr<TCPStream> TCPAcceptor::accept() {
std::memset(&address, 0, sizeof(address));
int sd = ::accept(m_lsd, (struct sockaddr*)&address, &len);
if (sd < 0) {
if (!m_shutdown) perror("accept() failed");
if (!m_shutdown) ERROR("accept() failed: " << strerror(errno));
return nullptr;
}
return std::unique_ptr<TCPStream>(new TCPStream(sd, &address));

View File

@@ -31,6 +31,8 @@
#include <arpa/inet.h>
#include <netinet/in.h>
#include "Log.h"
static int ResolveHostName(const char* hostname, struct in_addr* addr) {
struct addrinfo* res;
@@ -54,7 +56,7 @@ std::unique_ptr<TCPStream> TCPConnector::connect(const char* server, int port) {
}
int sd = socket(AF_INET, SOCK_STREAM, 0);
if (::connect(sd, (struct sockaddr*)&address, sizeof(address)) != 0) {
perror("connect() failed");
DEBUG("connect() failed: " << strerror(errno));
return nullptr;
}
return std::unique_ptr<TCPStream>(new TCPStream(sd, &address));
@@ -97,16 +99,15 @@ std::unique_ptr<TCPStream> TCPConnector::connect(const char* server, int port,
len = sizeof(int);
getsockopt(sd, SOL_SOCKET, SO_ERROR, (void*)(&valopt), &len);
if (valopt) {
fprintf(stderr, "connect() error %d - %s\n", valopt,
strerror(valopt));
DEBUG("connect() error " << valopt << " - " << strerror(valopt));
}
// connection established
else
result = 0;
} else
fprintf(stderr, "connect() timed out\n");
DEBUG("connect() timed out");
} else
fprintf(stderr, "connect() error %d - %s\n", errno, strerror(errno));
DEBUG("connect() error " << errno << " - " << strerror(errno));
}
// Return socket to blocking mode