resolved some issue and added method to read readystate of the websocket

This commit is contained in:
Pasquale Boemio
2013-05-11 09:55:41 +02:00
parent 7ac70dbdcf
commit bc1368b849
3 changed files with 20 additions and 16 deletions
+16 -15
View File
@@ -57,7 +57,10 @@ struct _DummyWebSocket : public WebSocket
{ {
void poll() { } void poll() { }
void send(std::string message) { } void send(std::string message) { }
void close() { } void _dispatch(Callback & callable) { } void close() { }
void _dispatch(Callback & callable) { }
const readyStateValues getReadyState() {}
}; };
@@ -107,17 +110,17 @@ struct _RealWebSocket : public WebSocket
std::vector<char> txbuf; std::vector<char> txbuf;
int sockfd; int sockfd;
/* I suppose we should add the others websocket status. readyStateValues readyState;
* Right now only this two are used, so, to avoid problems,
* I've added a dummy status that includes all the non "closing" ones */
enum clientStatusValues { CLOSING, CLOSE, DUMMYSTATUS } clientStatus;
_RealWebSocket(int sockfd) : sockfd(sockfd), readyState(OPEN) {
}
_RealWebSocket(int sockfd) : sockfd(sockfd), clientStatus(DUMMYSTATUS) { const readyStateValues getReadyState() {
return readyState;
} }
void poll() { void poll() {
if(clientStatus==CLOSE) { 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();
@@ -132,7 +135,8 @@ struct _RealWebSocket : public WebSocket
else if (ret == 0) { else if (ret == 0) {
rxbuf.resize(N); rxbuf.resize(N);
::close(sockfd); ::close(sockfd);
clientStatus = CLOSE; readyState = CLOSED;
fprintf(stderr, "Connection closed!\n");
break; break;
} }
else { else {
@@ -211,10 +215,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) {
if(clientStatus != CLOSING) { close(); } if(readyState != CLOSING) { close(); }
::close(sockfd);
clientStatus = CLOSE;
fprintf(stderr, "Connection closed!\n");
} }
else { fprintf(stderr, "ERROR: Got unexpected WebSocket message.\n"); close(); } else { fprintf(stderr, "ERROR: Got unexpected WebSocket message.\n"); close(); }
@@ -224,7 +225,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(clientStatus == CLOSING || clientStatus == CLOSE) { 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;
@@ -253,8 +254,8 @@ struct _RealWebSocket : public WebSocket
} }
void close() { void close() {
if(clientStatus == CLOSING || clientStatus == CLOSE) { return; } if(readyState == CLOSING || readyState == CLOSED) { return; }
clientStatus = CLOSING; readyState = CLOSING;
char closeFrame[4] = {0x88, 0x00, 0x00, 0x00}; char closeFrame[4] = {0x88, 0x00, 0x00, 0x00};
std::vector<char> header(closeFrame, closeFrame+4); std::vector<char> header(closeFrame, closeFrame+4);
txbuf.insert(txbuf.end(), header.begin(), header.end()); txbuf.insert(txbuf.end(), header.begin(), header.end());
+2
View File
@@ -14,6 +14,7 @@ namespace easywsclient {
struct WebSocket { struct WebSocket {
typedef WebSocket * pointer; typedef WebSocket * pointer;
typedef enum readyStateValues { CLOSING, CLOSED, CONNECTING, OPEN } readyStateValues;
// Factories: // Factories:
static pointer create_dummy(); static pointer create_dummy();
@@ -24,6 +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;
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 {
+2 -1
View File
@@ -17,7 +17,8 @@ int main()
ws->send("goodbye"); ws->send("goodbye");
ws->send("hello"); ws->send("hello");
ws->close(); ws->close();
while(true) { while(ws->getReadyState() != WebSocket::CLOSED)
{
ws->poll(); ws->poll();
ws->dispatch(handle_message); ws->dispatch(handle_message);
} }