PassiveSocket.h 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. /*---------------------------------------------------------------------------*/
  2. /* */
  3. /* Socket.h - Passive Socket Decleration. */
  4. /* */
  5. /* Author : Mark Carrier (mark@carrierlabs.com) */
  6. /* */
  7. /*---------------------------------------------------------------------------*/
  8. /* Copyright (c) 2007-2009 CarrierLabs, LLC. All rights reserved.
  9. *
  10. * Redistribution and use in source and binary forms, with or without
  11. * modification, are permitted provided that the following conditions
  12. * are met:
  13. *
  14. * 1. Redistributions of source code must retain the above copyright
  15. * notice, this list of conditions and the following disclaimer.
  16. *
  17. * 2. Redistributions in binary form must reproduce the above copyright
  18. * notice, this list of conditions and the following disclaimer in
  19. * the documentation and/or other materials provided with the
  20. * distribution.
  21. *
  22. * 3. The name of the author may not be used to endorse or promote products
  23. * derived from this software without specific prior written permission.
  24. *
  25. * 4. The name "CarrierLabs" must not be used to
  26. * endorse or promote products derived from this software without
  27. * prior written permission. For written permission, please contact
  28. * mark@carrierlabs.com.
  29. *
  30. * THIS SOFTWARE IS PROVIDED BY MARK CARRIER ``AS IS'' AND ANY
  31. * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  32. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  33. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL MARK CARRIER OR
  34. * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  35. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  36. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  37. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  38. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  39. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  40. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  41. * OF THE POSSIBILITY OF SUCH DAMAGE.
  42. *----------------------------------------------------------------------------*/
  43. #ifndef __PASSIVESOCKET_H__
  44. #define __PASSIVESOCKET_H__
  45. #include "ActiveSocket.h"
  46. /// Provides a platform independent class to create a passive socket.
  47. /// A passive socket is used to create a "listening" socket. This type
  48. /// of object would be used when an application needs to wait for
  49. /// inbound connections. Support for CSimpleSocket::SocketTypeTcp,
  50. /// CSimpleSocket::SocketTypeUdp, and CSimpleSocket::SocketTypeRaw is handled
  51. /// in a similar fashion. The big difference is that the method
  52. /// CPassiveSocket::Accept should not be called on the latter two socket
  53. /// types.
  54. class EXPORT CPassiveSocket : public CSimpleSocket {
  55. public:
  56. CPassiveSocket(CSocketType type = SocketTypeTcp);
  57. virtual ~CPassiveSocket() {
  58. Close();
  59. };
  60. /// Extracts the first connection request on the queue of pending
  61. /// connections and creates a newly connected socket. Used with
  62. /// CSocketType CSimpleSocket::SocketTypeTcp. It is the responsibility of
  63. /// the caller to delete the returned object when finished.
  64. /// @return if successful a pointer to a newly created CActiveSocket object
  65. /// will be returned and the internal error condition of the CPassiveSocket
  66. /// object will be CPassiveSocket::SocketSuccess. If an error condition was encountered
  67. /// the NULL will be returned and one of the following error conditions will be set:
  68. /// CPassiveSocket::SocketEwouldblock, CPassiveSocket::SocketInvalidSocket,
  69. /// CPassiveSocket::SocketConnectionAborted, CPassiveSocket::SocketInterrupted
  70. /// CPassiveSocket::SocketProtocolError, CPassiveSocket::SocketFirewallError
  71. virtual CActiveSocket *Accept(void);
  72. /// Bind to a multicast group on a specified interface, multicast group, and port
  73. ///
  74. /// @param pInterface - interface on which to bind.
  75. /// @param pGroup - multicast group address to bind.
  76. /// @param nPort - port on which multicast
  77. /// @return true if able to bind to interface and multicast group.
  78. /// If not successful, the false is returned and one of the following error
  79. /// condiitions will be set: CPassiveSocket::SocketAddressInUse, CPassiveSocket::SocketProtocolError,
  80. /// CPassiveSocket::SocketInvalidSocket. The following socket errors are for Linux/Unix
  81. /// derived systems only: CPassiveSocket::SocketInvalidSocketBuffer
  82. bool BindMulticast(const char *pInterface, const char *pGroup, uint16 nPort);
  83. /// Create a listening socket at local ip address 'x.x.x.x' or 'localhost'
  84. /// if pAddr is NULL on port nPort.
  85. ///
  86. /// @param pAddr specifies the IP address on which to listen.
  87. /// @param nPort specifies the port on which to listen.
  88. /// @param nConnectionBacklog specifies connection queue backlog (default 30,000)
  89. /// @return true if a listening socket was created.
  90. /// If not successful, the false is returned and one of the following error
  91. /// conditions will be set: CPassiveSocket::SocketAddressInUse, CPassiveSocket::SocketProtocolError,
  92. /// CPassiveSocket::SocketInvalidSocket. The following socket errors are for Linux/Unix
  93. /// derived systems only: CPassiveSocket::SocketInvalidSocketBuffer
  94. virtual bool Listen(const char *pAddr, uint16 nPort, int32 nConnectionBacklog = 30000);
  95. /// Attempts to send a block of data on an established connection.
  96. /// @param pBuf block of data to be sent.
  97. /// @param bytesToSend size of data block to be sent.
  98. /// @return number of bytes actually sent, return of zero means the
  99. /// connection has been shutdown on the other side, and a return of -1
  100. /// means that an error has occurred. If an error was signaled then one
  101. /// of the following error codes will be set: CPassiveSocket::SocketInvalidSocket,
  102. /// CPassiveSocket::SocketEwouldblock, SimpleSocket::SocketConnectionReset,
  103. /// CPassiveSocket::SocketInvalidSocketBuffer, CPassiveSocket::SocketInterrupted,
  104. /// CPassiveSocket::SocketProtocolError, CPassiveSocket::SocketNotconnected
  105. /// <br>\b Note: This function is used only for a socket of type
  106. /// CSimpleSocket::SocketTypeUdp
  107. virtual int32 Send(const uint8 *pBuf, size_t bytesToSend);
  108. private:
  109. struct ip_mreq m_stMulticastRequest; /// group address for multicast
  110. };
  111. #endif // __PASSIVESOCKET_H__