common/remoteconnection.h

Go to the documentation of this file.
00001 
00004 /* Copyright (C) 2006,2007 Olly Betts
00005  *
00006  * This program is free software; you can redistribute it and/or modify
00007  * it under the terms of the GNU General Public License as published by
00008  * the Free Software Foundation; either version 2 of the License, or
00009  * (at your option) any later version.
00010  *
00011  * This program is distributed in the hope that it will be useful,
00012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014  * GNU General Public License for more details.
00015  *
00016  * You should have received a copy of the GNU General Public License
00017  * along with this program; if not, write to the Free Software
00018  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
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         // FIXME - should we check the returned information in wsadata to check
00051         // that we have a version of winsock which is recent enough for us?
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 // Define some of the UNIX socket error constants to be negated versions of the
00076 // winsock ones.
00077 # define EADDRINUSE (-(WSAEADDRINUSE))
00078 # define ETIMEDOUT (-(WSAETIMEDOUT))
00079 # define EINPROGRESS (-(WSAEINPROGRESS))
00080 
00081 #else
00082 // Use a macro so we don't need to pull safeerrno.h in here.
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

Documentation for Xapian (version 1.0.10).
Generated on 24 Dec 2008 by Doxygen 1.5.2.