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: GSwifiInterface_HelloWorld GSwifiInterface_HelloServo GSwifiInterface_UDPEchoServer GSwifiInterface_UDPEchoClient ... more
Fork of WiflyInterface by
Diff: Socket/TCPSocketConnection.cpp
- Revision:
- 8:64184a968e3b
- Parent:
- 5:78943b3945b5
- Child:
- 11:71d67fea5ace
--- a/Socket/TCPSocketConnection.cpp Wed Jan 30 05:52:14 2013 +0000
+++ b/Socket/TCPSocketConnection.cpp Thu Oct 31 06:41:45 2013 +0000
@@ -26,66 +26,45 @@
int TCPSocketConnection::connect(const char* host, const int port)
{
- char cmd[CFG_CMD_SIZE];
- char ip[16];
+ if (set_address(host, port) != 0) return -1;
- if (_cid < 0 || !_wifi->is_connected(_cid)) {
- // Socket open
- if (_wifi->gethostbyname(host, ip) == false) return -1;
- _server = false;
- sprintf(cmd, "AT+NCTCP=%s,%d", ip, port);
- if (_wifi->sendCommand(cmd, GSwifi::RES_CONNECT) == false) return -1;
- _cid = _wifi->readCID();
- return 0;
- }
+ _server = false;
+ _cid = _wifi->open(GSwifi::PROTO_TCP, get_address(), get_port());
+ if (_cid < 0) return -1;
- return -1;
+ return 0;
}
bool TCPSocketConnection::is_connected(void)
{
- return _wifi->is_connected(_cid);
+ bool _is_connected = _wifi->isConnected(_cid);
+ if (!_is_connected) _cid = -1;
+ return _is_connected;
}
int TCPSocketConnection::send(char* data, int length)
{
- char cmd[CFG_CMD_SIZE];
- Timer tmr;
-
- if (!_blocking) {
- tmr.start();
- while (tmr.read_ms() < _timeout) {
- if (_wifi->writeable())
- break;
- }
- if (tmr.read_ms() >= _timeout) {
- return -1;
- }
- }
+ if (_cid < 0 || !is_connected()) return -1;
// TCP Client/Server
- sprintf(cmd, "\x1bZ%X%04d", _cid, length);
- _wifi->send(cmd, strlen(cmd));
- return _wifi->send(data, length, GSwifi::RES_NORMAL);
+ return _wifi->send(_cid, data, length);
}
// -1 if unsuccessful, else number of bytes written
int TCPSocketConnection::send_all(char* data, int length)
{
- char cmd[CFG_CMD_SIZE];
Timer tmr;
int idx = 0;
+
+ if (_cid < 0 || !is_connected()) return -1;
+
tmr.start();
-
// TCP Client/Server
- sprintf(cmd, "\x1bZ%X%04d", _cid, length);
- _wifi->send(cmd, strlen(cmd));
-
while ((tmr.read_ms() < _timeout) || _blocking) {
- idx += _wifi->send(data, length, GSwifi::RES_NORMAL);
+ idx += _wifi->send(_cid, &data[idx], length - idx);
+ if (idx < 0) return -1;
- if (idx == -1) return -1;
if (idx == length)
return idx;
}
@@ -97,9 +76,10 @@
{
Timer tmr;
int time = -1;
-
-
- if (!_blocking) {
+
+ if (_cid < 0 || !is_connected()) return -1;
+
+ if (_blocking) {
tmr.start();
while (time < _timeout + 20) {
if (_wifi->readable(_cid)) {
@@ -112,14 +92,9 @@
}
}
+ int nb_available = _wifi->recv(_cid, data, length);
- while(!_wifi->readable(_cid));
- int nb_available = _wifi->readable(_cid);
- for (int i = 0; i < min(nb_available, length); i++) {
- data[i] = _wifi->getc(_cid);
- }
-
- return min(nb_available, length);
+ return nb_available;
}
@@ -130,14 +105,14 @@
int idx = 0;
int time = -1;
+ if (_cid < 0 || !is_connected()) return -1;
+
tmr.start();
while (time < _timeout || _blocking) {
- int nb_available = _wifi->readable(_cid);
- for (int i = 0; i < min(nb_available, length); i++) {
- data[idx++] = _wifi->getc(_cid);
- }
+ idx += _wifi->recv(_cid, &data[idx], length - idx);
+ if (idx < 0) return -1;
if (idx == length)
break;
@@ -148,12 +123,12 @@
return (idx == 0) ? -1 : idx;
}
-void TCPSocketConnection::confCID (int cid) {
+void TCPSocketConnection::acceptCID (int cid) {
char *ip;
int port;
_server = true;
_cid = cid;
- if (_wifi->readRemote(_cid, &ip, &port)) {
+ if (!_wifi->getRemote(_cid, &ip, &port)) {
set_address(ip, port);
}
}

GainSpan Wi-Fi GS1011