Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependents: LPC11U68_NTPClient_HelloWorld_WIZ550io
Fork of WIZ550ioInterface by
Diff: Socket/TCPSocketConnection.cpp
- Revision:
- 5:fb15c35d1e28
- Parent:
- 1:fb4494783863
--- a/Socket/TCPSocketConnection.cpp Thu Dec 20 15:08:58 2012 +0000
+++ b/Socket/TCPSocketConnection.cpp Tue Aug 27 12:50:11 2013 +0000
@@ -17,109 +17,96 @@
*/
#include "TCPSocketConnection.h"
-#include <algorithm>
-TCPSocketConnection::TCPSocketConnection() {}
+TCPSocketConnection::TCPSocketConnection()
+{
+}
int TCPSocketConnection::connect(const char* host, const int port)
{
- if (!wifi->connect(host, port))
+ if (_sock_fd < 0) {
+ _sock_fd = eth->new_socket();
+ if (_sock_fd < 0) {
+ return -1;
+ }
+ }
+ if (set_address(host, port) != 0) {
return -1;
- wifi->flush();
+ }
+ if (!eth->connect(_sock_fd, get_address(), port)) {
+ return -1;
+ }
return 0;
}
bool TCPSocketConnection::is_connected(void)
{
- return wifi->is_connected();
+ return eth->is_connected(_sock_fd);
}
int TCPSocketConnection::send(char* data, int length)
{
- Timer tmr;
-
- if (!_blocking) {
- tmr.start();
- while (tmr.read_ms() < _timeout) {
- if (wifi->writeable())
- break;
- }
- if (tmr.read_ms() >= _timeout) {
- return -1;
- }
+ int size = eth->wait_writeable(_sock_fd, _blocking ? -1 : _timeout);
+ if (size < 0) {
+ return -1;
}
- return wifi->send(data, length);
+ if (size > length) {
+ size = length;
+ }
+ return eth->send(_sock_fd, data, size);
}
// -1 if unsuccessful, else number of bytes written
int TCPSocketConnection::send_all(char* data, int length)
{
- Timer tmr;
- int idx = 0;
- tmr.start();
-
- while ((tmr.read_ms() < _timeout) || _blocking) {
-
- idx += wifi->send(data, length);
-
- if (idx == length)
- return idx;
+ int writtenLen = 0;
+ while (writtenLen < length) {
+ int size = eth->wait_writeable(_sock_fd, _blocking ? -1 : _timeout);
+ if (size < 0) {
+ return -1;
+ }
+ if (size > (length-writtenLen)) {
+ size = (length-writtenLen);
+ }
+ int ret = eth->send(_sock_fd, data + writtenLen, size);
+ if (ret < 0) {
+ return -1;
+ }
+ writtenLen += ret;
}
- return (idx == 0) ? -1 : idx;
+ return writtenLen;
}
// -1 if unsuccessful, else number of bytes received
int TCPSocketConnection::receive(char* data, int length)
{
- Timer tmr;
- int time = -1;
-
-
- if (!_blocking) {
- tmr.start();
- while (time < _timeout + 20) {
- if (wifi->readable()) {
- break;
- }
- time = tmr.read_ms();
- }
- if (time >= _timeout + 20) {
- return -1;
- }
+ int size = eth->wait_readable(_sock_fd, _blocking ? -1 : _timeout);
+ if (size < 0) {
+ return -1;
}
-
-
- while(!wifi->readable());
- int nb_available = wifi->readable();
- for (int i = 0; i < min(nb_available, length); i++) {
- data[i] = wifi->getc();
+ if (size > length) {
+ size = length;
}
-
- return min(nb_available, length);
+ return eth->recv(_sock_fd, data, size);
}
-
// -1 if unsuccessful, else number of bytes received
int TCPSocketConnection::receive_all(char* data, int length)
{
- Timer tmr;
- int idx = 0;
- int time = -1;
-
- tmr.start();
-
- while (time < _timeout || _blocking) {
-
- int nb_available = wifi->readable();
- for (int i = 0; i < min(nb_available, length); i++) {
- data[idx++] = wifi->getc();
+ int readLen = 0;
+ while (readLen < length) {
+ int size = eth->wait_readable(_sock_fd, _blocking ? -1 :_timeout);
+ if (size <= 0) {
+ break;
+ }
+ if (size > (length - readLen)) {
+ size = length - readLen;
}
-
- if (idx == length)
- break;
-
- time = tmr.read_ms();
+ int ret = eth->recv(_sock_fd, data + readLen, size);
+ if (ret < 0) {
+ return -1;
+ }
+ readLen += ret;
}
-
- return (idx == 0) ? -1 : idx;
+ return readLen;
}
