FSocket_Win32.H 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. /* -*-C++-*-
  2. "$Id: FSocket_Win32.H,v 1.2 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_WIN32_H__
  21. #define __FSOCKET_WIN32_H__
  22. #include <FLU/FSocket.H>
  23. class FSocket_Win32 : public FSocket {
  24. protected:
  25. SOCKET s;
  26. int errcode; // error code of last failed operation
  27. char* address; // host address
  28. enum error_codes {
  29. ok = 0,
  30. not_opened = -1,
  31. bad_address = -2,
  32. connection_failed = -3,
  33. broken_pipe = -4,
  34. invalid_access_mode = -5
  35. };
  36. public:
  37. int open(int listen_queue_size);
  38. int connect(int max_attempts, time_t timeout);
  39. int read(void* buf, size_t min_size, size_t max_size,time_t timeout);
  40. int read(void* buf, size_t size);
  41. int write(void const* buf, size_t size);
  42. int valid();
  43. int close();
  44. int shutdown();
  45. void get_error_text(char* buf, size_t buf_size);
  46. FSocket* accept();
  47. int cancel_accept();
  48. FSocket_Win32(const char* address);
  49. FSocket_Win32(SOCKET new_sock);
  50. ~FSocket_Win32();
  51. };
  52. #define SOCKET_BUF_SIZE (8*1024)
  53. #define ACCEPT_TIMEOUT (30*1000)
  54. class FSocket_Win32_Local : public FSocket {
  55. protected:
  56. enum error_codes {
  57. ok = 0,
  58. not_opened = -1,
  59. broken_pipe = -2,
  60. timeout_expired = -3
  61. };
  62. enum socket_signals {
  63. RD, // receive data
  64. RTR, // ready to receive
  65. TD, // transfer data
  66. RTT // ready to transfer
  67. };
  68. //------------------------------------------------------
  69. // Mapping between signals at opposite ends of socket:
  70. // TD ---> RD
  71. // RTR ---> RTT
  72. //------------------------------------------------------
  73. struct socket_buf {
  74. volatile int RcvWaitFlag;
  75. volatile int SndWaitFlag;
  76. volatile int DataEnd;
  77. volatile int DataBeg;
  78. char Data[SOCKET_BUF_SIZE - 4*sizeof(int)];
  79. };
  80. struct accept_data {
  81. HANDLE Signal[4];
  82. HANDLE BufHnd;
  83. };
  84. struct connect_data {
  85. HANDLE Mutex;
  86. int Pid;
  87. };
  88. socket_buf* RcvBuf;
  89. socket_buf* SndBuf;
  90. HANDLE Signal[4];
  91. HANDLE Mutex;
  92. HANDLE BufHnd;
  93. int Error;
  94. char* Name;
  95. public:
  96. int open(int listen_queue_size);
  97. int connect(int max_attempts, time_t timeout);
  98. int read(void* buf, size_t min_size, size_t max_size,time_t timeout);
  99. int read(void* buf, size_t size);
  100. int write(void const* buf, size_t size);
  101. int valid();
  102. int close();
  103. int shutdown();
  104. void get_error_text(char* buf, size_t buf_size);
  105. FSocket* accept();
  106. int cancel_accept();
  107. FSocket_Win32_Local(const char* address);
  108. FSocket_Win32_Local();
  109. ~FSocket_Win32_Local();
  110. };
  111. #endif