00001
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #ifndef XAPIAN_INCLUDED_REMOTECONNECTION_H
00022 #define XAPIAN_INCLUDED_REMOTECONNECTION_H
00023
00024 #include <string>
00025
00026 #include "remoteprotocol.h"
00027
00028 #ifdef __WIN32__
00029 # include "safewinsock2.h"
00030
00031 # include <xapian/error.h>
00032
00046 struct WinsockInitializer {
00047 WinsockInitializer() {
00048 WSADATA wsadata;
00049 int wsaerror = WSAStartup(MAKEWORD(2,2), &wsadata);
00050
00051
00052
00053 if (wsaerror != 0) {
00054 throw Xapian::NetworkError("Failed to initialize winsock", "", wsaerror);
00055 }
00056 }
00057
00058 ~WinsockInitializer() {
00059 WSACleanup();
00060 }
00061 };
00062
00071 inline int socket_errno() {
00072 return -(int)WSAGetLastError();
00073 }
00074
00075
00076
00077 # define EADDRINUSE (-(WSAEADDRINUSE))
00078 # define ETIMEDOUT (-(WSAETIMEDOUT))
00079 # define EINPROGRESS (-(WSAEINPROGRESS))
00080
00081 #else
00082
00083 # define socket_errno() errno
00084 #endif
00085
00086 class OmTime;
00087
00095 class RemoteConnection {
00097 void operator=(const RemoteConnection &);
00098
00100 RemoteConnection(const RemoteConnection &);
00101
00103 int fdin;
00104
00106 int fdout;
00107
00109 std::string buffer;
00110
00120 void read_at_least(size_t min_len, const OmTime & end_time);
00121
00122 #ifdef __WIN32__
00123
00128 WSAOVERLAPPED overlapped;
00129
00134 DWORD calc_read_wait_msecs(const OmTime & end_time);
00135 #endif
00136
00137 protected:
00142 std::string context;
00143
00144 public:
00146 RemoteConnection(int fdin_, int fdout_, const std::string & context_);
00148 ~RemoteConnection();
00149
00154 bool ready_to_read() const;
00155
00166 char get_message(std::string &result, const OmTime & end_time);
00167
00177 void send_message(char type, const std::string & s, const OmTime & end_time);
00178
00184 void do_close(bool wait);
00185 };
00186
00187 #endif // XAPIAN_INCLUDED_REMOTECONNECTION_H