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/lyx_gui.h"
26 #include "support/environment.h"
27 #include "support/lyxlib.h"
28 #include "support/socktools.h"
30 #include <boost/bind.hpp>
34 using boost::shared_ptr;
41 // Address is the unix address for the socket.
42 // MAX_CLIENTS is the maximum number of clients
43 // that can connect at the same time.
44 LyXServerSocket::LyXServerSocket(LyXFunc * f, string const & addr)
46 fd_(lyx::support::socktools::listen(addr, 3)),
50 lyxerr << "lyx: Disabling LyX socket." << endl;
54 // These env vars are used by DVI inverse search
56 lyx::support::setEnv("XEDITOR", "lyxclient -g %f %l");
57 // Needed by lyxclient
58 lyx::support::setEnv("LYXSOCKET", address_);
60 lyx_gui::register_socket_callback(
62 boost::bind(&LyXServerSocket::serverCallback, this)
65 lyxerr[Debug::LYXSERVER] << "lyx: New server socket "
66 << fd_ << ' ' << address_ << endl;
70 // Close the socket and remove the address of the filesystem.
71 LyXServerSocket::~LyXServerSocket()
73 lyx_gui::unregister_socket_callback(fd_);
75 lyx::support::unlink(address_);
76 lyxerr[Debug::LYXSERVER] << "lyx: Server socket quitting" << endl;
80 string const & LyXServerSocket::address() const
86 // Creates a new LyXDataSocket and checks to see if the connection
87 // is OK and if the number of clients does not exceed MAX_CLIENTS
88 void LyXServerSocket::serverCallback()
90 int const client_fd = lyx::support::socktools::accept(fd_);
93 lyxerr[Debug::LYXSERVER] << "lyx: Failed to accept new client"
98 if (clients.size() >= MAX_CLIENTS) {
99 writeln("BYE:Too many clients connected");
103 // Register the new client.
105 shared_ptr<LyXDataSocket>(new LyXDataSocket(client_fd));
106 lyx_gui::register_socket_callback(
108 boost::bind(&LyXServerSocket::dataCallback,
114 // Reads and processes input from client and check
115 // if the connection has been closed
116 void LyXServerSocket::dataCallback(int fd)
118 shared_ptr<LyXDataSocket> client = clients[fd];
121 string::size_type pos;
122 bool saidbye = false;
123 while ((!saidbye) && client->readln(line)) {
124 // The protocol must be programmed here
125 // Split the key and the data
126 if ((pos = line.find(':')) == string::npos) {
127 client->writeln("ERROR:" + line + ":malformed message");
131 string const key = line.substr(0, pos);
132 if (key == "LYXCMD") {
133 string const cmd = line.substr(pos + 1);
134 func->dispatch(lyxaction.lookupFunc(cmd));
135 string const rval = func->getMessage();
136 if (func->errorStat()) {
137 client->writeln("ERROR:" + cmd + ':' + rval);
139 client->writeln("INFO:" + cmd + ':' + rval);
141 } else if (key == "HELLO") {
142 // no use for client name!
143 client->writeln("HELLO:");
144 } else if (key == "BYE") {
147 client->writeln("ERROR:unknown key " + key);
151 if (saidbye || (!client->connected())) {
157 void LyXServerSocket::writeln(string const & line)
159 string const linen(line + '\n');
160 int const size = linen.size();
161 int const written = ::write(fd_, linen.c_str(), size);
162 if (written < size) { // Allways mean end of connection.
163 if ((written == -1) && (errno == EPIPE)) {
164 // The program will also receive a SIGPIPE
165 // that must be caught
166 lyxerr << "lyx: Server socket " << fd_
167 << " connection closed while writing." << endl;
169 // Anything else, including errno == EAGAIN, must be
170 // considered IO error. EAGAIN should never happen
171 // when line is small
172 lyxerr << "lyx: Server socket " << fd_
173 << " IO error: " << strerror(errno);
179 // void LyXServerSocket::dump() const
181 // lyxerr << "LyXServerSocket debug dump.\n"
182 // << "fd = " << fd_ << ", address = " << address_ << ".\n"
183 // << "Clients: " << clients.size() << ".\n";
184 // std::map<int, shared_ptr<LyXDataSocket> >::const_iterator client = clients.begin();
185 // std::map<int, shared_ptr<LyXDataSocket> >::const_iterator end = clients.end();
186 // for (; client != end; ++client)
187 // lyxerr << "fd = " << client->first << '\n';
191 LyXDataSocket::LyXDataSocket(int fd)
192 : fd_(fd), connected_(true)
194 lyxerr[Debug::LYXSERVER] << "lyx: New data socket " << fd_ << endl;
198 LyXDataSocket::~LyXDataSocket()
202 lyx_gui::unregister_socket_callback(fd_);
203 lyxerr[Debug::LYXSERVER] << "lyx: Data socket " << fd_ << " quitting."
208 bool LyXDataSocket::connected() const
214 // Returns true if there was a complete line to input
215 bool LyXDataSocket::readln(string & line)
217 int const charbuf_size = 100;
218 char charbuf[charbuf_size]; // buffer for the ::read() system call
221 // read and store characters in buffer
222 while ((count = ::read(fd_, charbuf, charbuf_size - 1)) > 0) {
223 buffer_.append(charbuf, charbuf + count);
226 // Error conditions. The buffer must still be
227 // processed for lines read
228 if (count == 0) { // EOF -- connection closed
229 lyxerr[Debug::LYXSERVER] << "lyx: Data socket " << fd_
230 << ": connection closed." << endl;
232 } else if ((count == -1) && (errno != EAGAIN)) { // IO error
233 lyxerr << "lyx: Data socket " << fd_
234 << ": IO error." << endl;
238 // Cut a line from buffer
239 string::size_type pos = buffer_.find('\n');
240 if (pos == string::npos) {
241 lyxerr[Debug::LYXSERVER] << "lyx: Data socket " << fd_
242 << ": line not completed." << endl;
243 return false; // No complete line stored
245 line = buffer_.substr(0, pos);
246 buffer_.erase(0, pos + 1);
251 // Write a line of the form <key>:<value> to the socket
252 void LyXDataSocket::writeln(string const & line)
254 string const linen(line + '\n');
255 int const size = linen.size();
256 int const written = ::write(fd_, linen.c_str(), size);
257 if (written < size) { // Allways mean end of connection.
258 if ((written == -1) && (errno == EPIPE)) {
259 // The program will also receive a SIGPIPE
260 // that must be catched
261 lyxerr << "lyx: Data socket " << fd_
262 << " connection closed while writing." << endl;
264 // Anything else, including errno == EAGAIN, must be
265 // considered IO error. EAGAIN should never happen
266 // when line is small
267 lyxerr << "lyx: Data socket " << fd_
268 << " IO error: " << strerror(errno);