Ethernet interface for W5500 with bug fixed in socket::close()
Fork of EthernetInterfaceW5500 by
Diff: Socket/TCPSocketConnection.cpp
- Revision:
- 10:cadac6bcd169
- Parent:
- 5:fb15c35d1e28
- Child:
- 14:a089e591e530
--- a/Socket/TCPSocketConnection.cpp Mon Dec 23 13:51:35 2013 +0000 +++ b/Socket/TCPSocketConnection.cpp Thu Jul 17 07:10:36 2014 +0000 @@ -15,15 +15,21 @@ * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ +#include "TCPSocketConnection.h" +#include <cstring> -#include "TCPSocketConnection.h" +using std::memset; +using std::memcpy; -TCPSocketConnection::TCPSocketConnection() +// not a big code. +// refer from EthernetInterface by mbed official driver +TCPSocketConnection::TCPSocketConnection() : + _is_connected(false) { } int TCPSocketConnection::connect(const char* host, const int port) -{ +{ if (_sock_fd < 0) { _sock_fd = eth->new_socket(); if (_sock_fd < 0) { @@ -32,20 +38,29 @@ } if (set_address(host, port) != 0) { return -1; - } + } if (!eth->connect(_sock_fd, get_address(), port)) { return -1; } + // add code refer from EthernetInterface. + _is_connected = true; + return 0; } bool TCPSocketConnection::is_connected(void) { - return eth->is_connected(_sock_fd); + // force update recent state. + _is_connected = eth->is_connected(_sock_fd); + return _is_connected; } int TCPSocketConnection::send(char* data, int length) { + // add to cover exception. + if ((_sock_fd < 0) || !_is_connected) + return -1; + int size = eth->wait_writeable(_sock_fd, _blocking ? -1 : _timeout); if (size < 0) { return -1; @@ -80,6 +95,10 @@ // -1 if unsuccessful, else number of bytes received int TCPSocketConnection::receive(char* data, int length) { + // add to cover exception. + if ((_sock_fd < 0) || !_is_connected) + return -1; + int size = eth->wait_readable(_sock_fd, _blocking ? -1 : _timeout); if (size < 0) { return -1; @@ -109,4 +128,4 @@ readLen += ret; } return readLen; -} +} \ No newline at end of file