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/lassert.h"
30 #include "support/socktools.h"
32 #include <boost/assert.hpp>
43 using namespace lyx::support;
48 // Address is the unix address for the socket.
49 // MAX_CLIENTS is the maximum number of clients
50 // that can connect at the same time.
51 ServerSocket::ServerSocket(FileName const & addr)
52 : fd_(socktools::listen(addr, 3)),
56 LYXERR(Debug::LYXSERVER, "lyx: Disabling LyX socket.");
60 // These env vars are used by DVI inverse search
62 setEnv("XEDITOR", "lyxclient -g %f %l");
63 // Needed by lyxclient
64 setEnv("LYXSOCKET", address_.absFileName());
66 theApp()->registerSocketCallback(
68 bind(&ServerSocket::serverCallback, this)
71 LYXERR(Debug::LYXSERVER, "lyx: New server socket "
72 << fd_ << ' ' << address_.absFileName());
76 // Close the socket and remove the address of the filesystem.
77 ServerSocket::~ServerSocket()
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)
87 address_.removeFile();
88 LYXERR(Debug::LYXSERVER, "lyx: Server socket quitting");
92 string const ServerSocket::address() const
94 return address_.absFileName();
98 // Creates a new LyXDataSocket and checks to see if the connection
99 // is OK and if the number of clients does not exceed MAX_CLIENTS
100 void ServerSocket::serverCallback()
102 if (clients.size() >= MAX_CLIENTS) {
103 writeln("BYE:Too many clients connected");
107 int const client_fd = socktools::accept(fd_);
110 LYXERR(Debug::LYXSERVER, "lyx: Failed to accept new client");
114 // Register the new client.
115 clients[client_fd] = make_shared<LyXDataSocket>(client_fd);
116 theApp()->registerSocketCallback(
118 bind(&ServerSocket::dataCallback,
124 // Reads and processes input from client and check
125 // if the connection has been closed
126 void ServerSocket::dataCallback(int fd)
128 map<int, shared_ptr<LyXDataSocket> >::const_iterator it = clients.find(fd);
129 if (it == clients.end())
131 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
138 if ((pos = line.find(':')) == string::npos) {
139 client->writeln("ERROR:" + line + ":malformed message");
143 string const key = line.substr(0, pos);
144 if (key == "LYXCMD") {
145 string const cmd = line.substr(pos + 1);
146 FuncRequest fr(lyxaction.lookupFunc(cmd));
147 fr.setOrigin(FuncRequest::LYXSERVER);
149 theApp()->dispatch(fr, dr);
150 string const rval = to_utf8(dr.message());
152 client->writeln("ERROR:" + cmd + ':' + rval);
154 client->writeln("INFO:" + cmd + ':' + rval);
155 } else if (key == "HELLO") {
156 // no use for client name!
157 client->writeln("HELLO:");
158 } else if (key == "BYE") {
161 client->writeln("ERROR:unknown key " + key);
165 if (saidbye || !client->connected()) {
171 void ServerSocket::writeln(string const & line)
173 string const linen = line + '\n';
174 int const size = linen.size();
175 int const written = ::write(fd_, linen.c_str(), size);
176 if (written < size) { // Always mean end of connection.
177 if (written == -1 && errno == EPIPE) {
178 // The program will also receive a SIGPIPE
179 // that must be caught
180 lyxerr << "lyx: Server socket " << fd_
181 << " connection closed while writing." << endl;
183 // Anything else, including errno == EAGAIN, must be
184 // considered IO error. EAGAIN should never happen
185 // when line is small
186 lyxerr << "lyx: Server socket " << fd_
187 << " IO error: " << strerror(errno);
193 // void ServerSocket::dump() const
195 // lyxerr << "ServerSocket debug dump.\n"
196 // << "fd = " << fd_ << ", address = " << address_.absFileName() << ".\n"
197 // << "Clients: " << clients.size() << ".\n";
198 // map<int, shared_ptr<LyXDataSocket> >::const_iterator client = clients.begin();
199 // map<int, shared_ptr<LyXDataSocket> >::const_iterator end = clients.end();
200 // for (; client != end; ++client)
201 // lyxerr << "fd = " << client->first << '\n';
205 LyXDataSocket::LyXDataSocket(int fd)
206 : fd_(fd), connected_(true)
208 LYXERR(Debug::LYXSERVER, "lyx: New data socket " << fd_);
212 LyXDataSocket::~LyXDataSocket()
214 if (::close(fd_) != 0)
215 lyxerr << "lyx: Data socket " << fd_
216 << " IO error on closing: " << strerror(errno);
218 theApp()->unregisterSocketCallback(fd_);
219 LYXERR(Debug::LYXSERVER, "lyx: Data socket " << fd_ << " quitting.");
223 bool LyXDataSocket::connected() const
229 // Returns true if there was a complete line to input
230 bool LyXDataSocket::readln(string & line)
232 int const charbuf_size = 100;
233 char charbuf[charbuf_size]; // buffer for the ::read() system call
236 // read and store characters in buffer
237 while ((count = ::read(fd_, charbuf, charbuf_size - 1)) > 0) {
238 buffer_.append(charbuf, charbuf + count);
241 // Error conditions. The buffer must still be
242 // processed for lines read
243 if (count == 0) { // EOF -- connection closed
244 LYXERR(Debug::LYXSERVER, "lyx: Data socket " << fd_
245 << ": connection closed.");
247 } else if ((count == -1) && (errno != EAGAIN)) { // IO error
248 lyxerr << "lyx: Data socket " << fd_
249 << ": IO error." << endl;
253 // Cut a line from buffer
254 size_t pos = buffer_.find('\n');
255 if (pos == string::npos) {
256 LYXERR(Debug::LYXSERVER, "lyx: Data socket " << fd_
257 << ": line not completed.");
258 return false; // No complete line stored
260 line = buffer_.substr(0, pos);
261 buffer_.erase(0, pos + 1);
266 // Write a line of the form <key>:<value> to the socket
267 void LyXDataSocket::writeln(string const & line)
269 string const linen = line + '\n';
270 int const size = linen.size();
271 int const written = ::write(fd_, linen.c_str(), size);
272 if (written < size) { // Always mean end of connection.
273 if (written == -1 && errno == EPIPE) {
274 // The program will also receive a SIGPIPE
275 // that must be catched
276 lyxerr << "lyx: Data socket " << fd_
277 << " connection closed while writing." << endl;
279 // Anything else, including errno == EAGAIN, must be
280 // considered IO error. EAGAIN should never happen
281 // when line is small
282 lyxerr << "lyx: Data socket " << fd_
283 << " IO error: " << strerror(errno);