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 #include "safeunistd.h"
00028
00029 #ifdef __WIN32__
00030 # include "safewinsock2.h"
00031
00032 # include <xapian/error.h>
00033
00047 struct WinsockInitializer {
00048 WinsockInitializer() {
00049 WSADATA wsadata;
00050 int wsaerror = WSAStartup(MAKEWORD(2,2), &wsadata);
00051
00052
00053
00054 if (wsaerror != 0) {
00055 throw Xapian::NetworkError("Failed to initialize winsock", wsaerror);
00056 }
00057 }
00058
00059 ~WinsockInitializer() {
00060 WSACleanup();
00061 }
00062 };
00063
00072 inline int socket_errno() {
00073 return -(int)WSAGetLastError();
00074 }
00075
00076
00077
00078 # define EADDRINUSE (-(WSAEADDRINUSE))
00079 # define ETIMEDOUT (-(WSAETIMEDOUT))
00080 # define EINPROGRESS (-(WSAEINPROGRESS))
00081
00082 #else
00083
00084 # define socket_errno() errno
00085 #endif
00086
00094 class RemoteConnection {
00096 void operator=(const RemoteConnection &);
00097
00099 RemoteConnection(const RemoteConnection &);
00100
00106 int fdin;
00107
00114 int fdout;
00115
00117 std::string buffer;
00118
00120 off_t chunked_data_left;
00121
00131 void read_at_least(size_t min_len, double end_time);
00132
00133 #ifdef __WIN32__
00134
00139 WSAOVERLAPPED overlapped;
00140
00145 DWORD calc_read_wait_msecs(double end_time);
00146 #endif
00147
00148 protected:
00153 std::string context;
00154
00155 public:
00157 RemoteConnection(int fdin_, int fdout_,
00158 const std::string & context_ = std::string());
00159
00161 ~RemoteConnection();
00162
00167 bool ready_to_read() const;
00168
00186 char sniff_next_message_type(double end_time);
00187
00198 char get_message(std::string &result, double end_time);
00199
00216 char get_message_chunked(double end_time);
00217
00237 bool get_message_chunk(std::string &result, size_t at_least,
00238 double end_time);
00239
00251 char receive_file(const std::string &file, double end_time);
00252
00262 void send_message(char type, const std::string & s, double end_time);
00263
00273 void send_file(char type, int fd, double end_time);
00274
00280 void do_close(bool wait);
00281 };
00282
00283 #endif // XAPIAN_INCLUDED_REMOTECONNECTION_H