update SFML_Network to 1.6
git-svn-id: https://dolphin-emu.googlecode.com/svn/trunk@5317 8ced0084-cf51-0410-be5f-012b33b47a6e
This commit is contained in:
parent
9d1cb60aff
commit
25684ab5a9
|
@ -49,7 +49,7 @@
|
||||||
// MacOS
|
// MacOS
|
||||||
#define SFML_SYSTEM_MACOS
|
#define SFML_SYSTEM_MACOS
|
||||||
|
|
||||||
#elif defined(__FreeBSD__)
|
#elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
|
||||||
|
|
||||||
// FreeBSD
|
// FreeBSD
|
||||||
#define SFML_SYSTEM_FREEBSD
|
#define SFML_SYSTEM_FREEBSD
|
||||||
|
|
|
@ -214,8 +214,10 @@ private :
|
||||||
// Member data
|
// Member data
|
||||||
////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////
|
||||||
SocketHelper::SocketType mySocket; ///< Socket descriptor
|
SocketHelper::SocketType mySocket; ///< Socket descriptor
|
||||||
std::vector<char> myPendingPacket; ///< Data of the current pending packet, if any (in non-blocking mode)
|
Uint32 myPendingHeader; ///< Data of the current pending packet header, if any
|
||||||
Int32 myPendingPacketSize; ///< Size of the current pending packet, if any (in non-blocking mode)
|
Uint32 myPendingHeaderSize; ///< Size of the current pending packet header, if any
|
||||||
|
std::vector<char> myPendingPacket; ///< Data of the current pending packet, if any
|
||||||
|
Int32 myPendingPacketSize; ///< Size of the current pending packet, if any
|
||||||
bool myIsBlocking; ///< Is the socket blocking or non-blocking ?
|
bool myIsBlocking; ///< Is the socket blocking or non-blocking ?
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
@ -215,8 +215,10 @@ private :
|
||||||
////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////
|
||||||
SocketHelper::SocketType mySocket; ///< Socket identifier
|
SocketHelper::SocketType mySocket; ///< Socket identifier
|
||||||
unsigned short myPort; ///< Port to which the socket is bound
|
unsigned short myPort; ///< Port to which the socket is bound
|
||||||
std::vector<char> myPendingPacket; ///< Data of the current pending packet, if any (in non-blocking mode)
|
Uint32 myPendingHeader; ///< Data of the current pending packet header, if any
|
||||||
Int32 myPendingPacketSize; ///< Size of the current pending packet, if any (in non-blocking mode)
|
Uint32 myPendingHeaderSize; ///< Size of the current pending packet header, if any
|
||||||
|
std::vector<char> myPendingPacket; ///< Data of the current pending packet, if any
|
||||||
|
Int32 myPendingPacketSize; ///< Size of the current pending packet, if any
|
||||||
bool myIsBlocking; ///< Is the socket blocking or non-blocking ?
|
bool myIsBlocking; ///< Is the socket blocking or non-blocking ?
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
@ -380,7 +380,8 @@ Ftp::Response Ftp::Download(const std::string& DistantFile, const std::string& D
|
||||||
std::ofstream File((Path + Filename).c_str(), std::ios_base::binary);
|
std::ofstream File((Path + Filename).c_str(), std::ios_base::binary);
|
||||||
if (!File)
|
if (!File)
|
||||||
return Response(Response::InvalidFile);
|
return Response(Response::InvalidFile);
|
||||||
File.write(&FileData[0], static_cast<std::streamsize>(FileData.size()));
|
if (!FileData.empty())
|
||||||
|
File.write(&FileData[0], static_cast<std::streamsize>(FileData.size()));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -402,7 +403,8 @@ Ftp::Response Ftp::Upload(const std::string& LocalFile, const std::string& DestP
|
||||||
std::size_t Length = File.tellg();
|
std::size_t Length = File.tellg();
|
||||||
File.seekg(0, std::ios::beg);
|
File.seekg(0, std::ios::beg);
|
||||||
std::vector<char> FileData(Length);
|
std::vector<char> FileData(Length);
|
||||||
File.read(&FileData[0], static_cast<std::streamsize>(Length));
|
if (Length > 0)
|
||||||
|
File.read(&FileData[0], static_cast<std::streamsize>(Length));
|
||||||
|
|
||||||
// Extract the filename from the file path
|
// Extract the filename from the file path
|
||||||
std::string Filename = LocalFile;
|
std::string Filename = LocalFile;
|
||||||
|
@ -700,7 +702,8 @@ void Ftp::DataChannel::Receive(std::vector<char>& Data)
|
||||||
void Ftp::DataChannel::Send(const std::vector<char>& Data)
|
void Ftp::DataChannel::Send(const std::vector<char>& Data)
|
||||||
{
|
{
|
||||||
// Send data
|
// Send data
|
||||||
myDataSocket.Send(&Data[0], Data.size());
|
if (!Data.empty())
|
||||||
|
myDataSocket.Send(&Data[0], Data.size());
|
||||||
|
|
||||||
// Close the data socket
|
// Close the data socket
|
||||||
myDataSocket.Close();
|
myDataSocket.Close();
|
||||||
|
|
|
@ -27,6 +27,8 @@
|
||||||
////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////
|
||||||
#include <SFML/Network/Http.hpp>
|
#include <SFML/Network/Http.hpp>
|
||||||
#include <ctype.h>
|
#include <ctype.h>
|
||||||
|
#include <algorithm>
|
||||||
|
#include <iterator>
|
||||||
#include <sstream>
|
#include <sstream>
|
||||||
|
|
||||||
|
|
||||||
|
@ -51,14 +53,12 @@ namespace sf
|
||||||
////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////
|
||||||
/// Default constructor
|
/// Default constructor
|
||||||
////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////
|
||||||
Http::Request::Request(Method RequestMethod, const std::string& URI, const std::string& Body) :
|
Http::Request::Request(Method RequestMethod, const std::string& URI, const std::string& Body)
|
||||||
myMethod (RequestMethod),
|
|
||||||
myURI (URI),
|
|
||||||
myMajorVersion(1),
|
|
||||||
myMinorVersion(0),
|
|
||||||
myBody (Body)
|
|
||||||
{
|
{
|
||||||
|
SetMethod(RequestMethod);
|
||||||
|
SetURI(URI);
|
||||||
|
SetHttpVersion(1, 0);
|
||||||
|
SetBody(Body);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -180,7 +180,7 @@ myMinorVersion(0)
|
||||||
////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////
|
||||||
const std::string& Http::Response::GetField(const std::string& Field) const
|
const std::string& Http::Response::GetField(const std::string& Field) const
|
||||||
{
|
{
|
||||||
FieldTable::const_iterator It = myFields.find(Field);
|
FieldTable::const_iterator It = myFields.find(ToLower(Field));
|
||||||
if (It != myFields.end())
|
if (It != myFields.end())
|
||||||
{
|
{
|
||||||
return It->second;
|
return It->second;
|
||||||
|
@ -297,8 +297,7 @@ void Http::Response::FromString(const std::string& Data)
|
||||||
|
|
||||||
// Finally extract the body
|
// Finally extract the body
|
||||||
myBody.clear();
|
myBody.clear();
|
||||||
while (std::getline(In, Line))
|
std::copy(std::istreambuf_iterator<char>(In), std::istreambuf_iterator<char>(), std::back_inserter(myBody));
|
||||||
myBody += Line + "\n";
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -386,6 +385,14 @@ Http::Response Http::SendRequest(const Http::Request& Req, float Timeout)
|
||||||
Out << ToSend.myBody.size();
|
Out << ToSend.myBody.size();
|
||||||
ToSend.SetField("Content-Length", Out.str());
|
ToSend.SetField("Content-Length", Out.str());
|
||||||
}
|
}
|
||||||
|
if ((ToSend.myMethod == Request::Post) && !ToSend.HasField("Content-Type"))
|
||||||
|
{
|
||||||
|
ToSend.SetField("Content-Type", "application/x-www-form-urlencoded");
|
||||||
|
}
|
||||||
|
if ((ToSend.myMajorVersion * 10 + ToSend.myMinorVersion >= 11) && !ToSend.HasField("Connection"))
|
||||||
|
{
|
||||||
|
ToSend.SetField("Connection", "close");
|
||||||
|
}
|
||||||
|
|
||||||
// Prepare the response
|
// Prepare the response
|
||||||
Response Received;
|
Response Received;
|
||||||
|
|
|
@ -135,18 +135,18 @@ Socket::Status SocketTCP::Connect(unsigned short Port, const IPAddress& HostAddr
|
||||||
if (select(static_cast<int>(mySocket + 1), NULL, &Selector, NULL, &Time) > 0)
|
if (select(static_cast<int>(mySocket + 1), NULL, &Selector, NULL, &Time) > 0)
|
||||||
{
|
{
|
||||||
// At this point the connection may have been either accepted or refused.
|
// At this point the connection may have been either accepted or refused.
|
||||||
// To know whether it's a success or a failure, we try to retrieve the name of the connected peer
|
// To know whether it's a success or a failure, we try to retrieve the name of the connected peer
|
||||||
SocketHelper::LengthType Size = sizeof(SockAddr);
|
SocketHelper::LengthType Size = sizeof(SockAddr);
|
||||||
if (getpeername(mySocket, reinterpret_cast<sockaddr*>(&SockAddr), &Size) != -1)
|
if (getpeername(mySocket, reinterpret_cast<sockaddr*>(&SockAddr), &Size) != -1)
|
||||||
{
|
{
|
||||||
// Connection accepted
|
// Connection accepted
|
||||||
Status = Socket::Done;
|
Status = Socket::Done;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
// Connection failed
|
// Connection failed
|
||||||
Status = SocketHelper::GetErrorStatus();
|
Status = SocketHelper::GetErrorStatus();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
|
@ -344,11 +344,20 @@ Socket::Status SocketTCP::Receive(Packet& PacketToReceive)
|
||||||
std::size_t Received = 0;
|
std::size_t Received = 0;
|
||||||
if (myPendingPacketSize < 0)
|
if (myPendingPacketSize < 0)
|
||||||
{
|
{
|
||||||
Socket::Status Status = Receive(reinterpret_cast<char*>(&PacketSize), sizeof(PacketSize), Received);
|
// Loop until we've received the entire size of the packet
|
||||||
if (Status != Socket::Done)
|
// (even a 4 bytes variable may be received in more than one call)
|
||||||
return Status;
|
while (myPendingHeaderSize < sizeof(myPendingHeader))
|
||||||
|
{
|
||||||
|
char* Data = reinterpret_cast<char*>(&myPendingHeader) + myPendingHeaderSize;
|
||||||
|
Socket::Status Status = Receive(Data, sizeof(myPendingHeader) - myPendingHeaderSize, Received);
|
||||||
|
myPendingHeaderSize += Received;
|
||||||
|
|
||||||
PacketSize = ntohl(PacketSize);
|
if (Status != Socket::Done)
|
||||||
|
return Status;
|
||||||
|
}
|
||||||
|
|
||||||
|
PacketSize = ntohl(myPendingHeader);
|
||||||
|
myPendingHeaderSize = 0;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
|
@ -472,6 +481,7 @@ void SocketTCP::Create(SocketHelper::SocketType Descriptor)
|
||||||
myIsBlocking = true;
|
myIsBlocking = true;
|
||||||
|
|
||||||
// Reset the pending packet
|
// Reset the pending packet
|
||||||
|
myPendingHeaderSize = 0;
|
||||||
myPendingPacket.clear();
|
myPendingPacket.clear();
|
||||||
myPendingPacketSize = -1;
|
myPendingPacketSize = -1;
|
||||||
|
|
||||||
|
|
|
@ -244,20 +244,25 @@ Socket::Status SocketUDP::Send(Packet& PacketToSend, const IPAddress& Address, u
|
||||||
////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////
|
||||||
Socket::Status SocketUDP::Receive(Packet& PacketToReceive, IPAddress& Address, unsigned short& Port)
|
Socket::Status SocketUDP::Receive(Packet& PacketToReceive, IPAddress& Address, unsigned short& Port)
|
||||||
{
|
{
|
||||||
// This is not safe at all, as data can be lost, duplicated, or arrive in a different order.
|
|
||||||
// So if a packet is split into more than one chunk, nobody knows what could happen...
|
|
||||||
// Conclusion : we shouldn't use packets with UDP, unless we build a more complex protocol on top of it.
|
|
||||||
|
|
||||||
// We start by getting the size of the incoming packet
|
// We start by getting the size of the incoming packet
|
||||||
Uint32 PacketSize = 0;
|
Uint32 PacketSize = 0;
|
||||||
std::size_t Received = 0;
|
std::size_t Received = 0;
|
||||||
if (myPendingPacketSize < 0)
|
if (myPendingPacketSize < 0)
|
||||||
{
|
{
|
||||||
Socket::Status Status = Receive(reinterpret_cast<char*>(&PacketSize), sizeof(PacketSize), Received, Address, Port);
|
// Loop until we've received the entire size of the packet
|
||||||
if (Status != Socket::Done)
|
// (even a 4 bytes variable may be received in more than one call)
|
||||||
return Status;
|
while (myPendingHeaderSize < sizeof(myPendingHeader))
|
||||||
|
{
|
||||||
|
char* Data = reinterpret_cast<char*>(&myPendingHeader) + myPendingHeaderSize;
|
||||||
|
Socket::Status Status = Receive(Data, sizeof(myPendingHeader) - myPendingHeaderSize, Received, Address, Port);
|
||||||
|
myPendingHeaderSize += Received;
|
||||||
|
|
||||||
PacketSize = ntohl(PacketSize);
|
if (Status != Socket::Done)
|
||||||
|
return Status;
|
||||||
|
}
|
||||||
|
|
||||||
|
PacketSize = ntohl(myPendingHeader);
|
||||||
|
myPendingHeaderSize = 0;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
|
@ -265,9 +270,6 @@ Socket::Status SocketUDP::Receive(Packet& PacketToReceive, IPAddress& Address, u
|
||||||
PacketSize = myPendingPacketSize;
|
PacketSize = myPendingPacketSize;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Clear the user packet
|
|
||||||
PacketToReceive.Clear();
|
|
||||||
|
|
||||||
// Use another address instance for receiving the packet data ;
|
// Use another address instance for receiving the packet data ;
|
||||||
// chunks of data coming from a different sender will be discarded (and lost...)
|
// chunks of data coming from a different sender will be discarded (and lost...)
|
||||||
IPAddress Sender;
|
IPAddress Sender;
|
||||||
|
@ -402,6 +404,7 @@ void SocketUDP::Create(SocketHelper::SocketType Descriptor)
|
||||||
myPort = 0;
|
myPort = 0;
|
||||||
|
|
||||||
// Reset the pending packet
|
// Reset the pending packet
|
||||||
|
myPendingHeaderSize = 0;
|
||||||
myPendingPacket.clear();
|
myPendingPacket.clear();
|
||||||
myPendingPacketSize = -1;
|
myPendingPacketSize = -1;
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue