Azure IoT common library
Fork of azure_c_shared_utility by
Diff: tcpsocketconnection_c.cpp
- Revision:
- 33:5f2a48fc7e5b
- Parent:
- 0:fa2de1b79154
- Child:
- 34:651c23af382c
--- a/tcpsocketconnection_c.cpp Tue Aug 22 11:24:58 2017 +0000 +++ b/tcpsocketconnection_c.cpp Tue Aug 22 13:22:47 2017 +0100 @@ -4,64 +4,107 @@ #include "mbed.h" #include <stddef.h> -#include "TCPSocketConnection.h" + #include "azure_c_shared_utility/tcpsocketconnection_c.h" +typedef struct { + TCPSocket* socket; + bool connected; +} TCPSOCKETCONNECTION_HANDLE_T; TCPSOCKETCONNECTION_HANDLE tcpsocketconnection_create(void) { - return new TCPSocketConnection(); + TCPSOCKETCONNECTION_HANDLE_T* conn = new TCPSOCKETCONNECTION_HANDLE_T; + conn->socket = new TCPSocket(); + conn->connected = false; + return conn; } void tcpsocketconnection_set_blocking(TCPSOCKETCONNECTION_HANDLE tcpSocketConnectionHandle, bool blocking, unsigned int timeout) { - TCPSocketConnection* tsc = (TCPSocketConnection*)tcpSocketConnectionHandle; - tsc->set_blocking(blocking, timeout); + TCPSOCKETCONNECTION_HANDLE_T* tsc = (TCPSOCKETCONNECTION_HANDLE_T*)tcpSocketConnectionHandle; + tsc->socket->set_blocking(blocking); + tsc->socket->set_timeout(timeout); } void tcpsocketconnection_destroy(TCPSOCKETCONNECTION_HANDLE tcpSocketConnectionHandle) { - delete (TCPSocketConnection*)tcpSocketConnectionHandle; + delete (TCPSOCKETCONNECTION_HANDLE_T*)tcpSocketConnectionHandle; } int tcpsocketconnection_connect(TCPSOCKETCONNECTION_HANDLE tcpSocketConnectionHandle, const char* host, const int port) { - TCPSocketConnection* tsc = (TCPSocketConnection*)tcpSocketConnectionHandle; - return tsc->connect(host, port); + TCPSOCKETCONNECTION_HANDLE_T* tsc = (TCPSOCKETCONNECTION_HANDLE_T*)tcpSocketConnectionHandle; + // TODO: so, we set this but we really need to kill it if we get an error later too..... + auto rv = tsc->socket->connect(host, port); + if(rv == 0) { tsc->connected = true; } + return rv; } bool tcpsocketconnection_is_connected(TCPSOCKETCONNECTION_HANDLE tcpSocketConnectionHandle) { - TCPSocketConnection* tsc = (TCPSocketConnection*)tcpSocketConnectionHandle; - return tsc->is_connected(); + TCPSOCKETCONNECTION_HANDLE_T* tsc = (TCPSOCKETCONNECTION_HANDLE_T*)tcpSocketConnectionHandle; + return tsc->connected; } void tcpsocketconnection_close(TCPSOCKETCONNECTION_HANDLE tcpSocketConnectionHandle) { - TCPSocketConnection* tsc = (TCPSocketConnection*)tcpSocketConnectionHandle; - tsc->close(); + TCPSOCKETCONNECTION_HANDLE_T* tsc = (TCPSOCKETCONNECTION_HANDLE_T*)tcpSocketConnectionHandle; + tsc->socket->close(); } int tcpsocketconnection_send(TCPSOCKETCONNECTION_HANDLE tcpSocketConnectionHandle, const char* data, int length) { - TCPSocketConnection* tsc = (TCPSocketConnection*)tcpSocketConnectionHandle; - return tsc->send((char*)data, length); + TCPSOCKETCONNECTION_HANDLE_T* tsc = (TCPSOCKETCONNECTION_HANDLE_T*)tcpSocketConnectionHandle; + return tsc->socket->send((char*)data, length); } int tcpsocketconnection_send_all(TCPSOCKETCONNECTION_HANDLE tcpSocketConnectionHandle, const char* data, int length) { - TCPSocketConnection* tsc = (TCPSocketConnection*)tcpSocketConnectionHandle; - return tsc->send_all((char*)data, length); + TCPSOCKETCONNECTION_HANDLE_T* tsc = (TCPSOCKETCONNECTION_HANDLE_T*)tcpSocketConnectionHandle; + // TODO: this is shonky and quickly done.... + + auto rv = tsc->socket->send((char*)data, length); + + if(rv < 0 || rv == length) { + return rv; + } + + auto remain = length - rv; + + while(rv > 0 && remain > 0) { + data = data + rv; + rv = tsc->socket->send((char*)data, length); + remain = length - rv; + } + + return rv; } int tcpsocketconnection_receive(TCPSOCKETCONNECTION_HANDLE tcpSocketConnectionHandle, char* data, int length) { - TCPSocketConnection* tsc = (TCPSocketConnection*)tcpSocketConnectionHandle; - return tsc->receive(data, length); + TCPSOCKETCONNECTION_HANDLE_T* tsc = (TCPSOCKETCONNECTION_HANDLE_T*)tcpSocketConnectionHandle; + return tsc->socket->recv(data, length); } int tcpsocketconnection_receive_all(TCPSOCKETCONNECTION_HANDLE tcpSocketConnectionHandle, char* data, int length) { - TCPSocketConnection* tsc = (TCPSocketConnection*)tcpSocketConnectionHandle; - return tsc->receive_all(data, length); + TCPSOCKETCONNECTION_HANDLE_T* tsc = (TCPSOCKETCONNECTION_HANDLE_T*)tcpSocketConnectionHandle; + // TODO: this is shonky and quickly done.... + + auto rv = tsc->socket->recv((char*)data, length); + + if(rv < 0 || rv == length) { + return rv; + } + + auto remain = length - rv; + + while(rv > 0 && remain > 0) { + data = data + rv; + rv = tsc->socket->recv((char*)data, length); + remain = length - rv; + } + + return rv; }