1 #include "otsdaq/NetworkUtilities/TCPSocket.h"
3 #include <sys/socket.h>
12 TCPSocket::TCPSocket(
int socketId) : fSocketId(socketId) { open(); }
15 TCPSocket::~TCPSocket()
20 std::cout << __PRETTY_FUNCTION__ <<
"Clean close!" << std::endl;
36 void TCPSocket::open(
void)
38 if(fSocketId == invalidSocketId && (fSocketId = ::socket(PF_INET, SOCK_STREAM, 0)) == invalidSocketId)
39 throw std::runtime_error(std::string(
"Bad socket: ") + strerror(errno));
43 void TCPSocket::close(
void)
45 if(fSocketId == invalidSocketId)
47 throw std::logic_error(
"Bad socket object (this object was moved)");
49 int state = ::close(fSocketId);
50 std::cout << __PRETTY_FUNCTION__ <<
"Socket id: " << getSocketId() <<
" close state: " << state <<
" errno: " << errno << std::endl;
52 fSocketId = invalidSocketId;
58 throw std::domain_error(std::string(
"Close: EBADF: ") + std::to_string(fSocketId) +
" " + strerror(errno));
60 throw std::runtime_error(std::string(
"Close: EIO: ") + std::to_string(fSocketId) +
" " + strerror(errno));
69 throw std::runtime_error(std::string(
"Close: ???: ") + std::to_string(fSocketId) +
" " + strerror(errno));
78 swap(fSocketId, other.fSocketId);
82 TCPSocket::TCPSocket(
TCPSocket&& move) : fSocketId(invalidSocketId) { move.swap(*
this); }
92 void TCPSocket::sendClose()
94 if(::shutdown(getSocketId(), SHUT_WR) != 0)
96 throw std::domain_error(std::string(
"Shutdown: critical error: ") + strerror(errno));