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/signals/trackable.hpp>
31 /** This class managed the pipes used for communicating with clients.
32 Usage: Initialize with pipe-filename-base, client class to receive
33 messages, and callback-function that will be called with the messages.
34 When you want to send, use "send()".
35 This class encapsulates all the dirty communication and thus provides
36 a clean string interface.
39 class LyXComm : public boost::signals::trackable {
41 class LyXComm : public QObject {
44 friend DWORD WINAPI pipeServerWrapper(void *);
47 /// Max number of clients
48 enum { MAX_CLIENTS = 10 };
51 /// Max number of pipe instances
52 enum { MAX_PIPES = 2 * MAX_CLIENTS };
55 enum { PIPE_BUFSIZE = 512 };
57 /// Pipe client time-out
58 enum { PIPE_TIMEOUT = 5000 };
72 char readbuf[PIPE_BUFSIZE];
79 /** When we receive a message, we send it to a client.
80 This is one of the small things that would have been a lot
81 cleaner with a Signal/Slot thing.
83 typedef void (*ClientCallbackfct)(Server *, std::string const &);
85 /// Construct with pipe-basename and callback to receive messages
86 LyXComm(std::string const & pip, Server * cli, ClientCallbackfct ccb = 0);
89 ~LyXComm() { closeConnection(); }
91 /// clean up in emergency
92 void emergencyCleanup();
95 void send(std::string const &);
97 /// asynch ready-to-be-read notification
101 void read_ready(DWORD);
104 /// Tell whether we asked another instance of LyX to open the files
105 bool deferredLoading() { return deferred_loading_; }
108 /// the filename of the in pipe
109 std::string const inPipeName() const;
111 /// the filename of the out pipe
112 std::string const outPipeName() const;
115 void openConnection();
118 void closeConnection();
120 /// Load files in another running instance of LyX
121 bool loadFilesInOtherInstance();
125 int startPipe(std::string const &, bool);
128 void endPipe(int &, std::string const &, bool);
130 /// This is -1 if not open
133 /// This is -1 if not open
136 /// The pipe server returns false when exiting due to an error
139 /// Start an overlapped connection
140 bool startPipe(DWORD);
142 /// Reset an overlapped connection
143 bool resetPipe(DWORD, bool close_handle = false);
145 /// Close event and pipe handles
148 /// Catch pipe ready-to-be-read notification
149 bool event(QEvent *);
151 /// Check whether the pipe server must be stopped
152 bool checkStopServer(DWORD timeout = 0);
154 /// The filename of a (in or out) pipe instance
155 std::string const pipeName(DWORD) const;
158 PipeInst pipe_[MAX_PIPES];
160 /// Pipe server control events
161 HANDLE event_[MAX_PIPES + 1];
166 /// Synchronize access to outbuf_
167 HANDLE outbuf_mutex_;
169 /// Windows event for stopping the pipe server
172 /// Pipe server thread handle
173 HANDLE server_thread_;
176 /// Are we up and running?
179 /// Base of pipename including path
180 std::string pipename_;
185 /// The client callback function
186 ClientCallbackfct clientcb_;
188 /// Did we defer loading of files to another instance?
189 bool deferred_loading_;
197 // Hack! This should be changed in 0.13
199 // IMO lyxserver is atypical, and for the moment the only one, non-gui
200 // bufferview. We just have to find a way to handle situations like if
201 // lyxserver is using a buffer that is being edited with a bufferview.
202 // With a common buffer list this is not a problem, maybe. (Alejandro)
204 Server(std::string const & pip);
208 void notifyClient(std::string const &);
210 bool deferredLoadingToOtherInstance() { return pipes_.deferredLoading(); }
212 /// whilst crashing etc.
213 void emergencyCleanup() { pipes_.emergencyCleanup(); }
215 void callback(std::string const & msg);
218 /// Names and number of current clients
220 enum { MAX_CLIENTS = 10 };
222 enum { MAX_CLIENTS = LyXComm::MAX_CLIENTS };
225 std::string clients_[MAX_CLIENTS];
232 /// Implementation is in LyX.cpp
233 Server & theServer();
235 /// Implementation is in LyX.cpp
236 extern std::vector<std::string> & theFilesToLoad();