/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. #ifndef ESF_TCP_SOCKET_H
  18. #define ESF_TCP_SOCKET_H
  19. #ifndef ESF_CONFIG_H
  20. #include <ESFConfig.h>
  21. #endif
  22. #ifndef ESF_SOCKET_H
  23. #include <ESFSocket.h>
  24. #endif
  25. #ifndef ESF_SOCKET_ADDRESS_H
  26. #include <ESFSocketAddress.h>
  27. #endif
  28. #ifndef ESF_ERROR_H
  29. #include <ESFError.h>
  30. #endif
  31. /** ESFTCPSocket is a generic base class for connected and listening tcp
  32. * sockets.
  33. *
  34. * @ingroup network
  35. */
  36. class ESFTCPSocket {
  37. public:
  38. /** ESFListeningTCPSockets create these, ESFConnectedTCPSockets can be
  39. * constructed from these. The sole function of this struct is to
  40. * transfer data between those two classes.
  41. */
  42. typedef struct {
  43. bool _isBlocking;
  44. SOCKET _sockFd;
  45. ESFSocketAddress _listeningAddress;
  46. ESFSocketAddress _peerAddress;
  47. } AcceptData;
  48. /** Construct an uninitialized ESFTCPSocket.
  49. */
  50. ESFTCPSocket();
  51. /** Construct a new ESFTCPSocket.
  52. *
  53. * @param isBlocking true if this socket is a blocking socket.
  54. */
  55. ESFTCPSocket(bool isBlocking);
  56. /** Construct a new server ESFTCPSocket.
  57. *
  58. * @param acceptData An object created popupated by ESFListeningTCPSockets
  59. * when accepting a new connection.
  60. */
  61. ESFTCPSocket(AcceptData *acceptData);
  62. /** Destroy the socket. Will close the socket if it has not
  63. * already been closed.
  64. */
  65. virtual ~ESFTCPSocket();
  66. /** Reset a tcp socket. If the socket is currently open, this will close
  67. * it as a side-effect.
  68. *
  69. * @param acceptData An object created popupated by ESFListeningTCPSockets
  70. * when accepting a new connection.
  71. * @return ESF_SUCCESS if successful, another error code otherwise.
  72. */
  73. virtual ESFError reset(AcceptData *acceptData);
  74. /** Close the socket.
  75. */
  76. virtual void close();
  77. /** Close a socket descriptor
  78. *
  79. * @param socket The socket descriptor to close
  80. */
  81. static void Close(SOCKET socket);
  82. /** Determine whether or not this socket is a blocking socket.
  83. *
  84. * @return true if this socket is a blocking socket, false otherwise.
  85. */
  86. inline bool isBlocking() const {
  87. return _isBlocking;
  88. }
  89. /** Set the socket's blocking/non-blocking property.
  90. *
  91. * @param isBlocking Whether or not the socket is blocking.
  92. * @return ESF_SUCCESS if successful, another error code otherwise.
  93. */
  94. ESFError setBlocking(bool isBlocking);
  95. /** Set a socket descriptor's blocking/non-blocking property.
  96. *
  97. * @param isBlocking Whether or not the socket is blocking.
  98. * @return ESF_SUCCESS if successful, another error code otherwise.
  99. */
  100. static ESFError SetBlocking(SOCKET sockFd, bool isBlocking);
  101. /** Get the socket's socket descriptor.
  102. *
  103. * @return the socket descriptor
  104. */
  105. inline SOCKET getSocketDescriptor() const {
  106. return _sockFd;
  107. }
  108. /** Get and clear the last error on this socket.
  109. *
  110. * @return the last error that occurred on this socket or ESF_SUCCESS if
  111. * no error has occurred.
  112. */
  113. inline ESFError getLastError() {
  114. return GetLastError(_sockFd);
  115. }
  116. /** Get and clear the last error on a socket descriptor.
  117. *
  118. * @param socket The socked descriptor
  119. * @return the last error that occurred on the socket descriptor or ESF_SUCCESS if
  120. * no error has occurred.
  121. */
  122. static ESFError GetLastError(SOCKET socket);
  123. /** Placement new.
  124. *
  125. * @param size The size of the object.
  126. * @param allocator The source of the object's memory.
  127. * @return The new object or NULL of the memory allocation failed.
  128. */
  129. inline void *operator new(size_t size, ESFAllocator *allocator) {
  130. return allocator->allocate(size);
  131. }
  132. protected:
  133. bool _isBlocking;
  134. SOCKET _sockFd;
  135. private:
  136. // Disabled
  137. ESFTCPSocket(const ESFTCPSocket &socket);
  138. ESFTCPSocket &operator=(const ESFTCPSocket &);
  139. };
  140. #endif /* ! ESF_TCP_SOCKET_H */