4 * This file is part of LyX, the document processor.
5 * Licence details can be found in the file COPYING.
7 * \author Lars Gullik Bjønnes
8 * \author Jean-Marc Lasgouttes
9 * \author Enrico Forestieri
11 * Full author contact details are available in file CREDITS.
17 #include <boost/signals2/trackable.hpp>
33 /** This class managed the pipes used for communicating with clients.
34 Usage: Initialize with pipe-filename-base, client class to receive
35 messages, and callback-function that will be called with the messages.
36 When you want to send, use "send()".
37 This class encapsulates all the dirty communication and thus provides
38 a clean string interface.
41 class LyXComm : public boost::signals2::trackable {
43 class LyXComm : public QObject {
46 friend DWORD WINAPI pipeServerWrapper(void *);
49 /// Max number of clients
50 enum { MAX_CLIENTS = 10 };
53 /// Max number of pipe instances
54 enum { MAX_PIPES = 2 * MAX_CLIENTS };
57 enum { PIPE_BUFSIZE = 512 };
59 /// Pipe client time-out
60 enum { PIPE_TIMEOUT = 5000 };
74 char readbuf[PIPE_BUFSIZE];
81 /** When we receive a message, we send it to a client.
82 This is one of the small things that would have been a lot
83 cleaner with a Signal/Slot thing.
85 typedef void (*ClientCallbackfct)(Server *, std::string const &);
87 /// Construct with pipe-basename and callback to receive messages
88 LyXComm(std::string const & pip, Server * cli, ClientCallbackfct ccb = 0);
91 ~LyXComm() { closeConnection(); }
93 /// clean up in emergency
94 void emergencyCleanup();
97 void send(std::string const &);
99 /// asynch ready-to-be-read notification
103 void read_ready(DWORD);
106 /// Tell whether we asked another instance of LyX to open the files
107 bool deferredLoading() { return deferred_loading_; }
110 /// the filename of the in pipe
111 std::string const inPipeName() const;
113 /// the filename of the out pipe
114 std::string const outPipeName() const;
117 void openConnection();
120 void closeConnection();
122 /// Load files in another running instance of LyX
123 bool loadFilesInOtherInstance();
127 int startPipe(std::string const &, bool);
130 void endPipe(int &, std::string const &, bool);
132 /// This is -1 if not open
135 /// This is -1 if not open
138 /// The pipe server returns false when exiting due to an error
141 /// Start an overlapped connection
142 bool startPipe(DWORD);
144 /// Reset an overlapped connection
145 bool resetPipe(DWORD, bool close_handle = false);
147 /// Close event and pipe handles
150 /// Catch pipe ready-to-be-read notification
151 bool event(QEvent *);
153 /// Check whether the pipe server must be stopped
154 bool checkStopServer(DWORD timeout = 0);
156 /// The filename of a (in or out) pipe instance
157 std::string const pipeName(DWORD) const;
160 PipeInst pipe_[MAX_PIPES];
162 /// Pipe server control events
163 HANDLE event_[MAX_PIPES + 1];
168 /// Synchronize access to outbuf_
169 HANDLE outbuf_mutex_;
171 /// Windows event for stopping the pipe server
174 /// Pipe server thread handle
175 HANDLE server_thread_;
178 /// Are we up and running?
181 /// Base of pipename including path
182 std::string pipename_;
187 /// The client callback function
188 ClientCallbackfct clientcb_;
190 /// Did we defer loading of files to another instance?
191 bool deferred_loading_;
199 // Hack! This should be changed in 0.13
201 // IMO lyxserver is atypical, and for the moment the only one, non-gui
202 // bufferview. We just have to find a way to handle situations like if
203 // lyxserver is using a buffer that is being edited with a bufferview.
204 // With a common buffer list this is not a problem, maybe. (Alejandro)
206 Server(std::string const & pip);
210 void notifyClient(std::string const &);
212 bool deferredLoadingToOtherInstance() { return pipes_.deferredLoading(); }
214 /// whilst crashing etc.
215 void emergencyCleanup() { pipes_.emergencyCleanup(); }
217 void callback(std::string const & msg);
220 /// Names and number of current clients
222 enum { MAX_CLIENTS = 10 };
224 enum { MAX_CLIENTS = LyXComm::MAX_CLIENTS };
227 std::string clients_[MAX_CLIENTS];
234 /// Implementation is in LyX.cpp
235 Server & theServer();
237 /// Implementation is in LyX.cpp
238 extern std::vector<std::string> & theFilesToLoad();