unilink  0.4.3
A simple C++ library for unified async communication
context.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 
19 #include <optional>
20 #include <string>
21 #include <string_view>
22 #include <vector>
23 
26 
27 namespace unilink {
28 namespace wrapper {
29 
34  public:
35  MessageContext(size_t client_id, std::string_view data, std::string client_info = "")
36  : client_id_(client_id), data_(data), client_info_(std::move(client_info)) {}
37 
38  size_t client_id() const { return client_id_; }
39  std::string_view data() const { return data_; }
40  const std::string& client_info() const { return client_info_; }
41 
42  // Useful for servers (empty for point-to-point)
43  const std::string& remote_address() const { return client_info_; }
44 
45  private:
46  size_t client_id_;
47  std::string_view data_;
48  std::string client_info_;
49 };
50 
55  public:
56  ConnectionContext(size_t client_id, std::string client_info = "")
57  : client_id_(client_id), client_info_(std::move(client_info)) {}
58 
59  size_t client_id() const { return client_id_; }
60  const std::string& client_info() const { return client_info_; }
61 
62  private:
63  size_t client_id_;
64  std::string client_info_;
65 };
66 
70 class ErrorContext {
71  public:
72  ErrorContext(ErrorCode code, std::string_view message, std::optional<size_t> client_id = std::nullopt)
73  : code_(code), message_(message), client_id_(client_id) {}
74 
75  ErrorCode code() const { return code_; }
76  std::string_view message() const { return message_; }
77  std::optional<size_t> client_id() const { return client_id_; }
78 
79  private:
80  ErrorCode code_;
81  std::string message_;
82  std::optional<size_t> client_id_;
83 };
84 
85 } // namespace wrapper
86 } // namespace unilink