test
Revision 10:d24738f4ef99, committed 2012-07-31
- Comitter:
- emilmont
- Date:
- Tue Jul 31 11:50:55 2012 +0000
- Parent:
- 9:f972715add36
- Child:
- 11:3d83c348fb8b
- Commit message:
- Add explicit setting of socket blocking/non-blocking mode and timeout
Changed in this revision
--- a/Socket.cpp Fri Jul 27 15:56:20 2012 +0000 +++ b/Socket.cpp Tue Jul 31 11:50:55 2012 +0000 @@ -20,10 +20,15 @@ using std::memset; -Socket::Socket() : _sock_fd(-1) { +Socket::Socket() : _sock_fd(-1), _blocking(true), _timeout(3000) { } +void Socket::set_blocking(bool blocking, unsigned int timeout) { + _blocking = blocking; + _timeout = timeout; +} + int Socket::init_socket(int type) { if (_sock_fd != -1) return -1; @@ -37,9 +42,6 @@ } int Socket::select(struct timeval *timeout, bool read, bool write) { - if ((timeout->tv_sec == 0) && (timeout->tv_usec == 0)) - return 0; - fd_set fdSet; FD_ZERO(&fdSet); FD_SET(_sock_fd, &fdSet); @@ -69,7 +71,7 @@ return 0; } -TimeInterval::TimeInterval(int ms) { +TimeInterval::TimeInterval(unsigned int ms) { _time.tv_sec = ms / 1000; _time.tv_usec = (ms - (_time.tv_sec * 1000)) * 1000; }
--- a/Socket.h Fri Jul 27 15:56:20 2012 +0000 +++ b/Socket.h Tue Jul 31 11:50:55 2012 +0000 @@ -40,6 +40,13 @@ */ Socket(); + /** Set blocking or non-blocking mode of the socket and a timeout on + blocking socket operations + \param blocking true for blocking mode, false for non-blocking mode. + \param timeout timeout in ms. + */ + void set_blocking(bool blocking, unsigned int timeout=1500); + /** Close the socket file descriptor */ int close(); @@ -50,7 +57,10 @@ int wait_readable(TimeInterval& timeout); int wait_writable(TimeInterval& timeout); - + + bool _blocking; + unsigned int _timeout; + private: int select(struct timeval *timeout, bool read, bool write); }; @@ -64,7 +74,7 @@ /** Time Interval \param ms time interval expressed in milliseconds */ - TimeInterval(int ms); + TimeInterval(unsigned int ms); private: struct timeval _time;
--- a/TCPSocketConnection.cpp Fri Jul 27 15:56:20 2012 +0000 +++ b/TCPSocketConnection.cpp Tue Jul 31 11:50:55 2012 +0000 @@ -40,13 +40,15 @@ return 0; } -int TCPSocketConnection::send(char* data, int length, int timeout_ms) { +int TCPSocketConnection::send(char* data, int length) { if ((_sock_fd < 0) || _closedByRemoteHost) return -1; - TimeInterval timeout(timeout_ms); - if (wait_writable(timeout) != 0) - return -1; + if (!_blocking) { + TimeInterval timeout(_timeout); + if (wait_writable(timeout) != 0) + return -1; + } int n = lwip_send(_sock_fd, data, length, 0); _closedByRemoteHost = (n == 0); @@ -55,16 +57,19 @@ } // -1 if unsuccessful, else number of bytes written -int TCPSocketConnection::send_all(char* data, int length, int timeout_ms) { +int TCPSocketConnection::send_all(char* data, int length) { if ((_sock_fd < 0) || _closedByRemoteHost) return -1; size_t writtenLen = 0; - TimeInterval timeout(timeout_ms); + + TimeInterval timeout(_timeout); while (writtenLen < length) { - // Wait for socket to be writeable - if (wait_writable(timeout) != 0) - return writtenLen; //Timeout -- FIXME should we return -1 or writtenLength ? + if (!_blocking) { + // Wait for socket to be writeable + if (wait_writable(timeout) != 0) + return writtenLen; //Timeout -- FIXME should we return -1 or writtenLength ? + } int ret = lwip_send(_sock_fd, data + writtenLen, length - writtenLen, 0); if (ret > 0) { @@ -81,13 +86,15 @@ return writtenLen; } -int TCPSocketConnection::receive(char* data, int length, int timeout_ms) { +int TCPSocketConnection::receive(char* data, int length) { if ((_sock_fd < 0) || _closedByRemoteHost) return -1; - TimeInterval timeout(timeout_ms); - if (wait_readable(timeout) != 0) - return -1; + if (!_blocking) { + TimeInterval timeout(_timeout); + if (wait_readable(timeout) != 0) + return -1; + } int n = lwip_recv(_sock_fd, data, length, 0); _closedByRemoteHost = (n == 0); @@ -96,16 +103,18 @@ } // -1 if unsuccessful, else number of bytes received -int TCPSocketConnection::receive_all(char* data, int length, int timeout_ms) { +int TCPSocketConnection::receive_all(char* data, int length) { if ((_sock_fd < 0) || _closedByRemoteHost) return -1; size_t readLen = 0; - TimeInterval timeout(timeout_ms); + TimeInterval timeout(_timeout); while (readLen < length) { - //Wait for socket to be readable - if (wait_readable(timeout) != 0) - return readLen; //Timeout -- FIXME should we return -1 or writtenLength ? + if (!_blocking) { + //Wait for socket to be readable + if (wait_readable(timeout) != 0) + return readLen; //Timeout -- FIXME should we return -1 or writtenLength ? + } int ret = lwip_recv(_sock_fd, data + readLen, length - readLen, 0); if (ret > 0) {
--- a/TCPSocketConnection.h Fri Jul 27 15:56:20 2012 +0000 +++ b/TCPSocketConnection.h Tue Jul 31 11:50:55 2012 +0000 @@ -45,34 +45,30 @@ /** Send data to the remote host. \param data The buffer to send to the host. \param length The length of the buffer to send. - \param timeout_ms The maximum amount of time in ms to wait while trying to send the buffer. \return the number of written bytes on success (>=0) or -1 on failure */ - int send(char* data, int length, int timeout_ms=0); + int send(char* data, int length); /** Send all the data to the remote host. \param data The buffer to send to the host. \param length The length of the buffer to send. - \param timeout The maximum amount of time in ms to wait while trying to send the buffer. \return the number of written bytes on success (>=0) or -1 on failure */ - int send_all(char* data, int length, int timeout_ms=0); + int send_all(char* data, int length); /** Receive data from the remote host. \param data The buffer in which to store the data received from the host. \param length The maximum length of the buffer. - \param timeout The maximum amount of time in ms to wait while trying to receive data. \return the number of received bytes on success (>=0) or -1 on failure */ - int receive(char* data, int length, int timeout_ms=0); + int receive(char* data, int length); /** Receive all the data from the remote host. \param data The buffer in which to store the data received from the host. \param length The maximum length of the buffer. - \param timeout_ms The maximum amount of time in ms to wait while trying to receive data. \return the number of received bytes on success (>=0) or -1 on failure */ - int receive_all(char* data, int length, int timeout_ms=0); + int receive_all(char* data, int length); private: bool _closedByRemoteHost;
--- a/TCPSocketServer.cpp Fri Jul 27 15:56:20 2012 +0000 +++ b/TCPSocketServer.cpp Tue Jul 31 11:50:55 2012 +0000 @@ -57,14 +57,15 @@ return 0; } -int TCPSocketServer::accept(TCPSocketConnection& connection, int timeout_ms) { +int TCPSocketServer::accept(TCPSocketConnection& connection) { if (_sock_fd < 0) return -1; - TimeInterval timeout(timeout_ms); - if (wait_readable(timeout) != 0) - return -1; - + if (!_blocking) { + TimeInterval timeout(_timeout); + if (wait_readable(timeout) != 0) + return -1; + } socklen_t newSockRemoteHostLen = sizeof(connection._remoteHost); int fd = lwip_accept(_sock_fd, (struct sockaddr*) &connection._remoteHost, &newSockRemoteHostLen); if (fd < 0)
--- a/TCPSocketServer.h Fri Jul 27 15:56:20 2012 +0000 +++ b/TCPSocketServer.h Tue Jul 31 11:50:55 2012 +0000 @@ -45,10 +45,9 @@ /** Accept a new connection. \param connection A TCPSocketConnection instance that will handle the incoming connection. - \param timeout_ms The maximum amount of time in ms to wait for a new connection. \return 0 on success, -1 on failure. */ - int accept(TCPSocketConnection& connection, int timeout_ms=0); + int accept(TCPSocketConnection& connection); }; #endif
--- a/UDPSocket.cpp Fri Jul 27 15:56:20 2012 +0000 +++ b/UDPSocket.cpp Tue Jul 31 11:50:55 2012 +0000 @@ -50,12 +50,12 @@ } // -1 if unsuccessful, else number of bytes written -int UDPSocket::sendTo(UDPPacket& packet, int timeout_ms) { +int UDPSocket::sendTo(UDPPacket& packet) { if (_sock_fd < 0) return -1; - if (timeout_ms != 0) { - TimeInterval timeout(timeout_ms); + if (!_blocking) { + TimeInterval timeout(_timeout); if (wait_writable(timeout) != 0) return -1; } @@ -64,12 +64,12 @@ } // -1 if unsuccessful, else number of bytes received -int UDPSocket::receiveFrom(UDPPacket& packet, int timeout_ms) { +int UDPSocket::receiveFrom(UDPPacket& packet) { if (_sock_fd < 0) return -1; - if (timeout_ms != 0) { - TimeInterval timeout(timeout_ms); + if (!_blocking) { + TimeInterval timeout(_timeout); if (wait_readable(timeout) != 0) return -1; }
--- a/UDPSocket.h Fri Jul 27 15:56:20 2012 +0000 +++ b/UDPSocket.h Tue Jul 31 11:50:55 2012 +0000 @@ -48,19 +48,17 @@ /** Send a packet to the remote host. \param packet UDP Packet to be sent to the remote host - \param timeout_ms The maximum amount of time in ms to wait while trying to send the packet. \return the number of written bytes on success (>=0) or -1 on failure */ - int sendTo(UDPPacket& packet, int timeout_ms=0); + int sendTo(UDPPacket& packet); /** Receive data from a remote host. If a message is too long to fit in the supplied packet, excess bytes are discarded. \param packet UDP Packet received from the remote host - \param timeout The maximum amount of time in ms to wait while trying to receive packet. \return the number of received bytes on success (>=0) or -1 on failure */ - int receiveFrom(UDPPacket& packet, int timeout_ms=0); + int receiveFrom(UDPPacket& packet); }; #endif