]> git.lyx.org Git - lyx.git/blob - src/lyxsocket.C
the exception safety patch
[lyx.git] / src / lyxsocket.C
1 /**
2  * \file lyxsocket.C
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Lars Gullik Bjønnes
7  * \author Jean-Marc Lasgouttes
8  * \author Angus Leeming
9  * \author John Levon
10  * \author João Luis M. Assirati
11  *
12  * Full author contact details are available in file CREDITS.
13  */
14
15 #include "lyxsocket.h"
16
17 #include "debug.h"
18 #include "funcrequest.h"
19 #include "LyXAction.h"
20 #include "lyxfunc.h"
21
22 #include "frontends/lyx_gui.h"
23
24 #include "support/lyxlib.h"
25 #include "support/socktools.h"
26
27 #include <iostream>
28 #include <cerrno>
29
30
31 using std::auto_ptr;
32 using std::endl;
33 using std::string;
34
35
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)
40         : func(f),
41           fd_(lyx::support::socktools::listen(addr, MAX_CLIENTS)),
42           address_(addr)
43 {
44         if (fd_ == -1) {
45                 lyxerr << "lyx: Disabling LyX socket." << endl;
46                 return;
47         }
48         lyx_gui::set_serversocket_callback(this);
49         lyxerr[Debug::LYXSERVER] << "lyx: New server socket "
50                                  << fd_ << ' ' << address_ << endl;
51 }
52
53
54 // Close the socket and remove the address of the filesystem.
55 LyXServerSocket::~LyXServerSocket()
56 {
57         ::close(fd_);
58         lyx::support::unlink(address_);
59         while (!clients.empty()) close(*clients.rbegin());
60         lyxerr[Debug::LYXSERVER] << "lyx: Server socket quitting" << endl;
61 }
62
63
64 int LyXServerSocket::fd() const
65 {
66         return fd_;
67 }
68
69
70 string const & LyXServerSocket::address() const
71 {
72         return address_;
73 }
74
75
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()
79 {
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");
84                 } else {
85                         lyx_gui::set_datasocket_callback(client.get());
86                         clients.insert(client.release());
87                         return;
88                 }
89         }
90 }
91
92
93 // Reads and processes input from client and check
94 // if the connection has been closed
95 void LyXServerSocket::dataCallback(LyXDataSocket * client)
96 {
97         string line;
98         string::size_type pos;
99         bool saidbye = false;
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");
105                         continue;
106                 }
107
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);
115                         } else {
116                                 client->writeln("INFO:" + cmd + ':' + rval);
117                         }
118                 } else if (key == "HELLO") {
119                         // no use for client name!
120                         client->writeln("HELLO:");
121                 } else if (key == "BYE") {
122                         saidbye = true;
123                 } else {
124                         client->writeln("ERROR:unknown key " + key);
125                 }
126         }
127
128         if (saidbye || (!client->connected())) {
129                 close(client);
130         }
131 }
132
133
134 // Removes client callback and deletes client object
135 void LyXServerSocket::close(LyXDataSocket * client)
136 {
137         lyx_gui::remove_datasocket_callback(client);
138         clients.erase(client);
139         delete client;
140 }
141
142 // Debug
143 // void LyXServerSocket::dump() const
144 // {
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";
153 //      }
154 // }
155
156
157 LyXDataSocket::LyXDataSocket(LyXServerSocket * serv)
158         :server_(serv),
159          fd_(lyx::support::socktools::accept(serv->fd()))
160 {
161         if (fd_ == -1) {
162                 connected_ = false;
163         } else {
164                 lyxerr[Debug::LYXSERVER] << "lyx: New data socket " << fd_ << endl;
165                 connected_ = true;
166         }
167 }
168
169
170 LyXDataSocket::~LyXDataSocket()
171 {
172         ::close(fd_);
173         lyxerr[Debug::LYXSERVER] << "lyx: Data socket " << fd_ << " quitting." << endl;
174 }
175
176
177 LyXServerSocket * LyXDataSocket::server() const
178 {
179         return server_;
180 }
181
182
183 int LyXDataSocket::fd() const
184 {
185         return fd_;
186 }
187
188
189 bool LyXDataSocket::connected() const
190 {
191         return connected_;
192 }
193
194
195 // Returns true if there was a complete line to input
196 bool LyXDataSocket::readln(string & line)
197 {
198         int const charbuf_size = 100;
199         char charbuf[charbuf_size]; // buffer for the ::read() system call
200         int count;
201         string::size_type pos;
202
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
206                 buffer += charbuf;
207         }
208
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;
214                 connected_ = false;
215         } else if ((count == -1) && (errno != EAGAIN)) { // IO error
216                 lyxerr << "lyx: Data socket " << fd_
217                        << ": IO error." << endl;
218                 connected_ = false;
219         }
220
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
226         }
227         line = buffer.substr(0, pos);
228         buffer = buffer.substr(pos + 1);
229         return true;
230 }
231
232
233 // Write a line of the form <key>:<value> to the socket
234 void LyXDataSocket::writeln(string const & line)
235 {
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;
245                 } else {
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);
251                 }
252                 connected_ = false;
253         }
254 }