Added a way for partial sends over non-blocking TcpSockets to be handled properly.

This commit is contained in:
binary1248 2015-02-06 19:37:53 +01:00 committed by Lukas Dürrenberger
parent 67c7663c80
commit d790114df8
5 changed files with 66 additions and 9 deletions

View file

@ -282,6 +282,7 @@ private:
////////////////////////////////////////////////////////////
std::vector<char> m_data; ///< Data stored in the packet
std::size_t m_readPos; ///< Current reading position in the packet
std::size_t m_sendPos; ///< Current send position in the packet (for handling partial sends)
bool m_isValid; ///< Reading state of the packet
};

View file

@ -54,6 +54,7 @@ public:
{
Done, ///< The socket has sent / received the data
NotReady, ///< The socket is not ready to send / receive data yet
Partial, ///< The socket sent a part of the data
Disconnected, ///< The TCP socket has been disconnected
Error ///< An unexpected error happened
};

View file

@ -124,6 +124,9 @@ public:
////////////////////////////////////////////////////////////
/// \brief Send raw data to the remote peer
///
/// To be able to handle partial sends over non-blocking
/// sockets, use the send(const void*, std::size_t, std::size_t&)
/// overload instead.
/// This function will fail if the socket is not connected.
///
/// \param data Pointer to the sequence of bytes to send
@ -136,6 +139,22 @@ public:
////////////////////////////////////////////////////////////
Status send(const void* data, std::size_t size);
////////////////////////////////////////////////////////////
/// \brief Send raw data to the remote peer
///
/// This function will fail if the socket is not connected.
///
/// \param data Pointer to the sequence of bytes to send
/// \param size Number of bytes to send
/// \param sent The number of bytes sent will be written here
///
/// \return Status code
///
/// \see receive
///
////////////////////////////////////////////////////////////
Status send(const void* data, std::size_t size, std::size_t& sent);
////////////////////////////////////////////////////////////
/// \brief Receive raw data from the remote peer
///
@ -157,6 +176,10 @@ public:
////////////////////////////////////////////////////////////
/// \brief Send a formatted packet of data to the remote peer
///
/// In non-blocking mode, if this function returns sf::Socket::Partial,
/// you \em must retry sending the same unmodified packet before sending
/// anything else in order to guarantee the packet arrives at the remote
/// peer uncorrupted.
/// This function will fail if the socket is not connected.
///
/// \param packet Packet to send