β

Mina的WebSocket问题排查

Harries Blog™ 47 阅读

项目的客户端需要重构,探讨后决定客户端与 服务器 的交互使用WebSocket 协议 。后端的网络层框架是Mina,在现有基础上增加对WebSocket的 解析 即可完成无缝迁移。正好Apache讨论组上有人提供Mina的WebSocket过滤器 代码 下载 后添加到项目,扔到服务器上一切正常。

今天前端同事反馈说请求某个接口会导致客户端立马报错然后断开连接,提示信息为:One or more reserved bits are on, reserved1 = 0, reserved2 = 1, reserved3 = 1。看到信息后,第一反应是: 数据 没封包就发送到客户端了?排查log,确认数据按照WebSocket的格式封装后再发送出去的。

根据错误信息上网查解决方案,基本上甩锅给杀毒 软件 、系统、浏览器等,没找到靠谱的原因和 解决方法 。没办法只好让前端开wireshark抓包,与 服务端 数据进行对比。一番折腾后,确认是因为接口返回大量数据,客户端没能完整接收造成的。

数据量大才会出现,难道WebSocket客户端的缓冲区或者单次传输有大小限制?上网搜寻后发现都没有限制,很有可能是服务端没能正确封包数据造成的。由于使用的是第三方非权威代码对数据封包,过一遍编码的代码是必要的:

  /*
   * To change this template, choose Tools | Templates
   * and open the template in the editor.
   */
  package com.shephertz.appwarp.websocket.binary;
  
  import org.apache.mina.core.buffer.IoBuffer;
  import org.apache.mina.core.session.IoSession;
  import org.apache.mina.filter.codec.ProtocolEncoderAdapter;
  import org.apache.mina.filter.codec.ProtocolEncoderOutput;
  
  /**
   * Encodes incoming buffers in a manner that makes the receiving client type transparent to the 
   * encoders further up in the filter chain. If the receiving client is a native client then
   * the buffer contents are simply passed through. If the receiving client is a websocket, it will encode
   * the buffer contents in to WebSocket DataFrame before passing it along the filter chain.
   * 
   * Note: you must wrap the IoBuffer you want to send around a WebSocketCodecPacket instance.
   * 
   * @author DHRUV CHOPRA
   */
  public class WebSocketEncoder extends ProtocolEncoderAdapter{
  
      @Override
      public void encode(IoSession session, Object message, ProtocolEncoderOutput out) throws Exception {
          boolean isHandshakeResponse = message instanceof WebSocketHandShakeResponse;
          boolean isDataFramePacket = message instanceof WebSocketCodecPacket;
          boolean isRemoteWebSocket = session.containsAttribute(WebSocketUtils.SessionAttribute) && (true==(Boolean)session.getAttribute(WebSocketUtils.SessionAttribute));
          IoBuffer resultBuffer;
          if(isHandshakeResponse){
              WebSocketHandShakeResponse response = (WebSocketHandShakeResponse)message;
              resultBuffer = WebSocketEncoder.buildWSResponseBuffer(response);
          }
          else if(isDataFramePacket){
              WebSocketCodecPacket packet = (WebSocketCodecPacket)message;
              resultBuffer = isRemoteWebSocket ? WebSocketEncoder.buildWSDataFrameBuffer(packet.getPacket()) : packet.getPacket();
          }
          else{
              throw (new Exception("message not a websocket type"));
          }
          
          out.write(resultBuffer);
      }
      
      // Web Socket handshake response go as a plain string.
      private static IoBuffer buildWSResponseBuffer(WebSocketHandShakeResponse response) {                
          IoBuffer buffer = IoBuffer.allocate(response.getResponse().getBytes().length, false);
          buffer.setAutoExpand(true);
          buffer.put(response.getResponse().getBytes());
          buffer.flip();
          return buffer;
      }
      
      // Encode the in buffer according to the Section 5.2. RFC 6455
      private static IoBuffer buildWSDataFrameBuffer(IoBuffer buf) {
          
          IoBuffer buffer = IoBuffer.allocate(buf.limit() + 2, false);
          buffer.setAutoExpand(true);
          buffer.put((byte) 0x82);
          if(buffer.capacity() <= 125){
              byte capacity = (byte) (buf.limit());
              buffer.put(capacity);
          }
          else{
              buffer.put((byte)126);
              buffer.putShort((short)buf.limit());
          }        
          buffer.put(buf);
          buffer.flip();
          return buffer;
      }
  }
 

代码的51到71行对数据进行封包,直觉提示60行到67行的封包处理在数据量大时有问题。打开 WebSocket的rfc页面
,查看规范中定义的数据包结构。根据规范,数据包小于125时,只需增加两个字节的包头;大于等于126小于等于2^16时,包头有4个字节;数据长度更大时,包头有10个字节,其中8个字节用来存储数据的长度。

  0                   1                   2                   3
  0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
 +-+-+-+-+-------+-+-------------+-------------------------------+
 |F|R|R|R| opcode|M| Payload len |    Extended payload length    |
 |I|S|S|S|  (4)  |A|     (7)     |             (16/64)           |
 |N|V|V|V|       |S|             |   (if payload len==126/127)   |
 | |1|2|3|       |K|             |                               |
 +-+-+-+-+-------+-+-------------+ - - - - - - - - - - - - - - - +
 |     Extended payload length continued, if payload len == 127  |
 + - - - - - - - - - - - - - - - +-------------------------------+
 |                               |Masking-key, if MASK set to 1  |
 +-------------------------------+-------------------------------+
 | Masking-key (continued)       |          Payload Data         |
 +-------------------------------- - - - - - - - - - - - - - - - +
 :                     Payload Data continued ...                :
 + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - +
 |                     Payload Data continued ...                |
 +---------------------------------------------------------------+
 

下载的第三方代码只处理了payload len<=126的情形,数据量大时payload == 127的情形仍按照payload len == 126方式处理,显然会导致客户端接收不完整数据。将构建WebSocket数据帧的函数更改如下,顺利解决问题:

    // Encode the in buffer according to the Section 5.2. RFC 6455
    private static IoBuffer buildWSDataFrameBuffer(IoBuffer buf) {
        IoBuffer buffer = null;
        if (buf.limit() <= 125) {
            buffer = IoBuffer.allocate(buf.limit() + 2, false);
            buffer.put((byte) 0x82);
            buffer.put((byte)buf.limit());
        } else if (buf.limit() <= 0xFFFF) {
            buffer = IoBuffer.allocate(buf.limit() + 4, false);
            buffer.put((byte) 0x82);
            buffer.put((byte)126);
            buffer.putShort((short)buf.limit());
        } else {
            buffer = IoBuffer.allocate(buf.limit() + 10, false);
            buffer.put((byte) 0x82);
            buffer.put((byte)127);
            buffer.putLong((long)buf.limit());
        }
 
        buffer.put(buf);
        buffer.flip();
        return buffer;
    }
 

主要改动两点:1. 新增数据大于2^16时的处理; 2. 分配大小合适的缓冲区,避免触发重分配。

总结

根据规范,类似”One or more reserved bits are on”的错误提示,是服务端没有正确封包造成的。另外在 git hub上搜websocket解析库,发现 cjoo/WebSocket_mina
这个库中的封包处理也是错误的。错误的地方在 https ://github.com/cjoo/WebSocket_mina/blob/master/WebServer/ src /utils/WebSocketUtil. java
中的70行到72行。

参考

  1. http s://issues.apache.org/jira/browse/DIRMINA-907

  2. https://tools.ietf.org/html/rfc6455#section-5.2

原文

https://tlanyan.me/resolve-mina-websocket-problem/

本站部分文章源于互联网,本着传播知识、有益学习和研究的目的进行的转载,为网友免费提供。如有著作权人或出版方提出异议,本站将立即删除。如果您对文章转载有任何疑问请告之我们,以便我们及时纠正。 PS:推荐一个微信公众号: askHarries 或者qq群:474807195,里面会分享一些资深架构师录制的视频录像:有Spring,MyBatis,Netty源码分析,高并发、高性能、分布式、微服务架构的原理,JVM性能优化这些成为架构师必备的知识体系。还能领取免费的学习资源,目前受益良多

转载请注明原文出处: Harries Blog™ » Mina的WebSocket问题排查

作者:Harries Blog™
追心中的海,逐世界的梦
原文地址:Mina的WebSocket问题排查, 感谢原作者分享。

发表评论