#pragma once
#include <wtf/Assertions.h>
#include "Connection.h"
namespace IPC {
class MessageSender {
public:
virtual ~MessageSender();
template<typename U> bool send(const U& message)
{
return send(message, messageSenderDestinationID(), { });
}
template<typename U> bool send(const U& message, uint64_t destinationID, OptionSet<SendOption> sendOptions = { })
{
static_assert(!U::isSync, "Message is sync!");
auto encoder = std::make_unique<Encoder>(U::receiverName(), U::name(), destinationID);
encoder->encode(message.arguments());
return sendMessage(WTFMove(encoder), sendOptions);
}
template<typename T>
bool sendSync(T&& message, typename T::Reply&& reply, Seconds timeout = Seconds::infinity(), OptionSet<SendSyncOption> sendSyncOptions = { })
{
static_assert(T::isSync, "Message is not sync!");
return sendSync(std::forward<T>(message), WTFMove(reply), messageSenderDestinationID(), timeout, sendSyncOptions);
}
template<typename T>
bool sendSync(T&& message, typename T::Reply&& reply, uint64_t destinationID, Seconds timeout = Seconds::infinity(), OptionSet<SendSyncOption> sendSyncOptions = { })
{
ASSERT(messageSenderConnection());
return messageSenderConnection()->sendSync(WTFMove(message), WTFMove(reply), destinationID, timeout, sendSyncOptions);
}
template<typename T, typename... Args>
void sendWithAsyncReply(T&& message, CompletionHandler<void(Args...)>&& completionHandler)
{
messageSenderConnection()->sendWithAsyncReply(WTFMove(message), WTFMove(completionHandler), messageSenderDestinationID());
}
virtual bool sendMessage(std::unique_ptr<Encoder>, OptionSet<SendOption>);
private:
virtual Connection* messageSenderConnection() = 0;
virtual uint64_t messageSenderDestinationID() = 0;
};
}