lib/fmt/SocketAddressFormatter: new library
This commit is contained in:
parent
b36f5f1ec4
commit
64647edbe1
|
@ -8,9 +8,9 @@
|
||||||
#include "BackgroundCommand.hxx"
|
#include "BackgroundCommand.hxx"
|
||||||
#include "Partition.hxx"
|
#include "Partition.hxx"
|
||||||
#include "Instance.hxx"
|
#include "Instance.hxx"
|
||||||
|
#include "lib/fmt/SocketAddressFormatter.hxx"
|
||||||
#include "net/UniqueSocketDescriptor.hxx"
|
#include "net/UniqueSocketDescriptor.hxx"
|
||||||
#include "net/SocketAddress.hxx"
|
#include "net/SocketAddress.hxx"
|
||||||
#include "net/ToString.hxx"
|
|
||||||
#include "Log.hxx"
|
#include "Log.hxx"
|
||||||
#include "Version.h"
|
#include "Version.h"
|
||||||
|
|
||||||
|
@ -36,11 +36,10 @@ Client::Client(EventLoop &_loop, Partition &_partition,
|
||||||
|
|
||||||
void
|
void
|
||||||
client_new(EventLoop &loop, Partition &partition,
|
client_new(EventLoop &loop, Partition &partition,
|
||||||
UniqueSocketDescriptor fd, SocketAddress address, int uid,
|
UniqueSocketDescriptor fd, SocketAddress remote_address, int uid,
|
||||||
unsigned permission) noexcept
|
unsigned permission) noexcept
|
||||||
{
|
{
|
||||||
static unsigned int next_client_num;
|
static unsigned int next_client_num;
|
||||||
const auto remote = ToString(address);
|
|
||||||
|
|
||||||
assert(fd.IsDefined());
|
assert(fd.IsDefined());
|
||||||
|
|
||||||
|
@ -61,7 +60,7 @@ client_new(EventLoop &loop, Partition &partition,
|
||||||
partition.clients.push_back(*client);
|
partition.clients.push_back(*client);
|
||||||
|
|
||||||
FmtInfo(client_domain, "[{}] opened from {}",
|
FmtInfo(client_domain, "[{}] opened from {}",
|
||||||
num, remote);
|
num, remote_address);
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
|
|
|
@ -4,6 +4,7 @@
|
||||||
#include "ServerSocket.hxx"
|
#include "ServerSocket.hxx"
|
||||||
#include "lib/fmt/ExceptionFormatter.hxx"
|
#include "lib/fmt/ExceptionFormatter.hxx"
|
||||||
#include "lib/fmt/RuntimeError.hxx"
|
#include "lib/fmt/RuntimeError.hxx"
|
||||||
|
#include "lib/fmt/SocketAddressFormatter.hxx"
|
||||||
#include "net/IPv4Address.hxx"
|
#include "net/IPv4Address.hxx"
|
||||||
#include "net/IPv6Address.hxx"
|
#include "net/IPv6Address.hxx"
|
||||||
#include "net/StaticSocketAddress.hxx"
|
#include "net/StaticSocketAddress.hxx"
|
||||||
|
@ -13,7 +14,6 @@
|
||||||
#include "net/UniqueSocketDescriptor.hxx"
|
#include "net/UniqueSocketDescriptor.hxx"
|
||||||
#include "net/Resolver.hxx"
|
#include "net/Resolver.hxx"
|
||||||
#include "net/AddressInfo.hxx"
|
#include "net/AddressInfo.hxx"
|
||||||
#include "net/ToString.hxx"
|
|
||||||
#include "event/SocketEvent.hxx"
|
#include "event/SocketEvent.hxx"
|
||||||
#include "fs/AllocatedPath.hxx"
|
#include "fs/AllocatedPath.hxx"
|
||||||
#include "util/Domain.hxx"
|
#include "util/Domain.hxx"
|
||||||
|
@ -83,9 +83,9 @@ public:
|
||||||
event.Close();
|
event.Close();
|
||||||
}
|
}
|
||||||
|
|
||||||
[[nodiscard]] [[gnu::pure]]
|
[[nodiscard]]
|
||||||
std::string ToString() const noexcept {
|
SocketAddress GetAddress() const noexcept {
|
||||||
return ::ToString(address);
|
return address;
|
||||||
}
|
}
|
||||||
|
|
||||||
void SetFD(UniqueSocketDescriptor _fd) noexcept {
|
void SetFD(UniqueSocketDescriptor _fd) noexcept {
|
||||||
|
@ -224,23 +224,19 @@ ServerSocket::Open()
|
||||||
i.Open();
|
i.Open();
|
||||||
} catch (...) {
|
} catch (...) {
|
||||||
if (good != nullptr && good->GetSerial() == i.GetSerial()) {
|
if (good != nullptr && good->GetSerial() == i.GetSerial()) {
|
||||||
const auto address_string = i.ToString();
|
|
||||||
const auto good_string = good->ToString();
|
|
||||||
FmtError(server_socket_domain,
|
FmtError(server_socket_domain,
|
||||||
"bind to '{}' failed "
|
"bind to '{}' failed "
|
||||||
"(continuing anyway, because "
|
"(continuing anyway, because "
|
||||||
"binding to '{}' succeeded): {}",
|
"binding to '{}' succeeded): {}",
|
||||||
address_string,
|
i.GetAddress(),
|
||||||
good_string,
|
good->GetAddress(),
|
||||||
std::current_exception());
|
std::current_exception());
|
||||||
} else if (bad == nullptr) {
|
} else if (bad == nullptr) {
|
||||||
bad = &i;
|
bad = &i;
|
||||||
|
|
||||||
const auto address_string = i.ToString();
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
std::throw_with_nested(FmtRuntimeError("Failed to bind to '{}'",
|
std::throw_with_nested(FmtRuntimeError("Failed to bind to '{}'",
|
||||||
address_string));
|
i.GetAddress()));
|
||||||
} catch (...) {
|
} catch (...) {
|
||||||
last_error = std::current_exception();
|
last_error = std::current_exception();
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,21 @@
|
||||||
|
// SPDX-License-Identifier: BSD-2-Clause
|
||||||
|
// author: Max Kellermann <max.kellermann@gmail.com>
|
||||||
|
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include "net/SocketAddress.hxx"
|
||||||
|
#include "net/ToString.hxx"
|
||||||
|
|
||||||
|
#include <fmt/format.h>
|
||||||
|
|
||||||
|
#include <concepts>
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
requires std::convertible_to<T, SocketAddress>
|
||||||
|
struct fmt::formatter<T> : formatter<string_view>
|
||||||
|
{
|
||||||
|
template<typename FormatContext>
|
||||||
|
auto format(SocketAddress address, FormatContext &ctx) {
|
||||||
|
return formatter<string_view>::format(ToString(address), ctx);
|
||||||
|
}
|
||||||
|
};
|
|
@ -1,12 +1,14 @@
|
||||||
// SPDX-License-Identifier: GPL-2.0-or-later
|
// SPDX-License-Identifier: GPL-2.0-or-later
|
||||||
// Copyright The Music Player Daemon Project
|
// Copyright The Music Player Daemon Project
|
||||||
|
|
||||||
|
#include "lib/fmt/SocketAddressFormatter.hxx"
|
||||||
#include "net/Resolver.hxx"
|
#include "net/Resolver.hxx"
|
||||||
#include "net/AddressInfo.hxx"
|
#include "net/AddressInfo.hxx"
|
||||||
#include "net/ToString.hxx"
|
|
||||||
#include "net/SocketAddress.hxx"
|
#include "net/SocketAddress.hxx"
|
||||||
#include "util/PrintException.hxx"
|
#include "util/PrintException.hxx"
|
||||||
|
|
||||||
|
#include <fmt/format.h>
|
||||||
|
|
||||||
#include <exception>
|
#include <exception>
|
||||||
|
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
|
@ -20,7 +22,7 @@ try {
|
||||||
}
|
}
|
||||||
|
|
||||||
for (const auto &i : Resolve(argv[1], 80, AI_PASSIVE, SOCK_STREAM)) {
|
for (const auto &i : Resolve(argv[1], 80, AI_PASSIVE, SOCK_STREAM)) {
|
||||||
printf("%s\n", ToString(i).c_str());
|
fmt::print("{}\n", i);
|
||||||
}
|
}
|
||||||
|
|
||||||
return EXIT_SUCCESS;
|
return EXIT_SUCCESS;
|
||||||
|
|
Loading…
Reference in New Issue