Improved the API documentation of sf::IpAddress
git-svn-id: https://sfml.svn.sourceforge.net/svnroot/sfml/branches/sfml2@1452 4e206d99-4929-0410-ac5d-dfc041789085
This commit is contained in:
parent
b7eb09cb79
commit
e924c9cf39
34 changed files with 336 additions and 264 deletions
|
@ -26,7 +26,7 @@
|
|||
// Headers
|
||||
////////////////////////////////////////////////////////////
|
||||
#include <SFML/Network/Ftp.hpp>
|
||||
#include <SFML/Network/IPAddress.hpp>
|
||||
#include <SFML/Network/IpAddress.hpp>
|
||||
#include <algorithm>
|
||||
#include <fstream>
|
||||
#include <iterator>
|
||||
|
@ -192,7 +192,7 @@ Ftp::~Ftp()
|
|||
////////////////////////////////////////////////////////////
|
||||
/// Connect to the specified FTP server
|
||||
////////////////////////////////////////////////////////////
|
||||
Ftp::Response Ftp::Connect(const IPAddress& server, unsigned short port, float timeout)
|
||||
Ftp::Response Ftp::Connect(const IpAddress& server, unsigned short port, float timeout)
|
||||
{
|
||||
// Connect to the server
|
||||
if (myCommandSocket.Connect(port, server, timeout) != Socket::Done)
|
||||
|
@ -647,7 +647,7 @@ Ftp::Response Ftp::DataChannel::Open(Ftp::TransferMode mode)
|
|||
|
||||
// Reconstruct connection port and address
|
||||
unsigned short port = data[4] * 256 + data[5];
|
||||
IPAddress address(static_cast<Uint8>(data[0]),
|
||||
IpAddress address(static_cast<Uint8>(data[0]),
|
||||
static_cast<Uint8>(data[1]),
|
||||
static_cast<Uint8>(data[2]),
|
||||
static_cast<Uint8>(data[3]));
|
||||
|
|
|
@ -350,7 +350,7 @@ void Http::SetHost(const std::string& host, unsigned short port)
|
|||
if (!myHostName.empty() && (*myHostName.rbegin() == '/'))
|
||||
myHostName.erase(myHostName.size() - 1);
|
||||
|
||||
myHost = IPAddress(myHostName);
|
||||
myHost = IpAddress(myHostName);
|
||||
}
|
||||
|
||||
|
||||
|
|
|
@ -25,7 +25,7 @@
|
|||
////////////////////////////////////////////////////////////
|
||||
// Headers
|
||||
////////////////////////////////////////////////////////////
|
||||
#include <SFML/Network/IPAddress.hpp>
|
||||
#include <SFML/Network/IpAddress.hpp>
|
||||
#include <SFML/Network/Http.hpp>
|
||||
#include <SFML/Network/SocketHelper.hpp>
|
||||
#include <string.h>
|
||||
|
@ -34,15 +34,11 @@
|
|||
namespace sf
|
||||
{
|
||||
////////////////////////////////////////////////////////////
|
||||
/// Static member data
|
||||
////////////////////////////////////////////////////////////
|
||||
const IPAddress IPAddress::LocalHost(127, 0, 0, 1);
|
||||
const IpAddress IpAddress::LocalHost(127, 0, 0, 1);
|
||||
|
||||
|
||||
////////////////////////////////////////////////////////////
|
||||
/// Default constructor
|
||||
////////////////////////////////////////////////////////////
|
||||
IPAddress::IPAddress() :
|
||||
IpAddress::IpAddress() :
|
||||
myAddress(INADDR_NONE)
|
||||
{
|
||||
|
||||
|
@ -50,9 +46,7 @@ myAddress(INADDR_NONE)
|
|||
|
||||
|
||||
////////////////////////////////////////////////////////////
|
||||
/// Construct the address from a string
|
||||
////////////////////////////////////////////////////////////
|
||||
IPAddress::IPAddress(const std::string& address)
|
||||
IpAddress::IpAddress(const std::string& address)
|
||||
{
|
||||
// First try to convert it as a byte representation ("xxx.xxx.xxx.xxx")
|
||||
myAddress = inet_addr(address.c_str());
|
||||
|
@ -76,10 +70,7 @@ IPAddress::IPAddress(const std::string& address)
|
|||
|
||||
|
||||
////////////////////////////////////////////////////////////
|
||||
/// Construct the address from a C-style string ;
|
||||
/// Needed for implicit conversions from literal strings to IPAddress to work
|
||||
////////////////////////////////////////////////////////////
|
||||
IPAddress::IPAddress(const char* address)
|
||||
IpAddress::IpAddress(const char* address)
|
||||
{
|
||||
// First try to convert it as a byte representation ("xxx.xxx.xxx.xxx")
|
||||
myAddress = inet_addr(address);
|
||||
|
@ -103,36 +94,28 @@ IPAddress::IPAddress(const char* address)
|
|||
|
||||
|
||||
////////////////////////////////////////////////////////////
|
||||
/// Construct the address from 4 bytes
|
||||
////////////////////////////////////////////////////////////
|
||||
IPAddress::IPAddress(Uint8 byte0, Uint8 byte1, Uint8 byte2, Uint8 byte3)
|
||||
IpAddress::IpAddress(Uint8 byte0, Uint8 byte1, Uint8 byte2, Uint8 byte3)
|
||||
{
|
||||
myAddress = htonl((byte0 << 24) | (byte1 << 16) | (byte2 << 8) | byte3);
|
||||
}
|
||||
|
||||
|
||||
////////////////////////////////////////////////////////////
|
||||
/// Construct the address from a 32-bits integer
|
||||
////////////////////////////////////////////////////////////
|
||||
IPAddress::IPAddress(Uint32 address)
|
||||
IpAddress::IpAddress(Uint32 address)
|
||||
{
|
||||
myAddress = htonl(address);
|
||||
}
|
||||
|
||||
|
||||
////////////////////////////////////////////////////////////
|
||||
/// Tell if the address is a valid one
|
||||
////////////////////////////////////////////////////////////
|
||||
bool IPAddress::IsValid() const
|
||||
bool IpAddress::IsValid() const
|
||||
{
|
||||
return myAddress != INADDR_NONE;
|
||||
}
|
||||
|
||||
|
||||
////////////////////////////////////////////////////////////
|
||||
/// Get a string representation of the address
|
||||
////////////////////////////////////////////////////////////
|
||||
std::string IPAddress::ToString() const
|
||||
std::string IpAddress::ToString() const
|
||||
{
|
||||
in_addr address;
|
||||
address.s_addr = myAddress;
|
||||
|
@ -142,24 +125,20 @@ std::string IPAddress::ToString() const
|
|||
|
||||
|
||||
////////////////////////////////////////////////////////////
|
||||
/// Get an integer representation of the address
|
||||
////////////////////////////////////////////////////////////
|
||||
Uint32 IPAddress::ToInteger() const
|
||||
Uint32 IpAddress::ToInteger() const
|
||||
{
|
||||
return ntohl(myAddress);
|
||||
}
|
||||
|
||||
|
||||
////////////////////////////////////////////////////////////
|
||||
/// Get the computer's local IP address (from the LAN point of view)
|
||||
////////////////////////////////////////////////////////////
|
||||
IPAddress IPAddress::GetLocalAddress()
|
||||
IpAddress IpAddress::GetLocalAddress()
|
||||
{
|
||||
// The method here is to connect a UDP socket to anyone (here to localhost),
|
||||
// and get the local socket address with the getsockname function.
|
||||
// UDP connection will not send anything to the network, so this function won't cause any overhead.
|
||||
|
||||
IPAddress localAddress;
|
||||
IpAddress localAddress;
|
||||
|
||||
// Create the socket
|
||||
SocketHelper::SocketType sock = socket(PF_INET, SOCK_DGRAM, 0);
|
||||
|
@ -199,82 +178,80 @@ IPAddress IPAddress::GetLocalAddress()
|
|||
|
||||
|
||||
////////////////////////////////////////////////////////////
|
||||
/// Get the computer's public IP address (from the web point of view)
|
||||
////////////////////////////////////////////////////////////
|
||||
IPAddress IPAddress::GetPublicAddress(float timeout)
|
||||
IpAddress IpAddress::GetPublicAddress(float timeout)
|
||||
{
|
||||
// The trick here is more complicated, because the only way
|
||||
// to get our public IP address is to get it from a distant computer.
|
||||
// Here we get the web page from http://www.sfml-dev.org/ip-provider.php
|
||||
// and parse the result to extract our IP address
|
||||
// (not very hard : the web page contains only our IP address).
|
||||
// (not very hard: the web page contains only our IP address).
|
||||
|
||||
Http server("www.sfml-dev.org");
|
||||
Http::Request request(Http::Request::Get, "/ip-provider.php");
|
||||
Http::Response page = server.SendRequest(request, timeout);
|
||||
if (page.GetStatus() == Http::Response::Ok)
|
||||
return IPAddress(page.GetBody());
|
||||
return IpAddress(page.GetBody());
|
||||
|
||||
// Something failed: return an invalid address
|
||||
return IPAddress();
|
||||
return IpAddress();
|
||||
}
|
||||
|
||||
|
||||
////////////////////////////////////////////////////////////
|
||||
bool operator ==(const IPAddress& left, const IPAddress& right)
|
||||
bool operator ==(const IpAddress& left, const IpAddress& right)
|
||||
{
|
||||
return left.ToInteger() == right.ToInteger();
|
||||
}
|
||||
|
||||
|
||||
////////////////////////////////////////////////////////////
|
||||
bool operator !=(const IPAddress& left, const IPAddress& right)
|
||||
bool operator !=(const IpAddress& left, const IpAddress& right)
|
||||
{
|
||||
return !(left == right);
|
||||
}
|
||||
|
||||
|
||||
////////////////////////////////////////////////////////////
|
||||
bool operator <(const IPAddress& left, const IPAddress& right)
|
||||
bool operator <(const IpAddress& left, const IpAddress& right)
|
||||
{
|
||||
return left.ToInteger() < right.ToInteger();
|
||||
}
|
||||
|
||||
|
||||
////////////////////////////////////////////////////////////
|
||||
bool operator >(const IPAddress& left, const IPAddress& right)
|
||||
bool operator >(const IpAddress& left, const IpAddress& right)
|
||||
{
|
||||
return right < left;
|
||||
}
|
||||
|
||||
|
||||
////////////////////////////////////////////////////////////
|
||||
bool operator <=(const IPAddress& left, const IPAddress& right)
|
||||
bool operator <=(const IpAddress& left, const IpAddress& right)
|
||||
{
|
||||
return !(right < left);
|
||||
}
|
||||
|
||||
|
||||
////////////////////////////////////////////////////////////
|
||||
bool operator >=(const IPAddress& left, const IPAddress& right)
|
||||
bool operator >=(const IpAddress& left, const IpAddress& right)
|
||||
{
|
||||
return !(left < right);
|
||||
}
|
||||
|
||||
|
||||
////////////////////////////////////////////////////////////
|
||||
std::istream& operator >>(std::istream& stream, IPAddress& address)
|
||||
std::istream& operator >>(std::istream& stream, IpAddress& address)
|
||||
{
|
||||
std::string str;
|
||||
stream >> str;
|
||||
address = IPAddress(str);
|
||||
address = IpAddress(str);
|
||||
|
||||
return stream;
|
||||
}
|
||||
|
||||
|
||||
////////////////////////////////////////////////////////////
|
||||
std::ostream& operator <<(std::ostream& stream, const IPAddress& address)
|
||||
std::ostream& operator <<(std::ostream& stream, const IpAddress& address)
|
||||
{
|
||||
return stream << address.ToString();
|
||||
}
|
||||
|
|
|
@ -26,7 +26,7 @@
|
|||
// Headers
|
||||
////////////////////////////////////////////////////////////
|
||||
#include <SFML/Network/SocketTCP.hpp>
|
||||
#include <SFML/Network/IPAddress.hpp>
|
||||
#include <SFML/Network/IpAddress.hpp>
|
||||
#include <SFML/Network/Packet.hpp>
|
||||
#include <SFML/Network/SocketHelper.hpp>
|
||||
#include <SFML/System/Err.hpp>
|
||||
|
@ -67,7 +67,7 @@ void SocketTCP::SetBlocking(bool blocking)
|
|||
////////////////////////////////////////////////////////////
|
||||
/// Connect to another computer on a specified port
|
||||
////////////////////////////////////////////////////////////
|
||||
Socket::Status SocketTCP::Connect(unsigned short port, const IPAddress& host, float timeout)
|
||||
Socket::Status SocketTCP::Connect(unsigned short port, const IpAddress& host, float timeout)
|
||||
{
|
||||
// Make sure our socket is valid
|
||||
if (!IsValid())
|
||||
|
@ -203,7 +203,7 @@ bool SocketTCP::Listen(unsigned short port)
|
|||
/// Wait for a connection (must be listening to a port).
|
||||
/// This function will block if the socket is blocking
|
||||
////////////////////////////////////////////////////////////
|
||||
Socket::Status SocketTCP::Accept(SocketTCP& connected, IPAddress* address)
|
||||
Socket::Status SocketTCP::Accept(SocketTCP& connected, IpAddress* address)
|
||||
{
|
||||
// Address that will be filled with client informations
|
||||
sockaddr_in clientAddress;
|
||||
|
@ -216,14 +216,14 @@ Socket::Status SocketTCP::Accept(SocketTCP& connected, IPAddress* address)
|
|||
if (!connected.IsValid())
|
||||
{
|
||||
if (address)
|
||||
*address = IPAddress();
|
||||
*address = IpAddress();
|
||||
|
||||
return SocketHelper::GetErrorStatus();
|
||||
}
|
||||
|
||||
// Fill address if requested
|
||||
if (address)
|
||||
*address = IPAddress(inet_ntoa(clientAddress.sin_addr));
|
||||
*address = IpAddress(inet_ntoa(clientAddress.sin_addr));
|
||||
|
||||
return Socket::Done;
|
||||
}
|
||||
|
|
|
@ -26,7 +26,7 @@
|
|||
// Headers
|
||||
////////////////////////////////////////////////////////////
|
||||
#include <SFML/Network/SocketUDP.hpp>
|
||||
#include <SFML/Network/IPAddress.hpp>
|
||||
#include <SFML/Network/IpAddress.hpp>
|
||||
#include <SFML/Network/Packet.hpp>
|
||||
#include <SFML/System/Err.hpp>
|
||||
#include <algorithm>
|
||||
|
@ -115,7 +115,7 @@ bool SocketUDP::Unbind()
|
|||
////////////////////////////////////////////////////////////
|
||||
/// Send an array of bytes
|
||||
////////////////////////////////////////////////////////////
|
||||
Socket::Status SocketUDP::Send(const char* data, std::size_t sizeInBytes, const IPAddress& address, unsigned short port)
|
||||
Socket::Status SocketUDP::Send(const char* data, std::size_t sizeInBytes, const IpAddress& address, unsigned short port)
|
||||
{
|
||||
// Make sure the socket is valid
|
||||
if (!IsValid())
|
||||
|
@ -159,7 +159,7 @@ Socket::Status SocketUDP::Send(const char* data, std::size_t sizeInBytes, const
|
|||
/// Receive an array of bytes.
|
||||
/// This function will block if the socket is blocking
|
||||
////////////////////////////////////////////////////////////
|
||||
Socket::Status SocketUDP::Receive(char* data, std::size_t maxSize, std::size_t& sizeReceived, IPAddress& address, unsigned short& port)
|
||||
Socket::Status SocketUDP::Receive(char* data, std::size_t maxSize, std::size_t& sizeReceived, IpAddress& address, unsigned short& port)
|
||||
{
|
||||
// First clear the size received
|
||||
sizeReceived = 0;
|
||||
|
@ -192,14 +192,14 @@ Socket::Status SocketUDP::Receive(char* data, std::size_t maxSize, std::size_t&
|
|||
// Check the number of bytes received
|
||||
if (received > 0)
|
||||
{
|
||||
address = IPAddress(inet_ntoa(sockAddr.sin_addr));
|
||||
address = IpAddress(inet_ntoa(sockAddr.sin_addr));
|
||||
port = ntohs(sockAddr.sin_port);
|
||||
sizeReceived = static_cast<std::size_t>(received);
|
||||
return Socket::Done;
|
||||
}
|
||||
else
|
||||
{
|
||||
address = IPAddress();
|
||||
address = IpAddress();
|
||||
port = 0;
|
||||
return received == 0 ? Socket::Disconnected : SocketHelper::GetErrorStatus();
|
||||
}
|
||||
|
@ -216,7 +216,7 @@ Socket::Status SocketUDP::Receive(char* data, std::size_t maxSize, std::size_t&
|
|||
////////////////////////////////////////////////////////////
|
||||
/// Send a packet of data
|
||||
////////////////////////////////////////////////////////////
|
||||
Socket::Status SocketUDP::Send(Packet& packet, const IPAddress& address, unsigned short port)
|
||||
Socket::Status SocketUDP::Send(Packet& packet, const IpAddress& address, unsigned short port)
|
||||
{
|
||||
// Get the data to send from the packet
|
||||
std::size_t dataSize = 0;
|
||||
|
@ -242,7 +242,7 @@ Socket::Status SocketUDP::Send(Packet& packet, const IPAddress& address, unsigne
|
|||
/// Receive a packet.
|
||||
/// This function will block if the socket is blocking
|
||||
////////////////////////////////////////////////////////////
|
||||
Socket::Status SocketUDP::Receive(Packet& packet, IPAddress& address, unsigned short& port)
|
||||
Socket::Status SocketUDP::Receive(Packet& packet, IpAddress& address, unsigned short& port)
|
||||
{
|
||||
// We start by getting the size of the incoming packet
|
||||
Uint32 packetSize = 0;
|
||||
|
@ -272,7 +272,7 @@ Socket::Status SocketUDP::Receive(Packet& packet, IPAddress& address, unsigned s
|
|||
|
||||
// Use another address instance for receiving the packet data ;
|
||||
// chunks of data coming from a different sender will be discarded (and lost...)
|
||||
IPAddress sender;
|
||||
IpAddress sender;
|
||||
unsigned short senderPort;
|
||||
|
||||
// Then loop until we receive all the packet data
|
||||
|
|
|
@ -34,8 +34,6 @@
|
|||
namespace sf
|
||||
{
|
||||
////////////////////////////////////////////////////////////
|
||||
// Static member data
|
||||
////////////////////////////////////////////////////////////
|
||||
const std::size_t String::InvalidPos = std::basic_string<Uint32>::npos;
|
||||
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue