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 {
45 /// Max number of (read) pipe instances
46 enum { MAX_PIPES = 10 };
49 enum { PIPE_BUFSIZE = 512 };
51 /// Pipe client time-out
52 enum { PIPE_TIMEOUT = 5000 };
64 char pipebuf[PIPE_BUFSIZE];
71 /** When we receive a message, we send it to a client.
72 This is one of the small things that would have been a lot
73 cleaner with a Signal/Slot thing.
75 typedef void (*ClientCallbackfct)(Server *, std::string const &);
77 /// Construct with pipe-basename and callback to receive messages
78 LyXComm(std::string const & pip, Server * cli, ClientCallbackfct ccb = 0);
81 ~LyXComm() { closeConnection(); }
83 /// clean up in emergency
84 void emergencyCleanup();
87 void send(std::string const &);
89 /// asynch ready-to-be-read notification
93 void read_ready(DWORD);
100 /// the filename of the in pipe
101 std::string const inPipeName() const;
103 /// the filename of the out pipe
104 std::string const outPipeName() const;
107 void openConnection();
110 void closeConnection();
114 int startPipe(std::string const &, bool);
117 void endPipe(int &, std::string const &, bool);
119 /// This is -1 if not open
122 /// This is -1 if not open
125 /// Start an overlapped connection
126 void startPipe(DWORD);
128 /// Reset an overlapped connection
129 void resetPipe(DWORD, bool close_handle = false);
131 /// Close event and pipe handles
132 void closeHandles(DWORD);
134 /// The filename of a (in or out) pipe instance
135 std::string const pipeName(DWORD) const;
138 PipeInst pipe_[MAX_PIPES + 1];
140 /// Pipe server control events
141 HANDLE event_[MAX_PIPES + 2];
144 std::string readbuf_[MAX_PIPES];
147 std::string writebuf_;
150 /// Are we up and running?
153 /// Base of pipename including path
154 std::string pipename_;
159 /// The client callback function
160 ClientCallbackfct clientcb_;
163 /// Catch pipe ready-to-be-read notification
164 bool event(QEvent *);
166 /// Check whether the pipe server must be stopped
167 BOOL checkStopServer();
169 /// Windows event for stopping the pipe server
172 /// Pipe server thread handle
173 HANDLE server_thread_;
182 // Hack! This should be changed in 0.13
184 // The lyx server should not take an argument "LyXFunc" but this is
185 // how it will be done for 0.12. In 0.13 we must write a non-gui
187 // IMO lyxserver is atypical, and for the moment the only one, non-gui
188 // bufferview. We just have to find a way to handle situations like if
189 // lyxserver is using a buffer that is being edited with a bufferview.
190 // With a common buffer list this is not a problem, maybe. (Alejandro)
192 Server(LyXFunc * f, std::string const & pip);
196 void notifyClient(std::string const &);
198 /// whilst crashing etc.
199 void emergencyCleanup() { pipes_.emergencyCleanup(); }
201 void callback(std::string const & msg);
204 /// Names and number of current clients
205 enum { MAX_CLIENTS = 10 };
207 std::string clients_[MAX_CLIENTS];
216 /// Implementation is in LyX.cpp
217 extern Server & theServer();