3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
6 * \author Lars Gullik Bjønnes
7 * \author Jean-Marc Lasgouttes
8 * \author Angus Leeming
10 * \author João Luis M. Assirati
12 * Full author contact details are available in file CREDITS.
17 #include "lyxsocket.h"
20 #include "funcrequest.h"
21 #include "LyXAction.h"
24 #include "frontends/Application.h"
26 #include "support/environment.h"
27 #include "support/lyxlib.h"
28 #include "support/socktools.h"
30 #include <boost/bind.hpp>
38 using boost::shared_ptr;
47 // Address is the unix address for the socket.
48 // MAX_CLIENTS is the maximum number of clients
49 // that can connect at the same time.
50 LyXServerSocket::LyXServerSocket(LyXFunc * f, string const & addr)
52 fd_(support::socktools::listen(addr, 3)),
56 lyxerr << "lyx: Disabling LyX socket." << endl;
60 // These env vars are used by DVI inverse search
62 support::setEnv("XEDITOR", "lyxclient -g %f %l");
63 // Needed by lyxclient
64 support::setEnv("LYXSOCKET", address_);
66 theApp->registerSocketCallback(
68 boost::bind(&LyXServerSocket::serverCallback, this)
71 lyxerr[Debug::LYXSERVER] << "lyx: New server socket "
72 << fd_ << ' ' << address_ << endl;
76 // Close the socket and remove the address of the filesystem.
77 LyXServerSocket::~LyXServerSocket()
80 BOOST_ASSERT (theApp);
81 theApp->unregisterSocketCallback(fd_);
82 if (::close(fd_) != 0)
83 lyxerr << "lyx: Server socket " << fd_
84 << " IO error on closing: " << strerror(errno);
86 support::unlink(address_);
87 lyxerr[Debug::LYXSERVER] << "lyx: Server socket quitting" << endl;
91 string const & LyXServerSocket::address() const
97 // Creates a new LyXDataSocket and checks to see if the connection
98 // is OK and if the number of clients does not exceed MAX_CLIENTS
99 void LyXServerSocket::serverCallback()
101 int const client_fd = support::socktools::accept(fd_);
104 lyxerr[Debug::LYXSERVER] << "lyx: Failed to accept new client"
109 if (clients.size() >= MAX_CLIENTS) {
110 writeln("BYE:Too many clients connected");
114 // Register the new client.
116 shared_ptr<LyXDataSocket>(new LyXDataSocket(client_fd));
117 theApp->registerSocketCallback(
119 boost::bind(&LyXServerSocket::dataCallback,
125 // Reads and processes input from client and check
126 // if the connection has been closed
127 void LyXServerSocket::dataCallback(int fd)
129 shared_ptr<LyXDataSocket> client = clients[fd];
132 string::size_type pos;
133 bool saidbye = false;
134 while ((!saidbye) && client->readln(line)) {
135 // The protocol must be programmed here
136 // Split the key and the data
137 if ((pos = line.find(':')) == string::npos) {
138 client->writeln("ERROR:" + line + ":malformed message");
142 string const key = line.substr(0, pos);
143 if (key == "LYXCMD") {
144 string const cmd = line.substr(pos + 1);
145 func->dispatch(lyxaction.lookupFunc(cmd));
146 string const rval = to_utf8(func->getMessage());
147 if (func->errorStat()) {
148 client->writeln("ERROR:" + cmd + ':' + rval);
150 client->writeln("INFO:" + cmd + ':' + rval);
152 } else if (key == "HELLO") {
153 // no use for client name!
154 client->writeln("HELLO:");
155 } else if (key == "BYE") {
158 client->writeln("ERROR:unknown key " + key);
162 if (saidbye || (!client->connected())) {
168 void LyXServerSocket::writeln(string const & line)
170 string const linen(line + '\n');
171 int const size = linen.size();
172 int const written = ::write(fd_, linen.c_str(), size);
173 if (written < size) { // Always mean end of connection.
174 if ((written == -1) && (errno == EPIPE)) {
175 // The program will also receive a SIGPIPE
176 // that must be caught
177 lyxerr << "lyx: Server socket " << fd_
178 << " connection closed while writing." << endl;
180 // Anything else, including errno == EAGAIN, must be
181 // considered IO error. EAGAIN should never happen
182 // when line is small
183 lyxerr << "lyx: Server socket " << fd_
184 << " IO error: " << strerror(errno);
190 // void LyXServerSocket::dump() const
192 // lyxerr << "LyXServerSocket debug dump.\n"
193 // << "fd = " << fd_ << ", address = " << address_ << ".\n"
194 // << "Clients: " << clients.size() << ".\n";
195 // std::map<int, shared_ptr<LyXDataSocket> >::const_iterator client = clients.begin();
196 // std::map<int, shared_ptr<LyXDataSocket> >::const_iterator end = clients.end();
197 // for (; client != end; ++client)
198 // lyxerr << "fd = " << client->first << '\n';
202 LyXDataSocket::LyXDataSocket(int fd)
203 : fd_(fd), connected_(true)
205 lyxerr[Debug::LYXSERVER] << "lyx: New data socket " << fd_ << endl;
209 LyXDataSocket::~LyXDataSocket()
211 if (::close(fd_) != 0)
212 lyxerr << "lyx: Data socket " << fd_
213 << " IO error on closing: " << strerror(errno);
215 theApp->unregisterSocketCallback(fd_);
216 lyxerr[Debug::LYXSERVER] << "lyx: Data socket " << fd_ << " quitting."
221 bool LyXDataSocket::connected() const
227 // Returns true if there was a complete line to input
228 bool LyXDataSocket::readln(string & line)
230 int const charbuf_size = 100;
231 char charbuf[charbuf_size]; // buffer for the ::read() system call
234 // read and store characters in buffer
235 while ((count = ::read(fd_, charbuf, charbuf_size - 1)) > 0) {
236 buffer_.append(charbuf, charbuf + count);
239 // Error conditions. The buffer must still be
240 // processed for lines read
241 if (count == 0) { // EOF -- connection closed
242 lyxerr[Debug::LYXSERVER] << "lyx: Data socket " << fd_
243 << ": connection closed." << endl;
245 } else if ((count == -1) && (errno != EAGAIN)) { // IO error
246 lyxerr << "lyx: Data socket " << fd_
247 << ": IO error." << endl;
251 // Cut a line from buffer
252 string::size_type pos = buffer_.find('\n');
253 if (pos == string::npos) {
254 lyxerr[Debug::LYXSERVER] << "lyx: Data socket " << fd_
255 << ": line not completed." << endl;
256 return false; // No complete line stored
258 line = buffer_.substr(0, pos);
259 buffer_.erase(0, pos + 1);
264 // Write a line of the form <key>:<value> to the socket
265 void LyXDataSocket::writeln(string const & line)
267 string const linen(line + '\n');
268 int const size = linen.size();
269 int const written = ::write(fd_, linen.c_str(), size);
270 if (written < size) { // Always mean end of connection.
271 if ((written == -1) && (errno == EPIPE)) {
272 // The program will also receive a SIGPIPE
273 // that must be catched
274 lyxerr << "lyx: Data socket " << fd_
275 << " connection closed while writing." << endl;
277 // Anything else, including errno == EAGAIN, must be
278 // considered IO error. EAGAIN should never happen
279 // when line is small
280 lyxerr << "lyx: Data socket " << fd_
281 << " IO error: " << strerror(errno);