add endpoint print functions

master
Chunting Gu 5 years ago
parent 57f069135b
commit 2cf48aed35

@ -99,6 +99,8 @@ void Client::DoConnect(RequestPtr request, const std::string& default_port) {
port = default_port;
}
LOG_VERB("Resolve host (%s)...", request->host().c_str());
std::error_code ec;
auto endpoints = resolver.resolve(tcp::v4(), request->host(), port, ec);

@ -5,6 +5,7 @@
#include <filesystem>
#include <fstream>
#include <iomanip> // for put_time
#include <iostream>
#include <sstream>
#include "webcc/string.h"
@ -72,5 +73,21 @@ void DumpByLine(const std::string& data, std::ostream& os,
}
}
void PrintEndpoint(std::ostream& ostream,
const asio::ip::tcp::endpoint& endpoint) {
ostream << endpoint;
if (endpoint.protocol() == asio::ip::tcp::v4()) {
ostream << ", v4";
} else if (endpoint.protocol() == asio::ip::tcp::v6()) {
ostream << ", v6";
}
}
std::string EndpointToString(const asio::ip::tcp::endpoint& endpoint) {
std::stringstream ss;
PrintEndpoint(ss, endpoint);
return ss.str();
}
} // namespace utility
} // namespace webcc

@ -1,8 +1,11 @@
#ifndef WEBCC_UTILITY_H_
#define WEBCC_UTILITY_H_
#include <iosfwd>
#include <string>
#include "asio/ip/tcp.hpp"
#include "webcc/globals.h"
// Avoid include <filesystem> in the header.
@ -35,6 +38,14 @@ bool ReadFile(const std::filesystem::path& path, std::string* output);
void DumpByLine(const std::string& data, std::ostream& os,
const std::string& prefix);
// Print TCP endpoint.
// Usage: PrintEndpoint(std::cout, endpoint)
void PrintEndpoint(std::ostream& ostream,
const asio::ip::tcp::endpoint& endpoint);
// TCP endpoint to string.
std::string EndpointToString(const asio::ip::tcp::endpoint& endpoint);
} // namespace utility
} // namespace webcc

Loading…
Cancel
Save