1 #include "otsdaq/NetworkUtilities/Socket.h"
2 #include "otsdaq/Macros/CoutMacros.h"
3 #include "otsdaq/MessageFacility/MessageFacility.h"
27 Socket::Socket(
const std::string& IPAddress,
unsigned int port) : socketNumber_(-1), IPAddress_(IPAddress), requestedPort_(port)
30 __COUT__ <<
"Socket constructor " << IPAddress <<
":" << port << __E__;
34 __SS__ <<
"FATAL: Invalid Port " << port <<
". Max port number is " << (1 << 16) - 1 <<
"." << std::endl;
36 __COUT_ERR__ <<
"\n" << ss.str();
41 socketAddress_.sin_family = AF_INET;
42 socketAddress_.sin_port = htons(port);
44 __COUT__ <<
"IPAddress: " << IPAddress <<
" port: " << port <<
" htons: " << socketAddress_.sin_port << std::endl;
46 if(inet_aton(IPAddress.c_str(), &socketAddress_.sin_addr) == 0)
48 __SS__ <<
"FATAL: Invalid IP:Port combination. Please verify... " << IPAddress <<
":" << port << std::endl;
50 __COUT_ERR__ <<
"\n" << ss.str();
54 memset(&(socketAddress_.sin_zero),
'\0', 8);
56 __COUT__ <<
"Constructed socket for port " << ntohs(socketAddress_.sin_port) <<
"=" << getPort() <<
" htons: " << socketAddress_.sin_port << std::endl;
63 __SS__ <<
"ERROR: This method should never be called. This is the protected "
64 "constructor. There is something wrong in your inheritance scheme!"
66 __COUT_ERR__ <<
"\n" << ss.str();
74 __COUT__ <<
"CLOSING THE SOCKET #" << socketNumber_ <<
" IP: " << IPAddress_ <<
" port: " << getPort() <<
" htons: " << socketAddress_.sin_port
76 if(socketNumber_ != -1)
81 void Socket::initialize(
unsigned int socketReceiveBufferSize)
83 __COUT__ <<
"Initializing port " << ntohs(socketAddress_.sin_port) <<
" htons: " << socketAddress_.sin_port << std::endl;
84 struct addrinfo hints;
89 memset(&hints, 0,
sizeof hints);
90 hints.ai_family = AF_INET;
91 hints.ai_socktype = SOCK_DGRAM;
92 hints.ai_flags = AI_PASSIVE;
94 bool socketInitialized =
false;
95 int fromPort = FirstSocketPort;
96 int toPort = LastSocketPort;
98 if(ntohs(socketAddress_.sin_port) != 0)
99 fromPort = toPort = ntohs(socketAddress_.sin_port);
101 std::stringstream port;
103 for(
int p = fromPort; p <= toPort && !socketInitialized; p++)
107 __COUT__ <<
"]\tBinding port: " << port.str() << std::endl;
108 socketAddress_.sin_port = htons(p);
110 if((status = getaddrinfo(NULL, port.str().c_str(), &hints, &res)) != 0)
112 __COUT__ <<
"]\tGetaddrinfo error status: " << status << std::endl;
117 socketNumber_ = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
119 __COUT__ <<
"]\tSocket Number: " << socketNumber_ <<
" for port: " << ntohs(socketAddress_.sin_port) <<
" initialized." << std::endl;
121 if(bind(socketNumber_, res->ai_addr, res->ai_addrlen) == -1)
123 __COUT__ <<
"Error********Error********Error********Error********Error******"
126 __COUT__ <<
"FAILED BIND FOR PORT: " << port.str() <<
" ON IP: " << IPAddress_ << std::endl;
127 __COUT__ <<
"Error********Error********Error********Error********Error******"
134 __COUT__ <<
":):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):"
137 __COUT__ <<
":):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):"
140 __COUT__ <<
"SOCKET ON PORT: " << port.str() <<
" ON IP: " << IPAddress_ <<
" INITIALIZED OK!" << std::endl;
141 __COUT__ <<
":):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):"
144 __COUT__ <<
":):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):"
148 setsockopt(socketNumber_, SOL_SOCKET, SO_REUSEADDR, &yes,
sizeof(
int));
149 socketInitialized =
true;
150 __COUT__ <<
"]\tSocket Number: " << socketNumber_ <<
" for port: " << ntohs(socketAddress_.sin_port) <<
" initialized." << std::endl;
156 if(!socketInitialized)
158 __SS__ <<
"FATAL: Socket could not initialize socket (IP=" << IPAddress_ <<
", Port=" << ntohs(socketAddress_.sin_port)
159 <<
"). Perhaps it is already in use?" << std::endl;
160 std::cout << ss.str();
164 __COUT__ <<
"Setting socket receive buffer size = " << socketReceiveBufferSize <<
" 0x" << std::hex << socketReceiveBufferSize << std::dec << __E__;
165 if(setsockopt(socketNumber_, SOL_SOCKET, SO_RCVBUF, (
char*)&socketReceiveBufferSize,
sizeof(socketReceiveBufferSize)) < 0)
167 __COUT_ERR__ <<
"Failed to set socket receive size to " << socketReceiveBufferSize <<
". Attempting to revert to default." << std::endl;
169 socketReceiveBufferSize = defaultSocketReceiveSize_;
171 __COUT__ <<
"Setting socket receive buffer size = " << socketReceiveBufferSize <<
" 0x" << std::hex << socketReceiveBufferSize << std::dec << __E__;
172 if(setsockopt(socketNumber_, SOL_SOCKET, SO_RCVBUF, (
char*)&socketReceiveBufferSize,
sizeof(socketReceiveBufferSize)) < 0)
174 __SS__ <<
"Failed to set socket receive size to " << socketReceiveBufferSize <<
". Attempting to revert to default." << std::endl;
175 std::cout << ss.str();
181 uint16_t Socket::getPort()
183 return ntohs(socketAddress_.sin_port);
193 const struct sockaddr_in& Socket::getSocketAddress(
void) {
return socketAddress_; }