CIRCT 24.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 <exception>
23#include <functional>
24#include <memory>
25#include <optional>
26#include <string>
27#include <vector>
28
29namespace esi {
30class Logger;
31namespace backends {
32namespace cosim {
33
34/// A client for the cosim RPC server. Hides the WebSocket + JSON transport
35/// behind a small C++ API; see cosim-protocol.md for the wire format.
36class RpcClient {
37public:
38 RpcClient(Logger &logger, const std::string &hostname, uint16_t port);
40
41 // Non-copyable.
42 RpcClient(const RpcClient &) = delete;
43 RpcClient &operator=(const RpcClient &) = delete;
44
45 /// Get the ESI version from the manifest.
46 uint32_t getEsiVersion() const;
47
48 /// Get the compressed manifest from the server.
49 std::vector<uint8_t> getCompressedManifest() const;
50
51 /// Channel direction as reported by the server.
53
54 /// Description of a channel from the server.
55 struct ChannelDesc {
56 std::string name;
57 std::string type;
59 };
60
61 /// Get the channel description for a channel name.
62 /// Returns true if the channel was found.
63 bool getChannelDesc(const std::string &channelName, ChannelDesc &desc) const;
64
65 /// List all channels available on the server.
66 std::vector<ChannelDesc> listChannels() const;
67
68 /// Default maximum number of frames sent to a server-bound channel which
69 /// the simulation has not yet consumed.
70 static constexpr unsigned DefaultToServerMaxInFlight = 16;
71 /// Default maximum number of frames in flight (queued in the simulator or
72 /// sent but not yet consumed by the client) on a client-bound channel.
73 static constexpr unsigned DefaultToClientMaxInFlight = 32;
74
75 /// Send a message to a server-bound channel. Blocks while the channel's
76 /// in-flight limit is reached. Throws if the connection is lost. Frames
77 /// sent before the simulation has connected its end of the channel are
78 /// held by the server (and count as in flight), not dropped.
79 void writeToServer(const std::string &channelName, const MessageData &data);
80
81 /// Non-blocking send of one message, split into `frames`, to a server-bound
82 /// channel. If the channel is below its in-flight limit, sends all of them
83 /// (possibly overshooting the limit by `frames.size() - 1`)
84 /// and returns true. Otherwise sends nothing and returns false. With
85 /// `force`, sends regardless of the limit (but still counts the frames as
86 /// in flight); for the rest of a message whose first part was admitted.
87 bool tryWriteToServer(const std::string &channelName,
88 const std::vector<MessageData> &frames,
89 bool force = false);
90
91 /// Set the maximum number of unconsumed frames on a server-bound channel.
92 /// Zero means unbounded.
93 void setToServerMaxInFlight(const std::string &channelName,
94 unsigned maxInFlight);
95
96 /// Data source for a server-bound channel (see `connectServerSender()`):
97 /// returns the frames of the next message to send (which may be none, e.g.
98 /// for an empty message), or nullopt if no message is available yet. After
99 /// returning nullopt (or throwing), it isn't called again until
100 /// `WriteChannelConnection::notify()`.
102 std::function<std::optional<std::vector<MessageData>>()>;
103 /// Called with an exception thrown by a `SendFetchCallback` or while
104 /// sending, e.g. since the connection was lost.
105 using SendErrorCallback = std::function<void(std::exception_ptr)>;
106
107 /// Handle for a sender registered with `connectServerSender()`. Destructor
108 /// disconnects.
110 public:
111 virtual ~WriteChannelConnection() = default;
112 /// Notify that the fetch callback has data again. Doesn't block.
113 virtual void notify() = 0;
114 /// Stop calling the callbacks. Blocks while one is being called on
115 /// another thread.
116 virtual void disconnect() = 0;
117 };
118
119 /// The most messages a sender sends before other channels are serviced.
120 static constexpr size_t MaxSendsPerTurn = 64;
121
122 /// Send to a server-bound channel in callback (pull) mode, like
123 /// `WriteChannelPort`'s callback mode. Whenever the channel is below its
124 /// in-flight limit and `fetch` may have data, the client's transport thread
125 /// calls `fetch` and sends all of the message's frames. Errors are reported
126 /// to `onError`; exceptions it throws are logged. The callbacks are never
127 /// called concurrently with each other. A channel can have only one sender,
128 /// and nothing else should write to it meanwhile.
129 std::unique_ptr<WriteChannelConnection>
130 connectServerSender(const std::string &channelName, SendFetchCallback fetch,
131 SendErrorCallback onError);
132
133 /// Callback type for receiving messages from a client-bound channel.
134 /// Return true if the message was consumed, false to retry the same owning
135 /// message object.
137 std::function<bool(std::unique_ptr<SegmentedMessageData> &)>;
138
139 /// Abstract handle for a read channel connection.
140 /// Destructor disconnects from the channel.
142 public:
143 virtual ~ReadChannelConnection() = default;
144 virtual void disconnect() = 0;
145 };
146
147 /// Connect to a client-bound channel and receive messages via callback.
148 /// `maxInFlight` is the maximum number of frames the simulator may have in
149 /// flight for this channel; zero means unbounded. A frame stops counting
150 /// once `callback` returns true for it. Returns a handle that disconnects
151 /// when destroyed.
152 std::unique_ptr<ReadChannelConnection>
153 connectClientReceiver(const std::string &channelName, ReadCallback callback,
154 unsigned maxInFlight = DefaultToClientMaxInFlight);
155
156 /// Hide the implementation details from this header file.
157 class Impl;
158
159private:
160 std::unique_ptr<Impl> impl;
161};
162
163} // namespace cosim
164} // namespace backends
165} // namespace esi
166
167#endif // ESI_BACKENDS_RPCCLIENT_H
A concrete flat message backed by a single vector of bytes.
Definition Common.h:201
Abstract handle for a read channel connection.
Definition RpcClient.h:141
Handle for a sender registered with connectServerSender().
Definition RpcClient.h:109
virtual void disconnect()=0
Stop calling the callbacks.
virtual void notify()=0
Notify that the fetch callback has data again. Doesn't block.
void setToServerMaxInFlight(const std::string &channelName, unsigned maxInFlight)
Set the maximum number of unconsumed frames on a server-bound channel.
ChannelDirection
Channel direction as reported by the server.
Definition RpcClient.h:52
static constexpr unsigned DefaultToClientMaxInFlight
Default maximum number of frames in flight (queued in the simulator or sent but not yet consumed by t...
Definition RpcClient.h:73
std::function< bool(std::unique_ptr< SegmentedMessageData > &)> ReadCallback
Callback type for receiving messages from a client-bound channel.
Definition RpcClient.h:136
static constexpr unsigned DefaultToServerMaxInFlight
Default maximum number of frames sent to a server-bound channel which the simulation has not yet cons...
Definition RpcClient.h:70
std::unique_ptr< ReadChannelConnection > connectClientReceiver(const std::string &channelName, ReadCallback callback, unsigned maxInFlight=DefaultToClientMaxInFlight)
Connect to a client-bound channel and receive messages via callback.
RpcClient(Logger &logger, const std::string &hostname, uint16_t port)
bool tryWriteToServer(const std::string &channelName, const std::vector< MessageData > &frames, bool force=false)
Non-blocking send of one message, split into frames, to a server-bound channel.
std::vector< uint8_t > getCompressedManifest() const
Get the compressed manifest from the server.
uint32_t getEsiVersion() const
Get the ESI version from the manifest.
static constexpr size_t MaxSendsPerTurn
The most messages a sender sends before other channels are serviced.
Definition RpcClient.h:120
std::function< std::optional< std::vector< MessageData > >()> SendFetchCallback
Data source for a server-bound channel (see connectServerSender()): returns the frames of the next me...
Definition RpcClient.h:101
void writeToServer(const std::string &channelName, const MessageData &data)
Send a message to a server-bound channel.
std::function< void(std::exception_ptr)> SendErrorCallback
Called with an exception thrown by a SendFetchCallback or while sending, e.g.
Definition RpcClient.h:105
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< WriteChannelConnection > connectServerSender(const std::string &channelName, SendFetchCallback fetch, SendErrorCallback onError)
Send to a server-bound channel in callback (pull) mode, like WriteChannelPort's callback mode.
std::unique_ptr< Impl > impl
Definition RpcClient.h:160
RpcClient(const RpcClient &)=delete
Definition esi.py:1
Description of a channel from the server.
Definition RpcClient.h:55