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.
15 #include "lyxsocket.h"
18 #include "funcrequest.h"
19 #include "LyXAction.h"
22 #include "frontends/lyx_gui.h"
24 #include "support/lyxlib.h"
25 #include "support/socktools.h"
36 // Address is the unix address for the socket.
37 // MAX_CLIENTS is the maximum number of clients
38 // that can connect at the same time.
39 LyXServerSocket::LyXServerSocket(LyXFunc * f, string const & addr)
41 fd_(lyx::support::socktools::listen(addr, MAX_CLIENTS)),
45 lyxerr << "lyx: Disabling LyX socket." << endl;
48 lyx_gui::set_serversocket_callback(this);
49 lyxerr[Debug::LYXSERVER] << "lyx: New server socket "
50 << fd_ << ' ' << address_ << endl;
54 // Close the socket and remove the address of the filesystem.
55 LyXServerSocket::~LyXServerSocket()
58 lyx::support::unlink(address_);
59 while (!clients.empty()) close(*clients.rbegin());
60 lyxerr[Debug::LYXSERVER] << "lyx: Server socket quitting" << endl;
64 int LyXServerSocket::fd() const
70 string const & LyXServerSocket::address() const
76 // Creates a new LyXDataSocket and checks to see if the connection
77 // is OK and if the number of clients does not exceed MAX_CLIENTS
78 void LyXServerSocket::serverCallback()
80 auto_ptr<LyXDataSocket> client(new LyXDataSocket(this));
81 if (client->connected()) {
82 if (clients.size() == MAX_CLIENTS) {
83 client->writeln("BYE:Too many clients connected");
85 lyx_gui::set_datasocket_callback(client.get());
86 clients.insert(client.release());
93 // Reads and processes input from client and check
94 // if the connection has been closed
95 void LyXServerSocket::dataCallback(LyXDataSocket * client)
98 string::size_type pos;
100 while ((!saidbye) && client->readln(line)) {
101 // The protocol must be programmed here
102 // Split the key and the data
103 if ((pos = line.find(':')) == string::npos) {
104 client->writeln("ERROR:" + line + ":malformed message");
108 string const key = line.substr(0, pos);
109 if (key == "LYXCMD") {
110 string const cmd = line.substr(pos + 1);
111 func->dispatch(lyxaction.lookupFunc(cmd));
112 string const rval = func->getMessage();
113 if (func->errorStat()) {
114 client->writeln("ERROR:" + cmd + ':' + rval);
116 client->writeln("INFO:" + cmd + ':' + rval);
118 } else if (key == "HELLO") {
119 // no use for client name!
120 client->writeln("HELLO:");
121 } else if (key == "BYE") {
124 client->writeln("ERROR:unknown key " + key);
128 if (saidbye || (!client->connected())) {
134 // Removes client callback and deletes client object
135 void LyXServerSocket::close(LyXDataSocket * client)
137 lyx_gui::remove_datasocket_callback(client);
138 clients.erase(client);
143 // void LyXServerSocket::dump() const
145 // lyxerr << "LyXServerSocket debug dump.\n"
146 // << "fd = " << fd_ << ", address = " << address_ << ".\n"
147 // << "Clients: " << clients.size() << ".\n";
148 // if (!clients.empty()) {
149 // std::set<LyXDataSocket *>::const_iterator client = clients.begin();
150 // std::set<LyXDataSocket *>::const_iterator end = clients.end();
151 // for (; client != end; ++client)
152 // lyxerr << "fd = " << (*client)->fd() << "\n";
157 LyXDataSocket::LyXDataSocket(LyXServerSocket * serv)
159 fd_(lyx::support::socktools::accept(serv->fd()))
164 lyxerr[Debug::LYXSERVER] << "lyx: New data socket " << fd_ << endl;
170 LyXDataSocket::~LyXDataSocket()
173 lyxerr[Debug::LYXSERVER] << "lyx: Data socket " << fd_ << " quitting." << endl;
177 LyXServerSocket * LyXDataSocket::server() const
183 int LyXDataSocket::fd() const
189 bool LyXDataSocket::connected() const
195 // Returns true if there was a complete line to input
196 bool LyXDataSocket::readln(string & line)
198 int const charbuf_size = 100;
199 char charbuf[charbuf_size]; // buffer for the ::read() system call
201 string::size_type pos;
203 // read and store characters in buffer
204 while ((count = ::read(fd_, charbuf, charbuf_size - 1)) > 0) {
205 charbuf[count] = '\0'; // turn it into a c string
209 // Error conditions. The buffer must still be
210 // processed for lines read
211 if (count == 0) { // EOF -- connection closed
212 lyxerr[Debug::LYXSERVER] << "lyx: Data socket " << fd_
213 << ": connection closed." << endl;
215 } else if ((count == -1) && (errno != EAGAIN)) { // IO error
216 lyxerr << "lyx: Data socket " << fd_
217 << ": IO error." << endl;
221 // Cut a line from buffer
222 if ((pos = buffer.find('\n')) == string::npos) {
223 lyxerr[Debug::LYXSERVER] << "lyx: Data socket " << fd_
224 << ": line not completed." << endl;
225 return false; // No complete line stored
227 line = buffer.substr(0, pos);
228 buffer = buffer.substr(pos + 1);
233 // Write a line of the form <key>:<value> to the socket
234 void LyXDataSocket::writeln(string const & line)
236 string linen(line + '\n');
237 int size = linen.size();
238 int written = ::write(fd_, linen.c_str(), size);
239 if (written < size) { // Allways mean end of connection.
240 if ((written == -1) && (errno == EPIPE)) {
241 // The program will also receive a SIGPIPE
242 // that must be catched
243 lyxerr << "lyx: Data socket " << fd_
244 << " connection closed while writing." << endl;
246 // Anything else, including errno == EAGAIN, must be
247 // considered IO error. EAGAIN should never happen
248 // when line is small
249 lyxerr << "lyx: Data socket " << fd_
250 << " IO error: " << strerror(errno);