X-Git-Url: https://git.lyx.org/gitweb/?a=blobdiff_plain;f=src%2FServerSocket.cpp;h=fd903ce18fa4f69898da1516ae4d2acb5b2b2602;hb=28be7d552f62cc02fa86d7f79201d089bfb2d7b5;hp=4a718c0882df5e1a8173ef39d15eaac5a04015fa;hpb=7b392cfd2d0874d743ebf7ac1ea963e7444cc788;p=lyx.git diff --git a/src/ServerSocket.cpp b/src/ServerSocket.cpp index 4a718c0882..fd903ce18f 100644 --- a/src/ServerSocket.cpp +++ b/src/ServerSocket.cpp @@ -3,11 +3,11 @@ * This file is part of LyX, the document processor. * Licence details can be found in the file COPYING. * - * \author Lars Gullik Bjønnes + * \author Lars Gullik Bjønnes * \author Jean-Marc Lasgouttes * \author Angus Leeming * \author John Levon - * \author João Luis M. Assirati + * \author João Luis M. Assirati * * Full author contact details are available in file CREDITS. */ @@ -16,31 +16,35 @@ #include "ServerSocket.h" -#include "debug.h" +#include "DispatchResult.h" #include "FuncRequest.h" +#include "LyX.h" #include "LyXAction.h" -#include "LyXFunc.h" #include "frontends/Application.h" +#include "support/debug.h" #include "support/environment.h" #include "support/FileName.h" -#include "support/lyxlib.h" +#include "support/lassert.h" #include "support/socktools.h" -#include +#include #include +#include +#include #if defined (_WIN32) # include #endif -using boost::shared_ptr; +#ifdef HAVE_UNISTD_H +# include +#endif -using std::auto_ptr; -using std::endl; -using std::string; +using namespace std; +using namespace lyx::support; namespace lyx { @@ -48,29 +52,28 @@ namespace lyx { // Address is the unix address for the socket. // MAX_CLIENTS is the maximum number of clients // that can connect at the same time. -ServerSocket::ServerSocket(LyXFunc * f, support::FileName const & addr) - : func(f), - fd_(support::socktools::listen(addr, 3)), +ServerSocket::ServerSocket(FileName const & addr) + : fd_(socktools::listen(addr, 3)), address_(addr) { if (fd_ == -1) { - lyxerr << "lyx: Disabling LyX socket." << endl; + LYXERR(Debug::LYXSERVER, "lyx: Disabling LyX socket."); return; } // These env vars are used by DVI inverse search // Needed by xdvi - support::setEnv("XEDITOR", "lyxclient -g %f %l"); + setEnv("XEDITOR", "lyxclient -g %f %l"); // Needed by lyxclient - support::setEnv("LYXSOCKET", address_.absFilename()); + setEnv("LYXSOCKET", address_.absFileName()); theApp()->registerSocketCallback( fd_, - boost::bind(&ServerSocket::serverCallback, this) + bind(&ServerSocket::serverCallback, this) ); - LYXERR(Debug::LYXSERVER) << "lyx: New server socket " - << fd_ << ' ' << address_.absFilename() << endl; + LYXERR(Debug::LYXSERVER, "lyx: New server socket " + << fd_ << ' ' << address_.absFileName()); } @@ -82,16 +85,17 @@ ServerSocket::~ServerSocket() theApp()->unregisterSocketCallback(fd_); if (::close(fd_) != 0) lyxerr << "lyx: Server socket " << fd_ - << " IO error on closing: " << strerror(errno); + << " IO error on closing: " << strerror(errno) + << endl; } - support::unlink(address_); - LYXERR(Debug::LYXSERVER) << "lyx: Server socket quitting" << endl; + address_.removeFile(); + LYXERR(Debug::LYXSERVER, "lyx: Server socket quitting"); } string const ServerSocket::address() const { - return address_.absFilename(); + return address_.absFileName(); } @@ -99,25 +103,23 @@ string const ServerSocket::address() const // is OK and if the number of clients does not exceed MAX_CLIENTS void ServerSocket::serverCallback() { - int const client_fd = support::socktools::accept(fd_); - - if (fd_ == -1) { - LYXERR(Debug::LYXSERVER) << "lyx: Failed to accept new client" - << endl; + if (clients.size() >= MAX_CLIENTS) { + writeln("BYE:Too many clients connected"); return; } - if (clients.size() >= MAX_CLIENTS) { - writeln("BYE:Too many clients connected"); + int const client_fd = socktools::accept(fd_); + + if (fd_ == -1) { + LYXERR(Debug::LYXSERVER, "lyx: Failed to accept new client"); return; } // Register the new client. - clients[client_fd] = - shared_ptr(new LyXDataSocket(client_fd)); + clients[client_fd] = make_shared(client_fd); theApp()->registerSocketCallback( client_fd, - boost::bind(&ServerSocket::dataCallback, + bind(&ServerSocket::dataCallback, this, client_fd) ); } @@ -127,14 +129,16 @@ void ServerSocket::serverCallback() // if the connection has been closed void ServerSocket::dataCallback(int fd) { - shared_ptr client = clients[fd]; - + map >::const_iterator it = clients.find(fd); + if (it == clients.end()) + return; + shared_ptr client = it->second; string line; - string::size_type 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; @@ -143,13 +147,15 @@ void ServerSocket::dataCallback(int fd) string const key = line.substr(0, pos); if (key == "LYXCMD") { string const cmd = line.substr(pos + 1); - func->dispatch(lyxaction.lookupFunc(cmd)); - string const rval = to_utf8(func->getMessage()); - if (func->errorStat()) { + FuncRequest fr(lyxaction.lookupFunc(cmd)); + fr.setOrigin(FuncRequest::LYXSERVER); + DispatchResult dr; + theApp()->dispatch(fr, dr); + string const rval = to_utf8(dr.message()); + if (dr.error()) client->writeln("ERROR:" + cmd + ':' + rval); - } else { + else client->writeln("INFO:" + cmd + ':' + rval); - } } else if (key == "HELLO") { // no use for client name! client->writeln("HELLO:"); @@ -191,10 +197,10 @@ void ServerSocket::writeln(string const & line) // void ServerSocket::dump() const // { // lyxerr << "ServerSocket debug dump.\n" -// << "fd = " << fd_ << ", address = " << address_.absFilename() << ".\n" +// << "fd = " << fd_ << ", address = " << address_.absFileName() << ".\n" // << "Clients: " << clients.size() << ".\n"; -// std::map >::const_iterator client = clients.begin(); -// std::map >::const_iterator end = clients.end(); +// map >::const_iterator client = clients.begin(); +// map >::const_iterator end = clients.end(); // for (; client != end; ++client) // lyxerr << "fd = " << client->first << '\n'; // } @@ -203,7 +209,7 @@ void ServerSocket::writeln(string const & line) LyXDataSocket::LyXDataSocket(int fd) : fd_(fd), connected_(true) { - LYXERR(Debug::LYXSERVER) << "lyx: New data socket " << fd_ << endl; + LYXERR(Debug::LYXSERVER, "lyx: New data socket " << fd_); } @@ -214,8 +220,7 @@ LyXDataSocket::~LyXDataSocket() << " IO error on closing: " << strerror(errno); theApp()->unregisterSocketCallback(fd_); - LYXERR(Debug::LYXSERVER) << "lyx: Data socket " << fd_ << " quitting." - << endl; + LYXERR(Debug::LYXSERVER, "lyx: Data socket " << fd_ << " quitting."); } @@ -240,8 +245,8 @@ bool LyXDataSocket::readln(string & line) // Error conditions. The buffer must still be // processed for lines read if (count == 0) { // EOF -- connection closed - LYXERR(Debug::LYXSERVER) << "lyx: Data socket " << fd_ - << ": connection closed." << endl; + LYXERR(Debug::LYXSERVER, "lyx: Data socket " << fd_ + << ": connection closed."); connected_ = false; } else if ((count == -1) && (errno != EAGAIN)) { // IO error lyxerr << "lyx: Data socket " << fd_ @@ -250,10 +255,10 @@ bool LyXDataSocket::readln(string & line) } // Cut a line from buffer - string::size_type pos = buffer_.find('\n'); + size_t pos = buffer_.find('\n'); if (pos == string::npos) { - LYXERR(Debug::LYXSERVER) << "lyx: Data socket " << fd_ - << ": line not completed." << endl; + LYXERR(Debug::LYXSERVER, "lyx: Data socket " << fd_ + << ": line not completed."); return false; // No complete line stored } line = buffer_.substr(0, pos);