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 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;
int sockfd;
/* I suppose we should add the others websocket status.
* 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;
readyStateValues readyState;
_RealWebSocket(int sockfd) : sockfd(sockfd), readyState(OPEN) {
}
_RealWebSocket(int sockfd) : sockfd(sockfd), clientStatus(DUMMYSTATUS) {
const readyStateValues getReadyState() {
return readyState;
}
void poll() {
if(clientStatus==CLOSE) { return; }
if(readyState==CLOSED) { return; }
while (true) {
// FD_ISSET(0, &rfds) will be true
int N = rxbuf.size();
@@ -132,7 +135,8 @@ struct _RealWebSocket : public WebSocket
else if (ret == 0) {
rxbuf.resize(N);
::close(sockfd);
clientStatus = CLOSE;
readyState = CLOSED;
fprintf(stderr, "Connection closed!\n");
break;
}
else {
@@ -211,10 +215,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(clientStatus != CLOSING) { close(); }
::close(sockfd);
clientStatus = CLOSE;
fprintf(stderr, "Connection closed!\n");
if(readyState != CLOSING) { close(); }
}
else { fprintf(stderr, "ERROR: Got unexpected WebSocket message.\n"); close(); }
@@ -224,7 +225,7 @@ struct _RealWebSocket : public WebSocket
void send(std::string message) {
// TODO: consider acquiring a lock on txbuf...
if(clientStatus == CLOSING || clientStatus == CLOSE) { return; }
if(readyState == CLOSING || readyState == CLOSED) { return; }
std::vector<uint8_t> header;
header.assign(2 + (message.size() >= 126 ? 2 : 0) + (message.size() >= 65536 ? 6 : 0), 0);
header[0] = 0x80 | wsheader_type::TEXT_FRAME;
@@ -253,8 +254,8 @@ struct _RealWebSocket : public WebSocket
}
void close() {
if(clientStatus == CLOSING || clientStatus == CLOSE) { return; }
clientStatus = CLOSING;
if(readyState == CLOSING || readyState == CLOSED) { return; }
readyState = CLOSING;
char closeFrame[4] = {0x88, 0x00, 0x00, 0x00};
std::vector<char> header(closeFrame, closeFrame+4);
txbuf.insert(txbuf.end(), header.begin(), header.end());