2003-08-23 00:17:00 +00:00
|
|
|
/**
|
2007-04-29 16:22:46 +00:00
|
|
|
* \file Server.cpp
|
2003-08-23 00:17:00 +00:00
|
|
|
* This file is part of LyX, the document processor.
|
|
|
|
* Licence details can be found in the file COPYING.
|
2002-03-21 17:27:08 +00:00
|
|
|
*
|
2008-11-14 15:58:50 +00:00
|
|
|
* \author Lars Gullik Bjønnes
|
2003-08-23 00:17:00 +00:00
|
|
|
* \author Jean-Marc Lasgouttes
|
|
|
|
* \author Angus Leeming
|
|
|
|
* \author John Levon
|
2009-08-21 22:58:38 +00:00
|
|
|
* \author Enrico Forestieri
|
1999-10-02 16:21:10 +00:00
|
|
|
*
|
2003-08-23 00:17:00 +00:00
|
|
|
* Full author contact details are available in file CREDITS.
|
|
|
|
*/
|
1999-09-27 18:44:28 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
Docu : To use the lyxserver define the name of the pipe in your
|
2002-03-21 17:27:08 +00:00
|
|
|
lyxrc:
|
|
|
|
\serverpipe "/home/myhome/.lyxpipe"
|
|
|
|
Then use .lyxpipe.in and .lyxpipe.out to communicate to LyX.
|
|
|
|
Each message consists of a single line in ASCII. Input lines
|
|
|
|
(client -> LyX) have the following format:
|
|
|
|
"LYXCMD:<clientname>:<functionname>:<argument>"
|
|
|
|
Answers from LyX look like this:
|
|
|
|
"INFO:<clientname>:<functionname>:<data>"
|
1999-09-27 18:44:28 +00:00
|
|
|
[asierra970531] Or like this in case of error:
|
2002-03-21 17:27:08 +00:00
|
|
|
"ERROR:<clientname>:<functionname>:<error message>"
|
|
|
|
where <clientname> and <functionname> are just echoed.
|
|
|
|
If LyX notifies about a user defined extension key-sequence,
|
|
|
|
the line looks like this:
|
|
|
|
"NOTIFY:<key-sequence>"
|
1999-09-27 18:44:28 +00:00
|
|
|
[asierra970531] New server-only messages to implement a simple protocol
|
2002-03-21 17:27:08 +00:00
|
|
|
"LYXSRV:<clientname>:<protocol message>"
|
|
|
|
where <protocol message> can be "hello" or "bye". If hello is
|
|
|
|
received LyX will inform the client that it's listening its
|
|
|
|
messages, and 'bye' will inform that lyx is closing.
|
1999-09-27 18:44:28 +00:00
|
|
|
|
2010-02-18 09:58:20 +00:00
|
|
|
See development/lyxserver/server_monitor.cpp for an example client.
|
1999-09-27 18:44:28 +00:00
|
|
|
Purpose: implement a client/server lib for LyX
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <config.h>
|
|
|
|
|
2007-04-29 16:22:46 +00:00
|
|
|
#include "Server.h"
|
2010-01-29 11:22:33 +00:00
|
|
|
|
|
|
|
#include "DispatchResult.h"
|
2007-04-26 04:41:58 +00:00
|
|
|
#include "FuncRequest.h"
|
2010-02-09 16:11:13 +00:00
|
|
|
#include "LyX.h"
|
2003-10-13 00:38:09 +00:00
|
|
|
#include "LyXAction.h"
|
2008-02-18 07:14:42 +00:00
|
|
|
|
2006-10-12 14:10:13 +00:00
|
|
|
#include "frontends/Application.h"
|
2006-11-26 21:30:39 +00:00
|
|
|
|
2008-02-18 07:14:42 +00:00
|
|
|
#include "support/debug.h"
|
Rename files in src/support, step one.
src/support/package.h src/support/Package.h Package
src/support/package.C.in src/support/Package.C.in Package
src/support/path.h src/support/Path.h Path
src/support/fs_extras.h src/support/fs_extras.h NOCLASSES
src/support/RandomAccessList.h src/support/RandomAccessList.h RandomAccessList
src/support/lyxmanip.h src/support/lyxmanip.h NOCLASSES
src/support/rename.C src/support/rename.cpp NOCLASSES
src/support/abort.C src/support/abort.cpp NOCLASSES
src/support/lyxlib.h src/support/lyxlib.h NOCLASSES
src/support/ExceptionMessage.h src/support/ExceptionMessage.h ExceptionMessage
src/support/copy.C src/support/copy.cpp NOCLASSES
src/support/limited_stack.h src/support/limited_stack.h limited_stack
src/support/filefilterlist.C src/support/FileFilterList.cpp ['FileFilterList', 'Filter']
src/support/cow_ptr.h src/support/cow_ptr.h cow_ptr
src/support/os_unix.C src/support/os_unix.cpp NOCLASSES
src/support/socktools.h src/support/socktools.h NOCLASSES
src/support/forkedcontr.h src/support/ForkedcallsController.h ForkedcallsController
src/support/os.h src/support/os.h NOCLASSES
src/support/FileMonitor.h src/support/FileMonitor.h FileMonitor
src/support/copied_ptr.h src/support/copied_ptr.h copied_ptr
src/support/translator.h src/support/Translator.h Translator
src/support/filetools.C src/support/filetools.cpp NOCLASSES
src/support/unlink.C src/support/unlink.cpp NOCLASSES
src/support/os_win32.C src/support/os_win32.cpp GetFolderPath
src/support/lstrings.C src/support/lstrings.cpp NOCLASSES
src/support/qstring_helpers.C src/support/qstring_helpers.cpp NOCLASSES
src/support/getcwd.C src/support/getcwd.cpp NOCLASSES
src/support/systemcall.C src/support/Systemcall.cpp Systemcall
src/support/lyxalgo.h src/support/lyxalgo.h NOCLASSES
src/support/filefilterlist.h src/support/FileFilterList.h ['FileFilterList', 'Filter']
src/support/unicode.C src/support/unicode.cpp IconvProcessor
src/support/userinfo.C src/support/userinfo.cpp NOCLASSES
src/support/lyxtime.C src/support/lyxtime.cpp NOCLASSES
src/support/kill.C src/support/kill.cpp NOCLASSES
src/support/docstring.C src/support/docstring.cpp to_local8bit_failure
src/support/os_cygwin.C src/support/os_cygwin.cpp NOCLASSES
src/support/lyxsum.C src/support/lyxsum.cpp NOCLASSES
src/support/environment.C src/support/environment.cpp NOCLASSES
src/support/filetools.h src/support/filetools.h NOCLASSES
src/support/textutils.C src/support/textutils.cpp NOCLASSES
src/support/mkdir.C src/support/mkdir.cpp NOCLASSES
src/support/forkedcall.C src/support/Forkedcall.cpp ['ForkedProcess', 'Forkedcall']
src/support/tempname.C src/support/tempname.cpp NOCLASSES
src/support/os_win32.h src/support/os_win32.h GetFolderPath
src/support/types.h src/support/types.h NOCLASSES
src/support/lstrings.h src/support/lstrings.h NOCLASSES
src/support/forkedcallqueue.C src/support/ForkedCallQueue.cpp ForkedCallQueue
src/support/qstring_helpers.h src/support/qstring_helpers.h NOCLASSES
src/support/convert.C src/support/convert.cpp NOCLASSES
src/support/filename.C src/support/FileName.cpp ['FileName', 'DocFileName']
src/support/tests/convert.C src/support/tests/convert.cpp NOCLASSES
src/support/tests/filetools.C src/support/tests/filetools.cpp NOCLASSES
src/support/tests/lstrings.C src/support/tests/lstrings.cpp NOCLASSES
src/support/tests/boost.C src/support/tests/boost.cpp NOCLASSES
src/support/docstream.C src/support/docstream.cpp ['iconv_codecvt_facet_exception', 'idocfstream', 'odocfstream']
src/support/std_istream.h src/support/std_istream.h NOCLASSES
src/support/systemcall.h src/support/Systemcall.h Systemcall
src/support/chdir.C src/support/chdir.cpp NOCLASSES
src/support/std_ostream.h src/support/std_ostream.h NOCLASSES
src/support/unicode.h src/support/unicode.h IconvProcessor
src/support/path.C src/support/Path.cpp Path
src/support/fs_extras.C src/support/fs_extras.cpp NOCLASSES
src/support/userinfo.h src/support/userinfo.h NOCLASSES
src/support/lyxtime.h src/support/lyxtime.h NOCLASSES
src/support/docstring.h src/support/docstring.h to_local8bit_failure
src/support/debugstream.h src/support/debugstream.h basic_debugstream
src/support/environment.h src/support/environment.h NOCLASSES
src/support/textutils.h src/support/textutils.h NOCLASSES
src/support/forkedcall.h src/support/Forkedcall.h ['ForkedProcess', 'Forkedcall']
src/support/socktools.C src/support/socktools.cpp NOCLASSES
src/support/forkedcallqueue.h src/support/ForkedCallQueue.h ForkedCallQueue
src/support/forkedcontr.C src/support/ForkedcallsController.cpp ForkedcallsController
src/support/os.C src/support/os.cpp NOCLASSES
src/support/convert.h src/support/convert.h NOCLASSES
src/support/filename.h src/support/FileName.h ['FileName', 'DocFileName']
src/support/docstream.h src/support/docstream.h ['iconv_codecvt_facet_exception', 'idocfstream', 'odocfstream']
src/support/FileMonitor.C src/support/FileMonitor.cpp FileMonitor
git-svn-id: svn://svn.lyx.org/lyx/lyx-devel/trunk@18024 a592a061-630c-0410-9148-cb99ea01b6c8
2007-04-26 05:12:52 +00:00
|
|
|
#include "support/FileName.h"
|
2010-11-13 11:55:05 +00:00
|
|
|
#include "support/filetools.h"
|
2009-08-29 23:42:06 +00:00
|
|
|
#include "support/lassert.h"
|
1999-10-02 16:21:10 +00:00
|
|
|
#include "support/lstrings.h"
|
2009-08-21 22:58:38 +00:00
|
|
|
#include "support/os.h"
|
2017-05-28 11:25:53 +00:00
|
|
|
#include "support/signals.h"
|
2004-07-22 13:44:40 +00:00
|
|
|
|
2016-06-12 11:03:06 +00:00
|
|
|
#include <iostream>
|
|
|
|
|
2009-08-21 22:58:38 +00:00
|
|
|
#ifdef _WIN32
|
2010-11-14 16:27:08 +00:00
|
|
|
#include <io.h>
|
2009-08-21 22:58:38 +00:00
|
|
|
#include <QCoreApplication>
|
|
|
|
#endif
|
2010-11-13 11:55:05 +00:00
|
|
|
#include <QThread>
|
2009-08-21 22:58:38 +00:00
|
|
|
|
2003-09-05 22:17:02 +00:00
|
|
|
#include <cerrno>
|
2005-04-26 10:30:24 +00:00
|
|
|
#ifdef HAVE_SYS_STAT_H
|
|
|
|
# include <sys/stat.h>
|
|
|
|
#endif
|
2003-09-05 22:17:02 +00:00
|
|
|
#include <fcntl.h>
|
|
|
|
|
2007-12-12 10:16:00 +00:00
|
|
|
using namespace std;
|
2007-12-12 18:57:56 +00:00
|
|
|
using namespace lyx::support;
|
2009-08-21 22:58:38 +00:00
|
|
|
using os::external_path;
|
2006-10-21 00:16:43 +00:00
|
|
|
|
|
|
|
namespace lyx {
|
|
|
|
|
2007-09-16 20:58:22 +00:00
|
|
|
/////////////////////////////////////////////////////////////////////
|
|
|
|
//
|
|
|
|
// LyXComm
|
|
|
|
//
|
|
|
|
/////////////////////////////////////////////////////////////////////
|
|
|
|
|
2009-08-21 22:58:38 +00:00
|
|
|
#if defined(_WIN32)
|
|
|
|
|
2009-08-29 23:42:06 +00:00
|
|
|
class ReadReadyEvent : public QEvent {
|
2009-08-21 22:58:38 +00:00
|
|
|
public:
|
|
|
|
///
|
2009-08-29 23:42:06 +00:00
|
|
|
ReadReadyEvent(DWORD inpipe) : QEvent(QEvent::User), inpipe_(inpipe)
|
2009-08-21 22:58:38 +00:00
|
|
|
{}
|
|
|
|
///
|
2009-08-29 23:42:06 +00:00
|
|
|
DWORD inpipe() const { return inpipe_; }
|
2009-08-21 22:58:38 +00:00
|
|
|
|
|
|
|
private:
|
2009-08-29 23:42:06 +00:00
|
|
|
DWORD inpipe_;
|
2009-08-21 22:58:38 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
2009-09-05 16:39:33 +00:00
|
|
|
string errormsg(DWORD const error)
|
2009-08-21 22:58:38 +00:00
|
|
|
{
|
|
|
|
void * msgbuf;
|
2009-09-05 16:39:33 +00:00
|
|
|
string message;
|
|
|
|
if (FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER |
|
|
|
|
FORMAT_MESSAGE_FROM_SYSTEM |
|
|
|
|
FORMAT_MESSAGE_IGNORE_INSERTS,
|
|
|
|
NULL, error,
|
|
|
|
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
|
|
|
|
(LPTSTR) &msgbuf, 0, NULL)) {
|
|
|
|
message = static_cast<char *>(msgbuf);
|
|
|
|
LocalFree(msgbuf);
|
|
|
|
} else
|
|
|
|
message = "Unknown error";
|
|
|
|
|
|
|
|
return message;
|
2009-08-21 22:58:38 +00:00
|
|
|
}
|
|
|
|
|
2017-07-23 11:11:54 +00:00
|
|
|
} // namespace
|
2009-08-21 22:58:38 +00:00
|
|
|
|
|
|
|
|
|
|
|
DWORD WINAPI pipeServerWrapper(void * arg)
|
|
|
|
{
|
|
|
|
LyXComm * lyxcomm = reinterpret_cast<LyXComm *>(arg);
|
2009-09-05 16:39:33 +00:00
|
|
|
if (!lyxcomm->pipeServer()) {
|
|
|
|
// Error exit; perform cleanup.
|
|
|
|
lyxcomm->ready_ = false;
|
|
|
|
lyxcomm->closeHandles();
|
|
|
|
CloseHandle(lyxcomm->server_thread_);
|
|
|
|
CloseHandle(lyxcomm->stopserver_);
|
|
|
|
CloseHandle(lyxcomm->outbuf_mutex_);
|
|
|
|
lyxerr << "LyXComm: Closing connection" << endl;
|
|
|
|
}
|
2009-08-21 22:58:38 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
LyXComm::LyXComm(string const & pip, Server * cli, ClientCallbackfct ccb)
|
2017-07-06 12:46:03 +00:00
|
|
|
: stopserver_(0),
|
|
|
|
ready_(false), pipename_(pip), client_(cli), clientcb_(ccb),
|
|
|
|
deferred_loading_(false)
|
2009-08-21 22:58:38 +00:00
|
|
|
{
|
2009-09-05 16:39:33 +00:00
|
|
|
for (int i = 0; i < MAX_PIPES; ++i) {
|
|
|
|
event_[i] = 0;
|
|
|
|
pipe_[i].handle = INVALID_HANDLE_VALUE;
|
|
|
|
}
|
2009-08-21 22:58:38 +00:00
|
|
|
openConnection();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-09-05 16:39:33 +00:00
|
|
|
bool LyXComm::pipeServer()
|
2009-08-21 22:58:38 +00:00
|
|
|
{
|
2009-08-29 23:42:06 +00:00
|
|
|
DWORD i;
|
2009-09-05 16:39:33 +00:00
|
|
|
DWORD error;
|
2009-08-29 23:42:06 +00:00
|
|
|
|
2009-08-31 23:32:46 +00:00
|
|
|
for (i = 0; i < MAX_PIPES; ++i) {
|
|
|
|
bool const is_outpipe = i >= MAX_CLIENTS;
|
|
|
|
DWORD const open_mode = is_outpipe ? PIPE_ACCESS_OUTBOUND
|
|
|
|
: PIPE_ACCESS_INBOUND;
|
2009-08-29 23:42:06 +00:00
|
|
|
string const pipename = external_path(pipeName(i));
|
|
|
|
|
|
|
|
// Manual-reset event, initial state = signaled
|
|
|
|
event_[i] = CreateEvent(NULL, TRUE, TRUE, NULL);
|
|
|
|
if (!event_[i]) {
|
2009-09-05 16:39:33 +00:00
|
|
|
error = GetLastError();
|
2009-08-31 23:32:46 +00:00
|
|
|
lyxerr << "LyXComm: Could not create event for pipe "
|
2009-09-05 16:39:33 +00:00
|
|
|
<< pipename << "\nLyXComm: "
|
|
|
|
<< errormsg(error) << endl;
|
|
|
|
return false;
|
2009-08-29 23:42:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pipe_[i].overlap.hEvent = event_[i];
|
2009-08-31 23:32:46 +00:00
|
|
|
pipe_[i].iobuf.erase();
|
2009-08-29 23:42:06 +00:00
|
|
|
pipe_[i].handle = CreateNamedPipe(pipename.c_str(),
|
|
|
|
open_mode | FILE_FLAG_OVERLAPPED, PIPE_WAIT,
|
2009-08-31 23:32:46 +00:00
|
|
|
MAX_CLIENTS, PIPE_BUFSIZE, PIPE_BUFSIZE,
|
2009-08-29 23:42:06 +00:00
|
|
|
PIPE_TIMEOUT, NULL);
|
|
|
|
|
|
|
|
if (pipe_[i].handle == INVALID_HANDLE_VALUE) {
|
2009-09-05 16:39:33 +00:00
|
|
|
error = GetLastError();
|
2009-08-31 23:32:46 +00:00
|
|
|
lyxerr << "LyXComm: Could not create pipe "
|
2009-09-05 16:39:33 +00:00
|
|
|
<< pipename << "\nLyXComm: "
|
|
|
|
<< errormsg(error) << endl;
|
|
|
|
return false;
|
2009-08-29 23:42:06 +00:00
|
|
|
}
|
|
|
|
|
2009-09-05 16:39:33 +00:00
|
|
|
if (!startPipe(i))
|
|
|
|
return false;
|
2009-08-29 23:42:06 +00:00
|
|
|
pipe_[i].state = pipe_[i].pending_io ?
|
2009-08-31 23:32:46 +00:00
|
|
|
CONNECTING_STATE : (is_outpipe ? WRITING_STATE
|
|
|
|
: READING_STATE);
|
2009-08-21 22:58:38 +00:00
|
|
|
}
|
2009-08-29 23:42:06 +00:00
|
|
|
|
|
|
|
// Add the stopserver_ event
|
2009-08-31 23:32:46 +00:00
|
|
|
event_[MAX_PIPES] = stopserver_;
|
2009-08-29 23:42:06 +00:00
|
|
|
|
|
|
|
// We made it!
|
|
|
|
LYXERR(Debug::LYXSERVER, "LyXComm: Connection established");
|
|
|
|
ready_ = true;
|
2009-08-31 23:32:46 +00:00
|
|
|
outbuf_.erase();
|
2009-08-29 23:42:06 +00:00
|
|
|
DWORD status;
|
|
|
|
bool success;
|
|
|
|
|
|
|
|
while (!checkStopServer()) {
|
|
|
|
// Indefinitely wait for the completion of an overlapped
|
|
|
|
// read, write, or connect operation.
|
2009-08-31 23:32:46 +00:00
|
|
|
DWORD wait = WaitForMultipleObjects(MAX_PIPES + 1, event_,
|
2009-08-29 23:42:06 +00:00
|
|
|
FALSE, INFINITE);
|
|
|
|
|
|
|
|
// Determine which pipe instance completed the operation.
|
|
|
|
i = wait - WAIT_OBJECT_0;
|
2016-05-08 08:51:51 +00:00
|
|
|
LASSERT(i <= MAX_PIPES, /**/);
|
2009-08-29 23:42:06 +00:00
|
|
|
|
|
|
|
// Check whether we were waked up for stopping the pipe server.
|
2009-08-31 23:32:46 +00:00
|
|
|
if (i == MAX_PIPES)
|
2009-08-21 22:58:38 +00:00
|
|
|
break;
|
2009-08-29 23:42:06 +00:00
|
|
|
|
2009-08-31 23:32:46 +00:00
|
|
|
bool const is_outpipe = i >= MAX_CLIENTS;
|
|
|
|
|
2009-08-29 23:42:06 +00:00
|
|
|
// Get the result if the operation was pending.
|
|
|
|
if (pipe_[i].pending_io) {
|
|
|
|
success = GetOverlappedResult(pipe_[i].handle,
|
|
|
|
&pipe_[i].overlap, &status, FALSE);
|
|
|
|
|
|
|
|
switch (pipe_[i].state) {
|
|
|
|
case CONNECTING_STATE:
|
|
|
|
// Pending connect operation
|
|
|
|
if (!success) {
|
2009-09-05 16:39:33 +00:00
|
|
|
error = GetLastError();
|
2009-08-31 23:32:46 +00:00
|
|
|
lyxerr << "LyXComm: "
|
2009-09-05 16:39:33 +00:00
|
|
|
<< errormsg(error) << endl;
|
|
|
|
if (!resetPipe(i, true))
|
|
|
|
return false;
|
2009-08-29 23:42:06 +00:00
|
|
|
continue;
|
|
|
|
}
|
2009-08-31 23:32:46 +00:00
|
|
|
pipe_[i].state = is_outpipe ? WRITING_STATE
|
|
|
|
: READING_STATE;
|
2009-08-29 23:42:06 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case READING_STATE:
|
|
|
|
// Pending read operation
|
2009-08-31 23:32:46 +00:00
|
|
|
LASSERT(!is_outpipe, /**/);
|
2009-08-29 23:42:06 +00:00
|
|
|
if (!success || status == 0) {
|
2009-09-05 16:39:33 +00:00
|
|
|
if (!resetPipe(i, !success))
|
|
|
|
return false;
|
2009-08-29 23:42:06 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
pipe_[i].nbytes = status;
|
|
|
|
pipe_[i].state = WRITING_STATE;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case WRITING_STATE:
|
|
|
|
// Pending write operation
|
2009-08-31 23:32:46 +00:00
|
|
|
LASSERT(is_outpipe, /**/);
|
|
|
|
// Let's see whether we have a reply
|
|
|
|
if (!outbuf_.empty()) {
|
|
|
|
// Yep. Deliver it to all pipe
|
|
|
|
// instances if we get ownership
|
|
|
|
// of the mutex, otherwise we'll
|
|
|
|
// try again the next round.
|
|
|
|
DWORD result = WaitForSingleObject(
|
|
|
|
outbuf_mutex_, 200);
|
|
|
|
if (result == WAIT_OBJECT_0) {
|
|
|
|
DWORD j = MAX_CLIENTS;
|
|
|
|
while (j < MAX_PIPES) {
|
|
|
|
pipe_[j].iobuf = outbuf_;
|
|
|
|
++j;
|
|
|
|
}
|
|
|
|
outbuf_.erase();
|
|
|
|
}
|
|
|
|
ReleaseMutex(outbuf_mutex_);
|
2009-08-29 23:42:06 +00:00
|
|
|
}
|
2009-08-31 23:32:46 +00:00
|
|
|
if (pipe_[i].iobuf.empty())
|
|
|
|
pipe_[i].pending_io = false;
|
2009-08-29 23:42:06 +00:00
|
|
|
break;
|
|
|
|
}
|
2009-08-21 22:58:38 +00:00
|
|
|
}
|
|
|
|
|
2009-08-29 23:42:06 +00:00
|
|
|
// Operate according to the pipe state
|
|
|
|
switch (pipe_[i].state) {
|
|
|
|
case READING_STATE:
|
|
|
|
// The pipe instance is connected to a client
|
|
|
|
// and is ready to read a request.
|
2009-08-31 23:32:46 +00:00
|
|
|
LASSERT(!is_outpipe, /**/);
|
2009-08-29 23:42:06 +00:00
|
|
|
success = ReadFile(pipe_[i].handle,
|
2009-08-31 23:32:46 +00:00
|
|
|
pipe_[i].readbuf, PIPE_BUFSIZE - 1,
|
2009-08-29 23:42:06 +00:00
|
|
|
&pipe_[i].nbytes, &pipe_[i].overlap);
|
|
|
|
|
|
|
|
if (success && pipe_[i].nbytes != 0) {
|
|
|
|
// The read operation completed successfully.
|
|
|
|
pipe_[i].pending_io = false;
|
|
|
|
pipe_[i].state = WRITING_STATE;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2009-09-05 16:39:33 +00:00
|
|
|
error = GetLastError();
|
|
|
|
|
|
|
|
if (!success && error == ERROR_IO_PENDING) {
|
2009-08-29 23:42:06 +00:00
|
|
|
// The read operation is still pending.
|
|
|
|
pipe_[i].pending_io = true;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2009-09-05 16:39:33 +00:00
|
|
|
success = error == ERROR_BROKEN_PIPE;
|
|
|
|
|
2009-08-29 23:42:06 +00:00
|
|
|
// Client closed connection (ERROR_BROKEN_PIPE) or
|
|
|
|
// an error occurred; in either case, reset the pipe.
|
2009-09-05 16:39:33 +00:00
|
|
|
if (!success) {
|
|
|
|
lyxerr << "LyXComm: " << errormsg(error) << endl;
|
2009-08-31 23:32:46 +00:00
|
|
|
if (!pipe_[i].iobuf.empty()) {
|
|
|
|
lyxerr << "LyXComm: truncated command: "
|
|
|
|
<< pipe_[i].iobuf << endl;
|
|
|
|
pipe_[i].iobuf.erase();
|
2009-08-29 23:42:06 +00:00
|
|
|
}
|
2009-09-05 16:39:33 +00:00
|
|
|
}
|
|
|
|
if (!resetPipe(i, !success))
|
|
|
|
return false;
|
2009-08-29 23:42:06 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case WRITING_STATE:
|
2009-08-31 23:32:46 +00:00
|
|
|
if (!is_outpipe) {
|
2009-08-29 23:42:06 +00:00
|
|
|
// The request was successfully read
|
|
|
|
// from the client; commit it.
|
|
|
|
ReadReadyEvent * event = new ReadReadyEvent(i);
|
|
|
|
QCoreApplication::postEvent(this,
|
|
|
|
static_cast<QEvent *>(event));
|
|
|
|
// Wait for completion
|
2009-09-06 13:49:34 +00:00
|
|
|
while (pipe_[i].nbytes && !checkStopServer(100))
|
|
|
|
;
|
2009-08-29 23:42:06 +00:00
|
|
|
pipe_[i].pending_io = false;
|
|
|
|
pipe_[i].state = READING_STATE;
|
|
|
|
continue;
|
|
|
|
}
|
2009-08-31 23:32:46 +00:00
|
|
|
|
|
|
|
// This is an output pipe instance. Initiate the
|
|
|
|
// overlapped write operation or monitor its progress.
|
|
|
|
|
|
|
|
if (pipe_[i].pending_io) {
|
|
|
|
success = WriteFile(pipe_[i].handle,
|
|
|
|
pipe_[i].iobuf.c_str(),
|
|
|
|
pipe_[i].iobuf.length(),
|
|
|
|
&status,
|
|
|
|
&pipe_[i].overlap);
|
2009-08-29 23:42:06 +00:00
|
|
|
}
|
|
|
|
|
2009-08-31 23:32:46 +00:00
|
|
|
if (success && !pipe_[i].iobuf.empty()
|
|
|
|
&& status == pipe_[i].iobuf.length()) {
|
2009-08-29 23:42:06 +00:00
|
|
|
// The write operation completed successfully.
|
2009-08-31 23:32:46 +00:00
|
|
|
pipe_[i].iobuf.erase();
|
2009-08-29 23:42:06 +00:00
|
|
|
pipe_[i].pending_io = false;
|
2009-09-05 16:39:33 +00:00
|
|
|
if (!resetPipe(i))
|
|
|
|
return false;
|
2009-08-29 23:42:06 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2009-09-05 16:39:33 +00:00
|
|
|
error = GetLastError();
|
|
|
|
|
2018-08-07 15:56:07 +00:00
|
|
|
if (success && (error == ERROR_IO_PENDING || error == NO_ERROR)) {
|
2009-08-29 23:42:06 +00:00
|
|
|
// The write operation is still pending.
|
2009-08-31 23:32:46 +00:00
|
|
|
// We get here when a reader is started
|
|
|
|
// well before a reply is ready, so delay
|
|
|
|
// a bit in order to not burden the cpu.
|
2009-09-06 13:49:34 +00:00
|
|
|
checkStopServer(100);
|
2009-08-29 23:42:06 +00:00
|
|
|
pipe_[i].pending_io = true;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2009-09-05 16:39:33 +00:00
|
|
|
success = error == ERROR_NO_DATA;
|
|
|
|
|
2009-08-29 23:42:06 +00:00
|
|
|
// Client closed connection (ERROR_NO_DATA) or
|
|
|
|
// an error occurred; in either case, reset the pipe.
|
2009-09-05 16:39:33 +00:00
|
|
|
if (!success) {
|
2009-08-31 23:32:46 +00:00
|
|
|
lyxerr << "LyXComm: Error sending message: "
|
|
|
|
<< pipe_[i].iobuf << "\nLyXComm: "
|
2009-09-05 16:39:33 +00:00
|
|
|
<< errormsg(error) << endl;
|
|
|
|
}
|
|
|
|
if (!resetPipe(i, !success))
|
|
|
|
return false;
|
2009-08-21 22:58:38 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2009-08-29 23:42:06 +00:00
|
|
|
|
|
|
|
ready_ = false;
|
2009-09-05 16:39:33 +00:00
|
|
|
closeHandles();
|
|
|
|
return true;
|
2009-08-29 23:42:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-09-05 16:39:33 +00:00
|
|
|
void LyXComm::closeHandles()
|
2009-08-29 23:42:06 +00:00
|
|
|
{
|
2009-09-05 16:39:33 +00:00
|
|
|
for (int i = 0; i < MAX_PIPES; ++i) {
|
2009-08-29 23:42:06 +00:00
|
|
|
if (event_[i]) {
|
|
|
|
ResetEvent(event_[i]);
|
|
|
|
CloseHandle(event_[i]);
|
2009-09-05 16:39:33 +00:00
|
|
|
event_[i] = 0;
|
2009-08-29 23:42:06 +00:00
|
|
|
}
|
2009-09-05 16:39:33 +00:00
|
|
|
if (pipe_[i].handle != INVALID_HANDLE_VALUE) {
|
2009-08-29 23:42:06 +00:00
|
|
|
CloseHandle(pipe_[i].handle);
|
2009-09-05 16:39:33 +00:00
|
|
|
pipe_[i].handle = INVALID_HANDLE_VALUE;
|
|
|
|
}
|
2009-08-29 23:42:06 +00:00
|
|
|
}
|
2009-08-21 22:58:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool LyXComm::event(QEvent * e)
|
|
|
|
{
|
|
|
|
if (e->type() == QEvent::User) {
|
2009-08-29 23:42:06 +00:00
|
|
|
read_ready(static_cast<ReadReadyEvent *>(e)->inpipe());
|
2009-08-21 22:58:38 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-09-06 13:49:34 +00:00
|
|
|
bool LyXComm::checkStopServer(DWORD timeout)
|
2009-08-21 22:58:38 +00:00
|
|
|
{
|
2009-09-06 13:49:34 +00:00
|
|
|
return WaitForSingleObject(stopserver_, timeout) == WAIT_OBJECT_0;
|
2009-08-21 22:58:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-09-05 16:39:33 +00:00
|
|
|
bool LyXComm::startPipe(DWORD index)
|
2009-08-29 23:42:06 +00:00
|
|
|
{
|
|
|
|
pipe_[index].pending_io = false;
|
2009-09-11 18:19:59 +00:00
|
|
|
pipe_[index].overlap.Offset = 0;
|
|
|
|
pipe_[index].overlap.OffsetHigh = 0;
|
2009-08-29 23:42:06 +00:00
|
|
|
|
|
|
|
// Overlapped ConnectNamedPipe should return zero.
|
|
|
|
if (ConnectNamedPipe(pipe_[index].handle, &pipe_[index].overlap)) {
|
2009-09-05 16:39:33 +00:00
|
|
|
DWORD const error = GetLastError();
|
2009-08-31 23:32:46 +00:00
|
|
|
lyxerr << "LyXComm: Could not connect pipe "
|
|
|
|
<< external_path(pipeName(index))
|
2009-09-05 16:39:33 +00:00
|
|
|
<< "\nLyXComm: " << errormsg(error) << endl;
|
|
|
|
return false;
|
2009-08-29 23:42:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
switch (GetLastError()) {
|
|
|
|
case ERROR_IO_PENDING:
|
|
|
|
// The overlapped connection is in progress.
|
|
|
|
pipe_[index].pending_io = true;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case ERROR_PIPE_CONNECTED:
|
|
|
|
// Client is already connected, so signal an event.
|
|
|
|
if (SetEvent(pipe_[index].overlap.hEvent))
|
|
|
|
break;
|
2009-09-05 16:39:33 +00:00
|
|
|
// fall through
|
2009-08-29 23:42:06 +00:00
|
|
|
default:
|
|
|
|
// Anything else is an error.
|
2009-09-05 16:39:33 +00:00
|
|
|
DWORD const error = GetLastError();
|
2009-08-31 23:32:46 +00:00
|
|
|
lyxerr << "LyXComm: An error occurred while connecting pipe "
|
|
|
|
<< external_path(pipeName(index))
|
2009-09-05 16:39:33 +00:00
|
|
|
<< "\nLyXComm: " << errormsg(error) << endl;
|
|
|
|
return false;
|
2009-08-29 23:42:06 +00:00
|
|
|
}
|
2009-09-05 16:39:33 +00:00
|
|
|
|
|
|
|
return true;
|
2009-08-29 23:42:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-09-05 16:39:33 +00:00
|
|
|
bool LyXComm::resetPipe(DWORD index, bool close_handle)
|
2009-08-29 23:42:06 +00:00
|
|
|
{
|
|
|
|
// This method is called when an error occurs or when a client
|
|
|
|
// closes the connection. We first disconnect the pipe instance,
|
|
|
|
// then reconnect it, ready to wait for another client.
|
|
|
|
|
|
|
|
if (!DisconnectNamedPipe(pipe_[index].handle)) {
|
2009-09-05 16:39:33 +00:00
|
|
|
DWORD const error = GetLastError();
|
2009-08-31 23:32:46 +00:00
|
|
|
lyxerr << "LyXComm: Could not disconnect pipe "
|
|
|
|
<< external_path(pipeName(index))
|
2009-09-05 16:39:33 +00:00
|
|
|
<< "\nLyXComm: " << errormsg(error) << endl;
|
2009-08-29 23:42:06 +00:00
|
|
|
// What to do now? Let's try whether re-creating the pipe helps.
|
|
|
|
close_handle = true;
|
|
|
|
}
|
2009-08-31 23:32:46 +00:00
|
|
|
|
|
|
|
bool const is_outpipe = index >= MAX_CLIENTS;
|
|
|
|
|
2009-08-29 23:42:06 +00:00
|
|
|
if (close_handle) {
|
2009-08-31 23:32:46 +00:00
|
|
|
DWORD const open_mode = is_outpipe ? PIPE_ACCESS_OUTBOUND
|
|
|
|
: PIPE_ACCESS_INBOUND;
|
2009-08-29 23:42:06 +00:00
|
|
|
string const name = external_path(pipeName(index));
|
|
|
|
|
|
|
|
CloseHandle(pipe_[index].handle);
|
|
|
|
|
2009-08-31 23:32:46 +00:00
|
|
|
pipe_[index].iobuf.erase();
|
2009-08-29 23:42:06 +00:00
|
|
|
pipe_[index].handle = CreateNamedPipe(name.c_str(),
|
|
|
|
open_mode | FILE_FLAG_OVERLAPPED, PIPE_WAIT,
|
2009-08-31 23:32:46 +00:00
|
|
|
MAX_CLIENTS, PIPE_BUFSIZE, PIPE_BUFSIZE,
|
2009-08-29 23:42:06 +00:00
|
|
|
PIPE_TIMEOUT, NULL);
|
|
|
|
|
|
|
|
if (pipe_[index].handle == INVALID_HANDLE_VALUE) {
|
2009-09-05 16:39:33 +00:00
|
|
|
DWORD const error = GetLastError();
|
2009-08-31 23:32:46 +00:00
|
|
|
lyxerr << "LyXComm: Could not reset pipe " << name
|
2009-09-05 16:39:33 +00:00
|
|
|
<< "\nLyXComm: " << errormsg(error) << endl;
|
|
|
|
return false;
|
2009-08-29 23:42:06 +00:00
|
|
|
}
|
|
|
|
}
|
2009-08-31 23:32:46 +00:00
|
|
|
|
2009-09-05 16:39:33 +00:00
|
|
|
if (!startPipe(index))
|
|
|
|
return false;
|
2009-08-29 23:42:06 +00:00
|
|
|
pipe_[index].state = pipe_[index].pending_io ?
|
2009-08-31 23:32:46 +00:00
|
|
|
CONNECTING_STATE : (is_outpipe ? WRITING_STATE
|
|
|
|
: READING_STATE);
|
2009-09-05 16:39:33 +00:00
|
|
|
return true;
|
2009-08-29 23:42:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-08-21 22:58:38 +00:00
|
|
|
void LyXComm::openConnection()
|
|
|
|
{
|
|
|
|
LYXERR(Debug::LYXSERVER, "LyXComm: Opening connection");
|
|
|
|
|
|
|
|
// If we are up, that's an error
|
|
|
|
if (ready_) {
|
2009-08-31 23:32:46 +00:00
|
|
|
LYXERR(Debug::LYXSERVER, "LyXComm: Already connected");
|
2009-08-21 22:58:38 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (pipename_.empty()) {
|
|
|
|
LYXERR(Debug::LYXSERVER, "LyXComm: server is disabled, nothing to do");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2010-11-13 11:55:05 +00:00
|
|
|
// Check whether the pipe name is being used by some other instance.
|
2009-08-23 14:40:26 +00:00
|
|
|
if (!stopserver_ && WaitNamedPipe(inPipeName().c_str(), 0)) {
|
2010-11-13 11:55:05 +00:00
|
|
|
// Tell the running instance to load the files
|
|
|
|
if (run_mode == USE_REMOTE && loadFilesInOtherInstance()) {
|
|
|
|
deferred_loading_ = true;
|
|
|
|
pipename_.erase();
|
|
|
|
return;
|
|
|
|
}
|
2009-08-31 23:32:46 +00:00
|
|
|
lyxerr << "LyXComm: Pipe " << external_path(inPipeName())
|
|
|
|
<< " already exists.\nMaybe another instance of LyX"
|
|
|
|
" is using it." << endl;
|
|
|
|
pipename_.erase();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Mutex with no initial owner for synchronized access to outbuf_
|
|
|
|
outbuf_mutex_ = CreateMutex(NULL, FALSE, NULL);
|
|
|
|
if (!outbuf_mutex_) {
|
2009-09-05 16:39:33 +00:00
|
|
|
DWORD const error = GetLastError();
|
2009-08-31 23:32:46 +00:00
|
|
|
lyxerr << "LyXComm: Could not create output buffer mutex"
|
2009-09-05 16:39:33 +00:00
|
|
|
<< "\nLyXComm: " << errormsg(error) << endl;
|
2009-08-23 14:40:26 +00:00
|
|
|
pipename_.erase();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2009-08-29 23:42:06 +00:00
|
|
|
// Manual-reset event, initial state = not signaled
|
2009-08-21 22:58:38 +00:00
|
|
|
stopserver_ = CreateEvent(NULL, TRUE, FALSE, NULL);
|
2009-08-31 23:32:46 +00:00
|
|
|
if (!stopserver_) {
|
2009-09-05 16:39:33 +00:00
|
|
|
DWORD const error = GetLastError();
|
2009-08-31 23:32:46 +00:00
|
|
|
lyxerr << "LyXComm: Could not create stop server event"
|
2009-09-05 16:39:33 +00:00
|
|
|
<< "\nLyXComm: " << errormsg(error) << endl;
|
2009-08-31 23:32:46 +00:00
|
|
|
pipename_.erase();
|
|
|
|
CloseHandle(outbuf_mutex_);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2009-08-29 23:42:06 +00:00
|
|
|
server_thread_ = CreateThread(NULL, 0, pipeServerWrapper,
|
|
|
|
static_cast<void *>(this), 0, NULL);
|
|
|
|
if (!server_thread_) {
|
2009-09-05 16:39:33 +00:00
|
|
|
DWORD const error = GetLastError();
|
2009-08-31 23:32:46 +00:00
|
|
|
lyxerr << "LyXComm: Could not create pipe server thread"
|
2009-09-05 16:39:33 +00:00
|
|
|
<< "\nLyXComm: " << errormsg(error) << endl;
|
2009-08-29 23:42:06 +00:00
|
|
|
pipename_.erase();
|
2009-08-31 23:32:46 +00:00
|
|
|
CloseHandle(stopserver_);
|
|
|
|
CloseHandle(outbuf_mutex_);
|
2009-08-21 22:58:38 +00:00
|
|
|
return;
|
2009-08-29 23:42:06 +00:00
|
|
|
}
|
2009-08-21 22:58:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// Close pipes
|
|
|
|
void LyXComm::closeConnection()
|
|
|
|
{
|
|
|
|
LYXERR(Debug::LYXSERVER, "LyXComm: Closing connection");
|
|
|
|
|
|
|
|
if (pipename_.empty()) {
|
|
|
|
LYXERR(Debug::LYXSERVER, "LyXComm: server is disabled, nothing to do");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!ready_) {
|
2009-08-30 15:31:22 +00:00
|
|
|
LYXERR(Debug::LYXSERVER, "LyXComm: Already disconnected");
|
2009-08-21 22:58:38 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2009-08-30 16:06:51 +00:00
|
|
|
SetEvent(stopserver_);
|
|
|
|
// Wait for the pipe server to finish
|
|
|
|
WaitForSingleObject(server_thread_, INFINITE);
|
|
|
|
CloseHandle(server_thread_);
|
|
|
|
ResetEvent(stopserver_);
|
|
|
|
CloseHandle(stopserver_);
|
2009-08-31 23:32:46 +00:00
|
|
|
CloseHandle(outbuf_mutex_);
|
2009-08-21 22:58:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void LyXComm::emergencyCleanup()
|
|
|
|
{
|
2009-08-29 23:42:06 +00:00
|
|
|
if (ready_) {
|
2009-08-21 22:58:38 +00:00
|
|
|
SetEvent(stopserver_);
|
2009-08-30 16:06:51 +00:00
|
|
|
// Forcibly terminate the pipe server thread if it does
|
|
|
|
// not finish quickly.
|
2009-09-05 16:39:33 +00:00
|
|
|
if (WaitForSingleObject(server_thread_, 200) != WAIT_OBJECT_0) {
|
2009-08-30 16:06:51 +00:00
|
|
|
TerminateThread(server_thread_, 0);
|
2009-09-05 16:39:33 +00:00
|
|
|
ready_ = false;
|
|
|
|
closeHandles();
|
|
|
|
}
|
2009-08-29 23:42:06 +00:00
|
|
|
CloseHandle(server_thread_);
|
2009-08-23 13:27:08 +00:00
|
|
|
ResetEvent(stopserver_);
|
|
|
|
CloseHandle(stopserver_);
|
2009-08-31 23:32:46 +00:00
|
|
|
CloseHandle(outbuf_mutex_);
|
2009-08-21 22:58:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-08-29 23:42:06 +00:00
|
|
|
void LyXComm::read_ready(DWORD inpipe)
|
|
|
|
{
|
|
|
|
// Turn the pipe buffer into a C string
|
|
|
|
DWORD const nbytes = pipe_[inpipe].nbytes;
|
2009-08-31 23:32:46 +00:00
|
|
|
pipe_[inpipe].readbuf[nbytes] = '\0';
|
2009-08-29 23:42:06 +00:00
|
|
|
|
2009-08-31 23:32:46 +00:00
|
|
|
pipe_[inpipe].iobuf += rtrim(pipe_[inpipe].readbuf, "\r");
|
2009-08-29 23:42:06 +00:00
|
|
|
|
|
|
|
// Commit any commands read
|
2009-08-31 23:32:46 +00:00
|
|
|
while (pipe_[inpipe].iobuf.find('\n') != string::npos) {
|
2009-08-29 23:42:06 +00:00
|
|
|
// split() grabs the entire string if
|
|
|
|
// the delim /wasn't/ found. ?:-P
|
|
|
|
string cmd;
|
2009-08-31 23:32:46 +00:00
|
|
|
pipe_[inpipe].iobuf = split(pipe_[inpipe].iobuf, cmd, '\n');
|
2009-08-29 23:42:06 +00:00
|
|
|
cmd = rtrim(cmd, "\r");
|
2009-08-31 23:32:46 +00:00
|
|
|
LYXERR(Debug::LYXSERVER, "LyXComm: nbytes:" << nbytes
|
|
|
|
<< ", iobuf:" << pipe_[inpipe].iobuf
|
2009-08-29 23:42:06 +00:00
|
|
|
<< ", cmd:" << cmd);
|
|
|
|
if (!cmd.empty())
|
|
|
|
clientcb_(client_, cmd);
|
|
|
|
//\n or not \n?
|
2009-08-21 22:58:38 +00:00
|
|
|
}
|
2009-08-29 23:42:06 +00:00
|
|
|
// Signal that we are done.
|
|
|
|
pipe_[inpipe].nbytes = 0;
|
2009-08-21 22:58:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void LyXComm::send(string const & msg)
|
|
|
|
{
|
|
|
|
if (msg.empty()) {
|
2009-08-31 23:32:46 +00:00
|
|
|
lyxerr << "LyXComm: Request to send empty string. Ignoring."
|
|
|
|
<< endl;
|
2009-08-21 22:58:38 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
LYXERR(Debug::LYXSERVER, "LyXComm: Sending '" << msg << '\'');
|
|
|
|
|
2009-08-29 23:42:06 +00:00
|
|
|
if (pipename_.empty())
|
|
|
|
return;
|
2009-08-21 22:58:38 +00:00
|
|
|
|
|
|
|
if (!ready_) {
|
2009-08-31 23:32:46 +00:00
|
|
|
lyxerr << "LyXComm: Pipes are closed. Could not send "
|
|
|
|
<< msg << endl;
|
2009-08-21 22:58:38 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2009-08-31 23:32:46 +00:00
|
|
|
// Request ownership of the outbuf_mutex_
|
|
|
|
DWORD result = WaitForSingleObject(outbuf_mutex_, PIPE_TIMEOUT);
|
|
|
|
|
|
|
|
if (result == WAIT_OBJECT_0) {
|
|
|
|
// If a client doesn't care to read a reply (JabRef is one
|
|
|
|
// such client), the output buffer could grow without limit.
|
|
|
|
// So, we empty it when its size is larger than PIPE_BUFSIZE.
|
|
|
|
if (outbuf_.size() > PIPE_BUFSIZE)
|
|
|
|
outbuf_.erase();
|
|
|
|
outbuf_ += msg;
|
|
|
|
ReleaseMutex(outbuf_mutex_);
|
2009-08-29 23:42:06 +00:00
|
|
|
} else {
|
2009-08-31 23:32:46 +00:00
|
|
|
// Something is fishy, better resetting the connection.
|
2009-09-05 16:39:33 +00:00
|
|
|
DWORD const error = GetLastError();
|
2009-08-31 23:32:46 +00:00
|
|
|
lyxerr << "LyXComm: Error sending message: " << msg
|
2009-09-05 16:39:33 +00:00
|
|
|
<< "\nLyXComm: " << errormsg(error)
|
|
|
|
<< "\nLyXComm: Resetting connection" << endl;
|
2009-08-31 23:32:46 +00:00
|
|
|
ReleaseMutex(outbuf_mutex_);
|
2009-08-21 22:58:38 +00:00
|
|
|
closeConnection();
|
2009-09-06 14:26:59 +00:00
|
|
|
openConnection();
|
2009-08-21 22:58:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-08-29 23:42:06 +00:00
|
|
|
string const LyXComm::pipeName(DWORD index) const
|
|
|
|
{
|
2009-08-31 23:32:46 +00:00
|
|
|
return index < MAX_CLIENTS ? inPipeName() : outPipeName();
|
2009-08-29 23:42:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-08-21 22:58:38 +00:00
|
|
|
#elif !defined (HAVE_MKFIFO)
|
2005-05-02 13:35:30 +00:00
|
|
|
// We provide a stub class that disables the lyxserver.
|
|
|
|
|
2007-12-12 19:28:07 +00:00
|
|
|
LyXComm::LyXComm(string const &, Server *, ClientCallbackfct)
|
2007-09-16 20:58:22 +00:00
|
|
|
{}
|
|
|
|
|
2012-10-27 13:45:27 +00:00
|
|
|
|
2005-05-02 13:35:30 +00:00
|
|
|
void LyXComm::openConnection()
|
|
|
|
{}
|
|
|
|
|
|
|
|
|
|
|
|
void LyXComm::closeConnection()
|
|
|
|
{}
|
|
|
|
|
|
|
|
|
|
|
|
int LyXComm::startPipe(string const & filename, bool write)
|
|
|
|
{
|
|
|
|
return -1;
|
1999-09-27 18:44:28 +00:00
|
|
|
}
|
2005-05-02 13:35:30 +00:00
|
|
|
|
|
|
|
|
|
|
|
void LyXComm::endPipe(int & fd, string const & filename, bool write)
|
|
|
|
{}
|
|
|
|
|
|
|
|
|
|
|
|
void LyXComm::emergencyCleanup()
|
|
|
|
{}
|
|
|
|
|
2012-10-27 13:45:27 +00:00
|
|
|
|
2005-05-02 13:35:30 +00:00
|
|
|
void LyXComm::read_ready()
|
|
|
|
{}
|
|
|
|
|
|
|
|
|
|
|
|
void LyXComm::send(string const & msg)
|
|
|
|
{}
|
|
|
|
|
|
|
|
|
|
|
|
#else // defined (HAVE_MKFIFO)
|
1999-09-27 18:44:28 +00:00
|
|
|
|
2007-12-12 19:28:07 +00:00
|
|
|
LyXComm::LyXComm(string const & pip, Server * cli, ClientCallbackfct ccb)
|
2017-07-06 12:46:03 +00:00
|
|
|
: infd_(-1), outfd_(-1),
|
|
|
|
ready_(false), pipename_(pip), client_(cli), clientcb_(ccb),
|
|
|
|
deferred_loading_(false)
|
2007-09-16 20:58:22 +00:00
|
|
|
{
|
|
|
|
openConnection();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-09-09 22:02:19 +00:00
|
|
|
void LyXComm::openConnection()
|
|
|
|
{
|
2007-11-15 20:04:51 +00:00
|
|
|
LYXERR(Debug::LYXSERVER, "LyXComm: Opening connection");
|
2002-03-21 17:27:08 +00:00
|
|
|
|
1999-09-27 18:44:28 +00:00
|
|
|
// If we are up, that's an error
|
2007-09-16 20:58:22 +00:00
|
|
|
if (ready_) {
|
1999-10-07 18:44:17 +00:00
|
|
|
lyxerr << "LyXComm: Already connected" << endl;
|
1999-09-27 18:44:28 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
// We assume that we don't make it
|
2007-09-16 20:58:22 +00:00
|
|
|
ready_ = false;
|
2002-03-21 17:27:08 +00:00
|
|
|
|
2007-09-16 20:58:22 +00:00
|
|
|
if (pipename_.empty()) {
|
2007-11-15 20:04:51 +00:00
|
|
|
LYXERR(Debug::LYXSERVER, "LyXComm: server is disabled, nothing to do");
|
2001-11-14 12:00:51 +00:00
|
|
|
return;
|
|
|
|
}
|
1999-09-27 18:44:28 +00:00
|
|
|
|
2007-09-16 20:58:22 +00:00
|
|
|
infd_ = startPipe(inPipeName(), false);
|
|
|
|
if (infd_ == -1)
|
2001-10-19 15:13:49 +00:00
|
|
|
return;
|
|
|
|
|
2007-09-16 20:58:22 +00:00
|
|
|
outfd_ = startPipe(outPipeName(), true);
|
|
|
|
if (outfd_ == -1) {
|
|
|
|
endPipe(infd_, inPipeName(), false);
|
2001-10-19 15:13:49 +00:00
|
|
|
return;
|
|
|
|
}
|
2002-03-21 17:27:08 +00:00
|
|
|
|
2007-09-16 20:58:22 +00:00
|
|
|
if (fcntl(outfd_, F_SETFL, O_NONBLOCK) < 0) {
|
2001-11-12 17:43:21 +00:00
|
|
|
lyxerr << "LyXComm: Could not set flags on pipe " << outPipeName()
|
2001-10-19 15:13:49 +00:00
|
|
|
<< '\n' << strerror(errno) << endl;
|
|
|
|
return;
|
|
|
|
}
|
2002-03-21 17:27:08 +00:00
|
|
|
|
2001-10-19 15:13:49 +00:00
|
|
|
// We made it!
|
2007-09-16 20:58:22 +00:00
|
|
|
ready_ = true;
|
2007-11-15 20:04:51 +00:00
|
|
|
LYXERR(Debug::LYXSERVER, "LyXComm: Connection established");
|
2001-10-19 15:13:49 +00:00
|
|
|
}
|
|
|
|
|
2002-03-21 17:27:08 +00:00
|
|
|
|
2001-10-19 15:13:49 +00:00
|
|
|
/// Close pipes
|
|
|
|
void LyXComm::closeConnection()
|
|
|
|
{
|
2007-11-15 20:04:51 +00:00
|
|
|
LYXERR(Debug::LYXSERVER, "LyXComm: Closing connection");
|
2001-10-19 15:13:49 +00:00
|
|
|
|
2007-09-16 20:58:22 +00:00
|
|
|
if (pipename_.empty()) {
|
2007-11-15 20:04:51 +00:00
|
|
|
LYXERR(Debug::LYXSERVER, "LyXComm: server is disabled, nothing to do");
|
2002-03-21 17:27:08 +00:00
|
|
|
return;
|
2001-10-19 15:13:49 +00:00
|
|
|
}
|
|
|
|
|
2007-09-16 20:58:22 +00:00
|
|
|
if (!ready_) {
|
2007-11-30 22:02:33 +00:00
|
|
|
LYXERR0("LyXComm: Already disconnected");
|
2001-10-19 15:13:49 +00:00
|
|
|
return;
|
|
|
|
}
|
2002-03-21 17:27:08 +00:00
|
|
|
|
2007-09-16 20:58:22 +00:00
|
|
|
endPipe(infd_, inPipeName(), false);
|
|
|
|
endPipe(outfd_, outPipeName(), true);
|
2002-03-21 17:27:08 +00:00
|
|
|
|
2007-09-16 20:58:22 +00:00
|
|
|
ready_ = false;
|
2001-10-19 15:13:49 +00:00
|
|
|
}
|
|
|
|
|
2003-10-06 15:43:21 +00:00
|
|
|
|
2006-11-26 21:30:39 +00:00
|
|
|
int LyXComm::startPipe(string const & file, bool write)
|
2001-10-19 15:13:49 +00:00
|
|
|
{
|
2008-11-26 01:22:05 +00:00
|
|
|
static bool stalepipe = false;
|
2006-11-26 21:30:39 +00:00
|
|
|
FileName const filename(file);
|
2008-11-26 01:22:05 +00:00
|
|
|
if (filename.exists()) {
|
|
|
|
if (!write) {
|
|
|
|
// Let's see whether we have a stale pipe.
|
|
|
|
int fd = ::open(filename.toFilesystemEncoding().c_str(),
|
|
|
|
O_WRONLY | O_NONBLOCK);
|
|
|
|
if (fd >= 0) {
|
|
|
|
// Another LyX instance is using it.
|
2008-11-26 19:05:35 +00:00
|
|
|
::close(fd);
|
2010-11-13 11:55:05 +00:00
|
|
|
// Tell the running instance to load the files
|
|
|
|
if (run_mode == USE_REMOTE && loadFilesInOtherInstance()) {
|
|
|
|
deferred_loading_ = true;
|
|
|
|
pipename_.erase();
|
|
|
|
return -1;
|
|
|
|
}
|
2008-11-26 01:22:05 +00:00
|
|
|
} else if (errno == ENXIO) {
|
|
|
|
// No process is reading from the other end.
|
|
|
|
stalepipe = true;
|
|
|
|
LYXERR(Debug::LYXSERVER,
|
|
|
|
"LyXComm: trying to remove "
|
|
|
|
<< filename);
|
|
|
|
filename.removeFile();
|
|
|
|
}
|
|
|
|
} else if (stalepipe) {
|
|
|
|
LYXERR(Debug::LYXSERVER, "LyXComm: trying to remove "
|
|
|
|
<< filename);
|
|
|
|
filename.removeFile();
|
|
|
|
stalepipe = false;
|
|
|
|
}
|
|
|
|
if (filename.exists()) {
|
|
|
|
lyxerr << "LyXComm: Pipe " << filename
|
|
|
|
<< " already exists.\nIf no other LyX program"
|
|
|
|
" is active, please delete the pipe by hand"
|
|
|
|
" and try again."
|
|
|
|
<< endl;
|
|
|
|
pipename_.erase();
|
|
|
|
return -1;
|
|
|
|
}
|
1999-09-27 18:44:28 +00:00
|
|
|
}
|
2002-03-21 17:27:08 +00:00
|
|
|
|
2006-11-26 21:30:39 +00:00
|
|
|
if (::mkfifo(filename.toFilesystemEncoding().c_str(), 0600) < 0) {
|
2001-10-19 15:13:49 +00:00
|
|
|
lyxerr << "LyXComm: Could not create pipe " << filename << '\n'
|
1999-10-07 18:44:17 +00:00
|
|
|
<< strerror(errno) << endl;
|
2001-10-19 15:13:49 +00:00
|
|
|
return -1;
|
2007-09-16 20:58:22 +00:00
|
|
|
}
|
2006-11-26 21:30:39 +00:00
|
|
|
int const fd = ::open(filename.toFilesystemEncoding().c_str(),
|
2010-03-16 07:55:22 +00:00
|
|
|
write ? (O_RDWR) : (O_RDONLY|O_NONBLOCK));
|
2002-03-21 17:27:08 +00:00
|
|
|
|
2001-10-19 15:13:49 +00:00
|
|
|
if (fd < 0) {
|
|
|
|
lyxerr << "LyXComm: Could not open pipe " << filename << '\n'
|
1999-10-07 18:44:17 +00:00
|
|
|
<< strerror(errno) << endl;
|
2007-11-28 09:01:49 +00:00
|
|
|
filename.removeFile();
|
2001-10-19 15:13:49 +00:00
|
|
|
return -1;
|
2001-09-09 22:02:19 +00:00
|
|
|
}
|
2002-03-21 17:27:08 +00:00
|
|
|
|
2002-07-14 01:44:15 +00:00
|
|
|
if (!write) {
|
2017-05-28 11:25:53 +00:00
|
|
|
// Make sure not to call read_ready after destruction.
|
|
|
|
weak_ptr<void> tracker = tracker_.p();
|
|
|
|
theApp()->registerSocketCallback(fd, [=](){
|
|
|
|
if (!tracker.expired())
|
|
|
|
read_ready();
|
|
|
|
});
|
2002-07-14 01:44:15 +00:00
|
|
|
}
|
2002-03-21 17:27:08 +00:00
|
|
|
|
2001-10-19 15:13:49 +00:00
|
|
|
return fd;
|
1999-09-27 18:44:28 +00:00
|
|
|
}
|
2001-09-09 22:02:19 +00:00
|
|
|
|
2001-10-19 15:13:49 +00:00
|
|
|
|
2002-07-30 22:35:43 +00:00
|
|
|
void LyXComm::endPipe(int & fd, string const & filename, bool write)
|
2001-09-09 22:02:19 +00:00
|
|
|
{
|
2001-10-19 15:13:49 +00:00
|
|
|
if (fd < 0)
|
|
|
|
return;
|
|
|
|
|
2007-09-16 20:58:22 +00:00
|
|
|
if (!write)
|
2006-11-30 08:42:29 +00:00
|
|
|
theApp()->unregisterSocketCallback(fd);
|
2002-11-27 10:30:28 +00:00
|
|
|
|
2001-10-19 15:13:49 +00:00
|
|
|
if (::close(fd) < 0) {
|
|
|
|
lyxerr << "LyXComm: Could not close pipe " << filename
|
|
|
|
<< '\n' << strerror(errno) << endl;
|
1999-09-27 18:44:28 +00:00
|
|
|
}
|
2002-03-21 17:27:08 +00:00
|
|
|
|
2014-11-01 15:02:34 +00:00
|
|
|
if (!FileName(filename).removeFile()) {
|
2001-10-19 15:13:49 +00:00
|
|
|
lyxerr << "LyXComm: Could not remove pipe " << filename
|
|
|
|
<< '\n' << strerror(errno) << endl;
|
2007-09-16 20:58:22 +00:00
|
|
|
}
|
2001-11-08 11:46:06 +00:00
|
|
|
|
|
|
|
fd = -1;
|
2001-10-19 15:13:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void LyXComm::emergencyCleanup()
|
|
|
|
{
|
2007-09-16 20:58:22 +00:00
|
|
|
if (!pipename_.empty()) {
|
|
|
|
endPipe(infd_, inPipeName(), false);
|
|
|
|
endPipe(outfd_, outPipeName(), true);
|
2001-11-14 12:00:51 +00:00
|
|
|
}
|
1999-09-27 18:44:28 +00:00
|
|
|
}
|
2001-09-09 22:02:19 +00:00
|
|
|
|
|
|
|
|
1999-09-27 18:44:28 +00:00
|
|
|
// Receives messages and sends then to client
|
2002-07-14 01:44:15 +00:00
|
|
|
void LyXComm::read_ready()
|
1999-09-27 18:44:28 +00:00
|
|
|
{
|
2007-09-16 20:58:22 +00:00
|
|
|
// FIXME: make read_buffer_ a class-member for multiple sessions
|
2002-09-11 16:48:07 +00:00
|
|
|
static string read_buffer_;
|
|
|
|
read_buffer_.erase();
|
2002-03-21 17:27:08 +00:00
|
|
|
|
2002-09-11 16:48:07 +00:00
|
|
|
int const charbuf_size = 100;
|
|
|
|
char charbuf[charbuf_size];
|
1999-09-27 18:44:28 +00:00
|
|
|
|
2008-10-05 00:50:39 +00:00
|
|
|
// As O_NONBLOCK is set, until no data is available for reading,
|
|
|
|
// read() doesn't block but returns -1 and set errno to EAGAIN.
|
|
|
|
// After a client that opened the pipe for writing, closes it
|
|
|
|
// (and no other client is using the pipe), read() would always
|
|
|
|
// return 0 and thus the connection has to be reset.
|
|
|
|
|
1999-09-27 18:44:28 +00:00
|
|
|
errno = 0;
|
|
|
|
int status;
|
|
|
|
// the single = is intended here.
|
2007-09-16 20:58:22 +00:00
|
|
|
while ((status = ::read(infd_, charbuf, charbuf_size - 1))) {
|
2002-07-14 01:44:15 +00:00
|
|
|
if (status > 0) {
|
2002-09-11 16:48:07 +00:00
|
|
|
charbuf[status] = '\0'; // turn it into a c string
|
|
|
|
read_buffer_ += rtrim(charbuf, "\r");
|
2003-02-06 01:18:36 +00:00
|
|
|
// commit any commands read
|
|
|
|
while (read_buffer_.find('\n') != string::npos) {
|
|
|
|
// split() grabs the entire string if
|
|
|
|
// the delim /wasn't/ found. ?:-P
|
|
|
|
string cmd;
|
|
|
|
read_buffer_= split(read_buffer_, cmd,'\n');
|
2007-11-15 20:04:51 +00:00
|
|
|
LYXERR(Debug::LYXSERVER, "LyXComm: status:" << status
|
2003-02-06 01:18:36 +00:00
|
|
|
<< ", read_buffer_:" << read_buffer_
|
2007-11-15 20:04:51 +00:00
|
|
|
<< ", cmd:" << cmd);
|
2003-02-06 01:18:36 +00:00
|
|
|
if (!cmd.empty())
|
2007-09-16 20:58:22 +00:00
|
|
|
clientcb_(client_, cmd);
|
2003-02-06 01:18:36 +00:00
|
|
|
//\n or not \n?
|
|
|
|
}
|
2008-10-05 00:50:39 +00:00
|
|
|
} else {
|
|
|
|
if (errno == EAGAIN) {
|
|
|
|
// Nothing to read, continue
|
|
|
|
errno = 0;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// An error occurred, better bailing out
|
2007-11-30 22:02:33 +00:00
|
|
|
LYXERR0("LyXComm: " << strerror(errno));
|
2002-09-11 16:48:07 +00:00
|
|
|
if (!read_buffer_.empty()) {
|
2007-11-30 22:02:33 +00:00
|
|
|
LYXERR0("LyXComm: truncated command: " << read_buffer_);
|
2002-09-11 16:48:07 +00:00
|
|
|
read_buffer_.erase();
|
1999-09-27 18:44:28 +00:00
|
|
|
}
|
2003-02-06 01:18:36 +00:00
|
|
|
break; // reset connection
|
1999-09-27 18:44:28 +00:00
|
|
|
}
|
|
|
|
}
|
2002-09-11 16:48:07 +00:00
|
|
|
|
2008-10-05 00:50:39 +00:00
|
|
|
// The connection gets reset when read() returns 0 (meaning that the
|
|
|
|
// last client closed the pipe) or an error occurred, in which case
|
|
|
|
// read() returns -1 and errno != EAGAIN.
|
2003-02-06 01:18:36 +00:00
|
|
|
closeConnection();
|
|
|
|
openConnection();
|
2002-09-11 16:48:07 +00:00
|
|
|
errno = 0;
|
1999-09-27 18:44:28 +00:00
|
|
|
}
|
1999-10-25 14:18:30 +00:00
|
|
|
|
|
|
|
|
2001-09-09 22:02:19 +00:00
|
|
|
void LyXComm::send(string const & msg)
|
|
|
|
{
|
1999-09-27 18:44:28 +00:00
|
|
|
if (msg.empty()) {
|
2007-11-30 22:02:33 +00:00
|
|
|
LYXERR0("LyXComm: Request to send empty string. Ignoring.");
|
1999-09-27 18:44:28 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2007-11-15 20:04:51 +00:00
|
|
|
LYXERR(Debug::LYXSERVER, "LyXComm: Sending '" << msg << '\'');
|
1999-09-27 18:44:28 +00:00
|
|
|
|
2012-10-27 13:45:27 +00:00
|
|
|
if (pipename_.empty())
|
|
|
|
return;
|
1999-09-27 18:44:28 +00:00
|
|
|
|
2007-09-16 20:58:22 +00:00
|
|
|
if (!ready_) {
|
2007-11-30 22:02:33 +00:00
|
|
|
LYXERR0("LyXComm: Pipes are closed. Could not send " << msg);
|
2007-09-16 20:58:22 +00:00
|
|
|
} else if (::write(outfd_, msg.c_str(), msg.length()) < 0) {
|
1999-10-07 18:44:17 +00:00
|
|
|
lyxerr << "LyXComm: Error sending message: " << msg
|
|
|
|
<< '\n' << strerror(errno)
|
|
|
|
<< "\nLyXComm: Resetting connection" << endl;
|
1999-09-27 18:44:28 +00:00
|
|
|
closeConnection();
|
|
|
|
openConnection();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-05-02 13:35:30 +00:00
|
|
|
#endif // defined (HAVE_MKFIFO)
|
|
|
|
|
2010-11-13 11:55:05 +00:00
|
|
|
namespace {
|
|
|
|
|
|
|
|
struct Sleep : QThread
|
|
|
|
{
|
|
|
|
static void millisec(unsigned long ms)
|
|
|
|
{
|
|
|
|
QThread::usleep(ms * 1000);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2017-07-23 11:11:54 +00:00
|
|
|
} // namespace
|
2010-11-13 11:55:05 +00:00
|
|
|
|
|
|
|
|
|
|
|
bool LyXComm::loadFilesInOtherInstance()
|
|
|
|
{
|
2020-04-17 01:05:38 +00:00
|
|
|
if (theFilesToLoad().empty())
|
2020-04-08 21:50:09 +00:00
|
|
|
return true;
|
|
|
|
|
2010-11-13 11:55:05 +00:00
|
|
|
int pipefd;
|
|
|
|
int loaded_files = 0;
|
|
|
|
FileName const pipe(inPipeName());
|
|
|
|
vector<string>::iterator it = theFilesToLoad().begin();
|
|
|
|
while (it != theFilesToLoad().end()) {
|
|
|
|
FileName fname = fileSearch(string(), os::internal_path(*it),
|
|
|
|
"lyx", may_not_exist);
|
|
|
|
if (fname.empty()) {
|
|
|
|
++it;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
// Wait a while to allow time for the other
|
|
|
|
// instance to reset the connection
|
|
|
|
Sleep::millisec(200);
|
|
|
|
pipefd = ::open(pipe.toFilesystemEncoding().c_str(), O_WRONLY);
|
|
|
|
if (pipefd < 0)
|
|
|
|
break;
|
|
|
|
string const cmd = "LYXCMD:pipe:file-open:" +
|
|
|
|
fname.absFileName() + '\n';
|
2011-05-10 15:03:49 +00:00
|
|
|
if (::write(pipefd, cmd.c_str(), cmd.length()) < 0)
|
|
|
|
LYXERR0("Cannot write to pipe!");
|
2010-11-13 11:55:05 +00:00
|
|
|
::close(pipefd);
|
|
|
|
++loaded_files;
|
|
|
|
it = theFilesToLoad().erase(it);
|
|
|
|
}
|
|
|
|
return loaded_files > 0;
|
|
|
|
}
|
|
|
|
|
1999-09-27 18:44:28 +00:00
|
|
|
|
2005-01-20 15:07:36 +00:00
|
|
|
string const LyXComm::inPipeName() const
|
|
|
|
{
|
2007-09-16 20:58:22 +00:00
|
|
|
return pipename_ + ".in";
|
2005-01-20 15:07:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
string const LyXComm::outPipeName() const
|
|
|
|
{
|
2007-09-16 20:58:22 +00:00
|
|
|
return pipename_ + ".out";
|
2005-01-20 15:07:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-09-16 20:58:22 +00:00
|
|
|
/////////////////////////////////////////////////////////////////////
|
|
|
|
//
|
|
|
|
// Server
|
|
|
|
//
|
|
|
|
/////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
void ServerCallback(Server * server, string const & msg)
|
|
|
|
{
|
|
|
|
server->callback(msg);
|
|
|
|
}
|
|
|
|
|
2010-02-09 16:11:13 +00:00
|
|
|
Server::Server(string const & pipes)
|
|
|
|
: numclients_(0), pipes_(pipes, this, &ServerCallback)
|
2007-09-16 20:58:22 +00:00
|
|
|
{}
|
|
|
|
|
1999-09-27 18:44:28 +00:00
|
|
|
|
2007-04-29 16:22:46 +00:00
|
|
|
Server::~Server()
|
1999-09-27 18:44:28 +00:00
|
|
|
{
|
|
|
|
// say goodbye to clients so they stop sending messages
|
2007-09-16 20:58:22 +00:00
|
|
|
// send as many bye messages as there are clients,
|
|
|
|
// each with client's name.
|
1999-10-02 16:21:10 +00:00
|
|
|
string message;
|
2007-09-16 20:58:22 +00:00
|
|
|
for (int i = 0; i != numclients_; ++i) {
|
|
|
|
message = "LYXSRV:" + clients_[i] + ":bye\n";
|
|
|
|
pipes_.send(message);
|
1999-09-27 18:44:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-11-30 22:02:33 +00:00
|
|
|
int compare(char const * a, char const * b, unsigned int len)
|
|
|
|
{
|
|
|
|
return strncmp(a, b, len);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-09-16 20:58:22 +00:00
|
|
|
// Handle data gotten from communication, called by LyXComm
|
|
|
|
void Server::callback(string const & msg)
|
1999-09-27 18:44:28 +00:00
|
|
|
{
|
2007-11-15 20:04:51 +00:00
|
|
|
LYXERR(Debug::LYXSERVER, "Server: Received: '" << msg << '\'');
|
2002-03-21 17:27:08 +00:00
|
|
|
|
2000-09-26 13:54:57 +00:00
|
|
|
char const * p = msg.c_str();
|
2002-03-21 17:27:08 +00:00
|
|
|
|
1999-09-27 18:44:28 +00:00
|
|
|
// --- parse the string --------------------------------------------
|
|
|
|
//
|
|
|
|
// Format: LYXCMD:<client>:<func>:<argstring>\n
|
|
|
|
//
|
|
|
|
bool server_only = false;
|
2001-12-05 08:04:20 +00:00
|
|
|
while (*p) {
|
1999-09-27 18:44:28 +00:00
|
|
|
// --- 1. check 'header' ---
|
2000-04-11 16:57:16 +00:00
|
|
|
|
2002-03-21 17:27:08 +00:00
|
|
|
if (compare(p, "LYXSRV:", 7) == 0) {
|
|
|
|
server_only = true;
|
2001-02-16 09:25:43 +00:00
|
|
|
} else if (0 != compare(p, "LYXCMD:", 7)) {
|
2007-04-29 16:22:46 +00:00
|
|
|
lyxerr << "Server: Unknown request \""
|
2002-11-27 10:30:28 +00:00
|
|
|
<< p << '"' << endl;
|
1999-09-27 18:44:28 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
p += 7;
|
2002-03-21 17:27:08 +00:00
|
|
|
|
1999-09-27 18:44:28 +00:00
|
|
|
// --- 2. for the moment ignore the client name ---
|
1999-10-02 16:21:10 +00:00
|
|
|
string client;
|
2001-12-05 08:04:20 +00:00
|
|
|
while (*p && *p != ':')
|
1999-09-27 18:44:28 +00:00
|
|
|
client += char(*p++);
|
2007-09-16 20:58:22 +00:00
|
|
|
if (*p == ':')
|
|
|
|
++p;
|
|
|
|
if (!*p)
|
|
|
|
return;
|
2002-03-21 17:27:08 +00:00
|
|
|
|
1999-09-27 18:44:28 +00:00
|
|
|
// --- 3. get function name ---
|
1999-10-02 16:21:10 +00:00
|
|
|
string cmd;
|
2001-12-05 08:04:20 +00:00
|
|
|
while (*p && *p != ':')
|
1999-09-27 18:44:28 +00:00
|
|
|
cmd += char(*p++);
|
2002-03-21 17:27:08 +00:00
|
|
|
|
1999-09-27 18:44:28 +00:00
|
|
|
// --- 4. parse the argument ---
|
1999-10-02 16:21:10 +00:00
|
|
|
string arg;
|
2000-11-04 10:00:12 +00:00
|
|
|
if (!server_only && *p == ':' && *(++p)) {
|
2001-12-05 08:04:20 +00:00
|
|
|
while (*p && *p != '\n')
|
1999-09-27 18:44:28 +00:00
|
|
|
arg += char(*p++);
|
2000-11-04 10:00:12 +00:00
|
|
|
if (*p) ++p;
|
1999-09-27 18:44:28 +00:00
|
|
|
}
|
2002-03-21 17:27:08 +00:00
|
|
|
|
2007-11-15 20:04:51 +00:00
|
|
|
LYXERR(Debug::LYXSERVER, "Server: Client: '" << client
|
|
|
|
<< "' Command: '" << cmd << "' Argument: '" << arg << '\'');
|
2002-03-21 17:27:08 +00:00
|
|
|
|
1999-09-27 18:44:28 +00:00
|
|
|
// --- lookup and exec the command ------------------
|
2002-03-21 17:27:08 +00:00
|
|
|
|
1999-09-27 18:44:28 +00:00
|
|
|
if (server_only) {
|
1999-10-02 16:21:10 +00:00
|
|
|
string buf;
|
2002-03-21 17:27:08 +00:00
|
|
|
// return the greeting to inform the client that
|
1999-09-27 18:44:28 +00:00
|
|
|
// we are listening.
|
|
|
|
if (cmd == "hello") {
|
|
|
|
// One more client
|
2007-09-16 20:58:22 +00:00
|
|
|
if (numclients_ == MAX_CLIENTS) { //paranoid check
|
2007-11-15 20:04:51 +00:00
|
|
|
LYXERR(Debug::LYXSERVER, "Server: too many clients...");
|
1999-09-27 18:44:28 +00:00
|
|
|
return;
|
|
|
|
}
|
2007-09-16 20:58:22 +00:00
|
|
|
int i = 0;
|
|
|
|
while (!clients_[i].empty() && i < numclients_)
|
2000-01-24 18:34:46 +00:00
|
|
|
++i;
|
2007-09-16 20:58:22 +00:00
|
|
|
clients_[i] = client;
|
|
|
|
++numclients_;
|
1999-09-27 18:44:28 +00:00
|
|
|
buf = "LYXSRV:" + client + ":hello\n";
|
2007-11-15 20:04:51 +00:00
|
|
|
LYXERR(Debug::LYXSERVER, "Server: Greeting " << client);
|
2007-09-16 20:58:22 +00:00
|
|
|
pipes_.send(buf);
|
1999-09-27 18:44:28 +00:00
|
|
|
} else if (cmd == "bye") {
|
2007-09-16 20:58:22 +00:00
|
|
|
// If clients_ == 0 maybe we should reset the pipes
|
1999-09-27 18:44:28 +00:00
|
|
|
// to prevent fake callbacks
|
2000-01-24 18:34:46 +00:00
|
|
|
int i = 0; //look if client is registered
|
2007-09-16 20:58:22 +00:00
|
|
|
for (; i < numclients_; ++i) {
|
|
|
|
if (clients_[i] == client)
|
|
|
|
break;
|
1999-09-27 18:44:28 +00:00
|
|
|
}
|
2007-09-16 20:58:22 +00:00
|
|
|
if (i < numclients_) {
|
|
|
|
--numclients_;
|
|
|
|
clients_[i].erase();
|
2007-11-15 20:04:51 +00:00
|
|
|
LYXERR(Debug::LYXSERVER, "Server: Client "
|
|
|
|
<< client << " said goodbye");
|
1999-09-27 18:44:28 +00:00
|
|
|
} else {
|
2007-11-15 20:04:51 +00:00
|
|
|
LYXERR(Debug::LYXSERVER,
|
2017-09-22 07:29:16 +00:00
|
|
|
"Server: ignoring bye message from unregistered client" << client);
|
1999-09-27 18:44:28 +00:00
|
|
|
}
|
|
|
|
} else {
|
2007-11-30 22:02:33 +00:00
|
|
|
LYXERR0("Server: Undefined server command " << cmd << '.');
|
1999-09-27 18:44:28 +00:00
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
2002-03-21 17:27:08 +00:00
|
|
|
|
1999-09-27 18:44:28 +00:00
|
|
|
if (!cmd.empty()) {
|
|
|
|
// which lyxfunc should we let it connect to?
|
|
|
|
// The correct solution would be to have a
|
|
|
|
// specialized (non-gui) BufferView. But how do
|
|
|
|
// we do it now? Probably we should just let it
|
2010-02-09 17:06:40 +00:00
|
|
|
// connect to the lyxfunc in the single GuiView we
|
1999-09-27 18:44:28 +00:00
|
|
|
// support currently. (Lgb)
|
|
|
|
|
2018-07-02 08:49:58 +00:00
|
|
|
FuncRequest fr(lyxaction.lookupFunc(cmd), from_utf8(arg));
|
2012-04-13 00:57:25 +00:00
|
|
|
fr.setOrigin(FuncRequest::LYXSERVER);
|
2010-01-29 11:22:33 +00:00
|
|
|
DispatchResult dr;
|
2010-02-09 16:11:13 +00:00
|
|
|
theApp()->dispatch(fr, dr);
|
2010-01-29 11:22:33 +00:00
|
|
|
string const rval = to_utf8(dr.message());
|
1999-09-27 18:44:28 +00:00
|
|
|
|
2007-09-16 20:58:22 +00:00
|
|
|
// all commands produce an INFO or ERROR message
|
|
|
|
// in the output pipe, even if they do not return
|
|
|
|
// anything. See chapter 4 of Customization doc.
|
2002-05-28 23:04:57 +00:00
|
|
|
string buf;
|
2010-01-29 11:22:33 +00:00
|
|
|
if (dr.error())
|
1999-09-27 18:44:28 +00:00
|
|
|
buf = "ERROR:";
|
|
|
|
else
|
|
|
|
buf = "INFO:";
|
2002-11-27 10:30:28 +00:00
|
|
|
buf += client + ':' + cmd + ':' + rval + '\n';
|
2007-09-16 20:58:22 +00:00
|
|
|
pipes_.send(buf);
|
1999-09-27 18:44:28 +00:00
|
|
|
|
|
|
|
// !!! we don't do any error checking -
|
|
|
|
// if the client won't listen, the
|
|
|
|
// message is lost and others too
|
|
|
|
// maybe; so the client should empty
|
|
|
|
// the outpipe before issuing a request.
|
2002-03-21 17:27:08 +00:00
|
|
|
|
1999-09-27 18:44:28 +00:00
|
|
|
// not found
|
|
|
|
}
|
2007-09-16 20:58:22 +00:00
|
|
|
} // while *p
|
1999-09-27 18:44:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-07-07 17:26:07 +00:00
|
|
|
// Send a notify message to a client, called by WorkAreaKeyPress
|
2007-04-29 16:22:46 +00:00
|
|
|
void Server::notifyClient(string const & s)
|
1999-09-27 18:44:28 +00:00
|
|
|
{
|
2007-09-16 20:58:22 +00:00
|
|
|
pipes_.send("NOTIFY:" + s + "\n");
|
1999-09-27 18:44:28 +00:00
|
|
|
}
|
2006-10-21 00:16:43 +00:00
|
|
|
|
|
|
|
|
|
|
|
} // namespace lyx
|
2009-08-21 22:58:38 +00:00
|
|
|
|
|
|
|
#ifdef _WIN32
|
|
|
|
#include "moc_Server.cpp"
|
|
|
|
#endif
|