LibQuicR
simple_publish/main.cpp
// SPDX-FileCopyrightText: Copyright (c) 2026 Cisco Systems
// SPDX-License-Identifier: BSD-2-Clause
#include <iostream>
class SimpleClientCallbacks : public quicr::Session::ClientCallbacks
{
struct Private
{
explicit Private() = default;
};
public:
SimpleClientCallbacks(Private) {}
static std::shared_ptr<SimpleClientCallbacks> Create()
{
return std::make_shared<SimpleClientCallbacks>(Private{});
}
const std::shared_ptr<quicr::Session>&,
const quicr::SetupAttributes& setup_attributes) override
{
std::cerr << "Setup received from '" << setup_attributes.endpoint_id << "'" << std::endl;
return {};
}
};
int
main()
try {
std::cerr << "Enter relay URI (prefixed with either moq:// or moqt://): ";
std::getline(std::cin, config.connect_uri);
quicr::SessionManager session_mgr;
auto weak_session = session_mgr.AddTransport(config, SimpleClientCallbacks::Create());
auto session = weak_session.lock();
if (!session) {
std::cerr << "Failed to connect" << std::endl;
return EXIT_FAILURE;
}
while (session->GetStatus() != quicr::Session::Status::kReady) {
const auto status = session->GetStatus();
std::cerr << "Failed to connect" << std::endl;
return EXIT_FAILURE;
}
std::this_thread::sleep_for(std::chrono::milliseconds(20));
}
quicr::TrackNamespace track_namespace = [] {
std::string str;
std::cerr << "Enter track namespace entries delimited by / (e.g. test/track): ";
std::cin >> str;
std::vector<std::string> entries;
std::size_t pos = 0;
while ((pos = str.find('/')) != std::string::npos) {
entries.emplace_back(str.substr(0, pos));
str.erase(0, pos + 1);
}
entries.emplace_back(std::move(str));
return quicr::TrackNamespace{ entries };
}();
std::string str;
std::cerr << "Enter a name for the track: ";
std::cin >> str;
}(track_namespace);
auto handler =
session_mgr.AddHandler(session, handler);
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
while (!handler->CanPublish()) {
const auto status = handler->GetStatus();
std::cerr << "Publisher failed to become ready" << std::endl;
return EXIT_FAILURE;
}
std::this_thread::sleep_for(std::chrono::milliseconds(20));
}
std::string text;
for (;;) {
std::cerr << "Enter text (/quit to exit): ";
if (!std::getline(std::cin, text) || text == "/quit") {
std::cerr << std::endl;
break;
}
auto data = quicr::AsBytes(text);
headers.payload_length = data.size();
headers.priority = 127;
handler->PublishObject(headers, data);
headers.group_id++;
}
std::cerr << "Done publishing! Goodbye" << std::endl;
return EXIT_SUCCESS;
} catch (const std::exception& e) {
std::cerr << "Caught exception: " << e.what() << std::endl;
return EXIT_FAILURE;
}
Holds either a success value or an error value.
Definition: expected.h:65
static std::shared_ptr< PublishTrackHandler > Create(const FullTrackName &full_track_name, TrackMode track_mode, uint8_t publisher_priority, uint8_t publish_object_priority, uint32_t default_ttl, messages::Location largest_location)
Create a shared Publish track handler.
Definition: publish_track_handler.h:123
@ kNoSubscribers
No subscriber is currently bound.
@ kPendingPublishOk
Waiting for acknowledgement of publisher-initiated publish.
@ kPaused
Subscriber disabled forwarding.
Owns transports and sessions and routes generic track handlers to a session.
Definition: session_manager.h:29
void AddHandler(const std::shared_ptr< Session > &session, std::shared_ptr< TrackHandler > handler)
Register a concrete track or namespace handler with a session.
std::weak_ptr< Session > AddTransport(const ClientConfig &config, std::shared_ptr< Session::ClientCallbacks > callbacks=nullptr)
Create a client transport and wait for its session to be established.
static std::shared_ptr< Session > Create(const ClientConfig &cfg, std::shared_ptr< Transport > transport, std::shared_ptr< Connection > connection, std::shared_ptr< ClientCallbacks > callbacks, std::shared_ptr< Clock > clock, std::shared_ptr< Logger > logger=nullptr)
Create a client-mode endpoint.
Definition: session.h:163
@ kReady
Setup is complete and the session is ready.
@ kNotConnected
No active underlying connection exists.
@ kFailedToConnect
Underlying connection establishment failed.
@ kInvalidParams
Session configuration or peer parameters are invalid.
@ kNotReady
Session is not ready for protocol operations.
@ kInternalError
Session encountered an internal failure.
An N-tuple representation of a MOQ namespace.
Definition: track_name.h:22
requires std::is_standard_layout_v< T > std::is_same_v< std::decay_t< T >, std::string > std::span< const uint8_t > AsBytes(const T &value)
Views a value's in-memory object representation as bytes.
Definition: bytes.h:34
requires std::is_standard_layout_v< T > std::is_same_v< std::decay_t< T >, std::string > Bytes AsOwnedBytes(const T &value)
Copies a value's native object representation into an owning byte buffer.
Definition: bytes.h:60
@ kStream
Object is or should be carried on a stream.
@ kAvailable
The object exists and has payload data.
Configuration for an outbound client session.
Definition: config.h:78
std::string connect_uri
Destination URI using moq://, moqt://, or https://.
Definition: config.h:79
quicr::TransportConfig transport_config
Underlying QUIC transport configuration.
Definition: config.h:72
Full track name struct.
Definition: track_name.h:436
Object headers struct.
Definition: object.h:49
virtual Expected< void, Error< ErrorCode > > SetupReceived(const std::shared_ptr< Session > &session, const SetupAttributes &setup_attributes)
Callback on receipt of the peer's SETUP message.
Callback interface for session events specific to client mode.
Definition: session_callbacks.h:266
Peer setup attributes.
Definition: attributes.h:39
std::string endpoint_id
Endpoint identifier advertised by the peer, or an empty string if absent.
Definition: attributes.h:41
std::string tls_cert_filename
Path to the external PEM certificate chain used by a server.
Definition: config.h:30
std::string tls_key_filename
Path to the external private-key file corresponding to tls_cert_filename.
Definition: config.h:35
uint32_t time_queue_max_duration
Maximum time-queue duration in milliseconds.
Definition: config.h:38
Absolute group/object location ordered lexicographically by group then object.
Definition: ctrl_message_types.h:171