码迷,mamicode.com
首页 > Web开发 > 详细

Sword websocket分析二

时间:2018-10-19 22:44:02      阅读:228      评论:0      收藏:0      [点我收藏+]

标签:mask   否则   type   ase   HERE   load   protoc   printf   web   

//websocket发送数据
int send(uint8_t* message, uint64_t message_size)
{
    //掩码
    const uint8_t masking_key[4] = { 0x12, 0x34, 0x56, 0x78 };
    //协议最基本的两个字节标志
    //如果data长度在126~65535之间,则此处playload的值为126
    //(message_size >= 126 ? 2 : 0) --如果playload == 126,增加额外两个字节表示data的长度
    //如果数据的长度大于65535,则此处playload的值为127
    //需要增加8个字节来表示数据的长度
    //MASK 掩码标识位,用来表明负载是否经过掩码处理,浏览器发送的数据都是经过掩码处理(浏览器自动处理,
//无需开发者编码),服务器发送的帧必须不经过掩码处理。所以此处浏览器发送的帧必为1,服务器发送的帧必为0,否则应断开WebSocket连接
//payload length 后面4个字节可能是掩码的key(如果掩码位是1则有这4个字节的key,否则没有) const int headlen = 2 + (message_size >= 126 ? 2 : 0) + (message_size >= 65536 ? 6 : 0) + (_useMask ? 4 : 0); uint8_t* header = new uint8_t[headlen]; uint8_t* txbuf = NULL; uint64_t i = 0; memset(header, 0, sizeof(uint8_t)*headlen); //0x80(1000 0000) 表示一帧之内将所有的数据全部发过去 header[0] = 0x80 | type; if (message_size < 126) { header[1] = (message_size & 0xff) | (_useMask ? 0x80 : 0); if (_useMask) { //payload length 后面4个字节是掩码的key header[2] = masking_key[0]; header[3] = masking_key[1]; header[4] = masking_key[2]; header[5] = masking_key[3]; } } else if (message_size < 65536) { header[1] = 126 | (_useMask ? 0x80 : 0); header[2] = (message_size >> 8) & 0xff; header[3] = (message_size >> 0) & 0xff; if (_useMask) { header[4] = masking_key[0]; header[5] = masking_key[1]; header[6] = masking_key[2]; header[7] = masking_key[3]; } } else { // TODO: run coverage testing here header[1] = 127 | (_useMask ? 0x80 : 0); header[2] = (message_size >> 56) & 0xff; header[3] = (message_size >> 48) & 0xff; header[4] = (message_size >> 40) & 0xff; header[5] = (message_size >> 32) & 0xff; header[6] = (message_size >> 24) & 0xff; header[7] = (message_size >> 16) & 0xff; header[8] = (message_size >> 8) & 0xff; header[9] = (message_size >> 0) & 0xff; if (_useMask) { header[10] = masking_key[0]; header[11] = masking_key[1]; header[12] = masking_key[2]; header[13] = masking_key[3]; } } }
//服务器响应客户端websocket升级请求
int response()
{
    strcat(resData, "HTTP/1.1 101 Switching Protocols\r\n");
    strcat(resData, "Upgrade:websocket\r\n");
    strcat(resData, "Connection: upgrade\r\n");
    strcat(resData, "Sec-WebSocket-Version:13\r\n");

    snprintf(serverkey, 1024, "%s258EAFA5-E914-47DA-95CA-C5AB0DC85B11", stHeader.websocketKey);

    /*
    把Sec-WebSocket-Key加上一个魔幻字符串258EAFA5-E914-47DA-95CA-C5AB0DC85B11。
    使用 SHA-1 加密,之后进行 BASE-64编码,将结果作为 Sec-WebSocket-Accept 头的值
    此处注意,网上的sha1加密都已经字符串化了,并非实际得到的sha1密文,sha1的密文不完全是由[0-9a-z]组成的
    */
    result = sha_encode(SHA_1, (unsigned char *)serverkey, strlen(serverkey), (unsigned char **)&shaTmp, &shalen);
    if (result)
    {
        return -1;
    }
    result = base64_encode(shaTmp, shalen, &pcOut, &outlen);
    if (result)
    {
        return -1;
    }
    strcat(resData, "Sec-WebSocket-Accept:");
    strcat(resData, pcOut);
    strcat(resData, "\r\n");
}

 

Sword websocket分析二

标签:mask   否则   type   ase   HERE   load   protoc   printf   web   

原文地址:https://www.cnblogs.com/zhanggaofeng/p/9819251.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!