| 1 |
/*---------------------------------------------------------------------------*/ |
| 2 |
/* */ |
| 3 |
/* Socket.h - Passive Socket Decleration. */ |
| 4 |
/* */ |
| 5 |
/* Author : Mark Carrier ([email protected]) */ |
| 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 |
* [email protected]. |
| 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 |
|
| 47 |
/// Provides a platform independent class to create a passive socket. |
| 48 |
/// A passive socket is used to create a "listening" socket. This type |
| 49 |
/// of object would be used when an application needs to wait for |
| 50 |
/// inbound connections. Support for CSimpleSocket::SocketTypeTcp, |
| 51 |
/// CSimpleSocket::SocketTypeUdp, and CSimpleSocket::SocketTypeRaw is handled |
| 52 |
/// in a similar fashion. The big difference is that the method |
| 53 |
/// CPassiveSocket::Accept should not be called on the latter two socket |
| 54 |
/// types. |
| 55 |
class CPassiveSocket : public CSimpleSocket { |
| 56 |
public: |
| 57 |
CPassiveSocket(CSocketType type = SocketTypeTcp); |
| 58 |
virtual ~CPassiveSocket() { |
| 59 |
Close(); |
| 60 |
}; |
| 61 |
|
| 62 |
/// Extracts the first connection request on the queue of pending |
| 63 |
/// connections and creates a newly connected socket. Used with |
| 64 |
/// CSocketType CSimpleSocket::SocketTypeTcp. It is the responsibility of |
| 65 |
/// the caller to delete the returned object when finished. |
| 66 |
/// @return if successful a pointer to a newly created CActiveSocket object |
| 67 |
/// will be returned and the internal error condition of the CPassiveSocket |
| 68 |
/// object will be CPassiveSocket::SocketSuccess. If an error condition was encountered |
| 69 |
/// the NULL will be returned and one of the following error conditions will be set: |
| 70 |
/// CPassiveSocket::SocketEwouldblock, CPassiveSocket::SocketInvalidSocket, |
| 71 |
/// CPassiveSocket::SocketConnectionAborted, CPassiveSocket::SocketInterrupted |
| 72 |
/// CPassiveSocket::SocketProtocolError, CPassiveSocket::SocketFirewallError |
| 73 |
virtual CActiveSocket *Accept(void); |
| 74 |
|
| 75 |
/// Bind to a multicast group on a specified interface, multicast group, and port |
| 76 |
/// |
| 77 |
/// @param pInterface - interface on which to bind. |
| 78 |
/// @param pGroup - multicast group address to bind. |
| 79 |
/// @param nPort - port on which multicast |
| 80 |
/// @return true if able to bind to interface and multicast group. |
| 81 |
/// If not successful, the false is returned and one of the following error |
| 82 |
/// condiitions will be set: CPassiveSocket::SocketAddressInUse, CPassiveSocket::SocketProtocolError, |
| 83 |
/// CPassiveSocket::SocketInvalidSocket. The following socket errors are for Linux/Unix |
| 84 |
/// derived systems only: CPassiveSocket::SocketInvalidSocketBuffer |
| 85 |
bool BindMulticast(const char *pInterface, const char *pGroup, uint16 nPort); |
| 86 |
|
| 87 |
/// Create a listening socket at local ip address 'x.x.x.x' or 'localhost' |
| 88 |
/// if pAddr is NULL on port nPort. |
| 89 |
/// |
| 90 |
/// @param pAddr specifies the IP address on which to listen. |
| 91 |
/// @param nPort specifies the port on which to listen. |
| 92 |
/// @param nConnectionBacklog specifies connection queue backlog (default 30,000) |
| 93 |
/// @return true if a listening socket was created. |
| 94 |
/// If not successful, the false is returned and one of the following error |
| 95 |
/// conditions will be set: CPassiveSocket::SocketAddressInUse, CPassiveSocket::SocketProtocolError, |
| 96 |
/// CPassiveSocket::SocketInvalidSocket. The following socket errors are for Linux/Unix |
| 97 |
/// derived systems only: CPassiveSocket::SocketInvalidSocketBuffer |
| 98 |
virtual bool Listen(const char *pAddr, uint16 nPort, int32 nConnectionBacklog = 30000); |
| 99 |
|
| 100 |
/// Attempts to send a block of data on an established connection. |
| 101 |
/// @param pBuf block of data to be sent. |
| 102 |
/// @param bytesToSend size of data block to be sent. |
| 103 |
/// @return number of bytes actually sent, return of zero means the |
| 104 |
/// connection has been shutdown on the other side, and a return of -1 |
| 105 |
/// means that an error has occurred. If an error was signaled then one |
| 106 |
/// of the following error codes will be set: CPassiveSocket::SocketInvalidSocket, |
| 107 |
/// CPassiveSocket::SocketEwouldblock, SimpleSocket::SocketConnectionReset, |
| 108 |
/// CPassiveSocket::SocketInvalidSocketBuffer, CPassiveSocket::SocketInterrupted, |
| 109 |
/// CPassiveSocket::SocketProtocolError, CPassiveSocket::SocketNotconnected |
| 110 |
/// <br>\b Note: This function is used only for a socket of type |
| 111 |
/// CSimpleSocket::SocketTypeUdp |
| 112 |
virtual int32 Send(const uint8 *pBuf, size_t bytesToSend); |
| 113 |
|
| 114 |
private: |
| 115 |
struct ip_mreq m_stMulticastRequest; /// group address for multicast |
| 116 |
|
| 117 |
}; |
| 118 |
|
| 119 |
#endif // __PASSIVESOCKET_H__ |