cpp-msgpack-rpc 0.2.0
An RPC library implementing MessagePack RPC.
Loading...
Searching...
No Matches
tcp_address.cpp
Go to the documentation of this file.
1/*
2 * Copyright 2023 MusicScience37 (Kenta Kabashima)
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 */
21
22#include <optional>
23#include <utility>
24
25#include <asio/ip/address.hpp>
26#include <asio/ip/basic_endpoint.hpp>
27#include <fmt/format.h>
28#include <fmt/ostream.h>
29
32
33namespace msgpack_rpc::addresses {
34
35TCPAddress::TCPAddress(std::string_view ip_address, std::uint16_t port_number)
36 : TCPAddress(
37 AsioTCPAddress(asio::ip::make_address(ip_address), port_number)) {}
38
40
41std::string TCPAddress::ip_address() const {
42 return address_.address().to_string();
43}
44
45std::uint16_t TCPAddress::port_number() const { return address_.port(); }
46
48 return URI("tcp", address_.address().to_string(), address_.port());
49}
50
51std::string TCPAddress::to_string() const { return fmt::format("{}", *this); }
52
54
55bool TCPAddress::operator==(const TCPAddress& right) const {
56 return address_ == right.address_;
57}
58
59bool TCPAddress::operator!=(const TCPAddress& right) const {
60 return !operator==(right);
61}
62
63} // namespace msgpack_rpc::addresses
64
65namespace fmt {
66
67format_context::iterator
68formatter<msgpack_rpc::addresses::TCPAddress>::format( // NOLINT
70 format_context& context) const {
71 return fmt::format_to(context.out(), "{}://{}",
73}
74
75} // namespace fmt
76
77std::ostream& operator<<(
78 std::ostream& stream, const msgpack_rpc::addresses::TCPAddress& address) {
79 stream << address.asio_address();
80 return stream;
81}
Class of addresses of TCP.
Definition tcp_address.h:44
std::uint16_t port_number() const
Get the port number.
bool operator!=(const TCPAddress &right) const
Compare with an address.
AsioTCPAddress address_
Address in asio library.
bool operator==(const TCPAddress &right) const
Compare with an address.
std::string ip_address() const
Get the IP address.
URI to_uri() const override
Convert to a URI.
std::string to_string() const override
Convert to a string specifying URI.
TCPAddress(std::string_view ip_address, std::uint16_t port_number)
Constructor.
const AsioTCPAddress & asio_address() const
Get the address in asio library.
Class of URIs (Uniform Resource Identifiers) to specify endpoints in this library.
Definition uri.h:38
Namespace of fmt library.
Definition uri.h:113
Namespace of addresses.
Definition i_address.h:26
constexpr std::string_view TCP_SCHEME
Scheme of TCP.
Definition schemes.h:27
asio::ip::tcp::endpoint AsioTCPAddress
Type of addresses of TCP in asio library.
Definition tcp_address.h:39
STL namespace.
Definition of constants of schemes.
Definition of TCPAddress.
Definition of URI class.
std::ostream & operator<<(std::ostream &stream, const msgpack_rpc::addresses::URI &uri)
Format a URI.
Definition uri.cpp:110