Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

sf::IpAddress is always valid, resolution is performed via static member function #2145

Merged

Conversation

vittorioromeo
Copy link
Member

Thanks a lot for making a contribution to SFML! 🙂

Before you create the pull request, we ask you to check the follow boxes. (For small changes not everything needs to ticked, but the more the better!)

  • Has this change been discussed on the forum or in an issue before?
  • Does the code follow the SFML Code Style Guide?
  • Have you provided some example/test code for your changes?
  • If you have additional steps which need to be performed list them as tasks!

Description

This PR is related to the issue #2138.

Tasks

  • Tested on Linux
  • Tested on Windows
  • Tested on macOS
  • Tested on iOS
  • Tested on Android

How to test this PR?

CI and examples.

@codecov
Copy link

codecov bot commented Jun 23, 2022

Codecov Report

Merging #2145 (c571c13) into master (fd3526f) will decrease coverage by 0.05%.
The diff coverage is 68.42%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master    #2145      +/-   ##
==========================================
- Coverage   14.06%   14.00%   -0.06%     
==========================================
  Files         190      189       -1     
  Lines       15879    15868      -11     
  Branches     4200     4195       -5     
==========================================
- Hits         2233     2223      -10     
+ Misses      13519    13517       -2     
- Partials      127      128       +1     
Impacted Files Coverage Δ
include/SFML/Network/Http.hpp 66.66% <ø> (ø)
include/SFML/Network/TcpListener.hpp 0.00% <ø> (ø)
include/SFML/Network/TcpSocket.hpp 100.00% <ø> (ø)
include/SFML/Network/UdpSocket.hpp 0.00% <ø> (ø)
src/SFML/Network/TcpListener.cpp 0.00% <0.00%> (ø)
src/SFML/Network/TcpSocket.cpp 31.44% <0.00%> (ø)
src/SFML/Network/UdpSocket.cpp 0.00% <0.00%> (ø)
src/SFML/Network/IpAddress.cpp 94.00% <85.71%> (-0.55%) ⬇️
src/SFML/Network/Http.cpp 76.76% <100.00%> (+0.50%) ⬆️
... and 3 more

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update fd3526f...c571c13. Read the comment docs.

test/Network/IpAddress.cpp Outdated Show resolved Hide resolved
@@ -369,7 +369,7 @@ Http::Response Http::sendRequest(const Http::Request& request, Time timeout)
Response received;

// Connect the socket to the host
if (m_connection.connect(m_host, m_port, timeout) == Socket::Done)
if (m_connection.connect(m_host.value(), m_port, timeout) == Socket::Done)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we want to check this contains a value to avoid risking a std::bad_optional_access? Maybe just assert that it has a value?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think assert would be appropriate here, as far as I understand at this point m_host should be valid.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Was there ever a conclusion made about this or is it not possible for .value() to be called on a null optional here?

include/SFML/Network/IpAddress.hpp Show resolved Hide resolved
@ChrisThrasher
Copy link
Member

/// std::string message = "Hi, I am " + sf::IpAddress::getLocalAddress().toString();

Here's another reference to the old getLocalAddress() API.

src/SFML/Network/IpAddress.cpp Outdated Show resolved Hide resolved
src/SFML/Network/IpAddress.cpp Outdated Show resolved Hide resolved
Comment on lines 67 to 81
// Try to convert the address as a byte representation ("xxx.xxx.xxx.xxx")
sf::Uint32 ip = inet_addr(address.c_str());
if (ip != INADDR_NONE)
if (const Uint32 ip = inet_addr(address.data()); ip != INADDR_NONE)
{
m_address = ip;
return;
return IpAddress(NoByteOrderConversionTag{}, ip);
}

// Not a valid address, try to convert it as a host name
addrinfo hints;
std::memset(&hints, 0, sizeof(hints));
addrinfo hints{}; // Zero-initialize
hints.ai_family = AF_INET;

addrinfo* result = nullptr;
if (getaddrinfo(address.c_str(), nullptr, &hints, &result) == 0 && result)
if (getaddrinfo(address.data(), nullptr, &hints, &result) == 0 && result != nullptr)
{
sockaddr_in sin;
std::memcpy(&sin, result->ai_addr, sizeof(*result->ai_addr));
ip = sin.sin_addr.s_addr;

const Uint32 ip = sin.sin_addr.s_addr;
freeaddrinfo(result);
m_address = ip;
return;

return IpAddress(NoByteOrderConversionTag{}, ip);
}
Copy link
Member

@binary1248 binary1248 Jun 24, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is the overload with NoByteOrderConversionTag necessary? It is only used internally yet requires being part of the externally visible API. Since this functionality is required from within the class itself, couldn't one just create a value that is initialized to e.g. sf::IpAddress::Any and manually set m_address in it? Any compiler that is reasonable at optimizing should be able to combine the statements into a single operation.

@vittorioromeo vittorioromeo force-pushed the feature/ipaddress_overhaul_pt0 branch 2 times, most recently from e433da8 to 5546886 Compare June 26, 2022 22:56
@vittorioromeo
Copy link
Member Author

@binary1248: please take one more look, I removed the private constructor.

src/SFML/Network/IpAddress.cpp Outdated Show resolved Hide resolved
src/SFML/Network/IpAddress.cpp Outdated Show resolved Hide resolved
src/SFML/Network/IpAddress.cpp Outdated Show resolved Hide resolved
@vittorioromeo
Copy link
Member Author

@binary1248: applied the requested formatting changes.

@SFML SFML deleted a comment from vittorioromeo Jun 27, 2022
@vittorioromeo vittorioromeo merged commit 8c8d97c into SFML:master Jun 27, 2022
@SFML SFML deleted a comment from vittorioromeo Dec 26, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Status: Done
Development

Successfully merging this pull request may close these issues.

5 participants