wuwudeqi
V2EX  ›  Java

netty 的问题

  •  
  •   wuwudeqi · Dec 20, 2019 · 5262 views
    This topic created in 2513 days ago, the information mentioned may be changed or developed.

    现在我是用 netty 服务端来和硬件做通讯,用 spring boot 框架,硬件每次连接上线,我通过 HashMap<String, Channel> channelHashMap,将 ip 与对应的 channel 存储,当我需要主动发起请求,就根据 ip 去 map 中拿 channel,然后发送数据,因为会有很多设备连接会上报消息,那么我该怎么判断在 RequestDecoder 收到的消息是我刚刚发出的消息

    7 replies  •  2019-12-22 18:04:12 +08:00
    xzg
    xzg
    Dec 20, 2019
    1
    不存在你想的那个问题,设备上报的数据是通过 decoder 是 ChannelInbound 的 handler,你下发的数据 write 是通过 ChannelHandlerContext 写入 ChannelOutbound。下面是官方的图

    I/O Request
    via Channel or
    ChannelHandlerContext
    |
    +---------------------------------------------------+---------------+
    | ChannelPipeline | |
    | \|/ |
    | +---------------------+ +-----------+----------+ |
    | | Inbound Handler N | | Outbound Handler 1 | |
    | +----------+----------+ +-----------+----------+ |
    | /|\ | |
    | | \|/ |
    | +----------+----------+ +-----------+----------+ |
    | | Inbound Handler N-1 | | Outbound Handler 2 | |
    | +----------+----------+ +-----------+----------+ |
    | /|\ . |
    | . . |
    | ChannelHandlerContext.fireIN_EVT() ChannelHandlerContext.OUT_EVT()|
    | [ method call] [method call] |
    | . . |
    | . \|/ |
    | +----------+----------+ +-----------+----------+ |
    | | Inbound Handler 2 | | Outbound Handler M-1 | |
    | +----------+----------+ +-----------+----------+ |
    | /|\ | |
    | | \|/ |
    | +----------+----------+ +-----------+----------+ |
    | | Inbound Handler 1 | | Outbound Handler M | |
    | +----------+----------+ +-----------+----------+ |
    | /|\ | |
    +---------------+-----------------------------------+---------------+
    | \|/
    +---------------+-----------------------------------+---------------+
    | | | |
    | [ Socket.read() ] [ Socket.write() ] |
    | |
    | Netty Internal I/O Threads (Transport Implementation) |
    +-------------------------------------------------------------------+
    xzg
    xzg
    Dec 20, 2019
    2
    算了 这个排版贼鸡儿恶心
    wysnylc
    wysnylc
    Dec 20, 2019
    3
    把 HashMap 换成 ConCurrentHashMap 或者 ConCurrentSkipListMap
    JYDXL
    JYDXL
    Dec 20, 2019
    4
    没太看懂要表达的意思,如果说的是请求-响应模式,tcp 这种全双工的原本就不是一一对应的,一般是在协议里放一个请求序号字段,对应的返回用同一个序号来区分。
    Jimmy2Angel
    Jimmy2Angel
    Dec 20, 2019
    5
    先把问题搞清楚,什么叫“该怎么判断在 RequestDecoder 收到的消息是我刚刚发出的消息”。简单来说,你 write 的数据会经过 encoder 编码后下发给设备,设备上传的数据先经过 decoder 解码成你要的格式数据,然后信息均是从设备上传的数据报文中解析出来的,包括设备 ID (哪个设备的数据)、消息 ID (什么数据)等
    MoHen9
    MoHen9
    Dec 20, 2019 via Android
    6
    一般 TCP 连接建立后会在自己的线程读写消息,所以你直接将连接的设备信息放在 handler 中即可,只要 handler 不共享,就是每个 TCP 对应一个 handler
    ZiLong
    ZiLong
    Dec 22, 2019
    7
    给消息一个标志符如 Id?
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   1650 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 34ms · UTC 04:37 · PVG 12:37 · LAX 20:37 · JFK 23:37
    ♥ Do have faith in what you're doing.