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:
Shawn Hoffman 2010-04-10 16:53:06 +00:00
parent 9d1cb60aff
commit 25684ab5a9
7 changed files with 71 additions and 44 deletions

View File

@ -49,7 +49,7 @@
// MacOS
#define SFML_SYSTEM_MACOS
#elif defined(__FreeBSD__)
#elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
// FreeBSD
#define SFML_SYSTEM_FREEBSD

View File

@ -214,8 +214,10 @@ private :
// Member data
////////////////////////////////////////////////////////////
SocketHelper::SocketType mySocket; ///< Socket descriptor
std::vector<char> myPendingPacket; ///< Data of the current pending packet, if any (in non-blocking mode)
Int32 myPendingPacketSize; ///< Size of the current pending packet, if any (in non-blocking mode)
Uint32 myPendingHeader; ///< Data of the current pending packet header, if any
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 ?
};

View File

@ -215,8 +215,10 @@ private :
////////////////////////////////////////////////////////////
SocketHelper::SocketType mySocket; ///< Socket identifier
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)
Int32 myPendingPacketSize; ///< Size of the current pending packet, if any (in non-blocking mode)
Uint32 myPendingHeader; ///< Data of the current pending packet header, if any
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 ?
};

View File

@ -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);
if (!File)
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();
File.seekg(0, std::ios::beg);
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
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)
{
// Send data
myDataSocket.Send(&Data[0], Data.size());
if (!Data.empty())
myDataSocket.Send(&Data[0], Data.size());
// Close the data socket
myDataSocket.Close();

View File

@ -27,6 +27,8 @@
////////////////////////////////////////////////////////////
#include <SFML/Network/Http.hpp>
#include <ctype.h>
#include <algorithm>
#include <iterator>
#include <sstream>
@ -51,14 +53,12 @@ namespace sf
////////////////////////////////////////////////////////////
/// Default constructor
////////////////////////////////////////////////////////////
Http::Request::Request(Method RequestMethod, const std::string& URI, const std::string& Body) :
myMethod (RequestMethod),
myURI (URI),
myMajorVersion(1),
myMinorVersion(0),
myBody (Body)
Http::Request::Request(Method RequestMethod, const std::string& URI, const std::string& 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
{
FieldTable::const_iterator It = myFields.find(Field);
FieldTable::const_iterator It = myFields.find(ToLower(Field));
if (It != myFields.end())
{
return It->second;
@ -297,8 +297,7 @@ void Http::Response::FromString(const std::string& Data)
// Finally extract the body
myBody.clear();
while (std::getline(In, Line))
myBody += Line + "\n";
std::copy(std::istreambuf_iterator<char>(In), std::istreambuf_iterator<char>(), std::back_inserter(myBody));
}
@ -386,6 +385,14 @@ Http::Response Http::SendRequest(const Http::Request& Req, float Timeout)
Out << ToSend.myBody.size();
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
Response Received;

View File

@ -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)
{
// 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);
if (getpeername(mySocket, reinterpret_cast<sockaddr*>(&SockAddr), &Size) != -1)
{
// Connection accepted
Status = Socket::Done;
}
else
{
// Connection failed
Status = SocketHelper::GetErrorStatus();
}
if (getpeername(mySocket, reinterpret_cast<sockaddr*>(&SockAddr), &Size) != -1)
{
// Connection accepted
Status = Socket::Done;
}
else
{
// Connection failed
Status = SocketHelper::GetErrorStatus();
}
}
else
{
@ -344,11 +344,20 @@ Socket::Status SocketTCP::Receive(Packet& PacketToReceive)
std::size_t Received = 0;
if (myPendingPacketSize < 0)
{
Socket::Status Status = Receive(reinterpret_cast<char*>(&PacketSize), sizeof(PacketSize), Received);
if (Status != Socket::Done)
return Status;
// Loop until we've received the entire size of the packet
// (even a 4 bytes variable may be received in more than one call)
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
{
@ -472,6 +481,7 @@ void SocketTCP::Create(SocketHelper::SocketType Descriptor)
myIsBlocking = true;
// Reset the pending packet
myPendingHeaderSize = 0;
myPendingPacket.clear();
myPendingPacketSize = -1;

View File

@ -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)
{
// 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
Uint32 PacketSize = 0;
std::size_t Received = 0;
if (myPendingPacketSize < 0)
{
Socket::Status Status = Receive(reinterpret_cast<char*>(&PacketSize), sizeof(PacketSize), Received, Address, Port);
if (Status != Socket::Done)
return Status;
// Loop until we've received the entire size of the packet
// (even a 4 bytes variable may be received in more than one call)
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
{
@ -265,9 +270,6 @@ Socket::Status SocketUDP::Receive(Packet& PacketToReceive, IPAddress& Address, u
PacketSize = myPendingPacketSize;
}
// Clear the user packet
PacketToReceive.Clear();
// Use another address instance for receiving the packet data ;
// chunks of data coming from a different sender will be discarded (and lost...)
IPAddress Sender;
@ -402,6 +404,7 @@ void SocketUDP::Create(SocketHelper::SocketType Descriptor)
myPort = 0;
// Reset the pending packet
myPendingHeaderSize = 0;
myPendingPacket.clear();
myPendingPacketSize = -1;