root / CCV-HAND / libs / poco / include / Poco / Net / Socket.h @ 59
View | Annotate | Download (14.3 KB)
| 1 | //
|
|---|---|
| 2 | // Socket.h
|
| 3 | //
|
| 4 | // $Id: //poco/1.3/Net/include/Poco/Net/Socket.h#3 $
|
| 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 | |
| 48 | namespace Poco {
|
| 49 | namespace Net {
|
| 50 | |
| 51 | |
| 52 | class 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 | {
|
| 59 | public:
|
| 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 | bool poll(const Poco::Timespan& timeout, int mode) const; |
| 138 | /// Determines the status of the socket, using a
|
| 139 | /// call to select().
|
| 140 | ///
|
| 141 | /// The mode argument is constructed by combining the values
|
| 142 | /// of the SelectMode enumeration.
|
| 143 | ///
|
| 144 | /// Returns true if the next operation corresponding to
|
| 145 | /// mode will not block, false otherwise.
|
| 146 | |
| 147 | int available() const; |
| 148 | /// Returns the number of bytes available that can be read
|
| 149 | /// without causing the socket to block.
|
| 150 | |
| 151 | void setSendBufferSize(int size); |
| 152 | /// Sets the size of the send buffer.
|
| 153 | |
| 154 | int getSendBufferSize() const; |
| 155 | /// Returns the size of the send buffer.
|
| 156 | ///
|
| 157 | /// The returned value may be different than the
|
| 158 | /// value previously set with setSendBufferSize(),
|
| 159 | /// as the system is free to adjust the value.
|
| 160 | |
| 161 | void setReceiveBufferSize(int size); |
| 162 | /// Sets the size of the receive buffer.
|
| 163 | |
| 164 | int getReceiveBufferSize() const; |
| 165 | /// Returns the size of the receive buffer.
|
| 166 | ///
|
| 167 | /// The returned value may be different than the
|
| 168 | /// value previously set with setReceiveBufferSize(),
|
| 169 | /// as the system is free to adjust the value.
|
| 170 | |
| 171 | void setSendTimeout(const Poco::Timespan& timeout); |
| 172 | /// Sets the send timeout for the socket.
|
| 173 | |
| 174 | Poco::Timespan getSendTimeout() const;
|
| 175 | /// Returns the send timeout for the socket.
|
| 176 | ///
|
| 177 | /// The returned timeout may be different than the
|
| 178 | /// timeout previously set with setSendTimeout(),
|
| 179 | /// as the system is free to adjust the value.
|
| 180 | |
| 181 | void setReceiveTimeout(const Poco::Timespan& timeout); |
| 182 | /// Sets the send timeout for the socket.
|
| 183 | ///
|
| 184 | /// On systems that do not support SO_RCVTIMEO, a
|
| 185 | /// workaround using poll() is provided.
|
| 186 | |
| 187 | Poco::Timespan getReceiveTimeout() const;
|
| 188 | /// Returns the receive timeout for the socket.
|
| 189 | ///
|
| 190 | /// The returned timeout may be different than the
|
| 191 | /// timeout previously set with getReceiveTimeout(),
|
| 192 | /// as the system is free to adjust the value.
|
| 193 | |
| 194 | void setOption(int level, int option, int value); |
| 195 | /// Sets the socket option specified by level and option
|
| 196 | /// to the given integer value.
|
| 197 | |
| 198 | void setOption(int level, int option, unsigned value); |
| 199 | /// Sets the socket option specified by level and option
|
| 200 | /// to the given integer value.
|
| 201 | |
| 202 | void setOption(int level, int option, unsigned char value); |
| 203 | /// Sets the socket option specified by level and option
|
| 204 | /// to the given integer value.
|
| 205 | |
| 206 | void setOption(int level, int option, const Poco::Timespan& value); |
| 207 | /// Sets the socket option specified by level and option
|
| 208 | /// to the given time value.
|
| 209 | |
| 210 | void setOption(int level, int option, const IPAddress& value); |
| 211 | /// Sets the socket option specified by level and option
|
| 212 | /// to the given time value.
|
| 213 | |
| 214 | void getOption(int level, int option, int& value) const; |
| 215 | /// Returns the value of the socket option
|
| 216 | /// specified by level and option.
|
| 217 | |
| 218 | void getOption(int level, int option, unsigned& value) const; |
| 219 | /// Returns the value of the socket option
|
| 220 | /// specified by level and option.
|
| 221 | |
| 222 | void getOption(int level, int option, unsigned char& value) const; |
| 223 | /// Returns the value of the socket option
|
| 224 | /// specified by level and option.
|
| 225 | |
| 226 | void getOption(int level, int option, Poco::Timespan& value) const; |
| 227 | /// Returns the value of the socket option
|
| 228 | /// specified by level and option.
|
| 229 | |
| 230 | void getOption(int level, int option, IPAddress& value) const; |
| 231 | /// Returns the value of the socket option
|
| 232 | /// specified by level and option.
|
| 233 | |
| 234 | void setLinger(bool on, int seconds); |
| 235 | /// Sets the value of the SO_LINGER socket option.
|
| 236 | |
| 237 | void getLinger(bool& on, int& seconds) const; |
| 238 | /// Returns the value of the SO_LINGER socket option.
|
| 239 | |
| 240 | void setNoDelay(bool flag); |
| 241 | /// Sets the value of the TCP_NODELAY socket option.
|
| 242 | |
| 243 | bool getNoDelay() const; |
| 244 | /// Returns the value of the TCP_NODELAY socket option.
|
| 245 | |
| 246 | void setKeepAlive(bool flag); |
| 247 | /// Sets the value of the SO_KEEPALIVE socket option.
|
| 248 | |
| 249 | bool getKeepAlive() const; |
| 250 | /// Returns the value of the SO_KEEPALIVE socket option.
|
| 251 | |
| 252 | void setReuseAddress(bool flag); |
| 253 | /// Sets the value of the SO_REUSEADDR socket option.
|
| 254 | |
| 255 | bool getReuseAddress() const; |
| 256 | /// Returns the value of the SO_REUSEADDR socket option.
|
| 257 | |
| 258 | void setReusePort(bool flag); |
| 259 | /// Sets the value of the SO_REUSEPORT socket option.
|
| 260 | /// Does nothing if the socket implementation does not
|
| 261 | /// support SO_REUSEPORT.
|
| 262 | |
| 263 | bool getReusePort() const; |
| 264 | /// Returns the value of the SO_REUSEPORT socket option.
|
| 265 | ///
|
| 266 | /// Returns false if the socket implementation does not
|
| 267 | /// support SO_REUSEPORT.
|
| 268 | |
| 269 | void setOOBInline(bool flag); |
| 270 | /// Sets the value of the SO_OOBINLINE socket option.
|
| 271 | |
| 272 | bool getOOBInline() const; |
| 273 | /// Returns the value of the SO_OOBINLINE socket option.
|
| 274 | |
| 275 | void setBlocking(bool flag); |
| 276 | /// Sets the socket in blocking mode if flag is true,
|
| 277 | /// disables blocking mode if flag is false.
|
| 278 | |
| 279 | bool getBlocking() const; |
| 280 | /// Returns the blocking mode of the socket.
|
| 281 | /// This method will only work if the blocking modes of
|
| 282 | /// the socket are changed via the setBlocking method!
|
| 283 | |
| 284 | SocketAddress address() const;
|
| 285 | /// Returns the IP address and port number of the socket.
|
| 286 | |
| 287 | SocketAddress peerAddress() const;
|
| 288 | /// Returns the IP address and port number of the peer socket.
|
| 289 | |
| 290 | SocketImpl* impl() const;
|
| 291 | /// Returns the SocketImpl for this socket.
|
| 292 | |
| 293 | static bool supportsIPv4(); |
| 294 | /// Returns true if the system supports IPv4.
|
| 295 | |
| 296 | static bool supportsIPv6(); |
| 297 | /// Returns true if the system supports IPv6.
|
| 298 | |
| 299 | protected:
|
| 300 | Socket(SocketImpl* pImpl); |
| 301 | /// Creates the Socket and attaches the given SocketImpl.
|
| 302 | /// The socket takes owership of the SocketImpl.
|
| 303 | |
| 304 | poco_socket_t sockfd() const;
|
| 305 | /// Returns the socket descriptor for this socket.
|
| 306 | |
| 307 | private:
|
| 308 | SocketImpl* _pImpl; |
| 309 | }; |
| 310 | |
| 311 | |
| 312 | //
|
| 313 | // inlines
|
| 314 | //
|
| 315 | inline bool Socket::operator == (const Socket& socket) const |
| 316 | {
|
| 317 | return _pImpl == socket._pImpl;
|
| 318 | } |
| 319 | |
| 320 | |
| 321 | inline bool Socket::operator != (const Socket& socket) const |
| 322 | {
|
| 323 | return _pImpl != socket._pImpl;
|
| 324 | } |
| 325 | |
| 326 | |
| 327 | inline bool Socket::operator < (const Socket& socket) const |
| 328 | {
|
| 329 | return _pImpl < socket._pImpl;
|
| 330 | } |
| 331 | |
| 332 | |
| 333 | inline bool Socket::operator <= (const Socket& socket) const |
| 334 | {
|
| 335 | return _pImpl <= socket._pImpl;
|
| 336 | } |
| 337 | |
| 338 | |
| 339 | inline bool Socket::operator > (const Socket& socket) const |
| 340 | {
|
| 341 | return _pImpl > socket._pImpl;
|
| 342 | } |
| 343 | |
| 344 | |
| 345 | inline bool Socket::operator >= (const Socket& socket) const |
| 346 | {
|
| 347 | return _pImpl >= socket._pImpl;
|
| 348 | } |
| 349 | |
| 350 | |
| 351 | inline void Socket::close() |
| 352 | {
|
| 353 | _pImpl->close(); |
| 354 | } |
| 355 | |
| 356 | |
| 357 | inline bool Socket::poll(const Poco::Timespan& timeout, int mode) const |
| 358 | {
|
| 359 | return _pImpl->poll(timeout, mode);
|
| 360 | } |
| 361 | |
| 362 | |
| 363 | inline int Socket::available() const |
| 364 | {
|
| 365 | return _pImpl->available();
|
| 366 | } |
| 367 | |
| 368 | |
| 369 | inline void Socket::setSendBufferSize(int size) |
| 370 | {
|
| 371 | _pImpl->setSendBufferSize(size); |
| 372 | } |
| 373 | |
| 374 | |
| 375 | inline int Socket::getSendBufferSize() const |
| 376 | {
|
| 377 | return _pImpl->getSendBufferSize();
|
| 378 | } |
| 379 | |
| 380 | |
| 381 | inline void Socket::setReceiveBufferSize(int size) |
| 382 | {
|
| 383 | _pImpl->setReceiveBufferSize(size); |
| 384 | } |
| 385 | |
| 386 | |
| 387 | inline int Socket::getReceiveBufferSize() const |
| 388 | {
|
| 389 | return _pImpl->getReceiveBufferSize();
|
| 390 | } |
| 391 | |
| 392 | |
| 393 | inline void Socket::setSendTimeout(const Poco::Timespan& timeout) |
| 394 | {
|
| 395 | _pImpl->setSendTimeout(timeout); |
| 396 | } |
| 397 | |
| 398 | |
| 399 | inline Poco::Timespan Socket::getSendTimeout() const |
| 400 | {
|
| 401 | return _pImpl->getSendTimeout();
|
| 402 | } |
| 403 | |
| 404 | |
| 405 | inline void Socket::setReceiveTimeout(const Poco::Timespan& timeout) |
| 406 | {
|
| 407 | _pImpl->setReceiveTimeout(timeout); |
| 408 | } |
| 409 | |
| 410 | |
| 411 | inline Poco::Timespan Socket::getReceiveTimeout() const |
| 412 | {
|
| 413 | return _pImpl->getReceiveTimeout();
|
| 414 | } |
| 415 | |
| 416 | |
| 417 | inline void Socket::setOption(int level, int option, int value) |
| 418 | {
|
| 419 | _pImpl->setOption(level, option, value); |
| 420 | } |
| 421 | |
| 422 | |
| 423 | inline void Socket::setOption(int level, int option, unsigned value) |
| 424 | {
|
| 425 | _pImpl->setOption(level, option, value); |
| 426 | } |
| 427 | |
| 428 | |
| 429 | inline void Socket::setOption(int level, int option, unsigned char value) |
| 430 | {
|
| 431 | _pImpl->setOption(level, option, value); |
| 432 | } |
| 433 | |
| 434 | |
| 435 | inline void Socket::setOption(int level, int option, const Poco::Timespan& value) |
| 436 | {
|
| 437 | _pImpl->setOption(level, option, value); |
| 438 | } |
| 439 | |
| 440 | |
| 441 | inline void Socket::setOption(int level, int option, const IPAddress& value) |
| 442 | {
|
| 443 | _pImpl->setOption(level, option, value); |
| 444 | } |
| 445 | |
| 446 | |
| 447 | inline void Socket::getOption(int level, int option, int& value) const |
| 448 | {
|
| 449 | _pImpl->getOption(level, option, value); |
| 450 | } |
| 451 | |
| 452 | |
| 453 | inline void Socket::getOption(int level, int option, unsigned& value) const |
| 454 | {
|
| 455 | _pImpl->getOption(level, option, value); |
| 456 | } |
| 457 | |
| 458 | |
| 459 | inline void Socket::getOption(int level, int option, unsigned char& value) const |
| 460 | {
|
| 461 | _pImpl->getOption(level, option, value); |
| 462 | } |
| 463 | |
| 464 | |
| 465 | inline void Socket::getOption(int level, int option, Poco::Timespan& value) const |
| 466 | {
|
| 467 | _pImpl->getOption(level, option, value); |
| 468 | } |
| 469 | |
| 470 | |
| 471 | inline void Socket::getOption(int level, int option, IPAddress& value) const |
| 472 | {
|
| 473 | _pImpl->getOption(level, option, value); |
| 474 | } |
| 475 | |
| 476 | |
| 477 | inline void Socket::setLinger(bool on, int seconds) |
| 478 | {
|
| 479 | _pImpl->setLinger(on, seconds); |
| 480 | } |
| 481 | |
| 482 | |
| 483 | inline void Socket::getLinger(bool& on, int& seconds) const |
| 484 | {
|
| 485 | _pImpl->getLinger(on, seconds); |
| 486 | } |
| 487 | |
| 488 | |
| 489 | inline void Socket::setNoDelay(bool flag) |
| 490 | {
|
| 491 | _pImpl->setNoDelay(flag); |
| 492 | } |
| 493 | |
| 494 | |
| 495 | inline bool Socket::getNoDelay() const |
| 496 | {
|
| 497 | return _pImpl->getNoDelay();
|
| 498 | } |
| 499 | |
| 500 | |
| 501 | inline void Socket::setKeepAlive(bool flag) |
| 502 | {
|
| 503 | _pImpl->setKeepAlive(flag); |
| 504 | } |
| 505 | |
| 506 | |
| 507 | inline bool Socket::getKeepAlive() const |
| 508 | {
|
| 509 | return _pImpl->getKeepAlive();
|
| 510 | } |
| 511 | |
| 512 | |
| 513 | inline void Socket::setReuseAddress(bool flag) |
| 514 | {
|
| 515 | _pImpl->setReuseAddress(flag); |
| 516 | } |
| 517 | |
| 518 | |
| 519 | inline bool Socket::getReuseAddress() const |
| 520 | {
|
| 521 | return _pImpl->getReuseAddress();
|
| 522 | } |
| 523 | |
| 524 | |
| 525 | inline void Socket::setReusePort(bool flag) |
| 526 | {
|
| 527 | _pImpl->setReusePort(flag); |
| 528 | } |
| 529 | |
| 530 | |
| 531 | inline bool Socket::getReusePort() const |
| 532 | {
|
| 533 | return _pImpl->getReusePort();
|
| 534 | } |
| 535 | |
| 536 | |
| 537 | inline void Socket::setOOBInline(bool flag) |
| 538 | {
|
| 539 | _pImpl->setOOBInline(flag); |
| 540 | } |
| 541 | |
| 542 | |
| 543 | inline bool Socket::getOOBInline() const |
| 544 | {
|
| 545 | return _pImpl->getOOBInline();
|
| 546 | } |
| 547 | |
| 548 | |
| 549 | inline void Socket::setBlocking(bool flag) |
| 550 | {
|
| 551 | _pImpl->setBlocking(flag); |
| 552 | } |
| 553 | |
| 554 | |
| 555 | inline bool Socket::getBlocking() const |
| 556 | {
|
| 557 | return _pImpl->getBlocking();
|
| 558 | } |
| 559 | |
| 560 | |
| 561 | inline SocketImpl* Socket::impl() const |
| 562 | {
|
| 563 | return _pImpl;
|
| 564 | } |
| 565 | |
| 566 | |
| 567 | inline poco_socket_t Socket::sockfd() const |
| 568 | {
|
| 569 | return _pImpl->sockfd();
|
| 570 | } |
| 571 | |
| 572 | |
| 573 | inline SocketAddress Socket::address() const |
| 574 | {
|
| 575 | return _pImpl->address();
|
| 576 | } |
| 577 | |
| 578 | |
| 579 | inline SocketAddress Socket::peerAddress() const |
| 580 | {
|
| 581 | return _pImpl->peerAddress();
|
| 582 | } |
| 583 | |
| 584 | |
| 585 | inline bool Socket::supportsIPv4() |
| 586 | {
|
| 587 | return true; |
| 588 | } |
| 589 | |
| 590 | |
| 591 | inline bool Socket::supportsIPv6() |
| 592 | {
|
| 593 | #if defined(POCO_HAVE_IPv6)
|
| 594 | return true; |
| 595 | #else
|
| 596 | return false; |
| 597 | #endif
|
| 598 | } |
| 599 | |
| 600 | |
| 601 | } } // namespace Poco::Net
|
| 602 | |
| 603 | |
| 604 | #endif // Net_Socket_INCLUDED |
