summaryrefslogtreecommitdiff
path: root/src/SFML/Network/SocketTCP.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/SFML/Network/SocketTCP.cpp')
-rwxr-xr-xsrc/SFML/Network/SocketTCP.cpp35
1 files changed, 28 insertions, 7 deletions
diff --git a/src/SFML/Network/SocketTCP.cpp b/src/SFML/Network/SocketTCP.cpp
index 03d96d3..6a200fc 100755
--- a/src/SFML/Network/SocketTCP.cpp
+++ b/src/SFML/Network/SocketTCP.cpp
@@ -131,11 +131,22 @@ Socket::Status SocketTCP::Connect(unsigned short Port, const IPAddress& HostAddr
Time.tv_sec = static_cast<long>(Timeout);
Time.tv_usec = (static_cast<long>(Timeout * 1000) % 1000) * 1000;
- // Wait for something to write on our socket (would mean the connection has been accepted)
+ // Wait for something to write on our socket (which means that the connection request has returned)
if (select(static_cast<int>(mySocket + 1), NULL, &Selector, NULL, &Time) > 0)
{
- // Connection succeeded
- Status = Socket::Done;
+ // 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
+ 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();
+ }
}
else
{
@@ -333,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;
+
+ if (Status != Socket::Done)
+ return Status;
+ }
- PacketSize = ntohl(PacketSize);
+ PacketSize = ntohl(myPendingHeader);
+ myPendingHeaderSize = 0;
}
else
{
@@ -461,6 +481,7 @@ void SocketTCP::Create(SocketHelper::SocketType Descriptor)
myIsBlocking = true;
// Reset the pending packet
+ myPendingHeaderSize = 0;
myPendingPacket.clear();
myPendingPacketSize = -1;