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 <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 /// Default maximum number of frames sent to a server-bound channel which
67 /// the simulation has not yet consumed.
68 static constexpr unsigned DefaultToServerMaxInFlight = 16;
69 /// Default maximum number of frames in flight (queued in the simulator or
70 /// sent but not yet consumed by the client) on a client-bound channel.
71 static constexpr unsigned DefaultToClientMaxInFlight = 32;
72
73 /// Send a message to a server-bound channel. Blocks while the channel's
74 /// in-flight limit is reached. Throws if the connection is lost. Frames
75 /// sent before the simulation has connected its end of the channel are
76 /// held by the server (and count as in flight), not dropped.
77 void writeToServer(const std::string &channelName, const MessageData &data);
78
79 /// Non-blocking send of one message, split into `frames`, to a server-bound
80 /// channel. If the channel is below its in-flight limit, sends all of them
81 /// (possibly overshooting the limit by `frames.size() - 1`)
82 /// and returns true. Otherwise sends nothing and returns false. With
83 /// `force`, sends regardless of the limit (but still counts the frames as
84 /// in flight); for the rest of a message whose first part was admitted.
85 bool tryWriteToServer(const std::string &channelName,
86 const std::vector<MessageData> &frames,
87 bool force = false);
88
89 /// Set the maximum number of unconsumed frames on a server-bound channel.
90 /// Zero means unbounded.
91 void setToServerMaxInFlight(const std::string &channelName,
92 unsigned maxInFlight);
93
94 /// Callback type for receiving messages from a client-bound channel.
95 /// Return true if the message was consumed, false to retry the same owning
96 /// message object.
98 std::function<bool(std::unique_ptr<SegmentedMessageData> &)>;
99
100 /// Abstract handle for a read channel connection.
101 /// Destructor disconnects from the channel.
103 public:
104 virtual ~ReadChannelConnection() = default;
105 virtual void disconnect() = 0;
106 };
107
108 /// Connect to a client-bound channel and receive messages via callback.
109 /// `maxInFlight` is the maximum number of frames the simulator may have in
110 /// flight for this channel; zero means unbounded. A frame stops counting
111 /// once `callback` returns true for it. Returns a handle that disconnects
112 /// when destroyed.
113 std::unique_ptr<ReadChannelConnection>
114 connectClientReceiver(const std::string &channelName, ReadCallback callback,
115 unsigned maxInFlight = DefaultToClientMaxInFlight);
116
117 /// Hide the implementation details from this header file.
118 class Impl;
119
120private:
121 std::unique_ptr<Impl> impl;
122};
123
124} // namespace cosim
125} // namespace backends
126} // namespace esi
127
128#endif // ESI_BACKENDS_RPCCLIENT_H
A concrete flat message backed by a single vector of bytes.
Definition Common.h:197
Abstract handle for a read channel connection.
Definition RpcClient.h:102
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:50
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:71
std::function< bool(std::unique_ptr< SegmentedMessageData > &)> ReadCallback
Callback type for receiving messages from a client-bound channel.
Definition RpcClient.h:97
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:68
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.
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:121
RpcClient(const RpcClient &)=delete
Definition esi.py:1
Description of a channel from the server.
Definition RpcClient.h:53