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>
32 /** This class managed the pipes used for communicating with clients.
33 Usage: Initialize with pipe-filename-base, client class to receive
34 messages, and callback-function that will be called with the messages.
35 When you want to send, use "send()".
36 This class encapsulates all the dirty communication and thus provides
37 a clean string interface.
40 class LyXComm : public boost::signals::trackable {
42 class LyXComm : public QObject {
46 /// Max number of clients
47 enum { MAX_CLIENTS = 10 };
50 /// Max number of pipe instances
51 enum { MAX_PIPES = 2 * MAX_CLIENTS };
54 enum { PIPE_BUFSIZE = 512 };
56 /// Pipe client time-out
57 enum { PIPE_TIMEOUT = 5000 };
71 char readbuf[PIPE_BUFSIZE];
78 /** When we receive a message, we send it to a client.
79 This is one of the small things that would have been a lot
80 cleaner with a Signal/Slot thing.
82 typedef void (*ClientCallbackfct)(Server *, std::string const &);
84 /// Construct with pipe-basename and callback to receive messages
85 LyXComm(std::string const & pip, Server * cli, ClientCallbackfct ccb = 0);
88 ~LyXComm() { closeConnection(); }
90 /// clean up in emergency
91 void emergencyCleanup();
94 void send(std::string const &);
96 /// asynch ready-to-be-read notification
100 void read_ready(DWORD);
107 /// the filename of the in pipe
108 std::string const inPipeName() const;
110 /// the filename of the out pipe
111 std::string const outPipeName() const;
114 void openConnection();
117 void closeConnection();
121 int startPipe(std::string const &, bool);
124 void endPipe(int &, std::string const &, bool);
126 /// This is -1 if not open
129 /// This is -1 if not open
132 /// Start an overlapped connection
133 void startPipe(DWORD);
135 /// Reset an overlapped connection
136 void resetPipe(DWORD, bool close_handle = false);
138 /// Close event and pipe handles
139 void closeHandles(DWORD);
141 /// Catch pipe ready-to-be-read notification
142 bool event(QEvent *);
144 /// Check whether the pipe server must be stopped
145 BOOL checkStopServer();
147 /// The filename of a (in or out) pipe instance
148 std::string const pipeName(DWORD) const;
151 PipeInst pipe_[MAX_PIPES];
153 /// Pipe server control events
154 HANDLE event_[MAX_PIPES + 1];
159 /// Synchronize access to outbuf_
160 HANDLE outbuf_mutex_;
162 /// Windows event for stopping the pipe server
165 /// Pipe server thread handle
166 HANDLE server_thread_;
169 /// Are we up and running?
172 /// Base of pipename including path
173 std::string pipename_;
178 /// The client callback function
179 ClientCallbackfct clientcb_;
187 // Hack! This should be changed in 0.13
189 // The lyx server should not take an argument "LyXFunc" but this is
190 // how it will be done for 0.12. In 0.13 we must write a non-gui
192 // IMO lyxserver is atypical, and for the moment the only one, non-gui
193 // bufferview. We just have to find a way to handle situations like if
194 // lyxserver is using a buffer that is being edited with a bufferview.
195 // With a common buffer list this is not a problem, maybe. (Alejandro)
197 Server(LyXFunc * f, std::string const & pip);
201 void notifyClient(std::string const &);
203 /// whilst crashing etc.
204 void emergencyCleanup() { pipes_.emergencyCleanup(); }
206 void callback(std::string const & msg);
209 /// Names and number of current clients
211 enum { MAX_CLIENTS = 10 };
213 enum { MAX_CLIENTS = LyXComm::MAX_CLIENTS };
216 std::string clients_[MAX_CLIENTS];
225 /// Implementation is in LyX.cpp
226 extern Server & theServer();