* Calling ::close() when txbuf empties and readyState is CLOSING

* Removing unnecessary const.
* Fixing up spacing to be consistent with rest of code.
This commit is contained in:
David Baird
2013-05-14 10:51:37 -06:00
parent bc1368b849
commit 86ec5a65b1
2 changed files with 7 additions and 9 deletions
+6 -8
View File
@@ -59,8 +59,7 @@ struct _DummyWebSocket : public WebSocket
void send(std::string message) { } void send(std::string message) { }
void close() { } void close() { }
void _dispatch(Callback & callable) { } 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) { _RealWebSocket(int sockfd) : sockfd(sockfd), readyState(OPEN) {
} }
const readyStateValues getReadyState() { readyStateValues getReadyState() {
return readyState; return readyState;
} }
void poll() { void poll() {
if(readyState==CLOSED) { return; } if (readyState == CLOSED) { return; }
while (true) { while (true) {
// FD_ISSET(0, &rfds) will be true // FD_ISSET(0, &rfds) will be true
int N = rxbuf.size(); int N = rxbuf.size();
@@ -149,6 +148,7 @@ struct _RealWebSocket : public WebSocket
if (ret > 0) { txbuf.erase(txbuf.begin(), txbuf.begin() + ret); } if (ret > 0) { txbuf.erase(txbuf.begin(), txbuf.begin() + ret); }
else { break; } else { break; }
} }
if (!txbuf.size() && readyState == CLOSING) { ::close(); }
} }
// Callable must have signature: void(const std::string & message). // 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::PING) { }
else if (ws.opcode == wsheader_type::PONG) { } else if (ws.opcode == wsheader_type::PONG) { }
else if (ws.opcode == wsheader_type::CLOSE) { else if (ws.opcode == wsheader_type::CLOSE) { close(); }
if(readyState != CLOSING) { close(); }
}
else { fprintf(stderr, "ERROR: Got unexpected WebSocket message.\n"); close(); } else { fprintf(stderr, "ERROR: Got unexpected WebSocket message.\n"); close(); }
rxbuf.erase(rxbuf.begin(), rxbuf.begin() + ws.header_size+ws.N); rxbuf.erase(rxbuf.begin(), rxbuf.begin() + ws.header_size+ws.N);
@@ -225,7 +223,7 @@ struct _RealWebSocket : public WebSocket
void send(std::string message) { void send(std::string message) {
// TODO: consider acquiring a lock on txbuf... // TODO: consider acquiring a lock on txbuf...
if(readyState == CLOSING || readyState == CLOSED) { return; } if (readyState == CLOSING || readyState == CLOSED) { return; }
std::vector<uint8_t> header; std::vector<uint8_t> header;
header.assign(2 + (message.size() >= 126 ? 2 : 0) + (message.size() >= 65536 ? 6 : 0), 0); header.assign(2 + (message.size() >= 126 ? 2 : 0) + (message.size() >= 65536 ? 6 : 0), 0);
header[0] = 0x80 | wsheader_type::TEXT_FRAME; header[0] = 0x80 | wsheader_type::TEXT_FRAME;
+1 -1
View File
@@ -25,7 +25,7 @@ struct WebSocket {
virtual void poll() = 0; virtual void poll() = 0;
virtual void send(std::string message) = 0; virtual void send(std::string message) = 0;
virtual void close() = 0; virtual void close() = 0;
virtual const readyStateValues getReadyState() = 0; virtual readyStateValues getReadyState() = 0;
template<class Callable> template<class Callable>
void dispatch(Callable callable) { // N.B. this is compatible with both C++11 lambdas, functors and C function pointers void dispatch(Callable callable) { // N.B. this is compatible with both C++11 lambdas, functors and C function pointers
struct _Callback : public Callback { struct _Callback : public Callback {