PageRenderTime 25ms CodeModel.GetById 21ms app.highlight 2ms RepoModel.GetById 1ms app.codeStats 0ms

/sparrowhawk/foundation/ESFTCPSocket.h

http://github.com/jtblatt/duderino
C Header | 165 lines | 52 code | 25 blank | 88 comment | 0 complexity | 803be8b277242f99be92d6bff93873ee MD5 | raw file
  1/** @file ESFTCPSocket.h
  2 *  @brief An abstract base class with code common to both
  3 *      ESFConnectedTCPSocket and ESFListeningTCPSocket instances
  4 *
  5 * Copyright (c) 2009 Yahoo! Inc.
  6 * The copyrights embodied in the content of this file are licensed by Yahoo! Inc.
  7 * under the BSD (revised) open source license.
  8 *
  9 * Derived from code that is Copyright (c) 2009 Joshua Blatt and offered under both
 10 * BSD and Apache 2.0 licenses (http://sourceforge.net/projects/sparrowhawk/).
 11 *
 12 *    $Author: blattj $
 13 *    $Date: 2009/05/25 21:51:08 $
 14 *    $Name:  $
 15 *    $Revision: 1.3 $
 16 */
 17
 18#ifndef ESF_TCP_SOCKET_H
 19#define ESF_TCP_SOCKET_H
 20
 21#ifndef ESF_CONFIG_H
 22#include <ESFConfig.h>
 23#endif
 24
 25#ifndef ESF_SOCKET_H
 26#include <ESFSocket.h>
 27#endif
 28
 29#ifndef ESF_SOCKET_ADDRESS_H
 30#include <ESFSocketAddress.h>
 31#endif
 32
 33#ifndef ESF_ERROR_H
 34#include <ESFError.h>
 35#endif
 36
 37/** ESFTCPSocket is a generic base class for connected and listening tcp
 38 *  sockets.
 39 *
 40 *  @ingroup network
 41 */
 42class ESFTCPSocket {
 43public:
 44    /** ESFListeningTCPSockets create these, ESFConnectedTCPSockets can be
 45     *  constructed from these.  The sole function of this struct is to
 46     *  transfer data between those two classes.
 47     */
 48    typedef struct {
 49        bool _isBlocking;
 50        SOCKET _sockFd;
 51        ESFSocketAddress _listeningAddress;
 52        ESFSocketAddress _peerAddress;
 53    } AcceptData;
 54
 55    /** Construct an uninitialized ESFTCPSocket.
 56     */
 57    ESFTCPSocket();
 58
 59    /** Construct a new ESFTCPSocket.
 60     *
 61     *  @param isBlocking true if this socket is a blocking socket.
 62     */
 63    ESFTCPSocket(bool isBlocking);
 64
 65    /** Construct a new server ESFTCPSocket.
 66     *
 67     * @param acceptData An object created popupated by ESFListeningTCPSockets
 68     *  when accepting a new connection.
 69     */
 70    ESFTCPSocket(AcceptData *acceptData);
 71
 72    /** Destroy the socket.  Will close the socket if it has not
 73     *  already been closed.
 74     */
 75    virtual ~ESFTCPSocket();
 76
 77    /** Reset a tcp socket.  If the socket is currently open, this will close
 78     *  it as a side-effect.
 79     *
 80     * @param acceptData An object created popupated by ESFListeningTCPSockets
 81     *  when accepting a new connection.
 82     * @return ESF_SUCCESS if successful, another error code otherwise.
 83     */
 84    virtual ESFError reset(AcceptData *acceptData);
 85
 86    /** Close the socket.
 87     */
 88    virtual void close();
 89
 90    /** Close a socket descriptor
 91     *
 92     * @param socket The socket descriptor to close
 93     */
 94    static void Close(SOCKET socket);
 95
 96    /** Determine whether or not this socket is a blocking socket.
 97     *
 98     *  @return true if this socket is a blocking socket, false otherwise.
 99     */
100    inline bool isBlocking() const {
101        return _isBlocking;
102    }
103
104    /** Set the socket's blocking/non-blocking property.
105     *
106     *  @param isBlocking Whether or not the socket is blocking.
107     *  @return ESF_SUCCESS if successful, another error code otherwise.
108     */
109    ESFError setBlocking(bool isBlocking);
110
111    /** Set a socket descriptor's blocking/non-blocking property.
112     *
113     *  @param isBlocking Whether or not the socket is blocking.
114     *  @return ESF_SUCCESS if successful, another error code otherwise.
115     */
116    static ESFError SetBlocking(SOCKET sockFd, bool isBlocking);
117
118    /** Get the socket's socket descriptor.
119     *
120     *  @return the socket descriptor
121     */
122    inline SOCKET getSocketDescriptor() const {
123        return _sockFd;
124    }
125
126    /** Get and clear the last error on this socket.
127     *
128     *  @return the last error that occurred on this socket or ESF_SUCCESS if
129     *      no error has occurred.
130     */
131    inline ESFError getLastError() {
132        return GetLastError(_sockFd);
133    }
134
135    /** Get and clear the last error on a socket descriptor.
136     *
137     *  @param socket The socked descriptor
138     *  @return the last error that occurred on the socket descriptor or ESF_SUCCESS if
139     *      no error has occurred.
140     */
141    static ESFError GetLastError(SOCKET socket);
142
143    /** Placement new.
144     *
145     *  @param size The size of the object.
146     *  @param allocator The source of the object's memory.
147     *  @return The new object or NULL of the memory allocation failed.
148     */
149    inline void *operator new(size_t size, ESFAllocator *allocator) {
150        return allocator->allocate(size);
151    }
152
153protected:
154
155    bool _isBlocking;
156    SOCKET _sockFd;
157
158private:
159
160    // Disabled
161    ESFTCPSocket(const ESFTCPSocket &socket);
162    ESFTCPSocket &operator=(const ESFTCPSocket &);
163};
164
165#endif /* ! ESF_TCP_SOCKET_H */