unilink  0.4.3
A simple C++ library for unified async communication
channel.hpp
Go to the documentation of this file.
1 /*
2  * Copyright 2025 Jinwoo Sung
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #pragma once
18 #include <functional>
19 #include <memory>
20 #include <vector>
21 
22 #include "unilink/base/common.hpp"
25 
26 namespace unilink {
27 namespace interface {
29  public:
30  using OnBytes = std::function<void(memory::ConstByteSpan)>;
31  using OnState = std::function<void(base::LinkState)>;
32  using OnBackpressure = std::function<void(size_t /*queued_bytes*/)>;
33 
34  virtual ~Channel() = default;
35 
36  virtual void start() = 0;
37  virtual void stop() = 0;
38  virtual bool is_connected() const = 0;
39 
40  // Single send API (copies into internal queue)
41  virtual void async_write_copy(memory::ConstByteSpan data) = 0;
42  // Zero-copy APIs (ownership transfer or shared ownership)
43  virtual void async_write_move(std::vector<uint8_t>&& data) = 0;
44  virtual void async_write_shared(std::shared_ptr<const std::vector<uint8_t>> data) = 0;
45 
46  // Callbacks
47  virtual void on_bytes(OnBytes cb) = 0;
48  virtual void on_state(OnState cb) = 0;
49  virtual void on_backpressure(OnBackpressure cb) = 0;
50 };
51 } // namespace interface
52 } // namespace unilink
#define UNILINK_API
Definition: visibility.hpp:37