/poco-1.3.6p2/Net/include/Poco/Net/Socket.h
C++ Header | 610 lines | 268 code | 161 blank | 181 comment | 6 complexity | c4759d552326ba86dfcac78e450506fc MD5 | raw file
1// 2// Socket.h 3// 4// $Id: //poco/1.3/Net/include/Poco/Net/Socket.h#4 $ 5// 6// Library: Net 7// Package: Sockets 8// Module: Socket 9// 10// Definition of the Socket class. 11// 12// Copyright (c) 2005-2006, Applied Informatics Software Engineering GmbH. 13// and Contributors. 14// 15// Permission is hereby granted, free of charge, to any person or organization 16// obtaining a copy of the software and accompanying documentation covered by 17// this license (the "Software") to use, reproduce, display, distribute, 18// execute, and transmit the Software, and to prepare derivative works of the 19// Software, and to permit third-parties to whom the Software is furnished to 20// do so, all subject to the following: 21// 22// The copyright notices in the Software and this entire statement, including 23// the above license grant, this restriction and the following disclaimer, 24// must be included in all copies of the Software, in whole or in part, and 25// all derivative works of the Software, unless such copies or derivative 26// works are solely in the form of machine-executable object code generated by 27// a source language processor. 28// 29// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 30// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 31// FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT 32// SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE 33// FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, 34// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 35// DEALINGS IN THE SOFTWARE. 36// 37 38 39#ifndef Net_Socket_INCLUDED 40#define Net_Socket_INCLUDED 41 42 43#include "Poco/Net/Net.h" 44#include "Poco/Net/SocketImpl.h" 45#include <vector> 46 47 48namespace Poco { 49namespace Net { 50 51 52class Net_API Socket 53 /// Socket is the common base class for 54 /// StreamSocket, ServerSocket, DatagramSocket and other 55 /// socket classes. 56 /// 57 /// It provides operations common to all socket types. 58{ 59public: 60 enum SelectMode 61 /// The mode argument to poll() and select(). 62 { 63 SELECT_READ = 1, 64 SELECT_WRITE = 2, 65 SELECT_ERROR = 4 66 }; 67 68 typedef std::vector<Socket> SocketList; 69 70 Socket(); 71 /// Creates an uninitialized socket. 72 73 Socket(const Socket& socket); 74 /// Copy constructor. 75 /// 76 /// Attaches the SocketImpl from the other socket and 77 /// increments the reference count of the SocketImpl. 78 79 Socket& operator = (const Socket& socket); 80 /// Assignment operator. 81 /// 82 /// Releases the socket's SocketImpl and 83 /// attaches the SocketImpl from the other socket and 84 /// increments the reference count of the SocketImpl. 85 86 virtual ~Socket(); 87 /// Destroys the Socket and releases the 88 /// SocketImpl. 89 90 bool operator == (const Socket& socket) const; 91 /// Returns true if both sockets share the same 92 /// SocketImpl, false otherwise. 93 94 bool operator != (const Socket& socket) const; 95 /// Returns false if both sockets share the same 96 /// SocketImpl, true otherwise. 97 98 bool operator < (const Socket& socket) const; 99 /// Compares the SocketImpl pointers. 100 101 bool operator <= (const Socket& socket) const; 102 /// Compares the SocketImpl pointers. 103 104 bool operator > (const Socket& socket) const; 105 /// Compares the SocketImpl pointers. 106 107 bool operator >= (const Socket& socket) const; 108 /// Compares the SocketImpl pointers. 109 110 void close(); 111 /// Closes the socket. 112 113 static int select(SocketList& readList, SocketList& writeList, SocketList& exceptList, const Poco::Timespan& timeout); 114 /// Determines the status of one or more sockets, 115 /// using a call to select(). 116 /// 117 /// ReadList contains the list of sockets which should be 118 /// checked for readability. 119 /// 120 /// WriteList contains the list of sockets which should be 121 /// checked for writeability. 122 /// 123 /// ExceptList contains a list of sockets which should be 124 /// checked for a pending error. 125 /// 126 /// Returns the number of sockets ready. 127 /// 128 /// After return, 129 /// * readList contains those sockets ready for reading, 130 /// * writeList contains those sockets ready for writing, 131 /// * exceptList contains those sockets with a pending error. 132 /// 133 /// If the total number of sockets passed in readList, writeList and 134 /// exceptList is zero, select() will return immediately and the 135 /// return value will be 0. 136 /// 137 /// If one of the sockets passed to select() is closed while 138 /// select() runs, select will return immediately. However, 139 /// the closed socket will not be included in any list. 140 /// In this case, the return value may be greater than the sum 141 /// of all sockets in all list. 142 143 bool poll(const Poco::Timespan& timeout, int mode) const; 144 /// Determines the status of the socket, using a 145 /// call to select(). 146 /// 147 /// The mode argument is constructed by combining the values 148 /// of the SelectMode enumeration. 149 /// 150 /// Returns true if the next operation corresponding to 151 /// mode will not block, false otherwise. 152 153 int available() const; 154 /// Returns the number of bytes available that can be read 155 /// without causing the socket to block. 156 157 void setSendBufferSize(int size); 158 /// Sets the size of the send buffer. 159 160 int getSendBufferSize() const; 161 /// Returns the size of the send buffer. 162 /// 163 /// The returned value may be different than the 164 /// value previously set with setSendBufferSize(), 165 /// as the system is free to adjust the value. 166 167 void setReceiveBufferSize(int size); 168 /// Sets the size of the receive buffer. 169 170 int getReceiveBufferSize() const; 171 /// Returns the size of the receive buffer. 172 /// 173 /// The returned value may be different than the 174 /// value previously set with setReceiveBufferSize(), 175 /// as the system is free to adjust the value. 176 177 void setSendTimeout(const Poco::Timespan& timeout); 178 /// Sets the send timeout for the socket. 179 180 Poco::Timespan getSendTimeout() const; 181 /// Returns the send timeout for the socket. 182 /// 183 /// The returned timeout may be different than the 184 /// timeout previously set with setSendTimeout(), 185 /// as the system is free to adjust the value. 186 187 void setReceiveTimeout(const Poco::Timespan& timeout); 188 /// Sets the send timeout for the socket. 189 /// 190 /// On systems that do not support SO_RCVTIMEO, a 191 /// workaround using poll() is provided. 192 193 Poco::Timespan getReceiveTimeout() const; 194 /// Returns the receive timeout for the socket. 195 /// 196 /// The returned timeout may be different than the 197 /// timeout previously set with getReceiveTimeout(), 198 /// as the system is free to adjust the value. 199 200 void setOption(int level, int option, int value); 201 /// Sets the socket option specified by level and option 202 /// to the given integer value. 203 204 void setOption(int level, int option, unsigned value); 205 /// Sets the socket option specified by level and option 206 /// to the given integer value. 207 208 void setOption(int level, int option, unsigned char value); 209 /// Sets the socket option specified by level and option 210 /// to the given integer value. 211 212 void setOption(int level, int option, const Poco::Timespan& value); 213 /// Sets the socket option specified by level and option 214 /// to the given time value. 215 216 void setOption(int level, int option, const IPAddress& value); 217 /// Sets the socket option specified by level and option 218 /// to the given time value. 219 220 void getOption(int level, int option, int& value) const; 221 /// Returns the value of the socket option 222 /// specified by level and option. 223 224 void getOption(int level, int option, unsigned& value) const; 225 /// Returns the value of the socket option 226 /// specified by level and option. 227 228 void getOption(int level, int option, unsigned char& value) const; 229 /// Returns the value of the socket option 230 /// specified by level and option. 231 232 void getOption(int level, int option, Poco::Timespan& value) const; 233 /// Returns the value of the socket option 234 /// specified by level and option. 235 236 void getOption(int level, int option, IPAddress& value) const; 237 /// Returns the value of the socket option 238 /// specified by level and option. 239 240 void setLinger(bool on, int seconds); 241 /// Sets the value of the SO_LINGER socket option. 242 243 void getLinger(bool& on, int& seconds) const; 244 /// Returns the value of the SO_LINGER socket option. 245 246 void setNoDelay(bool flag); 247 /// Sets the value of the TCP_NODELAY socket option. 248 249 bool getNoDelay() const; 250 /// Returns the value of the TCP_NODELAY socket option. 251 252 void setKeepAlive(bool flag); 253 /// Sets the value of the SO_KEEPALIVE socket option. 254 255 bool getKeepAlive() const; 256 /// Returns the value of the SO_KEEPALIVE socket option. 257 258 void setReuseAddress(bool flag); 259 /// Sets the value of the SO_REUSEADDR socket option. 260 261 bool getReuseAddress() const; 262 /// Returns the value of the SO_REUSEADDR socket option. 263 264 void setReusePort(bool flag); 265 /// Sets the value of the SO_REUSEPORT socket option. 266 /// Does nothing if the socket implementation does not 267 /// support SO_REUSEPORT. 268 269 bool getReusePort() const; 270 /// Returns the value of the SO_REUSEPORT socket option. 271 /// 272 /// Returns false if the socket implementation does not 273 /// support SO_REUSEPORT. 274 275 void setOOBInline(bool flag); 276 /// Sets the value of the SO_OOBINLINE socket option. 277 278 bool getOOBInline() const; 279 /// Returns the value of the SO_OOBINLINE socket option. 280 281 void setBlocking(bool flag); 282 /// Sets the socket in blocking mode if flag is true, 283 /// disables blocking mode if flag is false. 284 285 bool getBlocking() const; 286 /// Returns the blocking mode of the socket. 287 /// This method will only work if the blocking modes of 288 /// the socket are changed via the setBlocking method! 289 290 SocketAddress address() const; 291 /// Returns the IP address and port number of the socket. 292 293 SocketAddress peerAddress() const; 294 /// Returns the IP address and port number of the peer socket. 295 296 SocketImpl* impl() const; 297 /// Returns the SocketImpl for this socket. 298 299 static bool supportsIPv4(); 300 /// Returns true if the system supports IPv4. 301 302 static bool supportsIPv6(); 303 /// Returns true if the system supports IPv6. 304 305protected: 306 Socket(SocketImpl* pImpl); 307 /// Creates the Socket and attaches the given SocketImpl. 308 /// The socket takes owership of the SocketImpl. 309 310 poco_socket_t sockfd() const; 311 /// Returns the socket descriptor for this socket. 312 313private: 314 SocketImpl* _pImpl; 315}; 316 317 318// 319// inlines 320// 321inline bool Socket::operator == (const Socket& socket) const 322{ 323 return _pImpl == socket._pImpl; 324} 325 326 327inline bool Socket::operator != (const Socket& socket) const 328{ 329 return _pImpl != socket._pImpl; 330} 331 332 333inline bool Socket::operator < (const Socket& socket) const 334{ 335 return _pImpl < socket._pImpl; 336} 337 338 339inline bool Socket::operator <= (const Socket& socket) const 340{ 341 return _pImpl <= socket._pImpl; 342} 343 344 345inline bool Socket::operator > (const Socket& socket) const 346{ 347 return _pImpl > socket._pImpl; 348} 349 350 351inline bool Socket::operator >= (const Socket& socket) const 352{ 353 return _pImpl >= socket._pImpl; 354} 355 356 357inline void Socket::close() 358{ 359 _pImpl->close(); 360} 361 362 363inline bool Socket::poll(const Poco::Timespan& timeout, int mode) const 364{ 365 return _pImpl->poll(timeout, mode); 366} 367 368 369inline int Socket::available() const 370{ 371 return _pImpl->available(); 372} 373 374 375inline void Socket::setSendBufferSize(int size) 376{ 377 _pImpl->setSendBufferSize(size); 378} 379 380 381inline int Socket::getSendBufferSize() const 382{ 383 return _pImpl->getSendBufferSize(); 384} 385 386 387inline void Socket::setReceiveBufferSize(int size) 388{ 389 _pImpl->setReceiveBufferSize(size); 390} 391 392 393inline int Socket::getReceiveBufferSize() const 394{ 395 return _pImpl->getReceiveBufferSize(); 396} 397 398 399inline void Socket::setSendTimeout(const Poco::Timespan& timeout) 400{ 401 _pImpl->setSendTimeout(timeout); 402} 403 404 405inline Poco::Timespan Socket::getSendTimeout() const 406{ 407 return _pImpl->getSendTimeout(); 408} 409 410 411inline void Socket::setReceiveTimeout(const Poco::Timespan& timeout) 412{ 413 _pImpl->setReceiveTimeout(timeout); 414} 415 416 417inline Poco::Timespan Socket::getReceiveTimeout() const 418{ 419 return _pImpl->getReceiveTimeout(); 420} 421 422 423inline void Socket::setOption(int level, int option, int value) 424{ 425 _pImpl->setOption(level, option, value); 426} 427 428 429inline void Socket::setOption(int level, int option, unsigned value) 430{ 431 _pImpl->setOption(level, option, value); 432} 433 434 435inline void Socket::setOption(int level, int option, unsigned char value) 436{ 437 _pImpl->setOption(level, option, value); 438} 439 440 441inline void Socket::setOption(int level, int option, const Poco::Timespan& value) 442{ 443 _pImpl->setOption(level, option, value); 444} 445 446 447inline void Socket::setOption(int level, int option, const IPAddress& value) 448{ 449 _pImpl->setOption(level, option, value); 450} 451 452 453inline void Socket::getOption(int level, int option, int& value) const 454{ 455 _pImpl->getOption(level, option, value); 456} 457 458 459inline void Socket::getOption(int level, int option, unsigned& value) const 460{ 461 _pImpl->getOption(level, option, value); 462} 463 464 465inline void Socket::getOption(int level, int option, unsigned char& value) const 466{ 467 _pImpl->getOption(level, option, value); 468} 469 470 471inline void Socket::getOption(int level, int option, Poco::Timespan& value) const 472{ 473 _pImpl->getOption(level, option, value); 474} 475 476 477inline void Socket::getOption(int level, int option, IPAddress& value) const 478{ 479 _pImpl->getOption(level, option, value); 480} 481 482 483inline void Socket::setLinger(bool on, int seconds) 484{ 485 _pImpl->setLinger(on, seconds); 486} 487 488 489inline void Socket::getLinger(bool& on, int& seconds) const 490{ 491 _pImpl->getLinger(on, seconds); 492} 493 494 495inline void Socket::setNoDelay(bool flag) 496{ 497 _pImpl->setNoDelay(flag); 498} 499 500 501inline bool Socket::getNoDelay() const 502{ 503 return _pImpl->getNoDelay(); 504} 505 506 507inline void Socket::setKeepAlive(bool flag) 508{ 509 _pImpl->setKeepAlive(flag); 510} 511 512 513inline bool Socket::getKeepAlive() const 514{ 515 return _pImpl->getKeepAlive(); 516} 517 518 519inline void Socket::setReuseAddress(bool flag) 520{ 521 _pImpl->setReuseAddress(flag); 522} 523 524 525inline bool Socket::getReuseAddress() const 526{ 527 return _pImpl->getReuseAddress(); 528} 529 530 531inline void Socket::setReusePort(bool flag) 532{ 533 _pImpl->setReusePort(flag); 534} 535 536 537inline bool Socket::getReusePort() const 538{ 539 return _pImpl->getReusePort(); 540} 541 542 543inline void Socket::setOOBInline(bool flag) 544{ 545 _pImpl->setOOBInline(flag); 546} 547 548 549inline bool Socket::getOOBInline() const 550{ 551 return _pImpl->getOOBInline(); 552} 553 554 555inline void Socket::setBlocking(bool flag) 556{ 557 _pImpl->setBlocking(flag); 558} 559 560 561inline bool Socket::getBlocking() const 562{ 563 return _pImpl->getBlocking(); 564} 565 566 567inline SocketImpl* Socket::impl() const 568{ 569 return _pImpl; 570} 571 572 573inline poco_socket_t Socket::sockfd() const 574{ 575 return _pImpl->sockfd(); 576} 577 578 579inline SocketAddress Socket::address() const 580{ 581 return _pImpl->address(); 582} 583 584 585inline SocketAddress Socket::peerAddress() const 586{ 587 return _pImpl->peerAddress(); 588} 589 590 591inline bool Socket::supportsIPv4() 592{ 593 return true; 594} 595 596 597inline bool Socket::supportsIPv6() 598{ 599#if defined(POCO_HAVE_IPv6) 600 return true; 601#else 602 return false; 603#endif 604} 605 606 607} } // namespace Poco::Net 608 609 610#endif // Net_Socket_INCLUDED