From 86ec5a65b139af45fb911bd7c3529f223aab5c11 Mon Sep 17 00:00:00 2001 From: David Baird Date: Tue, 14 May 2013 10:51:37 -0600 Subject: [PATCH] * Calling ::close() when txbuf empties and readyState is CLOSING * Removing unnecessary const. * Fixing up spacing to be consistent with rest of code. --- easywsclient.cpp | 14 ++++++-------- easywsclient.hpp | 2 +- 2 files changed, 7 insertions(+), 9 deletions(-) diff --git a/easywsclient.cpp b/easywsclient.cpp index 0e47a69..5f60642 100644 --- a/easywsclient.cpp +++ b/easywsclient.cpp @@ -59,8 +59,7 @@ struct _DummyWebSocket : public WebSocket void send(std::string message) { } void close() { } void _dispatch(Callback & callable) { } - const readyStateValues getReadyState() {} - + readyStateValues getReadyState() { return CLOSED; } }; @@ -115,12 +114,12 @@ struct _RealWebSocket : public WebSocket _RealWebSocket(int sockfd) : sockfd(sockfd), readyState(OPEN) { } - const readyStateValues getReadyState() { + readyStateValues getReadyState() { return readyState; } void poll() { - if(readyState==CLOSED) { return; } + if (readyState == CLOSED) { return; } while (true) { // FD_ISSET(0, &rfds) will be true int N = rxbuf.size(); @@ -149,6 +148,7 @@ struct _RealWebSocket : public WebSocket if (ret > 0) { txbuf.erase(txbuf.begin(), txbuf.begin() + ret); } else { break; } } + if (!txbuf.size() && readyState == CLOSING) { ::close(); } } // Callable must have signature: void(const std::string & message). @@ -214,9 +214,7 @@ struct _RealWebSocket : public WebSocket } else if (ws.opcode == wsheader_type::PING) { } else if (ws.opcode == wsheader_type::PONG) { } - else if (ws.opcode == wsheader_type::CLOSE) { - if(readyState != CLOSING) { close(); } - } + else if (ws.opcode == wsheader_type::CLOSE) { close(); } else { fprintf(stderr, "ERROR: Got unexpected WebSocket message.\n"); close(); } rxbuf.erase(rxbuf.begin(), rxbuf.begin() + ws.header_size+ws.N); @@ -225,7 +223,7 @@ struct _RealWebSocket : public WebSocket void send(std::string message) { // TODO: consider acquiring a lock on txbuf... - if(readyState == CLOSING || readyState == CLOSED) { return; } + if (readyState == CLOSING || readyState == CLOSED) { return; } std::vector header; header.assign(2 + (message.size() >= 126 ? 2 : 0) + (message.size() >= 65536 ? 6 : 0), 0); header[0] = 0x80 | wsheader_type::TEXT_FRAME; diff --git a/easywsclient.hpp b/easywsclient.hpp index 799c648..f1d2f09 100644 --- a/easywsclient.hpp +++ b/easywsclient.hpp @@ -25,7 +25,7 @@ struct WebSocket { virtual void poll() = 0; virtual void send(std::string message) = 0; virtual void close() = 0; - virtual const readyStateValues getReadyState() = 0; + virtual readyStateValues getReadyState() = 0; template void dispatch(Callable callable) { // N.B. this is compatible with both C++11 lambdas, functors and C function pointers struct _Callback : public Callback {