1 #include "otsdaq/NetworkUtilities/Socket.h"
2 #include "otsdaq/Macros/CoutMacros.h"
3 #include "otsdaq/MessageFacility/MessageFacility.h"
28 : socketNumber_(-1), IPAddress_(IPAddress), requestedPort_(port)
31 __COUTT__ <<
"Socket constructor " << IPAddress <<
":" << port << __E__;
35 __SS__ <<
"FATAL: Invalid Port " << port <<
". Max port number is "
36 << (1 << 16) - 1 <<
"." << std::endl;
41 socketAddress_.sin_family = AF_INET;
42 socketAddress_.sin_port = htons(port);
44 __COUTT__ <<
"IPAddress: " << IPAddress <<
" port: " << port
45 <<
" htons: " << socketAddress_.sin_port << std::endl;
47 if(inet_aton(IPAddress.c_str(), &socketAddress_.sin_addr) == 0)
49 __SS__ <<
"FATAL: Invalid IP:Port combination. Please verify... " << IPAddress
50 <<
":" << port << std::endl;
54 memset(&(socketAddress_.sin_zero),
'\0', 8);
56 __COUTT__ <<
"Constructed socket for port " << ntohs(socketAddress_.sin_port) <<
"="
57 << getPort() <<
" htons: " << socketAddress_.sin_port << std::endl;
64 __SS__ <<
"ERROR: This method should never be called. This is the protected "
65 "constructor. There is something wrong in your inheritance scheme!"
73 __COUTT__ <<
"CLOSING THE SOCKET #" << socketNumber_ <<
" IP: " << IPAddress_
74 <<
" 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)
84 <<
" htons: " << socketAddress_.sin_port << std::endl;
85 struct addrinfo hints;
90 memset(&hints, 0,
sizeof hints);
91 hints.ai_family = AF_INET;
92 hints.ai_socktype = SOCK_DGRAM;
93 hints.ai_flags = AI_PASSIVE;
95 bool socketInitialized =
false;
96 int fromPort = FirstSocketPort;
97 int toPort = LastSocketPort;
99 if(ntohs(socketAddress_.sin_port) != 0)
100 fromPort = toPort = ntohs(socketAddress_.sin_port);
102 std::stringstream port;
104 for(
int p = fromPort; p <= toPort && !socketInitialized; p++)
108 __COUT__ <<
"]\tBinding port: " << port.str() << std::endl;
109 socketAddress_.sin_port = htons(p);
111 if((status = getaddrinfo(NULL, port.str().c_str(), &hints, &res)) != 0)
113 __COUT__ <<
"]\tGetaddrinfo error status: " << status << std::endl;
118 socketNumber_ = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
120 __COUT__ <<
"]\tSocket Number: " << socketNumber_
121 <<
" for port: " << ntohs(socketAddress_.sin_port) <<
" initialized."
124 if(bind(socketNumber_, res->ai_addr, res->ai_addrlen) == -1)
126 __COUT__ <<
"Error********Error********Error********Error********Error******"
129 __COUT__ <<
"FAILED BIND FOR PORT: " << port.str() <<
" ON IP: " << IPAddress_
131 __COUT__ <<
"Error********Error********Error********Error********Error******"
138 __COUT__ <<
":):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):"
141 __COUT__ <<
":):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):"
144 __COUT__ <<
"SOCKET ON PORT: " << port.str() <<
" ON IP: " << IPAddress_
145 <<
" INITIALIZED OK!" << std::endl;
146 __COUT__ <<
":):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):"
149 __COUT__ <<
":):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):"
153 setsockopt(socketNumber_, SOL_SOCKET, SO_REUSEADDR, &yes,
sizeof(
int));
154 socketInitialized =
true;
155 __COUT__ <<
"]\tSocket Number: " << socketNumber_
156 <<
" for port: " << ntohs(socketAddress_.sin_port)
157 <<
" htons: " << socketAddress_.sin_port <<
" initialized."
164 if(!socketInitialized)
166 __SS__ <<
"FATAL: Socket could not initialize socket (IP=" << IPAddress_
167 <<
", Port=" << ntohs(socketAddress_.sin_port)
168 <<
"). Perhaps it is already in use?" << std::endl;
172 __COUT__ <<
"Setting socket receive buffer size = " << socketReceiveBufferSize
173 <<
" 0x" << std::hex << socketReceiveBufferSize << std::dec << __E__;
174 if(setsockopt(socketNumber_,
177 (
char*)&socketReceiveBufferSize,
178 sizeof(socketReceiveBufferSize)) < 0)
180 __COUT_ERR__ <<
"Failed to set socket receive size to " << socketReceiveBufferSize
181 <<
". Attempting to revert to default." << std::endl;
183 socketReceiveBufferSize = defaultSocketReceiveSize_;
185 __COUT__ <<
"Setting socket receive buffer size = " << socketReceiveBufferSize
186 <<
" 0x" << std::hex << socketReceiveBufferSize << std::dec << __E__;
187 if(setsockopt(socketNumber_,
190 (
char*)&socketReceiveBufferSize,
191 sizeof(socketReceiveBufferSize)) < 0)
193 __SS__ <<
"Failed to set socket receive size to " << socketReceiveBufferSize
194 <<
". Attempting to revert to default." << std::endl;
200 uint16_t Socket::getPort()
202 return ntohs(socketAddress_.sin_port);
212 const struct sockaddr_in& Socket::getSocketAddress(
void) {
return socketAddress_; }
Socket(void)
protected constructor