1 #include "otsdaq-core/NetworkUtilities/Socket.h"
2 #include "otsdaq-core/Macros/CoutMacros.h"
3 #include "otsdaq-core/MessageFacility/MessageFacility.h"
27 Socket::Socket(
const std::string& IPAddress,
unsigned int port)
28 : socketNumber_(-1), IPAddress_(IPAddress), requestedPort_(port)
31 __COUT__ <<
"Socket constructor " << IPAddress <<
":" << port << __E__;
35 __SS__ <<
"FATAL: Invalid Port " << port <<
". Max port number is "
36 << (1 << 16) - 1 <<
"." << std::endl;
38 __COUT_ERR__ <<
"\n" << ss.str();
43 socketAddress_.sin_family = AF_INET;
44 socketAddress_.sin_port = htons(port);
46 __COUT__ <<
"IPAddress: " << IPAddress <<
" port: " << port
47 <<
" htons: " << socketAddress_.sin_port << std::endl;
49 if(inet_aton(IPAddress.c_str(), &socketAddress_.sin_addr) == 0)
51 __SS__ <<
"FATAL: Invalid IP:Port combination. Please verify... " << IPAddress
52 <<
":" << port << std::endl;
54 __COUT_ERR__ <<
"\n" << ss.str();
58 memset(&(socketAddress_.sin_zero),
'\0', 8);
60 __COUT__ <<
"Constructed socket for port " << ntohs(socketAddress_.sin_port) <<
"="
61 << getPort() <<
" htons: " << socketAddress_.sin_port << std::endl;
68 __SS__ <<
"ERROR: This method should never be called. This is the protected "
69 "constructor. There is something wrong in your inheritance scheme!"
71 __COUT_ERR__ <<
"\n" << ss.str();
79 __COUT__ <<
"CLOSING THE SOCKET #" << socketNumber_ <<
" IP: " << IPAddress_
80 <<
" port: " << getPort() <<
" htons: " << socketAddress_.sin_port
82 if(socketNumber_ != -1)
87 void Socket::initialize(
unsigned int socketReceiveBufferSize)
89 __COUT__ <<
"Initializing port " << ntohs(socketAddress_.sin_port)
90 <<
" htons: " << socketAddress_.sin_port << std::endl;
91 struct addrinfo hints;
96 memset(&hints, 0,
sizeof hints);
97 hints.ai_family = AF_INET;
98 hints.ai_socktype = SOCK_DGRAM;
99 hints.ai_flags = AI_PASSIVE;
101 bool socketInitialized =
false;
102 int fromPort = FirstSocketPort;
103 int toPort = LastSocketPort;
105 if(ntohs(socketAddress_.sin_port) != 0)
106 fromPort = toPort = ntohs(socketAddress_.sin_port);
108 std::stringstream port;
110 for(
int p = fromPort; p <= toPort && !socketInitialized; p++)
114 __COUT__ <<
"]\tBinding port: " << port.str() << std::endl;
115 socketAddress_.sin_port = htons(p);
117 if((status = getaddrinfo(NULL, port.str().c_str(), &hints, &res)) != 0)
119 __COUT__ <<
"]\tGetaddrinfo error status: " << status << std::endl;
124 socketNumber_ = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
126 __COUT__ <<
"]\tSocket Number: " << socketNumber_
127 <<
" for port: " << ntohs(socketAddress_.sin_port) <<
" initialized."
130 if(bind(socketNumber_, res->ai_addr, res->ai_addrlen) == -1)
132 std::cout << __COUT_HDR_FL__
133 <<
"Error********Error********Error********Error********Error******"
136 std::cout << __COUT_HDR_FL__ <<
"FAILED BIND FOR PORT: " << port.str()
137 <<
" ON IP: " << IPAddress_ << std::endl;
138 std::cout << __COUT_HDR_FL__
139 <<
"Error********Error********Error********Error********Error******"
146 std::cout << __COUT_HDR_FL__
147 <<
":):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):"
150 std::cout << __COUT_HDR_FL__
151 <<
":):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):"
154 std::cout << __COUT_HDR_FL__ <<
"SOCKET ON PORT: " << port.str()
155 <<
" ON IP: " << IPAddress_ <<
" INITIALIZED OK!" << std::endl;
156 std::cout << __COUT_HDR_FL__
157 <<
":):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):"
160 std::cout << __COUT_HDR_FL__
161 <<
":):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):"
165 setsockopt(socketNumber_, SOL_SOCKET, SO_REUSEADDR, &yes,
sizeof(
int));
166 socketInitialized =
true;
167 __COUT__ <<
"]\tSocket Number: " << socketNumber_
168 <<
" for port: " << ntohs(socketAddress_.sin_port) <<
" initialized."
175 if(!socketInitialized)
177 __SS__ <<
"FATAL: Socket could not initialize socket (IP=" << IPAddress_
178 <<
", Port=" << ntohs(socketAddress_.sin_port)
179 <<
"). Perhaps it is already in use?" << std::endl;
180 std::cout << ss.str();
184 __COUT__ <<
"Setting socket receive buffer size = " << socketReceiveBufferSize
185 <<
" 0x" << std::hex << socketReceiveBufferSize << std::dec << __E__;
186 if(setsockopt(socketNumber_,
189 (
char*)&socketReceiveBufferSize,
190 sizeof(socketReceiveBufferSize)) < 0)
192 __COUT_ERR__ <<
"Failed to set socket receive size to " << socketReceiveBufferSize
193 <<
". Attempting to revert to default." << std::endl;
195 socketReceiveBufferSize = defaultSocketReceiveSize_;
197 __COUT__ <<
"Setting socket receive buffer size = " << socketReceiveBufferSize
198 <<
" 0x" << std::hex << socketReceiveBufferSize << std::dec << __E__;
199 if(setsockopt(socketNumber_,
202 (
char*)&socketReceiveBufferSize,
203 sizeof(socketReceiveBufferSize)) < 0)
205 __SS__ <<
"Failed to set socket receive size to " << socketReceiveBufferSize
206 <<
". Attempting to revert to default." << std::endl;
207 std::cout << ss.str();
213 uint16_t Socket::getPort()
215 return ntohs(socketAddress_.sin_port);
225 const struct sockaddr_in& Socket::getSocketAddress(
void) {
return socketAddress_; }