#ifndef __JNET__JSOCKETBLOCKING__ #define __JNET__JSOCKETBLOCKING__ #include "JNet/JTCPSocket.hh" /** * \author mdejong */ namespace JNET {} namespace JPP { using namespace JNET; } namespace JNET { /** * Blocking socket I/O. * This class implements blocking I/O regardless of the configuration * of the socket (see method JSocket::setNonBlocking()). */ class JSocketBlocking : public JTCPSocket { public: /** * Default constructor. */ JSocketBlocking() : JTCPSocket() {} /** * Constructor. * * \param socket socket */ JSocketBlocking(const JTCPSocket& socket) : JTCPSocket(socket) {} /** * Read data from socket. * * \param buffer buffer * \param length number of bytes to read * \return number of bytes read */ int read(char* buffer, const int length) override { char* data = buffer; int size = length; while (size != 0) { const int pos = JSocket::read(data, size); if (pos != 0) { data += pos; size -= pos; } else { in_avail(1); } } return length - size; } /** * Write data to socket. * * \param buffer buffer * \param length number of bytes to write * \return number of bytes written */ int write(const char* buffer, const int length) override { const char* data = buffer; int size = length; while (size != 0) { const int pos = JSocket::write(data, size); if (pos != 0) { data += pos; size -= pos; } else { out_avail(1); } } return length - size; } }; } #endif