Skip to content

Instantly share code, notes, and snippets.

@pepepper
Created April 4, 2020 04:48
Show Gist options
  • Save pepepper/63408cc076799d8d0b4113ae03817c43 to your computer and use it in GitHub Desktop.
Save pepepper/63408cc076799d8d0b4113ae03817c43 to your computer and use it in GitHub Desktop.
//-----------------------------------------------
//
// This file is part of the Siv3D Engine.
//
// Copyright (c) 2008-2019 Ryo Suzuki
// Copyright (c) 2016-2019 OpenSiv3D Project
//
// Licensed under the MIT License.
//
//-----------------------------------------------
//参考
//https://github.com/Siv3D/OpenSiv3D/blob/master/Siv3D/src/Siv3D/TCPClient/TCPClientDetail.hpp
//https://boostjp.github.io/tips/network/tcp.html#resolve
//https://www.boost.org/doc/libs/1_72_0/doc/html/boost_asio/reference/ip__basic_resolver_query/basic_resolver_query/overload4.html
//https://www.boost.org/doc/libs/1_72_0/doc/html/boost_asio/reference/ip__basic_endpoint/address/overload1.html
//https://www.boost.org/doc/libs/1_59_0/doc/html/boost_asio/reference/ip__address_v4/to_string.html
//https://subscription.packtpub.com/book/application_development/9781783986545/1/ch01lvl1sec13/resolving-a-dns-name
//https://stackoverflow.com/questions/51427957/boost-protocol-type-namespace
# include <Siv3D/IPv4.hpp>
# include <Siv3D/EngineLog.hpp>
# include <Siv3D/Format.hpp>
# define ASIO_STANDALONE
# include <asio/asio.hpp>
namespace s3d
{
IPv4::IPv4(const String& ipv4)
{
asio::io_service io_service;
asio::ip::tcp::resolver resolver(io_service);
asio::ip::tcp::resolver::query query(asio::ip::tcp::v4(),Unicode::NarrowAscii(ipv4),"80");
asio::error_code ec;
auto it=resolver.resolve(query,ec);
if(ec.value()!=0){
// Failed to resolve the DNS name. Breaking execution.
LOG_ERROR(U"Failed to resolve a DNS name.");
LOG_ERROR(U"Error Code: {}"_fmt(ec.value()));
LOG_ERROR(U"Message: {}"_fmt(Unicode::Widen(ec.message())));
}
if(!it.empty())std::istringstream(it->endpoint().address().to_v4().to_string())>>*this;
}
String IPv4::toStr() const
{
return Format(asUint8[0], U'.', asUint8[1], U'.', asUint8[2], U'.', asUint8[3]);
}
void Formatter(FormatData& formatData, const IPv4& value)
{
formatData.string.append(value.toStr());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment