CIRCT 23.0.0git
Loading...
Searching...
No Matches
RpcClient.h
Go to the documentation of this file.
1//===- RpcClient.h - ESI Cosim RPC client -----------------------*- C++ -*-===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// Public C++ interface for the cosim RPC client. The on-the-wire protocol is
10// WebSocket + JSON; see cosim-protocol.md for the spec. This header exposes
11// no transport-specific types, so the implementation (RpcClient.cpp) is free
12// to evolve independently.
13//
14//===----------------------------------------------------------------------===//
15
16// NOLINTNEXTLINE(llvm-header-guard)
17#ifndef ESI_BACKENDS_RPCCLIENT_H
18#define ESI_BACKENDS_RPCCLIENT_H
19
20#include "esi/Common.h"
21
22#include <functional>
23#include <memory>
24#include <string>
25#include <vector>
26
27namespace esi {
28class Logger;
29namespace backends {
30namespace cosim {
31
32/// A client for the cosim RPC server. Hides the WebSocket + JSON transport
33/// behind a small C++ API; see cosim-protocol.md for the wire format.
34class RpcClient {
35public:
36 RpcClient(Logger &logger, const std::string &hostname, uint16_t port);
38
39 // Non-copyable.
40 RpcClient(const RpcClient &) = delete;
41 RpcClient &operator=(const RpcClient &) = delete;
42
43 /// Get the ESI version from the manifest.
44 uint32_t getEsiVersion() const;
45
46 /// Get the compressed manifest from the server.
47 std::vector<uint8_t> getCompressedManifest() const;
48
49 /// Channel direction as reported by the server.
51
52 /// Description of a channel from the server.
53 struct ChannelDesc {
54 std::string name;
55 std::string type;
57 };
58
59 /// Get the channel description for a channel name.
60 /// Returns true if the channel was found.
61 bool getChannelDesc(const std::string &channelName, ChannelDesc &desc) const;
62
63 /// List all channels available on the server.
64 std::vector<ChannelDesc> listChannels() const;
65
66 /// Send a message to a server-bound channel.
67 void writeToServer(const std::string &channelName, const MessageData &data);
68
69 /// Callback type for receiving messages from a client-bound channel.
70 /// Return true if the message was consumed, false to retry the same owning
71 /// message object.
73 std::function<bool(std::unique_ptr<SegmentedMessageData> &)>;
74
75 /// Abstract handle for a read channel connection.
76 /// Destructor disconnects from the channel.
78 public:
79 virtual ~ReadChannelConnection() = default;
80 virtual void disconnect() = 0;
81 };
82
83 /// Connect to a client-bound channel and receive messages via callback.
84 /// Returns a handle that disconnects when destroyed.
85 std::unique_ptr<ReadChannelConnection>
86 connectClientReceiver(const std::string &channelName, ReadCallback callback);
87
88 /// Hide the implementation details from this header file.
89 class Impl;
90
91private:
92 std::unique_ptr<Impl> impl;
93};
94
95} // namespace cosim
96} // namespace backends
97} // namespace esi
98
99#endif // ESI_BACKENDS_RPCCLIENT_H
A concrete flat message backed by a single vector of bytes.
Definition Common.h:155
Abstract handle for a read channel connection.
Definition RpcClient.h:77
A client for the cosim RPC server.
Definition RpcClient.h:34
std::function< bool(std::unique_ptr< SegmentedMessageData > &)> ReadCallback
Callback type for receiving messages from a client-bound channel.
Definition RpcClient.h:73
ChannelDirection
Channel direction as reported by the server.
Definition RpcClient.h:50
std::unique_ptr< ReadChannelConnection > connectClientReceiver(const std::string &channelName, ReadCallback callback)
Connect to a client-bound channel and receive messages via callback.
std::vector< uint8_t > getCompressedManifest() const
Get the compressed manifest from the server.
uint32_t getEsiVersion() const
Get the ESI version from the manifest.
void writeToServer(const std::string &channelName, const MessageData &data)
Send a message to a server-bound channel.
bool getChannelDesc(const std::string &channelName, ChannelDesc &desc) const
Get the channel description for a channel name.
RpcClient & operator=(const RpcClient &)=delete
std::vector< ChannelDesc > listChannels() const
List all channels available on the server.
std::unique_ptr< Impl > impl
Definition RpcClient.h:92
RpcClient(const RpcClient &)=delete
Definition esi.py:1
Description of a channel from the server.
Definition RpcClient.h:53