/* $Id: tcpconnection.hpp,v 1.6 2005/12/06 21:31:52 chfreund Exp $ */ #ifndef _TCPCONNECTION_HPP_ #define _TCPCONNECTION_HPP_ #include #include #include "message.hpp" #if SDL_BYTEORDER == SDL_LIL_ENDIAN #define ENCODE_IP(a,b,c,d) (((((((d) << 8) + (c)) << 8) + (b)) << 8) + (a)) #define DECODE_IP(h) (255 & h), (255 & ((h) >> 8)), \ (255 & ((h) >> 16)), (255 & ((h) >> 24)) #else // SDL_BYTEORDER == SDL_BIG_ENDIAN #define ENCODE_IP(a,b,c,d) (((((((a) << 8) + (b)) << 8) + (c)) << 8) + (d)) #define DECODE_IP(h) (255 & ((h) >> 24)), (255 & ((h) >> 16)), \ (255 & ((h) >> 8)), (255 & h) #endif class TCPProgressListener { public: virtual ~TCPProgressListener() { } virtual void sendProgress( Sint32 size, Sint32 sent ) = 0; virtual void recvProgress( Sint32 size, Sint32 received ) = 0; }; /*! \brief Class for TCP connections * * This class provides the functions sendMessage() and recvMessage() * for easy communication */ class TCPConnection { public: /*! \brief Send a message over the given TCP socket * \param socket The TCP socket over which the data is to be sent * \param packet Pointer to the message to be sent * \return 0 on success, -1 on failure */ static int sendMessage( TCPsocket socket, Message* message, bool compressData = false, TCPProgressListener* listener = 0 ); /*! \brief Receive a message over the given TCP socket * \param socket The TCP socket for the reception of the packet * \param message Message pointer which points to the received * message or is 0 if no message could be received * \return -1 on errors, 0 else * \note The received message is allocated dynamically so it has to be * deleted when it is no longer used. */ static int recvMessage( TCPsocket socket, Message*& message, TCPProgressListener* listener = 0 ); }; #endif // _TCPCONNECTION_HPP_