FSocket.H 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. /* -*-C++-*-
  2. "$Id: FSocket.H,v 1.3 2000/05/16 16:58:12 jamespalmer Exp $"
  3. Copyright 1997 GARRET.
  4. Copyright 1999-2000 by the Flek development team.
  5. This library is free software; you can redistribute it and/or
  6. modify it under the terms of the GNU Library General Public
  7. License as published by the Free Software Foundation; either
  8. version 2 of the License, or (at your option) any later version.
  9. This library is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. Library General Public License for more details.
  13. You should have received a copy of the GNU Library General Public
  14. License along with this library; if not, write to the Free Software
  15. Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
  16. USA.
  17. Please report all bugs and problems to "[email protected]".
  18. */
  19. // FSocket was adapted from K.A. Knizhnik's very nice SAL library.
  20. #ifndef __FSOCKET_H__
  21. #define __FSOCKET_H__
  22. #include <time.h>
  23. #define DEFAULT_CONNECT_MAX_ATTEMPTS 100
  24. #define DEFAULT_RECONNECT_TIMEOUT 1 // seconds
  25. #define DEFAULT_LISTEN_QUEUE_SIZE 5
  26. #define LINGER_TIME 10 // seconds
  27. #define WAIT_FOREVER ((time_t)-1)
  28. #if defined(_WIN32) && !defined(__CYGWIN__)
  29. #include <windows.h>
  30. typedef HANDLE descriptor_t;
  31. #else
  32. typedef int descriptor_t;
  33. #endif
  34. /**
  35. * @package libflek_core
  36. * FSocket is an abstract socket interface.
  37. * FSocket was adapted from K.A. Knizhnik's very nice SAL library.
  38. * It provides a socket implementation based on the socket library
  39. * provided by the operating system. As local sockets are not supported
  40. * under Win32, shared memory and semaphore objects are used to provide
  41. * high speed communications on the same computer.
  42. */
  43. class FSocket {
  44. public:
  45. virtual int read(void* buf, size_t min_size, size_t max_size,
  46. time_t timeout = WAIT_FOREVER) = 0;
  47. /**
  48. * Read data from socket.
  49. * @param buf Buffer to hold fetched data.
  50. * @param buf_size Number of bytes to fetch.
  51. * @return 1 if operation successfully completed, 0 otherwise.
  52. */
  53. virtual int read(void* buf, size_t size) = 0;
  54. /**
  55. * Write data to socket.
  56. * @param buf Buffer that contains the data to be sent.
  57. * @param buf_size Number of bytes to send.
  58. * @return 1 if operation successfully completed, 0 otherwise.
  59. */
  60. virtual int write(void const* buf, size_t size) = 0;
  61. /**
  62. * Check the status of the last operation with socket.
  63. * @return 1 if the last operation completed successfully, 0 otherwise.
  64. */
  65. virtual int valid() = 0;
  66. /**
  67. * Get error message text for the last operation.
  68. * @param buf Buffer to receive text of the error message.
  69. * @param buf_size Size of buffer, no more than buf_size bytes will be
  70. * placed in the buffer.
  71. */
  72. virtual void get_error_text(char* buf, size_t buf_size) = 0;
  73. /**
  74. * Accept new socket. This method is called by server to establish a
  75. * connection with the new client. When the client executes a connect
  76. * method to the access server's accept port, accept method will create
  77. * new socket, which can be used for communication with the client.
  78. * The accept method will block the current task until some connection
  79. * is established.
  80. * @return Pointer to new socket or NULL if operation failed.
  81. */
  82. virtual FSocket* accept() = 0;
  83. /**
  84. * Cancel accept operation. Task blocked in accept call is woken
  85. * and execution continues.
  86. * @return 1 if socket was successfully closed, 0 otherwise.
  87. */
  88. virtual int cancel_accept() = 0;
  89. /**
  90. * Shutdown the socket. This function prohibits write and read
  91. * operation on the socket. All further attempts to read or write
  92. * data from/to the socket will be denied. But all previously
  93. * initiated operations are guaranteed to be completed.
  94. * @return 1 if operation successfully completed, 0 otherwise.
  95. */
  96. virtual int shutdown() = 0;
  97. /**
  98. * Close socket connection.
  99. * @return 1 if operation successfully completed, 0 otherwise.
  100. */
  101. virtual int close() = 0;
  102. enum socket_domain {
  103. sock_any_domain, // domain is chosen automatically
  104. sock_local_domain, // local domain (i.e. Unix domain socket)
  105. sock_global_domain // global domain (i.e. INET sockets)
  106. };
  107. /**
  108. * Establish connection with server. This method will do at most
  109. * max_attempts attempts to connect server, with timeout interval
  110. * between attempts.
  111. * @param address Address of server socket in format "hostname:port".
  112. * @param domain Type of connection. The following values of this
  113. * parameter are recognized:
  114. * <ul>
  115. * <li><b>sock_any_domain</b> = domain is chosen automatically.</li>
  116. * <li><b>sock_local_domain</b> = local domain (connection with one host).</li>
  117. * <li><b>sock_global_domain</b> = internet domain.</li>
  118. * </ul>
  119. * If sock_any_domain is specified, local connection is chosen when
  120. * either port was omitted in specification of the address or hostname
  121. * is "localhost", and global connection is used in all other cases.
  122. * @param max_attempts Maximal number of attempts to connect to server.
  123. * @param timeout Timeout in seconds between attempts to connect the server.
  124. * @return This method always create new socket object and returns a
  125. * pointer to it. If connection with server was not established, this
  126. * socket contains error code describing reason of failure. So returned
  127. * socket should be first checked by the valid() method.
  128. */
  129. static FSocket* connect(char const* address,
  130. socket_domain domain = sock_any_domain,
  131. int max_attempts = DEFAULT_CONNECT_MAX_ATTEMPTS,
  132. time_t timeout = DEFAULT_RECONNECT_TIMEOUT);
  133. /**
  134. * Create and open socket in local domain at the server site.
  135. * @param address Address to be assigned to the socket.
  136. * @param listen_queue_size Size of listen queue.
  137. * @return This method always create new socket object and returns
  138. * pointer to it. If socket can not be opened, error code field of
  139. * returned socket describes the reason of failure. So returned
  140. * socket should be first checked by the valid() method.
  141. */
  142. static FSocket* create_local(char const* address,
  143. int listen_queue_size =
  144. DEFAULT_LISTEN_QUEUE_SIZE);
  145. /**
  146. * Create and open socket in global (internet) domain at the server site.
  147. * @param address Address to be assigned to the socket.
  148. * @param listen_queue_size Size of listen queue.
  149. * @return This method always create new socket object and returns
  150. * pointer to it. If socket can not be opened, error code field of
  151. * returned socket describes the reason of failure. So returned
  152. * socket should be first checked by the valid() method.
  153. */
  154. static FSocket* create_global(char const* address,
  155. int listen_queue_size =
  156. DEFAULT_LISTEN_QUEUE_SIZE);
  157. virtual ~FSocket() {}
  158. FSocket() { state = ss_close; }
  159. protected:
  160. enum { ss_open, ss_shutdown, ss_close } state;
  161. };
  162. //
  163. // Return current host name + identifier of current process
  164. //
  165. extern char const* get_process_name();
  166. #endif