1 #include "otsdaq-core/NetworkUtilities/Socket.h"
2 #include "otsdaq-core/MessageFacility/MessageFacility.h"
3 #include "otsdaq-core/Macros/CoutMacros.h"
10 #include <arpa/inet.h>
29 Socket::Socket(
const std::string &IPAddress,
unsigned int port)
31 , IPAddress_ (IPAddress)
32 , requestedPort_(port)
35 __COUT__ <<
"Socket constructor " << IPAddress <<
":" << port << __E__;
40 __SS__ <<
"FATAL: Invalid Port " << port <<
". Max port number is " <<
41 (1<<16)-1 <<
"." << std::endl;
43 __COUT_ERR__ <<
"\n" << ss.str();
44 throw std::runtime_error(ss.str());
49 socketAddress_.sin_family = AF_INET;
50 socketAddress_.sin_port = htons(port);
52 __COUT__ <<
"IPAddress: " << IPAddress <<
" port: " << port <<
" htons: " << socketAddress_.sin_port << std::endl;
54 if(inet_aton(IPAddress.c_str(), &socketAddress_.sin_addr) == 0)
56 __SS__ <<
"FATAL: Invalid IP:Port combination. Please verify... " <<
57 IPAddress <<
":" << port << std::endl;
59 __COUT_ERR__ <<
"\n" << ss.str();
60 throw std::runtime_error(ss.str());
63 memset(&(socketAddress_.sin_zero),
'\0', 8);
65 __COUT__ <<
"Constructed socket for port " << ntohs(socketAddress_.sin_port) <<
66 " htons: " << socketAddress_.sin_port << std::endl;
74 __SS__ <<
"ERROR: This method should never be called. This is the protected constructor. There is something wrong in your inheritance scheme!" << std::endl;
75 __COUT_ERR__ <<
"\n" << ss.str();
77 throw std::runtime_error(ss.str());
83 __COUT__ <<
"CLOSING THE SOCKET #" << socketNumber_ <<
" IP: " << IPAddress_ <<
" port: " << getPort() <<
" htons: " << socketAddress_.sin_port << std::endl;
84 if(socketNumber_ != -1)
89 void Socket::initialize(
unsigned int socketReceiveBufferSize)
91 __COUT__ <<
"Initializing port " << ntohs(socketAddress_.sin_port) <<
92 " htons: " << socketAddress_.sin_port << std::endl;
93 struct addrinfo hints;
98 memset(&hints, 0,
sizeof hints);
99 hints.ai_family = AF_INET;
100 hints.ai_socktype = SOCK_DGRAM;
101 hints.ai_flags = AI_PASSIVE;
103 bool socketInitialized =
false;
104 int fromPort = FirstSocketPort;
105 int toPort = LastSocketPort;
107 if(ntohs(socketAddress_.sin_port) != 0)
108 fromPort = toPort = ntohs(socketAddress_.sin_port);
110 std::stringstream port;
112 for(
int p=fromPort; p<=toPort && !socketInitialized; p++)
116 __COUT__ <<
"]\tBinding port: " << port.str() << std::endl;
117 socketAddress_.sin_port = htons(p);
119 if((status = getaddrinfo(NULL, port.str().c_str(), &hints, &res)) != 0)
121 __COUT__ <<
"]\tGetaddrinfo error status: " << status << std::endl;
126 socketNumber_ = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
128 __COUT__ <<
"]\tSocket Number: " << socketNumber_ <<
" for port: " << ntohs(socketAddress_.sin_port) <<
" initialized." << std::endl;
130 if(bind(socketNumber_, res->ai_addr, res->ai_addrlen) == -1)
132 std::cout << __COUT_HDR_FL__ <<
"Error********Error********Error********Error********Error********Error" << std::endl;
133 std::cout << __COUT_HDR_FL__ <<
"FAILED BIND FOR PORT: " << port.str() <<
" ON IP: " << IPAddress_ << std::endl;
134 std::cout << __COUT_HDR_FL__ <<
"Error********Error********Error********Error********Error********Error" << std::endl;
139 std::cout << __COUT_HDR_FL__ <<
":):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):)" << std::endl;
140 std::cout << __COUT_HDR_FL__ <<
":):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):)" << std::endl;
141 std::cout << __COUT_HDR_FL__ <<
"SOCKET ON PORT: " << port.str() <<
" ON IP: " << IPAddress_ <<
" INITIALIZED OK!" << std::endl;
142 std::cout << __COUT_HDR_FL__ <<
":):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):)" << std::endl;
143 std::cout << __COUT_HDR_FL__ <<
":):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):)" << std::endl;
145 setsockopt(socketNumber_,SOL_SOCKET,SO_REUSEADDR,&yes,
sizeof(
int));
146 socketInitialized =
true;
147 __COUT__ <<
"]\tSocket Number: " << socketNumber_ <<
" for port: " << ntohs(socketAddress_.sin_port) <<
" initialized." << std::endl;
153 if(!socketInitialized)
155 __SS__ <<
"FATAL: Socket could not initialize socket (IP=" << IPAddress_ <<
156 ", Port=" << ntohs(socketAddress_.sin_port) <<
"). Perhaps it is already in use?" << std::endl;
157 std::cout << ss.str();
158 throw std::runtime_error(ss.str());
162 __COUT__ <<
"Setting socket receive buffer size = " << socketReceiveBufferSize <<
163 " 0x" << std::hex << socketReceiveBufferSize << std::dec << __E__;
164 if (setsockopt(socketNumber_, SOL_SOCKET, SO_RCVBUF,
165 (
char*)&socketReceiveBufferSize,
166 sizeof(socketReceiveBufferSize)) < 0) {
167 __COUT_ERR__ <<
"Failed to set socket receive size to " <<
168 socketReceiveBufferSize <<
". Attempting to revert to default." << std::endl;
170 socketReceiveBufferSize = defaultSocketReceiveSize_;
173 __COUT__ <<
"Setting socket receive buffer size = " << socketReceiveBufferSize <<
174 " 0x" << std::hex << socketReceiveBufferSize << std::dec << __E__;
175 if (setsockopt(socketNumber_, SOL_SOCKET, SO_RCVBUF,
176 (
char*)&socketReceiveBufferSize,
177 sizeof(socketReceiveBufferSize)) < 0)
179 __SS__ <<
"Failed to set socket receive size to " <<
180 socketReceiveBufferSize <<
". Attempting to revert to default." << std::endl;
181 std::cout << ss.str();
182 throw std::runtime_error(ss.str());
188 uint16_t Socket::getPort()
190 struct sockaddr_in sin;
191 socklen_t len =
sizeof(sin);
192 getsockname(socketNumber_, (
struct sockaddr *)&sin, &len);
193 return ntohs(sin.sin_port);
197 const struct sockaddr_in& Socket::getSocketAddress(
void)
199 return socketAddress_;