Skip to content

Commit

Permalink
Implement IpcChannel/IpcServer for linux.
Browse files Browse the repository at this point in the history
  • Loading branch information
Dmitry Chapyshev committed Aug 27, 2023
1 parent 5551c8a commit a461eb4
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 11 deletions.
24 changes: 19 additions & 5 deletions source/base/ipc/ipc_channel.cc
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@ namespace {

const uint32_t kMaxMessageSize = 16 * 1024 * 1024; // 16MB

#if defined(OS_POSIX)
const char16_t kLocalSocketPrefix[] = u"aspia_host_";
#endif // defined(OS_POSIX)

#if defined(OS_WIN)

const char16_t kPipeNamePrefix[] = u"\\\\.\\pipe\\aspia.";
Expand Down Expand Up @@ -159,9 +163,9 @@ void IpcChannel::setListener(Listener* listener)
//--------------------------------------------------------------------------------------------------
bool IpcChannel::connect(std::u16string_view channel_id)
{
#if defined(OS_WIN)
DCHECK_CALLED_ON_VALID_THREAD(thread_checker_);

#if defined(OS_WIN)
const DWORD flags = SECURITY_SQOS_PRESENT | SECURITY_IDENTIFICATION | FILE_FLAG_OVERLAPPED;
channel_name_ = channelName(channel_id);

Expand Down Expand Up @@ -207,8 +211,17 @@ bool IpcChannel::connect(std::u16string_view channel_id)
is_connected_ = true;
return true;
#else
NOTIMPLEMENTED();
return false;
asio::local::stream_protocol::endpoint endpoint(base::utf8FromUtf16(channel_id));
std::error_code error_code;
stream_.connect(endpoint, error_code);
if (error_code)
{
LOG(LS_WARNING) << "Unable to connect: " << base::utf16FromLocal8Bit(error_code.message());
return false;
}

is_connected_ = true;
return true;
#endif
}

Expand Down Expand Up @@ -320,8 +333,9 @@ std::u16string IpcChannel::channelName(std::u16string_view channel_id)
name.append(channel_id);
return name;
#else
NOTIMPLEMENTED();
return std::u16string();
std::u16string name(kLocalSocketPrefix);
name.append(channel_id);
return name;
#endif
}

Expand Down
4 changes: 2 additions & 2 deletions source/base/ipc/ipc_channel.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
#if defined(OS_WIN)
#include <asio/windows/stream_handle.hpp>
#elif defined(OS_POSIX)
#include <asio/posix/stream_descriptor.hpp>
#include <asio/local/stream_protocol.hpp>
#endif

#include <filesystem>
Expand Down Expand Up @@ -84,7 +84,7 @@ class IpcChannel
#if defined(OS_WIN)
using Stream = asio::windows::stream_handle;
#elif defined(OS_POSIX)
using Stream = asio::posix::stream_descriptor;
using Stream = asio::local::stream_protocol::socket;
#endif

IpcChannel(std::u16string_view channel_name, Stream&& stream);
Expand Down
46 changes: 43 additions & 3 deletions source/base/ipc/ipc_server.cc
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@
#include <asio/windows/stream_handle.hpp>
#endif // defined(OS_WIN)

#if defined(OS_POSIX)
#include <asio/local/stream_protocol.hpp>
#endif // defined(OS_POSIX)

#include <random>

namespace base {
Expand All @@ -60,7 +64,13 @@ class IpcServer::Listener : public base::enable_shared_from_this<Listener>
void dettach() { server_ = nullptr; }

bool listen(asio::io_context& io_context, std::u16string_view channel_name);

#if defined(OS_WIN)
void onNewConnetion(const std::error_code& error_code, size_t bytes_transferred);
#elif defined(OS_POSIX)
void onNewConnetion(const std::error_code& error_code,
asio::local::stream_protocol::socket socket);
#endif

private:
IpcServer* server_;
Expand All @@ -70,7 +80,8 @@ class IpcServer::Listener : public base::enable_shared_from_this<Listener>
std::unique_ptr<asio::windows::stream_handle> handle_;
std::unique_ptr<asio::windows::overlapped_ptr> overlapped_;
#elif defined(OS_POSIX)
std::unique_ptr<asio::posix::stream_descriptor> handle_;
std::unique_ptr<asio::local::stream_protocol::acceptor> acceptor_;
std::unique_ptr<asio::local::stream_protocol::socket> handle_;
#endif

DISALLOW_COPY_AND_ASSIGN(Listener);
Expand Down Expand Up @@ -168,11 +179,18 @@ bool IpcServer::Listener::listen(asio::io_context& io_context, std::u16string_vi
overlapped_->complete(std::error_code(), 0);
return true;
#else
NOTIMPLEMENTED();
return false;
asio::local::stream_protocol::endpoint endpoint(base::utf8FromUtf16(channel_name));
acceptor_ = std::make_unique<asio::local::stream_protocol::acceptor>(io_context, endpoint);

acceptor_->async_accept(std::bind(&Listener::onNewConnetion,
shared_from_this(),
std::placeholders::_1,
std::placeholders::_2));
return true;
#endif
}

#if defined(OS_WIN)
//--------------------------------------------------------------------------------------------------
void IpcServer::Listener::onNewConnetion(
const std::error_code& error_code, size_t /* bytes_transferred */)
Expand All @@ -191,6 +209,28 @@ void IpcServer::Listener::onNewConnetion(

server_->onNewConnection(index_, std::move(channel));
}
#endif // defined(OS_WIN)

#if defined(OS_POSIX)
//--------------------------------------------------------------------------------------------------
void IpcServer::Listener::onNewConnetion(
const std::error_code& error_code, asio::local::stream_protocol::socket socket)
{
if (!server_)
return;

if (error_code)
{
server_->onErrorOccurred(FROM_HERE);
return;
}

std::unique_ptr<IpcChannel> channel =
std::unique_ptr<IpcChannel>(new IpcChannel(server_->channel_name_, std::move(socket)));

server_->onNewConnection(index_, std::move(channel));
}
#endif // defined(OS_POSIX)

//--------------------------------------------------------------------------------------------------
IpcServer::IpcServer()
Expand Down
9 changes: 8 additions & 1 deletion source/base/ipc/ipc_server.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#ifndef BASE_IPC_IPC_SERVER_H
#define BASE_IPC_IPC_SERVER_H

#include "build/build_config.h"
#include "base/memory/local_memory.h"
#include "base/threading/thread_checker.h"

Expand Down Expand Up @@ -61,8 +62,14 @@ class IpcServer
asio::io_context& io_context_;
std::u16string channel_name_;

#if defined(OS_WIN)
static const size_t kListenersCount = 8;
#elif defined(OS_POSIX)
static const size_t kListenersCount = 1;
#endif

class Listener;
std::array<base::local_shared_ptr<Listener>, 8> listeners_;
std::array<base::local_shared_ptr<Listener>, kListenersCount> listeners_;

THREAD_CHECKER(thread_checker_);

Expand Down

0 comments on commit a461eb4

Please sign in to comment.