![]() |
ListenSocket< X > Class Template ReferenceBinds incoming port number to new Socket class X.
More...
|
Public Member Functions | |
ListenSocket (ISocketHandler &h, bool use_creator=true) | |
Constructor. | |
~ListenSocket () | |
int | Close () |
Close file descriptor. | |
int | Bind (port_t port, int depth=20) |
Bind and listen to any interface. | |
int | Bind (SocketAddress &ad, int depth) |
int | Bind (port_t port, const std::string &protocol, int depth=20) |
Bind and listen to any interface, with optional protocol. | |
int | Bind (const std::string &intf, port_t port, int depth=20) |
Bind and listen to specific interface. | |
int | Bind (const std::string &intf, port_t port, const std::string &protocol, int depth=20) |
Bind and listen to specific interface. | |
int | Bind (ipaddr_t a, port_t port, int depth=20) |
Bind and listen to ipv4 interface. | |
int | Bind (ipaddr_t a, port_t port, const std::string &protocol, int depth) |
Bind and listen to ipv4 interface. | |
int | Bind (SocketAddress &ad, const std::string &protocol, int depth) |
Bind and listen to network interface. | |
port_t | GetPort () |
Return assigned port number. | |
int | GetDepth () |
Return listen queue depth. | |
void | OnRead () |
OnRead on a ListenSocket receives an incoming connection. | |
virtual SOCKET | Accept (SOCKET socket, struct sockaddr *saptr, socklen_t *lenptr) |
Please don't use this method. | |
bool | HasCreator () |
void | OnOptions (int, int, int, SOCKET) |
Called when a socket is created, to set socket options. | |
Protected Member Functions | |
ListenSocket (const ListenSocket &) | |
Private Member Functions | |
ListenSocket & | operator= (const ListenSocket &) |
Private Attributes | |
port_t | m_port |
int | m_depth |
X * | m_creator |
bool | m_bHasCreate |
Definition at line 55 of file ListenSocket.h.
ListenSocket< X >::ListenSocket | ( | ISocketHandler & | h, | |
bool | use_creator = true | |||
) | [inline] |
Constructor.
h | ISocketHandler reference | |
use_creator | Optional use of creator (default true) |
Definition at line 61 of file ListenSocket.h.
00061 : Socket(h), m_port(0), m_depth(0), m_creator(NULL) 00062 ,m_bHasCreate(false) 00063 { 00064 if (use_creator) 00065 { 00066 m_creator = new X(h); 00067 Socket *tmp = m_creator -> Create(); 00068 if (tmp && dynamic_cast<X *>(tmp)) 00069 { 00070 m_bHasCreate = true; 00071 } 00072 if (tmp) 00073 { 00074 delete tmp; 00075 } 00076 } 00077 }
ListenSocket< X >::~ListenSocket | ( | ) | [inline] |
ListenSocket< X >::ListenSocket | ( | const ListenSocket< X > & | ) | [inline, protected] |
int ListenSocket< X >::Close | ( | ) | [inline, virtual] |
Close file descriptor.
Reimplemented from Socket.
Definition at line 86 of file ListenSocket.h.
References closesocket, and INVALID_SOCKET.
00086 { 00087 if (GetSocket() != INVALID_SOCKET) 00088 { 00089 closesocket(GetSocket()); 00090 } 00091 return 0; 00092 }
int ListenSocket< X >::Bind | ( | port_t | port, | |
int | depth = 20 | |||
) | [inline] |
Bind and listen to any interface.
port | Port (0 is random) | |
depth | Listen queue depth |
Definition at line 97 of file ListenSocket.h.
Referenced by ResolvServer::Run().
00097 { 00098 #ifdef ENABLE_IPV6 00099 #ifdef IPPROTO_IPV6 00100 if (IsIpv6()) 00101 { 00102 Ipv6Address ad(port); 00103 return Bind(ad, depth); 00104 } 00105 else 00106 #endif 00107 #endif 00108 { 00109 Ipv4Address ad(port); 00110 return Bind(ad, depth); 00111 } 00112 }
int ListenSocket< X >::Bind | ( | SocketAddress & | ad, | |
int | depth | |||
) | [inline] |
Definition at line 114 of file ListenSocket.h.
00114 { 00115 #ifdef USE_SCTP 00116 if (dynamic_cast<SctpSocket *>(m_creator)) 00117 { 00118 return Bind(ad, "sctp", depth); 00119 } 00120 #endif 00121 return Bind(ad, "tcp", depth); 00122 }
int ListenSocket< X >::Bind | ( | port_t | port, | |
const std::string & | protocol, | |||
int | depth = 20 | |||
) | [inline] |
Bind and listen to any interface, with optional protocol.
port | Port (0 is random) | |
protocol | Network protocol | |
depth | Listen queue depth |
Definition at line 128 of file ListenSocket.h.
00128 { 00129 #ifdef ENABLE_IPV6 00130 #ifdef IPPROTO_IPV6 00131 if (IsIpv6()) 00132 { 00133 Ipv6Address ad(port); 00134 return Bind(ad, protocol, depth); 00135 } 00136 else 00137 #endif 00138 #endif 00139 { 00140 Ipv4Address ad(port); 00141 return Bind(ad, protocol, depth); 00142 } 00143 }
int ListenSocket< X >::Bind | ( | const std::string & | intf, | |
port_t | port, | |||
int | depth = 20 | |||
) | [inline] |
Bind and listen to specific interface.
intf | Interface hostname | |
port | Port (0 is random) | |
depth | Listen queue depth |
Definition at line 149 of file ListenSocket.h.
References Ipv4Address::IsValid(), and LOG_LEVEL_FATAL.
00149 { 00150 #ifdef ENABLE_IPV6 00151 #ifdef IPPROTO_IPV6 00152 if (IsIpv6()) 00153 { 00154 Ipv6Address ad(intf, port); 00155 if (ad.IsValid()) 00156 { 00157 return Bind(ad, depth); 00158 } 00159 Handler().LogError(this, "Bind", 0, "name resolution of interface name failed", LOG_LEVEL_FATAL); 00160 return -1; 00161 } 00162 else 00163 #endif 00164 #endif 00165 { 00166 Ipv4Address ad(intf, port); 00167 if (ad.IsValid()) 00168 { 00169 return Bind(ad, depth); 00170 } 00171 Handler().LogError(this, "Bind", 0, "name resolution of interface name failed", LOG_LEVEL_FATAL); 00172 return -1; 00173 } 00174 }
int ListenSocket< X >::Bind | ( | const std::string & | intf, | |
port_t | port, | |||
const std::string & | protocol, | |||
int | depth = 20 | |||
) | [inline] |
Bind and listen to specific interface.
intf | Interface hostname | |
port | Port (0 is random) | |
protocol | Network protocol | |
depth | Listen queue depth |
Definition at line 181 of file ListenSocket.h.
References Ipv4Address::IsValid(), and LOG_LEVEL_FATAL.
00181 { 00182 #ifdef ENABLE_IPV6 00183 #ifdef IPPROTO_IPV6 00184 if (IsIpv6()) 00185 { 00186 Ipv6Address ad(intf, port); 00187 if (ad.IsValid()) 00188 { 00189 return Bind(ad, protocol, depth); 00190 } 00191 Handler().LogError(this, "Bind", 0, "name resolution of interface name failed", LOG_LEVEL_FATAL); 00192 return -1; 00193 } 00194 else 00195 #endif 00196 #endif 00197 { 00198 Ipv4Address ad(intf, port); 00199 if (ad.IsValid()) 00200 { 00201 return Bind(ad, protocol, depth); 00202 } 00203 Handler().LogError(this, "Bind", 0, "name resolution of interface name failed", LOG_LEVEL_FATAL); 00204 return -1; 00205 } 00206 }
int ListenSocket< X >::Bind | ( | ipaddr_t | a, | |
port_t | port, | |||
int | depth = 20 | |||
) | [inline] |
Bind and listen to ipv4 interface.
a | Ipv4 interface address | |
port | Port (0 is random) | |
depth | Listen queue depth |
Definition at line 212 of file ListenSocket.h.
00212 { 00213 Ipv4Address ad(a, port); 00214 #ifdef USE_SCTP 00215 if (dynamic_cast<SctpSocket *>(m_creator)) 00216 { 00217 return Bind(ad, "sctp", depth); 00218 } 00219 #endif 00220 return Bind(ad, "tcp", depth); 00221 }
int ListenSocket< X >::Bind | ( | ipaddr_t | a, | |
port_t | port, | |||
const std::string & | protocol, | |||
int | depth | |||
) | [inline] |
Bind and listen to ipv4 interface.
a | Ipv4 interface address | |
port | Port (0 is random) | |
protocol | Network protocol | |
depth | Listen queue depth |
Definition at line 227 of file ListenSocket.h.
00227 { 00228 Ipv4Address ad(a, port); 00229 return Bind(ad, protocol, depth); 00230 }
int ListenSocket< X >::Bind | ( | SocketAddress & | ad, | |
const std::string & | protocol, | |||
int | depth | |||
) | [inline] |
Bind and listen to network interface.
ad | Interface address | |
protocol | Network protocol | |
depth | Listen queue depth |
Definition at line 264 of file ListenSocket.h.
References closesocket, Errno, SocketAddress::GetFamily(), INVALID_SOCKET, LOG_LEVEL_FATAL, and StrError.
00264 { 00265 SOCKET s; 00266 if ( (s = CreateSocket(ad.GetFamily(), SOCK_STREAM, protocol)) == INVALID_SOCKET) 00267 { 00268 return -1; 00269 } 00270 if (bind(s, ad, ad) == -1) 00271 { 00272 Handler().LogError(this, "bind", Errno, StrError(Errno), LOG_LEVEL_FATAL); 00273 closesocket(s); 00274 return -1; 00275 } 00276 if (listen(s, depth) == -1) 00277 { 00278 Handler().LogError(this, "listen", Errno, StrError(Errno), LOG_LEVEL_FATAL); 00279 closesocket(s); 00280 return -1; 00281 } 00282 // retrieve bound port 00283 #ifdef ENABLE_IPV6 00284 #ifdef IPPROTO_IPV6 00285 if (IsIpv6()) 00286 { 00287 struct sockaddr_in6 sa; 00288 socklen_t sockaddr_length = sizeof(struct sockaddr_in6); 00289 getsockname(s, (struct sockaddr *)&sa, (socklen_t*)&sockaddr_length); 00290 m_port = ntohs(sa.sin6_port); 00291 } 00292 else 00293 #endif 00294 #endif 00295 { 00296 struct sockaddr_in sa; 00297 socklen_t sockaddr_length = sizeof(struct sockaddr_in); 00298 getsockname(s, (struct sockaddr *)&sa, (socklen_t*)&sockaddr_length); 00299 m_port = ntohs(sa.sin_port); 00300 } 00301 m_depth = depth; 00302 Attach(s); 00303 return 0; 00304 }
port_t ListenSocket< X >::GetPort | ( | ) | [inline, virtual] |
Return assigned port number.
Reimplemented from Socket.
Definition at line 307 of file ListenSocket.h.
00308 { 00309 return m_port; 00310 }
int ListenSocket< X >::GetDepth | ( | ) | [inline] |
Return listen queue depth.
Definition at line 313 of file ListenSocket.h.
00314 { 00315 return m_depth; 00316 }
void ListenSocket< X >::OnRead | ( | ) | [inline, virtual] |
OnRead on a ListenSocket receives an incoming connection.
Reimplemented from Socket.
Definition at line 319 of file ListenSocket.h.
References closesocket, Errno, INVALID_SOCKET, LOG_LEVEL_ERROR, LOG_LEVEL_FATAL, LOG_LEVEL_WARNING, and StrError.
00320 { 00321 struct sockaddr sa; 00322 socklen_t sa_len = sizeof(struct sockaddr); 00323 SOCKET a_s = accept(GetSocket(), &sa, &sa_len); 00324 00325 if (a_s == INVALID_SOCKET) 00326 { 00327 Handler().LogError(this, "accept", Errno, StrError(Errno), LOG_LEVEL_ERROR); 00328 return; 00329 } 00330 if (!Handler().OkToAccept(this)) 00331 { 00332 Handler().LogError(this, "accept", -1, "Not OK to accept", LOG_LEVEL_WARNING); 00333 closesocket(a_s); 00334 return; 00335 } 00336 if (Handler().GetCount() >= FD_SETSIZE) 00337 { 00338 Handler().LogError(this, "accept", (int)Handler().GetCount(), "ISocketHandler fd_set limit reached", LOG_LEVEL_FATAL); 00339 closesocket(a_s); 00340 return; 00341 } 00342 Socket *tmp = m_bHasCreate ? m_creator -> Create() : new X(Handler()); 00343 #ifdef ENABLE_IPV6 00344 tmp -> SetIpv6( IsIpv6() ); 00345 #endif 00346 tmp -> SetParent(this); 00347 tmp -> Attach(a_s); 00348 tmp -> SetNonblocking(true); 00349 { 00350 #ifdef ENABLE_IPV6 00351 #ifdef IPPROTO_IPV6 00352 if (sa_len == sizeof(struct sockaddr_in6)) 00353 { 00354 struct sockaddr_in6 *p = (struct sockaddr_in6 *)&sa; 00355 if (p -> sin6_family == AF_INET6) 00356 { 00357 Ipv6Address ad(p -> sin6_addr,ntohs(p -> sin6_port)); 00358 ad.SetFlowinfo(p -> sin6_flowinfo); 00359 #ifndef _WIN32 00360 ad.SetScopeId(p -> sin6_scope_id); 00361 #endif 00362 tmp -> SetRemoteAddress(ad); 00363 } 00364 } 00365 #endif 00366 #endif 00367 if (sa_len == sizeof(struct sockaddr_in)) 00368 { 00369 struct sockaddr_in *p = (struct sockaddr_in *)&sa; 00370 if (p -> sin_family == AF_INET) 00371 { 00372 Ipv4Address ad(p -> sin_addr,ntohs(p -> sin_port)); 00373 tmp -> SetRemoteAddress(ad); 00374 } 00375 } 00376 } 00377 tmp -> SetConnected(true); 00378 tmp -> Init(); 00379 tmp -> SetDeleteByHandler(true); 00380 Handler().Add(tmp); 00381 #ifdef HAVE_OPENSSL 00382 if (tmp -> IsSSL()) // SSL Enabled socket 00383 { 00384 // %! OnSSLAccept calls SSLNegotiate that can finish in this one call. 00385 // %! If that happens and negotiation fails, the 'tmp' instance is 00386 // %! still added to the list of active sockets in the sockethandler. 00387 // %! See bugfix for this in SocketHandler::Select - don't Set rwx 00388 // %! flags if CloseAndDelete() flag is true. 00389 // %! An even better fugbix (see TcpSocket::OnSSLAccept) now avoids 00390 // %! the Add problem altogether, so ignore the above. 00391 // %! (OnSSLAccept does no longer call SSLNegotiate().) 00392 tmp -> OnSSLAccept(); 00393 } 00394 else 00395 #endif 00396 { 00397 tmp -> OnAccept(); 00398 } 00399 }
virtual SOCKET ListenSocket< X >::Accept | ( | SOCKET | socket, | |
struct sockaddr * | saptr, | |||
socklen_t * | lenptr | |||
) | [inline, virtual] |
Please don't use this method.
"accept()" is handled automatically in the OnRead() method.
Definition at line 403 of file ListenSocket.h.
bool ListenSocket< X >::HasCreator | ( | ) | [inline] |
void ListenSocket< X >::OnOptions | ( | int | family, | |
int | type, | |||
int | protocol, | |||
SOCKET | ||||
) | [inline, virtual] |
Called when a socket is created, to set socket options.
Implements Socket.
Definition at line 410 of file ListenSocket.h.
ListenSocket& ListenSocket< X >::operator= | ( | const ListenSocket< X > & | ) | [inline, private] |
port_t ListenSocket< X >::m_port [private] |
Definition at line 416 of file ListenSocket.h.
int ListenSocket< X >::m_depth [private] |
Definition at line 417 of file ListenSocket.h.
X* ListenSocket< X >::m_creator [private] |
Definition at line 418 of file ListenSocket.h.
bool ListenSocket< X >::m_bHasCreate [private] |
Definition at line 419 of file ListenSocket.h.