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>
34 /** This class managed the pipes used for communicating with clients.
35 Usage: Initialize with pipe-filename-base, client class to receive
36 messages, and callback-function that will be called with the messages.
37 When you want to send, use "send()".
38 This class encapsulates all the dirty communication and thus provides
39 a clean string interface.
42 class LyXComm : public boost::signals2::trackable {
44 class LyXComm : public QObject {
47 friend DWORD WINAPI pipeServerWrapper(void *);
50 /// Max number of clients
51 enum { MAX_CLIENTS = 10 };
54 /// Max number of pipe instances
55 enum { MAX_PIPES = 2 * MAX_CLIENTS };
58 enum { PIPE_BUFSIZE = 512 };
60 /// Pipe client time-out
61 enum { PIPE_TIMEOUT = 5000 };
75 char readbuf[PIPE_BUFSIZE];
82 /** When we receive a message, we send it to a client.
83 This is one of the small things that would have been a lot
84 cleaner with a Signal/Slot thing.
86 typedef void (*ClientCallbackfct)(Server *, std::string const &);
88 /// Construct with pipe-basename and callback to receive messages
89 LyXComm(std::string const & pip, Server * cli, ClientCallbackfct ccb = 0);
92 ~LyXComm() { closeConnection(); }
94 /// clean up in emergency
95 void emergencyCleanup();
98 void send(std::string const &);
100 /// asynch ready-to-be-read notification
104 void read_ready(DWORD);
107 /// Tell whether we asked another instance of LyX to open the files
108 bool deferredLoading() { return deferred_loading_; }
111 /// the filename of the in pipe
112 std::string const inPipeName() const;
114 /// the filename of the out pipe
115 std::string const outPipeName() const;
118 void openConnection();
121 void closeConnection();
123 /// Load files in another running instance of LyX
124 bool loadFilesInOtherInstance();
128 int startPipe(std::string const &, bool);
131 void endPipe(int &, std::string const &, bool);
133 /// This is -1 if not open
136 /// This is -1 if not open
139 /// The pipe server returns false when exiting due to an error
142 /// Start an overlapped connection
143 bool startPipe(DWORD);
145 /// Reset an overlapped connection
146 bool resetPipe(DWORD, bool close_handle = false);
148 /// Close event and pipe handles
151 /// Catch pipe ready-to-be-read notification
152 bool event(QEvent *);
154 /// Check whether the pipe server must be stopped
155 bool checkStopServer(DWORD timeout = 0);
157 /// The filename of a (in or out) pipe instance
158 std::string const pipeName(DWORD) const;
161 PipeInst pipe_[MAX_PIPES];
163 /// Pipe server control events
164 HANDLE event_[MAX_PIPES + 1];
169 /// Synchronize access to outbuf_
170 HANDLE outbuf_mutex_;
172 /// Windows event for stopping the pipe server
175 /// Pipe server thread handle
176 HANDLE server_thread_;
179 /// Are we up and running?
182 /// Base of pipename including path
183 std::string pipename_;
188 /// The client callback function
189 ClientCallbackfct clientcb_;
191 /// Did we defer loading of files to another instance?
192 bool deferred_loading_;
200 // Hack! This should be changed in 0.13
202 // IMO lyxserver is atypical, and for the moment the only one, non-gui
203 // bufferview. We just have to find a way to handle situations like if
204 // lyxserver is using a buffer that is being edited with a bufferview.
205 // With a common buffer list this is not a problem, maybe. (Alejandro)
207 Server(std::string const & pip);
211 void notifyClient(std::string const &);
213 bool deferredLoadingToOtherInstance() { return pipes_.deferredLoading(); }
215 /// whilst crashing etc.
216 void emergencyCleanup() { pipes_.emergencyCleanup(); }
218 void callback(std::string const & msg);
221 /// Names and number of current clients
223 enum { MAX_CLIENTS = 10 };
225 enum { MAX_CLIENTS = LyXComm::MAX_CLIENTS };
228 std::string clients_[MAX_CLIENTS];
235 /// Implementation is in LyX.cpp
236 Server & theServer();
238 /// Implementation is in LyX.cpp
239 extern std::vector<std::string> & theFilesToLoad();