Move JobClient to Boost::asio as well
I did this mainly so server and client use a common networking API IMO, using Boost::asio results in nicer name-lookup code. Since no longer needed, I removed the SocketComm stuff. The client is still synchronous; I see no benefit in having it asynchronous. I'm not super happy with the random backoff by the clients, if they can't connect to the server. It makes the code really messy, 3 retries is totally arbitrary, as is the backup windows. I believe launching the server and clients in the correct order should be handled by a launch script Change-Id: Ifea64919fc228aa530c90449686f51bf63eb70e7
This commit is contained in:
committed by
Horst Schirmeier
parent
191219ad06
commit
9272c5cbed
@ -1,7 +1,5 @@
|
||||
set(SRCS
|
||||
ExperimentData.hpp
|
||||
SocketComm.hpp
|
||||
SocketComm.cc
|
||||
)
|
||||
|
||||
## Setup desired protobuf descriptions HERE ##
|
||||
|
||||
@ -1,117 +0,0 @@
|
||||
#include <string>
|
||||
#include <errno.h>
|
||||
#include <signal.h>
|
||||
#include <poll.h>
|
||||
|
||||
#include "SocketComm.hpp"
|
||||
|
||||
namespace fail {
|
||||
|
||||
void SocketComm::init()
|
||||
{
|
||||
// It's usually much easier to handle the error on write(), than to do
|
||||
// anything intelligent in a SIGPIPE handler.
|
||||
signal(SIGPIPE, SIG_IGN);
|
||||
}
|
||||
|
||||
bool SocketComm::sendMsg(int sockfd, google::protobuf::Message& msg)
|
||||
{
|
||||
int size = htonl(msg.ByteSize());
|
||||
std::string buf;
|
||||
if (safe_write(sockfd, &size, sizeof(size)) == -1
|
||||
|| !msg.SerializeToString(&buf)
|
||||
|| safe_write(sockfd, buf.c_str(), buf.size()) == -1) {
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
bool SocketComm::rcvMsg(int sockfd, google::protobuf::Message& msg)
|
||||
{
|
||||
char *buf;
|
||||
int bufsiz;
|
||||
if ((buf = getBuf(sockfd, &bufsiz))) {
|
||||
std::string st(buf, bufsiz);
|
||||
delete [] buf;
|
||||
return msg.ParseFromString(st);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
bool SocketComm::dropMsg(int sockfd)
|
||||
{
|
||||
char *buf;
|
||||
int bufsiz;
|
||||
if ((buf = getBuf(sockfd, &bufsiz))) {
|
||||
delete [] buf;
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
char * SocketComm::getBuf(int sockfd, int *size)
|
||||
{
|
||||
char *buf;
|
||||
if (safe_read(sockfd, size, sizeof(int)) == -1) {
|
||||
return 0;
|
||||
}
|
||||
*size = ntohl(*size);
|
||||
buf = new char[*size];
|
||||
if (safe_read(sockfd, buf, *size) == -1) {
|
||||
delete [] buf;
|
||||
return 0;
|
||||
}
|
||||
return buf;
|
||||
}
|
||||
|
||||
int SocketComm::timedAccept(int sockfd, struct sockaddr *addr, socklen_t *addrlen, int timeout)
|
||||
{
|
||||
struct pollfd pfd = {sockfd, POLLIN, 0};
|
||||
int ret = poll(&pfd, 1, timeout);
|
||||
if (ret > 0) {
|
||||
return accept(sockfd, addr, addrlen);
|
||||
}
|
||||
errno = EWOULDBLOCK;
|
||||
return -1;
|
||||
}
|
||||
|
||||
ssize_t SocketComm::safe_write(int fd, const void *buf, size_t count)
|
||||
{
|
||||
ssize_t ret;
|
||||
const char *cbuf = (const char *) buf;
|
||||
do {
|
||||
ret = write(fd, cbuf, count);
|
||||
if (ret == -1) {
|
||||
if (errno == EINTR) {
|
||||
continue;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
count -= ret;
|
||||
cbuf += ret;
|
||||
} while (count);
|
||||
return cbuf - (const char *)buf;
|
||||
}
|
||||
|
||||
ssize_t SocketComm::safe_read(int fd, void *buf, size_t count)
|
||||
{
|
||||
ssize_t ret;
|
||||
char *cbuf = (char *) buf;
|
||||
do {
|
||||
ret = read(fd, cbuf, count);
|
||||
if (ret == -1) {
|
||||
if (errno == EINTR) {
|
||||
continue;
|
||||
}
|
||||
return -1;
|
||||
} else if (ret == 0) {
|
||||
// this deliberately deviates from read(2)
|
||||
return -1;
|
||||
}
|
||||
count -= ret;
|
||||
cbuf += ret;
|
||||
} while (count);
|
||||
return cbuf - (const char *) buf;
|
||||
}
|
||||
|
||||
} // end-of-namespace: fail
|
||||
@ -1,66 +0,0 @@
|
||||
/**
|
||||
* \brief Socket based communication wrapper functions.
|
||||
*/
|
||||
|
||||
#ifndef __SOCKET_COMM_HPP__
|
||||
#define __SOCKET_COMM_HPP__
|
||||
|
||||
#include <stdio.h>
|
||||
#include <unistd.h>
|
||||
#include <sys/types.h>
|
||||
#include <sys/socket.h>
|
||||
#include <netinet/in.h>
|
||||
#include <netdb.h>
|
||||
#include <iostream>
|
||||
#include <fstream>
|
||||
#include <google/protobuf/message.h>
|
||||
|
||||
namespace fail {
|
||||
|
||||
class SocketComm {
|
||||
public:
|
||||
/**
|
||||
* This allows us to ignore SIGPIPE.
|
||||
*/
|
||||
static void init();
|
||||
/**
|
||||
* Send Protobuf-generated message
|
||||
* @param sockfd open socket descriptor to write to
|
||||
* @param Msg Reference to Protobuf generated message type
|
||||
* \return false if message sending failed
|
||||
*/
|
||||
static bool sendMsg(int sockfd, google::protobuf::Message& msg);
|
||||
/**
|
||||
* Receive Protobuf-generated message
|
||||
* @param sockfd open socket descriptor to read from
|
||||
* @param Msg Reference to Protobuf generated message type
|
||||
* \return false if message reception failed
|
||||
*/
|
||||
static bool rcvMsg(int sockfd, google::protobuf::Message& msg);
|
||||
|
||||
/**
|
||||
* Receive Protobuf-generated message and just drop it
|
||||
* @param sockfd open socket descriptor to read from
|
||||
* \return false if message reception failed
|
||||
*/
|
||||
static bool dropMsg(int sockfd);
|
||||
|
||||
/**
|
||||
* An accept() wrapper that times out (using poll(2))
|
||||
* @param sockfd listening socket descriptor to accept connections from
|
||||
* @param addr same as accept()'s
|
||||
* @param addrlen same as accept()'s
|
||||
* @param timeout timeout in milliseconds (see poll(2))
|
||||
* \return < 0 on failure, > 0 for a new socket connection
|
||||
*/
|
||||
static int timedAccept(int sockfd, struct sockaddr *addr, socklen_t *addrlen, int timeout);
|
||||
|
||||
private:
|
||||
static char *getBuf(int sockfd, int *size);
|
||||
static ssize_t safe_write(int fd, const void *buf, size_t count);
|
||||
static ssize_t safe_read(int fd, void *buf, size_t count);
|
||||
};
|
||||
|
||||
} // end-of-namespace: fail
|
||||
|
||||
#endif // __SOCKET_COMM_HPP__
|
||||
Reference in New Issue
Block a user