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 theApp->unregisterSocketCallback(fd_);
81 if (::close(fd_) != 0)
82 lyxerr << "lyx: Server socket " << fd_
83 << " IO error on closing: " << strerror(errno);
85 support::unlink(address_);
86 lyxerr[Debug::LYXSERVER] << "lyx: Server socket quitting" << endl;
90 string const & LyXServerSocket::address() const
96 // Creates a new LyXDataSocket and checks to see if the connection
97 // is OK and if the number of clients does not exceed MAX_CLIENTS
98 void LyXServerSocket::serverCallback()
100 int const client_fd = support::socktools::accept(fd_);
103 lyxerr[Debug::LYXSERVER] << "lyx: Failed to accept new client"
108 if (clients.size() >= MAX_CLIENTS) {
109 writeln("BYE:Too many clients connected");
113 // Register the new client.
115 shared_ptr<LyXDataSocket>(new LyXDataSocket(client_fd));
116 theApp->registerSocketCallback(
118 boost::bind(&LyXServerSocket::dataCallback,
124 // Reads and processes input from client and check
125 // if the connection has been closed
126 void LyXServerSocket::dataCallback(int fd)
128 shared_ptr<LyXDataSocket> client = clients[fd];
131 string::size_type pos;
132 bool saidbye = false;
133 while ((!saidbye) && client->readln(line)) {
134 // The protocol must be programmed here
135 // Split the key and the data
136 if ((pos = line.find(':')) == string::npos) {
137 client->writeln("ERROR:" + line + ":malformed message");
141 string const key = line.substr(0, pos);
142 if (key == "LYXCMD") {
143 string const cmd = line.substr(pos + 1);
144 func->dispatch(lyxaction.lookupFunc(cmd));
145 string const rval = to_utf8(func->getMessage());
146 if (func->errorStat()) {
147 client->writeln("ERROR:" + cmd + ':' + rval);
149 client->writeln("INFO:" + cmd + ':' + rval);
151 } else if (key == "HELLO") {
152 // no use for client name!
153 client->writeln("HELLO:");
154 } else if (key == "BYE") {
157 client->writeln("ERROR:unknown key " + key);
161 if (saidbye || (!client->connected())) {
167 void LyXServerSocket::writeln(string const & line)
169 string const linen(line + '\n');
170 int const size = linen.size();
171 int const written = ::write(fd_, linen.c_str(), size);
172 if (written < size) { // Always mean end of connection.
173 if ((written == -1) && (errno == EPIPE)) {
174 // The program will also receive a SIGPIPE
175 // that must be caught
176 lyxerr << "lyx: Server socket " << fd_
177 << " connection closed while writing." << endl;
179 // Anything else, including errno == EAGAIN, must be
180 // considered IO error. EAGAIN should never happen
181 // when line is small
182 lyxerr << "lyx: Server socket " << fd_
183 << " IO error: " << strerror(errno);
189 // void LyXServerSocket::dump() const
191 // lyxerr << "LyXServerSocket debug dump.\n"
192 // << "fd = " << fd_ << ", address = " << address_ << ".\n"
193 // << "Clients: " << clients.size() << ".\n";
194 // std::map<int, shared_ptr<LyXDataSocket> >::const_iterator client = clients.begin();
195 // std::map<int, shared_ptr<LyXDataSocket> >::const_iterator end = clients.end();
196 // for (; client != end; ++client)
197 // lyxerr << "fd = " << client->first << '\n';
201 LyXDataSocket::LyXDataSocket(int fd)
202 : fd_(fd), connected_(true)
204 lyxerr[Debug::LYXSERVER] << "lyx: New data socket " << fd_ << endl;
208 LyXDataSocket::~LyXDataSocket()
210 if (::close(fd_) != 0)
211 lyxerr << "lyx: Data socket " << fd_
212 << " IO error on closing: " << strerror(errno);
214 theApp->unregisterSocketCallback(fd_);
215 lyxerr[Debug::LYXSERVER] << "lyx: Data socket " << fd_ << " quitting."
220 bool LyXDataSocket::connected() const
226 // Returns true if there was a complete line to input
227 bool LyXDataSocket::readln(string & line)
229 int const charbuf_size = 100;
230 char charbuf[charbuf_size]; // buffer for the ::read() system call
233 // read and store characters in buffer
234 while ((count = ::read(fd_, charbuf, charbuf_size - 1)) > 0) {
235 buffer_.append(charbuf, charbuf + count);
238 // Error conditions. The buffer must still be
239 // processed for lines read
240 if (count == 0) { // EOF -- connection closed
241 lyxerr[Debug::LYXSERVER] << "lyx: Data socket " << fd_
242 << ": connection closed." << endl;
244 } else if ((count == -1) && (errno != EAGAIN)) { // IO error
245 lyxerr << "lyx: Data socket " << fd_
246 << ": IO error." << endl;
250 // Cut a line from buffer
251 string::size_type pos = buffer_.find('\n');
252 if (pos == string::npos) {
253 lyxerr[Debug::LYXSERVER] << "lyx: Data socket " << fd_
254 << ": line not completed." << endl;
255 return false; // No complete line stored
257 line = buffer_.substr(0, pos);
258 buffer_.erase(0, pos + 1);
263 // Write a line of the form <key>:<value> to the socket
264 void LyXDataSocket::writeln(string const & line)
266 string const linen(line + '\n');
267 int const size = linen.size();
268 int const written = ::write(fd_, linen.c_str(), size);
269 if (written < size) { // Always mean end of connection.
270 if ((written == -1) && (errno == EPIPE)) {
271 // The program will also receive a SIGPIPE
272 // that must be catched
273 lyxerr << "lyx: Data socket " << fd_
274 << " connection closed while writing." << endl;
276 // Anything else, including errno == EAGAIN, must be
277 // considered IO error. EAGAIN should never happen
278 // when line is small
279 lyxerr << "lyx: Data socket " << fd_
280 << " IO error: " << strerror(errno);