]> git.lyx.org Git - lyx.git/blobdiff - src/ServerSocket.cpp
Account for old versions of Pygments
[lyx.git] / src / ServerSocket.cpp
index 12386af838687a1aef86a4edf1e1d407516b1998..fd903ce18fa4f69898da1516ae4d2acb5b2b2602 100644 (file)
 #include "support/debug.h"
 #include "support/environment.h"
 #include "support/FileName.h"
+#include "support/lassert.h"
 #include "support/socktools.h"
 
-#include "support/bind.h"
+#include <boost/assert.hpp>
 
 #include <cerrno>
+#include <cstring>
 #include <ostream>
 
 #if defined (_WIN32)
 # include <io.h>
 #endif
 
+#ifdef HAVE_UNISTD_H
+# include <unistd.h>
+#endif
+
 using namespace std;
 using namespace lyx::support;
 
-using boost::shared_ptr;
 
 namespace lyx {
 
@@ -98,6 +103,11 @@ string const ServerSocket::address() const
 // is OK and if the number of clients does not exceed MAX_CLIENTS
 void ServerSocket::serverCallback()
 {
+       if (clients.size() >= MAX_CLIENTS) {
+               writeln("BYE:Too many clients connected");
+               return;
+       }
+
        int const client_fd = socktools::accept(fd_);
 
        if (fd_ == -1) {
@@ -105,14 +115,8 @@ void ServerSocket::serverCallback()
                return;
        }
 
-       if (clients.size() >= MAX_CLIENTS) {
-               writeln("BYE:Too many clients connected");
-               return;
-       }
-
        // Register the new client.
-       clients[client_fd] =
-               shared_ptr<LyXDataSocket>(new LyXDataSocket(client_fd));
+       clients[client_fd] = make_shared<LyXDataSocket>(client_fd);
        theApp()->registerSocketCallback(
                client_fd,
                bind(&ServerSocket::dataCallback,
@@ -125,14 +129,16 @@ void ServerSocket::serverCallback()
 // if the connection has been closed
 void ServerSocket::dataCallback(int fd)
 {
-       shared_ptr<LyXDataSocket> client = clients[fd];
-
+       map<int, shared_ptr<LyXDataSocket> >::const_iterator it = clients.find(fd);
+       if (it == clients.end())
+               return;
+       shared_ptr<LyXDataSocket> client = it->second;
        string line;
-       size_t pos;
        bool saidbye = false;
        while (!saidbye && client->readln(line)) {
                // The protocol must be programmed here
                // Split the key and the data
+               size_t pos;
                if ((pos = line.find(':')) == string::npos) {
                        client->writeln("ERROR:" + line + ":malformed message");
                        continue;
@@ -141,8 +147,10 @@ void ServerSocket::dataCallback(int fd)
                string const key = line.substr(0, pos);
                if (key == "LYXCMD") {
                        string const cmd = line.substr(pos + 1);
+                       FuncRequest fr(lyxaction.lookupFunc(cmd));
+                       fr.setOrigin(FuncRequest::LYXSERVER);
                        DispatchResult dr;
-                       theApp()->dispatch(lyxaction.lookupFunc(cmd), dr);
+                       theApp()->dispatch(fr, dr);
                        string const rval = to_utf8(dr.message());
                        if (dr.error())
                                client->writeln("ERROR:" + cmd + ':' + rval);