websocket主要用于浏览器与服务器之间建立长连接,服务器需要主动给浏览器发送数据时。当然,这种情况,websocket不是唯一解决方案。websocket基本流程如下: 这里注意,在①②中,客户端与服务器建立连接请求网页数据和服务器返回网页数据时,还是使用的http协议,从③开始,才是使用websocket。 websocket重点由两部分组成。
握手
websocket握手用于检验连接数据是否合法,主要流程是浏览器发送一个随机base64编码的值,服务端接收后加上一段全局变量,再进行SHA-1和base64编码返回给客户端。客户端检验,正确后建立连接。
int readline(char* allbuf,int level,char* linebuf)
{
int len = strlen(allbuf);
for (;level < len; ++level)
{
if(allbuf[level]=='\r' && allbuf[level+1]=='\n')
return level+2;
else
*(linebuf++) = allbuf[level];
}
return -1;
}
int handshark(struct sockitem *si, struct reactor *mainloop)
{
char linebuf[256];
char sec_accept[32];
int level = 0;
unsigned char sha1_data[SHA_DIGEST_LENGTH+1] = {0};
char head[BUFFER_LENGTH] = {0};
do
{
memset(linebuf, 0, sizeof(linebuf));
level = readline(si->recvbuffer, level, linebuf);
if (strstr(linebuf,"Sec-WebSocket-Key") != NULL)
{
strcat(linebuf, GUID);
SHA1((unsigned char*)&linebuf+19,strlen(linebuf+19),(unsigned char*)&sha1_data);
base64_encode(sha1_data,strlen(sha1_data),sec_accept);
sprintf(head, "HTTP/1.1 101 Switching Protocols\r\n" \
"Upgrade: websocket\r\n" \
"Connection: Upgrade\r\n" \
"Sec-WebSocket-Accept: %s\r\n" \
"\r\n", sec_accept);
printf("response\n");
printf("%s\n\n\n", head);
memset(si->recvbuffer, 0, BUFFER_LENGTH);
memcpy(si->sendbuffer, head, strlen(head));
si->slength = strlen(head);
struct epoll_event ev;
ev.events = EPOLLOUT | EPOLLET;
si->sockfd = si->sockfd;
si->callback = send_cb;
si->status = WS_DATATRANSFORM;
ev.data.ptr = si;
epoll_ctl(mainloop->epfd, EPOLL_CTL_MOD, si->sockfd, &ev);
break;
}
} while((si->recvbuffer[level] != '\r' || si->recvbuffer[level+1] != '\n') && level != -1);
return 0;
}
通信
握手完成后,开始正常的通信,这里贴出websocket协议封装的代码,协议结构图就不贴了。
char* decode_packet(char *stream, char *mask, int length, int *ret)
{
nty_ophdr *hdr = (nty_ophdr*)stream;
unsigned char *data = stream + sizeof(nty_ophdr);
int size = 0;
int start = 0;
int i = 0;
if ((hdr->mask & 0x7F) == 126)
{
nty_websocket_head_126 *hdr126 = (nty_websocket_head_126*)data;
size = hdr126->payload_length;
for (i = 0;i < 4;i ++)
{
mask[i] = hdr126->mask_key[i];
}
start = 8;
} else if ((hdr->mask & 0x7F) == 127)
{
nty_websocket_head_127 *hdr127 = (nty_websocket_head_127*)data;
size = hdr127->payload_length;
for (i = 0;i < 4;i ++)
{
mask[i] = hdr127->mask_key[i];
}
start = 14;
} else {
size = hdr->payload_length;
memcpy(mask, data, 4);
start = 6;
}
*ret = size;
umask(stream+start, size, mask);
return stream + start;
}
int encode_packet(char *buffer,char *mask, char *stream, int length)
{
nty_ophdr head = {0};
head.fin = 1;
head.opcode = 1;
int size = 0;
if (length < 126)
{
head.payload_length = length;
memcpy(buffer, &head, sizeof(nty_ophdr));
size = 2;
} else if (length < 0xffff)
{
nty_websocket_head_126 hdr = {0};
hdr.payload_length = length;
memcpy(hdr.mask_key, mask, 4);
memcpy(buffer, &head, sizeof(nty_ophdr));
memcpy(buffer+sizeof(nty_ophdr), &hdr, sizeof(nty_websocket_head_126));
size = sizeof(nty_websocket_head_126);
} else
{
nty_websocket_head_127 hdr = {0};
hdr.payload_length = length;
memcpy(hdr.mask_key, mask, 4);
memcpy(buffer, &head, sizeof(nty_ophdr));
memcpy(buffer+sizeof(nty_ophdr), &hdr, sizeof(nty_websocket_head_127));
size = sizeof(nty_websocket_head_127);
}
memcpy(buffer+2, stream, length);
return length + 2;
}
再贴出通信的代码
int transform(struct sockitem *si, struct reactor *mainloop)
{
int ret = 0;
char mask[4] = {0};
char *data = decode_packet(si->recvbuffer, mask, si->rlength, &ret);
printf("data : %s , length : %d\n", data, ret);
ret = encode_packet(si->sendbuffer, mask, data, ret);
si->slength = ret;
memset(si->recvbuffer, 0, BUFFER_LENGTH);
struct epoll_event ev;
ev.events = EPOLLOUT | EPOLLET;
si->sockfd = si->sockfd;
si->callback = send_cb;
si->status = WS_DATATRANSFORM;
ev.data.ptr = si;
epoll_ctl(mainloop->epfd, EPOLL_CTL_MOD, si->sockfd, &ev);
return 0;
}
这里再提示一下,握手、通信等阶段使用状态机实现。注意使用epoll的话,在每个状态里写epoll_ctl,不要再最后总体结构中写,因为如果数据量大的话,不是一次recv/send就能搞定。 另外,从websocket协议中可以获得启示。今后,在自定义开发基于TCP的协议时,要注意三个方面:操作码、包长度、掩码(非必须)。当然,数据的话,可以使用xml、json等协议。
长连接
最后,关于长连接,再啰嗦一下。TCP中也有保持长连接的keepalive机制,但是不建议使用,不要使用TCP的keepalive去代替应用层的心跳包。因为keepalive超时,TCP会主动回收连接相关资源,并且应用层无法得到任何反馈,可控性不强,只知道超时回收了,没法对超时做出一些判断。所以,在应用层设计心跳包去维持长连接。
|