2012-10-24 00:55:44 +04:00
|
|
|
#include "remote.hh"
|
|
|
|
|
2013-03-12 21:53:18 +04:00
|
|
|
#include "buffer_manager.hh"
|
2015-06-06 13:54:48 +03:00
|
|
|
#include "buffer_utils.hh"
|
2013-04-09 22:05:40 +04:00
|
|
|
#include "client_manager.hh"
|
2013-03-12 21:53:18 +04:00
|
|
|
#include "command_manager.hh"
|
2013-04-09 22:05:40 +04:00
|
|
|
#include "display_buffer.hh"
|
|
|
|
#include "event_manager.hh"
|
2015-08-23 16:13:46 +03:00
|
|
|
#include "file.hh"
|
2017-03-07 04:12:37 +03:00
|
|
|
#include "hash_map.hh"
|
2017-01-21 15:14:44 +03:00
|
|
|
#include "optional.hh"
|
2016-11-29 22:53:11 +03:00
|
|
|
#include "user_interface.hh"
|
2012-10-24 00:55:44 +04:00
|
|
|
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/socket.h>
|
2013-03-13 22:59:39 +04:00
|
|
|
#include <sys/un.h>
|
|
|
|
#include <sys/wait.h>
|
|
|
|
#include <unistd.h>
|
2015-09-15 15:32:26 +03:00
|
|
|
#include <pwd.h>
|
2013-03-13 22:59:39 +04:00
|
|
|
#include <fcntl.h>
|
|
|
|
|
2012-10-24 00:55:44 +04:00
|
|
|
|
|
|
|
namespace Kakoune
|
|
|
|
{
|
|
|
|
|
2017-06-26 13:27:18 +03:00
|
|
|
enum class MessageType : uint8_t
|
2012-10-24 00:55:44 +04:00
|
|
|
{
|
2016-08-31 21:33:02 +03:00
|
|
|
Unknown,
|
2016-08-31 11:24:07 +03:00
|
|
|
Connect,
|
|
|
|
Command,
|
2012-10-24 00:55:44 +04:00
|
|
|
MenuShow,
|
|
|
|
MenuSelect,
|
|
|
|
MenuHide,
|
2012-12-14 22:04:34 +04:00
|
|
|
InfoShow,
|
|
|
|
InfoHide,
|
2014-04-15 22:19:44 +04:00
|
|
|
Draw,
|
2015-06-17 23:28:02 +03:00
|
|
|
DrawStatus,
|
2017-04-12 12:39:17 +03:00
|
|
|
SetCursor,
|
2014-11-11 02:29:16 +03:00
|
|
|
Refresh,
|
2016-08-31 11:24:07 +03:00
|
|
|
SetOptions,
|
2017-08-23 09:22:23 +03:00
|
|
|
Exit,
|
|
|
|
Key,
|
2012-10-24 00:55:44 +04:00
|
|
|
};
|
|
|
|
|
2016-08-31 21:33:02 +03:00
|
|
|
class MsgWriter
|
2012-10-24 00:55:44 +04:00
|
|
|
{
|
|
|
|
public:
|
2016-12-01 23:11:09 +03:00
|
|
|
MsgWriter(RemoteBuffer& buffer, MessageType type)
|
|
|
|
: m_buffer{buffer}, m_start{(uint32_t)buffer.size()}
|
2016-08-31 11:24:07 +03:00
|
|
|
{
|
|
|
|
write(type);
|
2016-08-31 21:33:02 +03:00
|
|
|
write((uint32_t)0); // message size, to be patched on write
|
2016-08-31 11:24:07 +03:00
|
|
|
}
|
|
|
|
|
2016-08-31 21:33:02 +03:00
|
|
|
~MsgWriter() noexcept(false)
|
2012-10-26 15:45:32 +04:00
|
|
|
{
|
2016-12-01 23:11:09 +03:00
|
|
|
uint32_t count = (uint32_t)m_buffer.size() - m_start;
|
2017-06-26 13:27:18 +03:00
|
|
|
memcpy(m_buffer.data() + m_start + sizeof(MessageType), &count, sizeof(uint32_t));
|
2012-10-26 15:45:32 +04:00
|
|
|
}
|
2012-10-24 00:55:44 +04:00
|
|
|
|
|
|
|
void write(const char* val, size_t size)
|
|
|
|
{
|
2016-12-01 23:11:09 +03:00
|
|
|
m_buffer.insert(m_buffer.end(), val, val + size);
|
2012-10-24 00:55:44 +04:00
|
|
|
}
|
|
|
|
|
2012-12-10 21:55:11 +04:00
|
|
|
template<typename T>
|
|
|
|
void write(const T& val)
|
|
|
|
{
|
|
|
|
write((const char*)&val, sizeof(val));
|
2013-04-03 21:20:38 +04:00
|
|
|
}
|
2012-10-24 00:55:44 +04:00
|
|
|
|
2014-04-30 22:39:52 +04:00
|
|
|
void write(StringView str)
|
2012-12-10 21:55:11 +04:00
|
|
|
{
|
|
|
|
write(str.length());
|
2014-04-30 22:39:52 +04:00
|
|
|
write(str.data(), (int)str.length());
|
2012-12-10 21:55:11 +04:00
|
|
|
};
|
2012-10-24 00:55:44 +04:00
|
|
|
|
2014-04-30 22:39:52 +04:00
|
|
|
void write(const String& str)
|
|
|
|
{
|
|
|
|
write(StringView{str});
|
|
|
|
}
|
|
|
|
|
2012-12-10 21:55:11 +04:00
|
|
|
template<typename T>
|
2015-03-09 16:48:41 +03:00
|
|
|
void write(ConstArrayView<T> view)
|
2012-12-10 21:55:11 +04:00
|
|
|
{
|
|
|
|
write<uint32_t>(view.size());
|
|
|
|
for (auto& val : view)
|
|
|
|
write(val);
|
2013-04-03 21:20:38 +04:00
|
|
|
}
|
2012-10-24 00:55:44 +04:00
|
|
|
|
2015-01-14 22:16:32 +03:00
|
|
|
template<typename T, MemoryDomain domain>
|
|
|
|
void write(const Vector<T, domain>& vec)
|
2012-12-10 21:55:11 +04:00
|
|
|
{
|
2015-03-09 16:48:41 +03:00
|
|
|
write(ConstArrayView<T>(vec));
|
2012-12-10 21:55:11 +04:00
|
|
|
}
|
2012-10-24 00:55:44 +04:00
|
|
|
|
2017-03-07 04:12:37 +03:00
|
|
|
template<typename Key, typename Val, MemoryDomain domain>
|
|
|
|
void write(const HashMap<Key, Val, domain>& map)
|
2014-04-08 00:25:44 +04:00
|
|
|
{
|
|
|
|
write<uint32_t>(map.size());
|
|
|
|
for (auto& val : map)
|
|
|
|
{
|
2015-09-16 21:57:57 +03:00
|
|
|
write(val.key);
|
|
|
|
write(val.value);
|
2014-04-08 00:25:44 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-21 15:14:44 +03:00
|
|
|
template<typename T>
|
|
|
|
void write(const Optional<T>& val)
|
|
|
|
{
|
|
|
|
write((bool)val);
|
|
|
|
if (val)
|
|
|
|
write(*val);
|
|
|
|
}
|
|
|
|
|
2013-07-26 02:26:43 +04:00
|
|
|
void write(Color color)
|
2013-05-07 20:52:23 +04:00
|
|
|
{
|
|
|
|
write(color.color);
|
2015-04-25 12:47:39 +03:00
|
|
|
if (color.color == Color::RGB)
|
2013-05-07 20:52:23 +04:00
|
|
|
{
|
|
|
|
write(color.r);
|
|
|
|
write(color.g);
|
|
|
|
write(color.b);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-12-10 21:55:11 +04:00
|
|
|
void write(const DisplayAtom& atom)
|
|
|
|
{
|
2013-07-24 16:55:57 +04:00
|
|
|
write(atom.content());
|
2014-07-11 03:27:04 +04:00
|
|
|
write(atom.face);
|
2012-12-10 21:55:11 +04:00
|
|
|
}
|
2012-10-24 00:55:44 +04:00
|
|
|
|
2012-12-10 21:55:11 +04:00
|
|
|
void write(const DisplayLine& line)
|
|
|
|
{
|
|
|
|
write(line.atoms());
|
|
|
|
}
|
2012-10-24 00:55:44 +04:00
|
|
|
|
2012-12-10 21:55:11 +04:00
|
|
|
void write(const DisplayBuffer& display_buffer)
|
|
|
|
{
|
|
|
|
write(display_buffer.lines());
|
|
|
|
}
|
2012-10-24 00:55:44 +04:00
|
|
|
|
2012-12-10 21:55:11 +04:00
|
|
|
private:
|
2016-12-01 23:11:09 +03:00
|
|
|
RemoteBuffer& m_buffer;
|
|
|
|
uint32_t m_start;
|
2012-12-10 21:55:11 +04:00
|
|
|
};
|
2012-10-24 00:55:44 +04:00
|
|
|
|
2016-08-31 21:33:02 +03:00
|
|
|
class MsgReader
|
2012-10-26 15:45:32 +04:00
|
|
|
{
|
2016-08-31 21:33:02 +03:00
|
|
|
public:
|
|
|
|
void read_available(int sock)
|
2012-11-19 22:07:32 +04:00
|
|
|
{
|
2016-08-31 21:33:02 +03:00
|
|
|
if (m_write_pos < header_size)
|
|
|
|
{
|
|
|
|
m_stream.resize(header_size);
|
|
|
|
read_from_socket(sock, header_size - m_write_pos);
|
|
|
|
if (m_write_pos == header_size)
|
2016-12-01 22:45:23 +03:00
|
|
|
{
|
|
|
|
if (size() < header_size)
|
2016-12-20 13:34:48 +03:00
|
|
|
throw disconnected{"invalid message received"};
|
2016-08-31 21:33:02 +03:00
|
|
|
m_stream.resize(size());
|
2016-12-01 22:45:23 +03:00
|
|
|
}
|
2016-08-31 21:33:02 +03:00
|
|
|
}
|
|
|
|
else
|
|
|
|
read_from_socket(sock, size() - m_write_pos);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ready() const
|
|
|
|
{
|
|
|
|
return m_write_pos >= header_size and m_write_pos == size();
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t size() const
|
|
|
|
{
|
|
|
|
kak_assert(m_write_pos >= header_size);
|
2017-06-26 13:27:18 +03:00
|
|
|
uint32_t res;
|
|
|
|
memcpy(&res, m_stream.data() + sizeof(MessageType), sizeof(uint32_t));
|
|
|
|
return res;
|
2016-08-31 21:33:02 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
MessageType type() const
|
|
|
|
{
|
|
|
|
kak_assert(m_write_pos >= header_size);
|
|
|
|
return *reinterpret_cast<const MessageType*>(m_stream.data());
|
|
|
|
}
|
|
|
|
|
|
|
|
void read(char* buffer, size_t size)
|
|
|
|
{
|
2016-09-04 19:54:07 +03:00
|
|
|
if (m_read_pos + size > m_stream.size())
|
2016-12-20 13:34:48 +03:00
|
|
|
throw disconnected{"tried to read after message end"};
|
2016-08-31 21:33:02 +03:00
|
|
|
memcpy(buffer, m_stream.data() + m_read_pos, size);
|
|
|
|
m_read_pos += size;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
T read()
|
|
|
|
{
|
|
|
|
union U
|
|
|
|
{
|
|
|
|
T object;
|
|
|
|
alignas(T) char data[sizeof(T)];
|
2017-03-17 02:08:10 +03:00
|
|
|
U() {}
|
2016-08-31 21:33:02 +03:00
|
|
|
~U() { object.~T(); }
|
|
|
|
} u;
|
|
|
|
read(u.data, sizeof(T));
|
|
|
|
return u.object;
|
|
|
|
}
|
2012-11-19 22:07:32 +04:00
|
|
|
|
2016-08-31 21:33:02 +03:00
|
|
|
template<typename T>
|
|
|
|
Vector<T> read_vector()
|
|
|
|
{
|
|
|
|
uint32_t size = read<uint32_t>();
|
|
|
|
Vector<T> res;
|
|
|
|
res.reserve(size);
|
|
|
|
while (size--)
|
|
|
|
res.push_back(read<T>());
|
|
|
|
return res;
|
2012-11-19 22:07:32 +04:00
|
|
|
}
|
2012-10-26 15:45:32 +04:00
|
|
|
|
2017-03-07 04:12:37 +03:00
|
|
|
template<typename Key, typename Val, MemoryDomain domain>
|
|
|
|
HashMap<Key, Val, domain> read_hash_map()
|
2013-07-26 03:30:01 +04:00
|
|
|
{
|
2016-08-31 21:33:02 +03:00
|
|
|
uint32_t size = read<uint32_t>();
|
2017-03-07 04:12:37 +03:00
|
|
|
HashMap<Key, Val, domain> res;
|
2016-08-31 21:33:02 +03:00
|
|
|
res.reserve(size);
|
|
|
|
while (size--)
|
|
|
|
{
|
2017-03-07 04:12:37 +03:00
|
|
|
auto key = read<Key>();
|
2016-08-31 21:33:02 +03:00
|
|
|
auto val = read<Val>();
|
2017-03-07 04:12:37 +03:00
|
|
|
res.insert({std::move(key), std::move(val)});
|
2016-08-31 21:33:02 +03:00
|
|
|
}
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2017-01-21 15:14:44 +03:00
|
|
|
template<typename T>
|
|
|
|
Optional<T> read_optional()
|
|
|
|
{
|
|
|
|
if (not read<bool>())
|
|
|
|
return {};
|
|
|
|
return read<T>();
|
|
|
|
}
|
|
|
|
|
2016-08-31 21:33:02 +03:00
|
|
|
void reset()
|
|
|
|
{
|
|
|
|
m_stream.resize(0);
|
|
|
|
m_write_pos = 0;
|
|
|
|
m_read_pos = header_size;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
void read_from_socket(int sock, size_t size)
|
|
|
|
{
|
2016-12-01 22:45:23 +03:00
|
|
|
kak_assert(m_write_pos + size <= m_stream.size());
|
2016-08-31 21:33:02 +03:00
|
|
|
int res = ::read(sock, m_stream.data() + m_write_pos, size);
|
2016-11-29 22:12:56 +03:00
|
|
|
if (res <= 0)
|
2017-08-23 09:22:23 +03:00
|
|
|
throw disconnected{format("socket read failed: {}", strerror(errno))};
|
2016-08-31 21:33:02 +03:00
|
|
|
m_write_pos += res;
|
|
|
|
}
|
2012-10-24 00:55:44 +04:00
|
|
|
|
2016-08-31 21:33:02 +03:00
|
|
|
static constexpr uint32_t header_size = sizeof(MessageType) + sizeof(uint32_t);
|
|
|
|
Vector<char, MemoryDomain::Remote> m_stream;
|
|
|
|
uint32_t m_write_pos = 0;
|
|
|
|
uint32_t m_read_pos = header_size;
|
|
|
|
};
|
2016-11-29 22:12:56 +03:00
|
|
|
|
2012-10-24 00:55:44 +04:00
|
|
|
template<>
|
2016-08-31 21:33:02 +03:00
|
|
|
String MsgReader::read<String>()
|
2012-10-24 00:55:44 +04:00
|
|
|
{
|
2016-08-31 21:33:02 +03:00
|
|
|
ByteCount length = read<ByteCount>();
|
2014-04-24 01:21:04 +04:00
|
|
|
String res;
|
|
|
|
if (length > 0)
|
|
|
|
{
|
2016-02-05 12:27:22 +03:00
|
|
|
res.force_size((int)length);
|
2016-08-31 21:33:02 +03:00
|
|
|
read(&res[0_byte], (int)length);
|
2014-04-24 01:21:04 +04:00
|
|
|
}
|
|
|
|
return res;
|
2013-04-03 21:20:38 +04:00
|
|
|
}
|
2012-10-24 00:55:44 +04:00
|
|
|
|
2013-05-07 20:52:23 +04:00
|
|
|
template<>
|
2016-08-31 21:33:02 +03:00
|
|
|
Color MsgReader::read<Color>()
|
2013-05-07 20:52:23 +04:00
|
|
|
{
|
|
|
|
Color res;
|
2016-08-31 21:33:02 +03:00
|
|
|
res.color = read<Color::NamedColor>();
|
2015-04-25 12:47:39 +03:00
|
|
|
if (res.color == Color::RGB)
|
2013-05-07 20:52:23 +04:00
|
|
|
{
|
2016-08-31 21:33:02 +03:00
|
|
|
res.r = read<unsigned char>();
|
|
|
|
res.g = read<unsigned char>();
|
|
|
|
res.b = read<unsigned char>();
|
2013-05-07 20:52:23 +04:00
|
|
|
}
|
2012-10-24 00:55:44 +04:00
|
|
|
return res;
|
2013-04-03 21:20:38 +04:00
|
|
|
}
|
2012-10-24 00:55:44 +04:00
|
|
|
|
|
|
|
template<>
|
2016-08-31 21:33:02 +03:00
|
|
|
DisplayAtom MsgReader::read<DisplayAtom>()
|
2012-10-24 00:55:44 +04:00
|
|
|
{
|
2016-08-31 21:33:02 +03:00
|
|
|
DisplayAtom atom(read<String>());
|
|
|
|
atom.face = read<Face>();
|
2012-10-24 00:55:44 +04:00
|
|
|
return atom;
|
|
|
|
}
|
2016-08-31 21:33:02 +03:00
|
|
|
|
2012-10-24 00:55:44 +04:00
|
|
|
template<>
|
2016-08-31 21:33:02 +03:00
|
|
|
DisplayLine MsgReader::read<DisplayLine>()
|
2012-10-24 00:55:44 +04:00
|
|
|
{
|
2016-08-31 21:33:02 +03:00
|
|
|
return DisplayLine(read_vector<DisplayAtom>());
|
2012-10-24 00:55:44 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
template<>
|
2016-08-31 21:33:02 +03:00
|
|
|
DisplayBuffer MsgReader::read<DisplayBuffer>()
|
2012-10-24 00:55:44 +04:00
|
|
|
{
|
|
|
|
DisplayBuffer db;
|
2016-08-31 21:33:02 +03:00
|
|
|
db.lines() = read_vector<DisplayLine>();
|
2012-10-24 00:55:44 +04:00
|
|
|
return db;
|
|
|
|
}
|
|
|
|
|
2014-04-08 00:25:44 +04:00
|
|
|
|
2012-12-19 00:20:36 +04:00
|
|
|
class RemoteUI : public UserInterface
|
|
|
|
{
|
|
|
|
public:
|
2016-09-22 22:36:26 +03:00
|
|
|
RemoteUI(int socket, DisplayCoord dimensions);
|
2017-01-09 01:30:15 +03:00
|
|
|
~RemoteUI() override;
|
2012-12-19 00:20:36 +04:00
|
|
|
|
2015-10-05 03:25:23 +03:00
|
|
|
void menu_show(ConstArrayView<DisplayLine> choices,
|
2016-09-22 22:36:26 +03:00
|
|
|
DisplayCoord anchor, Face fg, Face bg,
|
2013-04-04 15:53:47 +04:00
|
|
|
MenuStyle style) override;
|
2012-12-19 00:20:36 +04:00
|
|
|
void menu_select(int selected) override;
|
|
|
|
void menu_hide() override;
|
|
|
|
|
2014-04-30 22:39:52 +04:00
|
|
|
void info_show(StringView title, StringView content,
|
2016-09-22 22:36:26 +03:00
|
|
|
DisplayCoord anchor, Face face,
|
2014-11-08 20:59:38 +03:00
|
|
|
InfoStyle style) override;
|
2012-12-19 00:20:36 +04:00
|
|
|
void info_hide() override;
|
|
|
|
|
|
|
|
void draw(const DisplayBuffer& display_buffer,
|
2016-02-17 16:32:05 +03:00
|
|
|
const Face& default_face,
|
|
|
|
const Face& padding_face) override;
|
2015-06-17 23:28:02 +03:00
|
|
|
|
|
|
|
void draw_status(const DisplayLine& status_line,
|
|
|
|
const DisplayLine& mode_line,
|
|
|
|
const Face& default_face) override;
|
2012-12-19 00:20:36 +04:00
|
|
|
|
2017-04-12 12:39:17 +03:00
|
|
|
void set_cursor(CursorMode mode, DisplayCoord coord) override;
|
|
|
|
|
2016-03-07 16:54:20 +03:00
|
|
|
void refresh(bool force) override;
|
2014-04-15 22:19:44 +04:00
|
|
|
|
2016-11-30 00:35:53 +03:00
|
|
|
DisplayCoord dimensions() override { return m_dimensions; }
|
2012-12-19 00:20:36 +04:00
|
|
|
|
2016-11-30 00:35:53 +03:00
|
|
|
void set_on_key(OnKeyCallback callback) override
|
|
|
|
{ m_on_key = std::move(callback); }
|
2013-01-11 22:17:21 +04:00
|
|
|
|
2014-11-11 02:29:16 +03:00
|
|
|
void set_ui_options(const Options& options) override;
|
|
|
|
|
2016-09-04 19:54:07 +03:00
|
|
|
void set_client(Client* client) { m_client = client; }
|
|
|
|
Client* client() const { return m_client.get(); }
|
|
|
|
|
2017-08-23 09:22:23 +03:00
|
|
|
void exit(int status);
|
|
|
|
|
2012-12-19 00:20:36 +04:00
|
|
|
private:
|
2016-11-30 00:35:53 +03:00
|
|
|
FDWatcher m_socket_watcher;
|
|
|
|
MsgReader m_reader;
|
|
|
|
DisplayCoord m_dimensions;
|
|
|
|
OnKeyCallback m_on_key;
|
2016-12-01 23:11:09 +03:00
|
|
|
RemoteBuffer m_send_buffer;
|
2016-09-04 19:54:07 +03:00
|
|
|
|
|
|
|
SafePtr<Client> m_client;
|
2012-12-19 00:20:36 +04:00
|
|
|
};
|
|
|
|
|
2016-12-01 23:11:09 +03:00
|
|
|
static bool send_data(int fd, RemoteBuffer& buffer)
|
|
|
|
{
|
2017-03-17 02:08:10 +03:00
|
|
|
while (not buffer.empty() and fd_writable(fd))
|
2016-12-01 23:11:09 +03:00
|
|
|
{
|
|
|
|
int res = ::write(fd, buffer.data(), buffer.size());
|
|
|
|
if (res <= 0)
|
2017-08-23 09:22:23 +03:00
|
|
|
throw disconnected{format("socket write failed: {}", strerror(errno))};
|
2016-12-01 23:11:09 +03:00
|
|
|
buffer.erase(buffer.begin(), buffer.begin() + res);
|
|
|
|
}
|
|
|
|
return buffer.empty();
|
|
|
|
}
|
2012-12-19 00:20:36 +04:00
|
|
|
|
2016-09-22 22:36:26 +03:00
|
|
|
RemoteUI::RemoteUI(int socket, DisplayCoord dimensions)
|
2016-12-01 23:11:09 +03:00
|
|
|
: m_socket_watcher(socket, FdEvents::Read | FdEvents::Write,
|
|
|
|
[this](FDWatcher& watcher, FdEvents events, EventMode mode) {
|
2016-09-04 19:54:07 +03:00
|
|
|
const int sock = watcher.fd();
|
|
|
|
try
|
|
|
|
{
|
2016-12-01 23:11:09 +03:00
|
|
|
if (events & FdEvents::Write and send_data(sock, m_send_buffer))
|
|
|
|
m_socket_watcher.events() &= ~FdEvents::Write;
|
|
|
|
|
|
|
|
while (events & FdEvents::Read and fd_readable(sock))
|
2016-11-30 00:35:53 +03:00
|
|
|
{
|
2016-09-04 19:54:07 +03:00
|
|
|
m_reader.read_available(sock);
|
|
|
|
|
2016-11-30 00:35:53 +03:00
|
|
|
if (not m_reader.ready())
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (m_reader.type() != MessageType::Key)
|
2016-09-04 19:54:07 +03:00
|
|
|
{
|
2017-08-23 09:22:23 +03:00
|
|
|
ClientManager::instance().remove_client(*m_client, false, -1);
|
2016-11-30 00:35:53 +03:00
|
|
|
return;
|
2016-09-04 19:54:07 +03:00
|
|
|
}
|
2016-11-30 00:35:53 +03:00
|
|
|
|
|
|
|
auto key = m_reader.read<Key>();
|
|
|
|
m_reader.reset();
|
|
|
|
if (key.modifiers == Key::Modifiers::Resize)
|
|
|
|
m_dimensions = key.coord();
|
|
|
|
m_on_key(key);
|
2016-09-04 19:54:07 +03:00
|
|
|
}
|
|
|
|
}
|
2016-12-20 13:34:48 +03:00
|
|
|
catch (const disconnected& err)
|
2016-09-04 19:54:07 +03:00
|
|
|
{
|
2016-12-01 23:11:09 +03:00
|
|
|
write_to_debug_buffer(format("Error while transfering remote messages: {}", err.what()));
|
2017-08-23 09:22:23 +03:00
|
|
|
ClientManager::instance().remove_client(*m_client, false, -1);
|
2016-11-30 00:35:53 +03:00
|
|
|
}
|
2016-09-04 19:54:07 +03:00
|
|
|
}),
|
2015-11-18 16:43:43 +03:00
|
|
|
m_dimensions(dimensions)
|
2012-11-19 22:06:32 +04:00
|
|
|
{
|
2015-06-06 13:54:48 +03:00
|
|
|
write_to_debug_buffer(format("remote client connected: {}", m_socket_watcher.fd()));
|
2012-11-19 22:06:32 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
RemoteUI::~RemoteUI()
|
|
|
|
{
|
2017-08-23 09:22:23 +03:00
|
|
|
// Try to send the remaining data if possible, as it might contain the desired exit status
|
|
|
|
send_data(m_socket_watcher.fd(), m_send_buffer);
|
|
|
|
|
2015-06-06 13:54:48 +03:00
|
|
|
write_to_debug_buffer(format("remote client disconnected: {}", m_socket_watcher.fd()));
|
2014-12-03 16:56:02 +03:00
|
|
|
m_socket_watcher.close_fd();
|
2012-11-19 22:06:32 +04:00
|
|
|
}
|
|
|
|
|
2015-10-05 03:25:23 +03:00
|
|
|
void RemoteUI::menu_show(ConstArrayView<DisplayLine> choices,
|
2016-09-22 22:36:26 +03:00
|
|
|
DisplayCoord anchor, Face fg, Face bg,
|
2013-04-04 15:53:47 +04:00
|
|
|
MenuStyle style)
|
2012-10-24 00:55:44 +04:00
|
|
|
{
|
2016-12-01 23:11:09 +03:00
|
|
|
MsgWriter msg{m_send_buffer, MessageType::MenuShow};
|
2012-12-10 21:55:11 +04:00
|
|
|
msg.write(choices);
|
|
|
|
msg.write(anchor);
|
2013-04-04 15:53:47 +04:00
|
|
|
msg.write(fg);
|
|
|
|
msg.write(bg);
|
2012-12-10 21:55:11 +04:00
|
|
|
msg.write(style);
|
2016-12-01 23:11:09 +03:00
|
|
|
m_socket_watcher.events() |= FdEvents::Write;
|
2012-10-24 00:55:44 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void RemoteUI::menu_select(int selected)
|
|
|
|
{
|
2016-12-01 23:11:09 +03:00
|
|
|
MsgWriter msg{m_send_buffer, MessageType::MenuSelect};
|
2012-12-10 21:55:11 +04:00
|
|
|
msg.write(selected);
|
2016-12-01 23:11:09 +03:00
|
|
|
m_socket_watcher.events() |= FdEvents::Write;
|
2012-10-24 00:55:44 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void RemoteUI::menu_hide()
|
|
|
|
{
|
2016-12-01 23:11:09 +03:00
|
|
|
MsgWriter msg{m_send_buffer, MessageType::MenuHide};
|
|
|
|
m_socket_watcher.events() |= FdEvents::Write;
|
2012-10-24 00:55:44 +04:00
|
|
|
}
|
|
|
|
|
2014-04-30 22:39:52 +04:00
|
|
|
void RemoteUI::info_show(StringView title, StringView content,
|
2016-09-22 22:36:26 +03:00
|
|
|
DisplayCoord anchor, Face face,
|
2014-11-08 20:59:38 +03:00
|
|
|
InfoStyle style)
|
2012-12-14 22:04:34 +04:00
|
|
|
{
|
2016-12-01 23:11:09 +03:00
|
|
|
MsgWriter msg{m_send_buffer, MessageType::InfoShow};
|
2013-10-11 01:51:16 +04:00
|
|
|
msg.write(title);
|
2012-12-14 22:04:34 +04:00
|
|
|
msg.write(content);
|
|
|
|
msg.write(anchor);
|
2014-07-11 03:27:04 +04:00
|
|
|
msg.write(face);
|
2012-12-14 22:04:34 +04:00
|
|
|
msg.write(style);
|
2016-12-01 23:11:09 +03:00
|
|
|
m_socket_watcher.events() |= FdEvents::Write;
|
2012-12-14 22:04:34 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void RemoteUI::info_hide()
|
|
|
|
{
|
2016-12-01 23:11:09 +03:00
|
|
|
MsgWriter msg{m_send_buffer, MessageType::InfoHide};
|
|
|
|
m_socket_watcher.events() |= FdEvents::Write;
|
2012-12-14 22:04:34 +04:00
|
|
|
}
|
|
|
|
|
2012-10-24 00:55:44 +04:00
|
|
|
void RemoteUI::draw(const DisplayBuffer& display_buffer,
|
2016-02-17 16:32:05 +03:00
|
|
|
const Face& default_face,
|
|
|
|
const Face& padding_face)
|
2012-10-24 00:55:44 +04:00
|
|
|
{
|
2016-12-01 23:11:09 +03:00
|
|
|
MsgWriter msg{m_send_buffer, MessageType::Draw};
|
2012-12-10 21:55:11 +04:00
|
|
|
msg.write(display_buffer);
|
2015-06-17 23:28:02 +03:00
|
|
|
msg.write(default_face);
|
2016-02-17 16:32:05 +03:00
|
|
|
msg.write(padding_face);
|
2016-12-01 23:11:09 +03:00
|
|
|
m_socket_watcher.events() |= FdEvents::Write;
|
2015-06-17 23:28:02 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void RemoteUI::draw_status(const DisplayLine& status_line,
|
|
|
|
const DisplayLine& mode_line,
|
|
|
|
const Face& default_face)
|
|
|
|
{
|
2016-12-01 23:11:09 +03:00
|
|
|
MsgWriter msg{m_send_buffer, MessageType::DrawStatus};
|
2013-09-16 22:15:13 +04:00
|
|
|
msg.write(status_line);
|
2012-12-10 21:55:11 +04:00
|
|
|
msg.write(mode_line);
|
2015-06-17 23:28:02 +03:00
|
|
|
msg.write(default_face);
|
2016-12-01 23:11:09 +03:00
|
|
|
m_socket_watcher.events() |= FdEvents::Write;
|
2012-10-24 00:55:44 +04:00
|
|
|
}
|
|
|
|
|
2017-04-12 12:39:17 +03:00
|
|
|
void RemoteUI::set_cursor(CursorMode mode, DisplayCoord coord)
|
|
|
|
{
|
|
|
|
MsgWriter msg{m_send_buffer, MessageType::SetCursor};
|
|
|
|
msg.write(mode);
|
|
|
|
msg.write(coord);
|
|
|
|
m_socket_watcher.events() |= FdEvents::Write;
|
|
|
|
}
|
|
|
|
|
2016-03-07 16:54:20 +03:00
|
|
|
void RemoteUI::refresh(bool force)
|
2014-04-15 22:19:44 +04:00
|
|
|
{
|
2016-12-01 23:11:09 +03:00
|
|
|
MsgWriter msg{m_send_buffer, MessageType::Refresh};
|
2016-03-07 16:54:20 +03:00
|
|
|
msg.write(force);
|
2016-12-01 23:11:09 +03:00
|
|
|
m_socket_watcher.events() |= FdEvents::Write;
|
2014-04-15 22:19:44 +04:00
|
|
|
}
|
|
|
|
|
2014-11-11 02:29:16 +03:00
|
|
|
void RemoteUI::set_ui_options(const Options& options)
|
|
|
|
{
|
2016-12-01 23:11:09 +03:00
|
|
|
MsgWriter msg{m_send_buffer, MessageType::SetOptions};
|
2014-11-11 02:29:16 +03:00
|
|
|
msg.write(options);
|
2016-12-01 23:11:09 +03:00
|
|
|
m_socket_watcher.events() |= FdEvents::Write;
|
2014-11-11 02:29:16 +03:00
|
|
|
}
|
|
|
|
|
2017-08-23 09:22:23 +03:00
|
|
|
void RemoteUI::exit(int status)
|
|
|
|
{
|
|
|
|
MsgWriter msg{m_send_buffer, MessageType::Exit};
|
|
|
|
msg.write(status);
|
|
|
|
m_socket_watcher.events() |= FdEvents::Write;
|
|
|
|
}
|
|
|
|
|
2014-11-06 16:54:18 +03:00
|
|
|
static sockaddr_un session_addr(StringView session)
|
2012-10-25 14:51:01 +04:00
|
|
|
{
|
2014-11-06 16:54:18 +03:00
|
|
|
sockaddr_un addr;
|
|
|
|
addr.sun_family = AF_UNIX;
|
2016-01-21 01:58:39 +03:00
|
|
|
if (find(session, '/')!= session.end())
|
2016-12-16 02:47:34 +03:00
|
|
|
format_to(addr.sun_path, "{}/kakoune/{}", tmpdir(), session);
|
2016-01-21 01:58:39 +03:00
|
|
|
else
|
2016-12-16 02:47:34 +03:00
|
|
|
format_to(addr.sun_path, "{}/kakoune/{}/{}", tmpdir(),
|
|
|
|
getpwuid(geteuid())->pw_name, session);
|
2014-11-06 16:54:18 +03:00
|
|
|
return addr;
|
|
|
|
}
|
2014-11-05 16:57:12 +03:00
|
|
|
|
2014-11-06 16:54:18 +03:00
|
|
|
static int connect_to(StringView session)
|
|
|
|
{
|
2014-11-05 16:57:12 +03:00
|
|
|
int sock = socket(AF_UNIX, SOCK_STREAM, 0);
|
|
|
|
fcntl(sock, F_SETFD, FD_CLOEXEC);
|
2014-11-06 16:54:18 +03:00
|
|
|
sockaddr_un addr = session_addr(session);
|
2014-11-05 16:57:12 +03:00
|
|
|
if (connect(sock, (sockaddr*)&addr, sizeof(addr.sun_path)) == -1)
|
2016-12-20 13:34:48 +03:00
|
|
|
throw disconnected(format("connect to {} failed", addr.sun_path));
|
2014-11-06 16:54:18 +03:00
|
|
|
return sock;
|
|
|
|
}
|
|
|
|
|
2016-06-06 21:28:56 +03:00
|
|
|
bool check_session(StringView session)
|
|
|
|
{
|
|
|
|
int sock = socket(AF_UNIX, SOCK_STREAM, 0);
|
|
|
|
auto close_sock = on_scope_end([sock]{ close(sock); });
|
|
|
|
sockaddr_un addr = session_addr(session);
|
|
|
|
return connect(sock, (sockaddr*)&addr, sizeof(addr.sun_path)) != -1;
|
|
|
|
}
|
|
|
|
|
2014-11-06 16:54:18 +03:00
|
|
|
RemoteClient::RemoteClient(StringView session, std::unique_ptr<UserInterface>&& ui,
|
2017-08-28 09:12:15 +03:00
|
|
|
int pid, const EnvVarMap& env_vars, StringView init_command,
|
2017-01-21 15:14:44 +03:00
|
|
|
Optional<BufferCoord> init_coord)
|
2015-08-23 17:18:18 +03:00
|
|
|
: m_ui(std::move(ui))
|
2014-11-06 16:54:18 +03:00
|
|
|
{
|
|
|
|
int sock = connect_to(session);
|
2014-04-08 00:25:44 +04:00
|
|
|
|
2014-11-05 16:57:12 +03:00
|
|
|
{
|
2016-12-01 23:11:09 +03:00
|
|
|
MsgWriter msg{m_send_buffer, MessageType::Connect};
|
2017-08-28 09:12:15 +03:00
|
|
|
msg.write(pid);
|
2016-08-31 11:24:07 +03:00
|
|
|
msg.write(init_command);
|
2017-01-21 15:14:44 +03:00
|
|
|
msg.write(init_coord);
|
2015-11-18 16:43:43 +03:00
|
|
|
msg.write(m_ui->dimensions());
|
2014-11-05 16:57:12 +03:00
|
|
|
msg.write(env_vars);
|
|
|
|
}
|
2013-01-11 22:17:21 +04:00
|
|
|
|
2016-11-30 00:35:53 +03:00
|
|
|
m_ui->set_on_key([this](Key key){
|
2016-12-01 23:11:09 +03:00
|
|
|
MsgWriter msg(m_send_buffer, MessageType::Key);
|
2016-11-30 00:35:53 +03:00
|
|
|
msg.write(key);
|
2016-12-01 23:11:09 +03:00
|
|
|
m_socket_watcher->events() |= FdEvents::Write;
|
2016-11-30 00:35:53 +03:00
|
|
|
});
|
2014-11-05 16:57:12 +03:00
|
|
|
|
2016-08-31 21:33:02 +03:00
|
|
|
MsgReader reader;
|
2016-12-01 23:11:09 +03:00
|
|
|
m_socket_watcher.reset(new FDWatcher{sock, FdEvents::Read | FdEvents::Write,
|
|
|
|
[this, reader](FDWatcher& watcher, FdEvents events, EventMode) mutable {
|
2016-08-31 21:33:02 +03:00
|
|
|
const int sock = watcher.fd();
|
2016-12-01 23:11:09 +03:00
|
|
|
if (events & FdEvents::Write and send_data(sock, m_send_buffer))
|
|
|
|
m_socket_watcher->events() &= ~FdEvents::Write;
|
|
|
|
|
|
|
|
while (events & FdEvents::Read and
|
|
|
|
not reader.ready() and fd_readable(sock))
|
|
|
|
{
|
2016-08-31 21:33:02 +03:00
|
|
|
reader.read_available(sock);
|
2012-10-25 14:51:01 +04:00
|
|
|
|
2016-12-01 23:11:09 +03:00
|
|
|
if (not reader.ready())
|
|
|
|
continue;
|
2014-04-15 22:08:47 +04:00
|
|
|
|
2016-12-01 23:11:09 +03:00
|
|
|
auto clear_reader = on_scope_end([&reader] { reader.reset(); });
|
|
|
|
switch (reader.type())
|
|
|
|
{
|
|
|
|
case MessageType::MenuShow:
|
|
|
|
{
|
|
|
|
auto choices = reader.read_vector<DisplayLine>();
|
|
|
|
auto anchor = reader.read<DisplayCoord>();
|
|
|
|
auto fg = reader.read<Face>();
|
|
|
|
auto bg = reader.read<Face>();
|
|
|
|
auto style = reader.read<MenuStyle>();
|
|
|
|
m_ui->menu_show(choices, anchor, fg, bg, style);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case MessageType::MenuSelect:
|
|
|
|
m_ui->menu_select(reader.read<int>());
|
|
|
|
break;
|
|
|
|
case MessageType::MenuHide:
|
|
|
|
m_ui->menu_hide();
|
|
|
|
break;
|
|
|
|
case MessageType::InfoShow:
|
|
|
|
{
|
|
|
|
auto title = reader.read<String>();
|
|
|
|
auto content = reader.read<String>();
|
|
|
|
auto anchor = reader.read<DisplayCoord>();
|
|
|
|
auto face = reader.read<Face>();
|
|
|
|
auto style = reader.read<InfoStyle>();
|
|
|
|
m_ui->info_show(title, content, anchor, face, style);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case MessageType::InfoHide:
|
|
|
|
m_ui->info_hide();
|
|
|
|
break;
|
|
|
|
case MessageType::Draw:
|
|
|
|
{
|
|
|
|
auto display_buffer = reader.read<DisplayBuffer>();
|
|
|
|
auto default_face = reader.read<Face>();
|
|
|
|
auto padding_face = reader.read<Face>();
|
|
|
|
m_ui->draw(display_buffer, default_face, padding_face);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case MessageType::DrawStatus:
|
|
|
|
{
|
|
|
|
auto status_line = reader.read<DisplayLine>();
|
|
|
|
auto mode_line = reader.read<DisplayLine>();
|
|
|
|
auto default_face = reader.read<Face>();
|
|
|
|
m_ui->draw_status(status_line, mode_line, default_face);
|
|
|
|
break;
|
|
|
|
}
|
2017-04-12 12:39:17 +03:00
|
|
|
case MessageType::SetCursor:
|
|
|
|
{
|
|
|
|
auto mode = reader.read<CursorMode>();
|
|
|
|
auto coord = reader.read<DisplayCoord>();
|
|
|
|
m_ui->set_cursor(mode, coord);
|
|
|
|
break;
|
|
|
|
}
|
2016-12-01 23:11:09 +03:00
|
|
|
case MessageType::Refresh:
|
|
|
|
m_ui->refresh(reader.read<bool>());
|
|
|
|
break;
|
|
|
|
case MessageType::SetOptions:
|
2017-03-07 04:12:37 +03:00
|
|
|
m_ui->set_ui_options(reader.read_hash_map<String, String, MemoryDomain::Options>());
|
2016-12-01 23:11:09 +03:00
|
|
|
break;
|
2017-08-23 09:22:23 +03:00
|
|
|
case MessageType::Exit:
|
|
|
|
m_exit_status = reader.read<int>();
|
|
|
|
m_socket_watcher->close_fd();
|
|
|
|
m_socket_watcher.reset();
|
|
|
|
return; // This lambda is now dead
|
2016-12-01 23:11:09 +03:00
|
|
|
default:
|
|
|
|
kak_assert(false);
|
|
|
|
}
|
2016-08-31 21:33:02 +03:00
|
|
|
}
|
|
|
|
}});
|
2012-10-24 00:55:44 +04:00
|
|
|
}
|
|
|
|
|
2014-10-20 22:18:38 +04:00
|
|
|
void send_command(StringView session, StringView command)
|
2014-03-02 06:01:09 +04:00
|
|
|
{
|
2014-11-06 16:54:18 +03:00
|
|
|
int sock = connect_to(session);
|
2016-08-09 23:45:06 +03:00
|
|
|
auto close_sock = on_scope_end([sock]{ close(sock); });
|
2016-12-01 23:11:09 +03:00
|
|
|
RemoteBuffer buffer;
|
|
|
|
{
|
|
|
|
MsgWriter msg{buffer, MessageType::Command};
|
|
|
|
msg.write(command);
|
|
|
|
}
|
|
|
|
write(sock, {buffer.data(), buffer.data() + buffer.size()});
|
2014-03-02 06:01:09 +04:00
|
|
|
}
|
|
|
|
|
2013-03-12 21:53:18 +04:00
|
|
|
|
2013-03-13 22:59:39 +04:00
|
|
|
// A client accepter handle a connection until it closes or a nul byte is
|
|
|
|
// recieved. Everything recieved before is considered to be a command.
|
|
|
|
//
|
|
|
|
// * When a nul byte is recieved, the socket is handed to a new Client along
|
|
|
|
// with the command.
|
|
|
|
// * When the connection is closed, the command is run in an empty context.
|
2014-03-25 13:21:20 +04:00
|
|
|
class Server::Accepter
|
2012-12-19 00:20:36 +04:00
|
|
|
{
|
2013-03-13 22:59:39 +04:00
|
|
|
public:
|
2014-03-25 13:21:20 +04:00
|
|
|
Accepter(int socket)
|
2016-11-30 16:59:08 +03:00
|
|
|
: m_socket_watcher(socket, FdEvents::Read,
|
|
|
|
[this](FDWatcher&, FdEvents, EventMode mode) {
|
2014-11-25 04:00:18 +03:00
|
|
|
if (mode == EventMode::Normal)
|
|
|
|
handle_available_input();
|
|
|
|
})
|
2014-04-05 15:04:37 +04:00
|
|
|
{}
|
2013-03-13 22:59:39 +04:00
|
|
|
|
|
|
|
private:
|
|
|
|
void handle_available_input()
|
2013-03-12 21:53:18 +04:00
|
|
|
{
|
2016-08-31 11:24:07 +03:00
|
|
|
const int sock = m_socket_watcher.fd();
|
2016-09-05 15:47:56 +03:00
|
|
|
try
|
2016-08-31 21:33:02 +03:00
|
|
|
{
|
2016-12-01 23:11:09 +03:00
|
|
|
while (not m_reader.ready() and fd_readable(sock))
|
2016-09-05 15:47:56 +03:00
|
|
|
m_reader.read_available(sock);
|
2016-08-31 21:33:02 +03:00
|
|
|
|
2016-09-05 15:47:56 +03:00
|
|
|
if (not m_reader.ready())
|
|
|
|
return;
|
2016-08-31 21:33:02 +03:00
|
|
|
|
2016-09-05 15:47:56 +03:00
|
|
|
switch (m_reader.type())
|
|
|
|
{
|
2016-08-31 11:24:07 +03:00
|
|
|
case MessageType::Connect:
|
2013-03-12 21:53:18 +04:00
|
|
|
{
|
2017-08-28 09:12:15 +03:00
|
|
|
auto pid = m_reader.read<int>();
|
2016-12-01 23:40:50 +03:00
|
|
|
auto init_cmds = m_reader.read<String>();
|
2017-01-21 15:14:44 +03:00
|
|
|
auto init_coord = m_reader.read_optional<BufferCoord>();
|
2016-09-22 22:36:26 +03:00
|
|
|
auto dimensions = m_reader.read<DisplayCoord>();
|
2017-03-07 04:12:37 +03:00
|
|
|
auto env_vars = m_reader.read_hash_map<String, String, MemoryDomain::EnvVars>();
|
2017-01-09 01:30:15 +03:00
|
|
|
auto* ui = new RemoteUI{sock, dimensions};
|
2016-09-04 19:54:07 +03:00
|
|
|
if (auto* client = ClientManager::instance().create_client(
|
2017-08-28 09:12:15 +03:00
|
|
|
std::unique_ptr<UserInterface>(ui), pid,
|
2017-08-23 09:22:23 +03:00
|
|
|
std::move(env_vars), init_cmds, init_coord,
|
|
|
|
[ui](int status) { ui->exit(status); }))
|
2016-09-04 19:54:07 +03:00
|
|
|
ui->set_client(client);
|
|
|
|
|
2016-08-31 11:24:07 +03:00
|
|
|
Server::instance().remove_accepter(this);
|
2016-09-05 15:47:56 +03:00
|
|
|
break;
|
2016-08-31 11:24:07 +03:00
|
|
|
}
|
|
|
|
case MessageType::Command:
|
|
|
|
{
|
2016-08-31 21:33:02 +03:00
|
|
|
auto command = m_reader.read<String>();
|
2016-08-31 11:24:07 +03:00
|
|
|
if (not command.empty()) try
|
2013-03-13 22:59:39 +04:00
|
|
|
{
|
2015-04-19 20:47:52 +03:00
|
|
|
Context context{Context::EmptyContextFlag{}};
|
2016-08-31 11:24:07 +03:00
|
|
|
CommandManager::instance().execute(command, context);
|
2013-03-13 22:59:39 +04:00
|
|
|
}
|
2016-09-05 15:47:56 +03:00
|
|
|
catch (const runtime_error& e)
|
2013-03-13 22:59:39 +04:00
|
|
|
{
|
2015-06-06 13:54:48 +03:00
|
|
|
write_to_debug_buffer(format("error running command '{}': {}",
|
2016-08-31 11:24:07 +03:00
|
|
|
command, e.what()));
|
2013-03-13 22:59:39 +04:00
|
|
|
}
|
2016-08-31 11:24:07 +03:00
|
|
|
close(sock);
|
2014-03-25 13:21:20 +04:00
|
|
|
Server::instance().remove_accepter(this);
|
2016-09-05 15:47:56 +03:00
|
|
|
break;
|
2013-03-12 21:53:18 +04:00
|
|
|
}
|
2016-08-31 11:24:07 +03:00
|
|
|
default:
|
|
|
|
write_to_debug_buffer("Invalid introduction message received");
|
|
|
|
close(sock);
|
2014-03-25 13:21:20 +04:00
|
|
|
Server::instance().remove_accepter(this);
|
2016-09-05 15:47:56 +03:00
|
|
|
}
|
|
|
|
}
|
2016-12-20 13:34:48 +03:00
|
|
|
catch (const disconnected& err)
|
2016-09-05 15:47:56 +03:00
|
|
|
{
|
|
|
|
write_to_debug_buffer(format("accepting connection failed: {}", err.what()));
|
|
|
|
close(sock);
|
|
|
|
Server::instance().remove_accepter(this);
|
2013-03-13 22:59:39 +04:00
|
|
|
}
|
2013-03-12 21:53:18 +04:00
|
|
|
}
|
2013-03-13 22:59:39 +04:00
|
|
|
|
|
|
|
FDWatcher m_socket_watcher;
|
2016-08-31 21:33:02 +03:00
|
|
|
MsgReader m_reader;
|
2013-03-13 22:59:39 +04:00
|
|
|
};
|
|
|
|
|
2013-09-25 22:04:52 +04:00
|
|
|
Server::Server(String session_name)
|
|
|
|
: m_session{std::move(session_name)}
|
2013-03-13 22:59:39 +04:00
|
|
|
{
|
|
|
|
int listen_sock = socket(AF_UNIX, SOCK_STREAM, 0);
|
|
|
|
fcntl(listen_sock, F_SETFD, FD_CLOEXEC);
|
2014-11-06 16:54:18 +03:00
|
|
|
sockaddr_un addr = session_addr(m_session);
|
2013-03-13 22:59:39 +04:00
|
|
|
|
2016-12-16 02:47:34 +03:00
|
|
|
// set sticky bit on the shared kakoune directory
|
|
|
|
make_directory(format("{}/kakoune", tmpdir()), 01777);
|
|
|
|
make_directory(split_path(addr.sun_path).first, 0711);
|
|
|
|
|
|
|
|
// Do not give any access to the socket to other users by default
|
|
|
|
auto old_mask = umask(0077);
|
|
|
|
auto restore_mask = on_scope_end([old_mask]() { umask(old_mask); });
|
2015-08-23 16:13:46 +03:00
|
|
|
|
2013-03-13 22:59:39 +04:00
|
|
|
if (bind(listen_sock, (sockaddr*) &addr, sizeof(sockaddr_un)) == -1)
|
2016-12-17 08:46:04 +03:00
|
|
|
throw runtime_error(format("unable to bind listen socket '{}': {}",
|
|
|
|
addr.sun_path, strerror(errno)));
|
2013-03-13 22:59:39 +04:00
|
|
|
|
|
|
|
if (listen(listen_sock, 4) == -1)
|
2016-12-17 08:46:04 +03:00
|
|
|
throw runtime_error(format("unable to listen on socket '{}': {}",
|
|
|
|
addr.sun_path, strerror(errno)));
|
2013-03-13 22:59:39 +04:00
|
|
|
|
2016-11-30 16:59:08 +03:00
|
|
|
auto accepter = [this](FDWatcher& watcher, FdEvents, EventMode mode) {
|
2013-03-13 22:59:39 +04:00
|
|
|
sockaddr_un client_addr;
|
|
|
|
socklen_t client_addr_len = sizeof(sockaddr_un);
|
2014-04-05 15:04:37 +04:00
|
|
|
int sock = accept(watcher.fd(), (sockaddr*) &client_addr,
|
|
|
|
&client_addr_len);
|
2013-03-13 22:59:39 +04:00
|
|
|
if (sock == -1)
|
|
|
|
throw runtime_error("accept failed");
|
|
|
|
fcntl(sock, F_SETFD, FD_CLOEXEC);
|
|
|
|
|
2014-03-25 13:21:20 +04:00
|
|
|
m_accepters.emplace_back(new Accepter{sock});
|
2013-03-13 22:59:39 +04:00
|
|
|
};
|
2016-11-30 16:59:08 +03:00
|
|
|
m_listener.reset(new FDWatcher{listen_sock, FdEvents::Read, accepter});
|
2013-03-13 22:59:39 +04:00
|
|
|
}
|
|
|
|
|
2016-07-28 02:16:41 +03:00
|
|
|
bool Server::rename_session(StringView name)
|
2016-07-20 17:52:53 +03:00
|
|
|
{
|
2016-12-20 07:04:47 +03:00
|
|
|
String old_socket_file = format("{}/kakoune/{}/{}", tmpdir(),
|
|
|
|
getpwuid(geteuid())->pw_name, m_session);
|
|
|
|
String new_socket_file = format("{}/kakoune/{}/{}", tmpdir(),
|
|
|
|
getpwuid(geteuid())->pw_name, name);
|
2016-07-20 17:52:53 +03:00
|
|
|
|
2016-07-28 02:16:41 +03:00
|
|
|
if (rename(old_socket_file.c_str(), new_socket_file.c_str()) != 0)
|
2016-07-20 17:52:53 +03:00
|
|
|
return false;
|
|
|
|
|
2016-07-28 02:16:41 +03:00
|
|
|
m_session = name.str();
|
2016-07-20 17:52:53 +03:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-10-08 22:05:47 +03:00
|
|
|
void Server::close_session(bool do_unlink)
|
2013-03-13 22:59:39 +04:00
|
|
|
{
|
2015-10-08 22:05:47 +03:00
|
|
|
if (do_unlink)
|
|
|
|
{
|
2016-12-20 07:04:47 +03:00
|
|
|
String socket_file = format("{}/kakoune/{}/{}", tmpdir(),
|
|
|
|
getpwuid(geteuid())->pw_name, m_session);
|
2016-07-20 17:52:53 +03:00
|
|
|
unlink(socket_file.c_str());
|
2015-10-08 22:05:47 +03:00
|
|
|
}
|
2014-12-03 16:56:02 +03:00
|
|
|
m_listener->close_fd();
|
2014-01-27 23:53:17 +04:00
|
|
|
m_listener.reset();
|
|
|
|
}
|
|
|
|
|
|
|
|
Server::~Server()
|
|
|
|
{
|
2015-08-22 13:27:48 +03:00
|
|
|
if (m_listener)
|
|
|
|
close_session();
|
2012-12-19 00:20:36 +04:00
|
|
|
}
|
|
|
|
|
2014-03-25 13:21:20 +04:00
|
|
|
void Server::remove_accepter(Accepter* accepter)
|
|
|
|
{
|
|
|
|
auto it = find(m_accepters, accepter);
|
|
|
|
kak_assert(it != m_accepters.end());
|
|
|
|
m_accepters.erase(it);
|
|
|
|
}
|
|
|
|
|
2012-10-24 00:55:44 +04:00
|
|
|
}
|