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.
Diff: Websocket.cpp
- Revision:
- 9:9fa055ed54b4
- Parent:
- 8:ecd914cffdf6
- Child:
- 10:4f02275c34ee
diff -r ecd914cffdf6 -r 9fa055ed54b4 Websocket.cpp
--- a/Websocket.cpp Wed Aug 24 11:38:02 2011 +0000
+++ b/Websocket.cpp Wed Aug 24 15:22:52 2011 +0000
@@ -2,10 +2,11 @@
#include <string>
Websocket::Websocket(char * url, Wifly * wifi) {
- this->wifi = wifi;
- wifi_use = true;
- eth_use = false;
- fillFields(wifi_use, url);
+ this->wifi = wifi;
+ wifi_use = true;
+ eth_use = false;
+ response_server_eth = false;
+ fillFields(wifi_use, url);
}
void Websocket::fillFields(bool wifi, char * url) {
@@ -16,14 +17,11 @@
strcpy(buf, url);
res = strtok(buf, ":");
- if (strcmp(res, "ws"))
- {
+ if (strcmp(res, "ws")) {
this->ip_domain = NULL;
this->path = NULL;
printf("\r\nFormat error: please use: \"ws://ip-or-domain[:port]/path\"\r\n\r\n");
- }
- else
- {
+ } else {
//ip_domain and port
res = strtok(NULL, "/");
@@ -36,7 +34,7 @@
//ip_domain
res = strtok(res, ":");
-
+
//port
res1 = strtok(NULL, " ");
//port
@@ -47,7 +45,7 @@
port = (char *) malloc (sizeof(char) * 3);
strcpy(this->port, "80");
}
-
+
if (res != NULL) {
ip_domain = (char *) malloc (sizeof(char) * strlen(res));
strcpy(this->ip_domain, res);
@@ -80,12 +78,13 @@
Websocket::Websocket(char * url) {
- wifi_use = false;
- eth_use = true;
- eth_writeable = false;
- eth_readable = false;
- eth_connected = false;
- fillFields(wifi_use, url);
+ wifi_use = false;
+ eth_use = true;
+ eth_writeable = false;
+ eth_readable = false;
+ eth_connected = false;
+ response_server_eth = false;
+ fillFields(wifi_use, url);
}
@@ -156,7 +155,6 @@
Timer tmr;
tmr.start();
- char msg[10];
while (true) {
Net::poll();
@@ -204,31 +202,16 @@
i++;
break;
case 8:
- if(eth_readable)
- {
- string checking;
- size_t found = string::npos;
- sock.recv(msg, 10);
- Net::poll();
- checking = msg;
- found = checking.find("HTTP");
- if(found != string::npos)
- {
- //printf("checking: %s\r\n", checking);
- i++;
- break;
- }
- else
- return false;
- }
- break;
-
+ if (response_server_eth)
+ i++;
+ else
+ break;
+
default:
break;
}
}
- if (i==9)
- {
+ if (i==9) {
printf("\r\nip_domain: %s\r\npath: /%s\r\nport: %s\r\n\r\n",this->ip_domain, this->path, this->port);
return true;
}
@@ -254,35 +237,52 @@
bool Websocket::read(char * message) {
int i = 0;
- char char_read;
- if (!wifi->readable()) {
- message = NULL;
- return false;
- }
+ if (wifi_use) {
+ char char_read;
+ if (!wifi->readable()) {
+ message = NULL;
+ return false;
+ }
+
+ if (wifi->getc() != 0x00) {
+ message = NULL;
+ return false;
+ }
- if (wifi->getc() != 0x00) {
- message = NULL;
- return false;
+ while ( (char_read = wifi->getc()) != 0xff)
+ message[i++] = char_read;
+ message[i] = 0;
+ } else if (eth_use) {
+
+ if (eth_rx[0] != 0x00) {
+ message = NULL;
+ return false;
+ }
+ while (eth_rx[i + 1] != 0xff) {
+ message[i] = eth_rx[i + 1];
+ i++;
+ }
+ message[i] = 0;
}
-
- while ( (char_read = wifi->getc()) != 0xff)
- message[i++] = char_read;
-
return true;
}
bool Websocket::close() {
- if (!wifi->CmdMode()) {
- printf("Websocket::close: cannot enter in cmd mode\r\n");
- return false;
- }
+ if (wifi_use) {
+ if (!wifi->CmdMode()) {
+ printf("Websocket::close: cannot enter in cmd mode\r\n");
+ return false;
+ }
- wifi->Send("close\r", "NO");
+ wifi->Send("close\r", "NO");
- if (!wifi->exit())
- return false;
-
+ if (!wifi->exit())
+ return false;
+ } else if (eth_use) {
+ if (sock.close())
+ return false;
+ }
return true;
}
@@ -312,8 +312,7 @@
if (!wifi->exit())
printf("Websocket::connected: cannot exit\r\n");
return false;
- }
- else if (eth_use)
+ } else if (eth_use)
return eth_connected;
return true;
@@ -324,39 +323,30 @@
void Websocket::onTCPSocketEvent(TCPSocketEvent e) {
- switch (e) {
- case TCPSOCKET_CONNECTED:
- eth_connected = true;
- printf("TCP Socket Connected\r\n");
- break;
- case TCPSOCKET_WRITEABLE:
- eth_writeable = true;
- break;
- case TCPSOCKET_READABLE:
- printf("TCP Socket Readable\r\n");
- eth_readable = true;
- break;
- case TCPSOCKET_CONTIMEOUT:
- printf("TCP Socket Timeout\r\n");
- eth_connected = false;
- break;
- case TCPSOCKET_CONRST:
- printf("TCP Socket CONRST\r\n");
- eth_connected = false;
- break;
- case TCPSOCKET_CONABRT:
- printf("TCP Socket CONABRT\r\n");
- eth_connected = false;
- break;
- case TCPSOCKET_ERROR:
- printf("TCP Socket Error\r\n");
- eth_connected = false;
- break;
- case TCPSOCKET_DISCONNECTED:
- eth_connected = false;
- break;
- default:
- printf("DEFAULT\r\n");
+ if (e == TCPSOCKET_CONNECTED) {
+ eth_connected = true;
+ printf("TCP Socket Connected\r\n");
+ }
+ else if (e == TCPSOCKET_WRITEABLE)
+ {
+ }
+ else if (e == TCPSOCKET_READABLE)
+ {
+ int len = sock.recv(eth_rx, 512);
+ eth_rx[len] = 0;
+ if (!response_server_eth) {
+ string checking;
+ size_t found = string::npos;
+ checking = eth_rx;
+ found = checking.find("HTTP");
+ if (found != string::npos)
+ response_server_eth = true;
+ }
+ }
+ else
+ {
+ printf("TCP Socket Fail\r\n");
+ eth_connected = false;
}
}