2 * \file ServerSocket.cpp
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 "ServerSocket.h"
19 #include "DispatchResult.h"
20 #include "FuncRequest.h"
22 #include "LyXAction.h"
24 #include "frontends/Application.h"
26 #include "support/debug.h"
27 #include "support/environment.h"
28 #include "support/FileName.h"
29 #include "support/socktools.h"
31 #include "support/bind.h"
41 using namespace lyx::support;
46 // Address is the unix address for the socket.
47 // MAX_CLIENTS is the maximum number of clients
48 // that can connect at the same time.
49 ServerSocket::ServerSocket(FileName const & addr)
50 : fd_(socktools::listen(addr, 3)),
54 LYXERR(Debug::LYXSERVER, "lyx: Disabling LyX socket.");
58 // These env vars are used by DVI inverse search
60 setEnv("XEDITOR", "lyxclient -g %f %l");
61 // Needed by lyxclient
62 setEnv("LYXSOCKET", address_.absFileName());
64 theApp()->registerSocketCallback(
66 bind(&ServerSocket::serverCallback, this)
69 LYXERR(Debug::LYXSERVER, "lyx: New server socket "
70 << fd_ << ' ' << address_.absFileName());
74 // Close the socket and remove the address of the filesystem.
75 ServerSocket::~ServerSocket()
78 BOOST_ASSERT (theApp());
79 theApp()->unregisterSocketCallback(fd_);
80 if (::close(fd_) != 0)
81 lyxerr << "lyx: Server socket " << fd_
82 << " IO error on closing: " << strerror(errno)
85 address_.removeFile();
86 LYXERR(Debug::LYXSERVER, "lyx: Server socket quitting");
90 string const ServerSocket::address() const
92 return address_.absFileName();
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 ServerSocket::serverCallback()
100 int const client_fd = socktools::accept(fd_);
103 LYXERR(Debug::LYXSERVER, "lyx: Failed to accept new client");
107 if (clients.size() >= MAX_CLIENTS) {
108 writeln("BYE:Too many clients connected");
112 // Register the new client.
114 shared_ptr<LyXDataSocket>(new LyXDataSocket(client_fd));
115 theApp()->registerSocketCallback(
117 bind(&ServerSocket::dataCallback,
123 // Reads and processes input from client and check
124 // if the connection has been closed
125 void ServerSocket::dataCallback(int fd)
127 map<int, shared_ptr<LyXDataSocket> >::const_iterator it = clients.find(fd);
128 if (it == clients.end())
130 shared_ptr<LyXDataSocket> client = it->second;
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 FuncRequest fr(lyxaction.lookupFunc(cmd));
146 fr.setOrigin(FuncRequest::LYXSERVER);
148 theApp()->dispatch(fr, dr);
149 string const rval = to_utf8(dr.message());
151 client->writeln("ERROR:" + cmd + ':' + rval);
153 client->writeln("INFO:" + cmd + ':' + rval);
154 } else if (key == "HELLO") {
155 // no use for client name!
156 client->writeln("HELLO:");
157 } else if (key == "BYE") {
160 client->writeln("ERROR:unknown key " + key);
164 if (saidbye || !client->connected()) {
170 void ServerSocket::writeln(string const & line)
172 string const linen = line + '\n';
173 int const size = linen.size();
174 int const written = ::write(fd_, linen.c_str(), size);
175 if (written < size) { // Always mean end of connection.
176 if (written == -1 && errno == EPIPE) {
177 // The program will also receive a SIGPIPE
178 // that must be caught
179 lyxerr << "lyx: Server socket " << fd_
180 << " connection closed while writing." << endl;
182 // Anything else, including errno == EAGAIN, must be
183 // considered IO error. EAGAIN should never happen
184 // when line is small
185 lyxerr << "lyx: Server socket " << fd_
186 << " IO error: " << strerror(errno);
192 // void ServerSocket::dump() const
194 // lyxerr << "ServerSocket debug dump.\n"
195 // << "fd = " << fd_ << ", address = " << address_.absFileName() << ".\n"
196 // << "Clients: " << clients.size() << ".\n";
197 // map<int, shared_ptr<LyXDataSocket> >::const_iterator client = clients.begin();
198 // map<int, shared_ptr<LyXDataSocket> >::const_iterator end = clients.end();
199 // for (; client != end; ++client)
200 // lyxerr << "fd = " << client->first << '\n';
204 LyXDataSocket::LyXDataSocket(int fd)
205 : fd_(fd), connected_(true)
207 LYXERR(Debug::LYXSERVER, "lyx: New data socket " << fd_);
211 LyXDataSocket::~LyXDataSocket()
213 if (::close(fd_) != 0)
214 lyxerr << "lyx: Data socket " << fd_
215 << " IO error on closing: " << strerror(errno);
217 theApp()->unregisterSocketCallback(fd_);
218 LYXERR(Debug::LYXSERVER, "lyx: Data socket " << fd_ << " quitting.");
222 bool LyXDataSocket::connected() const
228 // Returns true if there was a complete line to input
229 bool LyXDataSocket::readln(string & line)
231 int const charbuf_size = 100;
232 char charbuf[charbuf_size]; // buffer for the ::read() system call
235 // read and store characters in buffer
236 while ((count = ::read(fd_, charbuf, charbuf_size - 1)) > 0) {
237 buffer_.append(charbuf, charbuf + count);
240 // Error conditions. The buffer must still be
241 // processed for lines read
242 if (count == 0) { // EOF -- connection closed
243 LYXERR(Debug::LYXSERVER, "lyx: Data socket " << fd_
244 << ": connection closed.");
246 } else if ((count == -1) && (errno != EAGAIN)) { // IO error
247 lyxerr << "lyx: Data socket " << fd_
248 << ": IO error." << endl;
252 // Cut a line from buffer
253 size_t pos = buffer_.find('\n');
254 if (pos == string::npos) {
255 LYXERR(Debug::LYXSERVER, "lyx: Data socket " << fd_
256 << ": line not completed.");
257 return false; // No complete line stored
259 line = buffer_.substr(0, pos);
260 buffer_.erase(0, pos + 1);
265 // Write a line of the form <key>:<value> to the socket
266 void LyXDataSocket::writeln(string const & line)
268 string const linen = line + '\n';
269 int const size = linen.size();
270 int const written = ::write(fd_, linen.c_str(), size);
271 if (written < size) { // Always mean end of connection.
272 if (written == -1 && errno == EPIPE) {
273 // The program will also receive a SIGPIPE
274 // that must be catched
275 lyxerr << "lyx: Data socket " << fd_
276 << " connection closed while writing." << endl;
278 // Anything else, including errno == EAGAIN, must be
279 // considered IO error. EAGAIN should never happen
280 // when line is small
281 lyxerr << "lyx: Data socket " << fd_
282 << " IO error: " << strerror(errno);