WebSocket client library
Diff: Websocket.cpp
- Revision:
- 23:80bffaf28bef
- Parent:
- 22:f4aac491ea26
- Child:
- 24:15112fd8ad45
--- a/Websocket.cpp Wed Feb 01 17:23:40 2012 +0000 +++ b/Websocket.cpp Mon Mar 19 13:53:13 2012 +0000 @@ -1,7 +1,7 @@ #include "Websocket.h" #include <string> -//#define DEBUG +#define DEBUG Websocket::Websocket(char * url, Wifly * wifi) { this->wifi = wifi; @@ -120,7 +120,7 @@ char cmd[50]; if (netif == WIF) { //enter in cmd mode - wifi->exit(); + wifi->exit(); while (!wifi->cmdMode()) { #ifdef DEBUG printf("cannot enter in CMD mode\r\n"); @@ -277,8 +277,8 @@ sendChar(len | (1<<7)); } else if (len < 65535) { sendChar(126 | (1<<7)); + sendChar((len >> 8) & 0xff); sendChar(len & 0xff); - sendChar((len >> 8) & 0xff); } else { sendChar(127 | (1<<7)); for (int i = 0; i < 8; i++) { @@ -329,7 +329,7 @@ bool Websocket::read(char * message) { int i = 0; int length_buffer = 0; - uint32_t len_msg; + uint64_t len_msg; char opcode = 0; char mask[4] = {0, 0, 0, 0}; Timer tmr; @@ -356,26 +356,38 @@ #endif len_msg = wifi->getc() & 0x7f; if (len_msg == 126) { - len_msg = wifi->getc(); - len_msg += wifi->getc() << 8; + len_msg = (wifi->getc() << 8); + len_msg += wifi->getc(); } else if (len_msg == 127) { len_msg = 0; for (int i = 0; i < 8; i++) { - len_msg += wifi->getc() << i*8; + len_msg += (wifi->getc() << (7-i)*8); } } - if(len_msg == 0) { + if (len_msg == 0) { return false; } #ifdef DEBUG - printf("length: %d\r\n", len_msg); + printf("length: %lld\r\n", len_msg); #endif if ((len_msg & 0x80)) { - for (int i = 0; i < 4; i++) +#ifdef DEBUG + printf("will read mask\r\n"); +#endif + for (int i = 0; i < 4; i++) { mask[i] = wifi->getc(); +#ifdef DEBUG + printf("mask[%d]: %d\r\n", i, mask[i]); +#endif + } + } else { +#ifdef DEBUG + printf("len not 0x80\r\n"); +#endif } - } else + } else { return false; + } for (i = 0; i < len_msg; i++) { @@ -409,19 +421,19 @@ len_msg = eth_rx[index++] & 0x7f; if (len_msg == 126) { - len_msg = eth_rx[index++]; - len_msg += eth_rx[index++] << 8; + len_msg = (eth_rx[index++] << 8); + len_msg += eth_rx[index++]; } else if (len_msg == 127) { len_msg = 0; for (int i = 0; i < 8; i++) { - len_msg += eth_rx[index++] << i*8; + len_msg += eth_rx[index++] << (7-i)*8; } } - if(len_msg == 0) { + if (len_msg == 0) { return false; } #ifdef DEBUG - printf("length: %d\r\n", len_msg); + printf("length: %lld\r\n", len_msg); #endif if ((len_msg & 0x80)) { for (int i = 0; i < 4; i++) @@ -444,13 +456,19 @@ } bool Websocket::close() { + sendOpcode(0x08); + sendLength(0); + sendMask(); if (netif == WIF) { + + wait(0.25); if (!wifi->cmdMode()) { #ifdef DEBUG printf("Websocket::close: cannot enter in cmd mode\r\n"); #endif return false; } + wait(0.25); wifi->send("close\r", "NO"); @@ -459,6 +477,9 @@ } #ifdef TARGET_LPC1768 else if (netif == ETH) { +#ifdef TARGET_LPC1768 + Net::poll(); +#endif //target if (sock->close()) return false; @@ -515,6 +536,7 @@ return false; } + std::string Websocket::getPath() { return path; }