You need to enable JavaScript to run this app. 如何为Network.framework编写基于分隔符拆分流的NWProtocolFramer?
优惠活动
大模型
产品
解决方案
定价
更多

如何为Network.framework编写基于分隔符拆分流的NWProtocolFramer?

问题根因

当前实现存在两处核心逻辑错误,导致数据流卡死:

  • 误解handleInput返回值的语义
    handleInput的返回值是:当没有匹配到完整消息时,你还需要框架额外推送多少新数据,才会再次唤起这个方法。这个值是增量值,不是需要收集的总长度。
    原代码在找不到分隔符时,返回值设置为“当前缓冲区长度 + 1”,会让框架误以为你需要攒非常多的新数据才会处理,哪怕缓冲区里已经收到了管道分隔符,框架也不会主动唤起Framer处理,最终表现为接收到不完整数据块后就再也无法处理后续数据。
  • 未正确通过parseInput回调标记消费的字节数
    parseInput的内部闭包必须返回本次实际从输入缓冲区消费掉的字节数,原代码无论解析结果如何都返回0,等于告诉框架“你没有处理任何内容,下次把整个缓冲区再原样传给你”。同时原代码在交付完消息后,还需要额外调用一次parseInput去“跳过”分隔符,这种写法很容易导致缓冲区状态不同步。
正确实现逻辑
  • 处理输入时,parseInput的minimumIncompleteLength固定传1,意思是只要缓冲区里有至少1个字节,就触发回调扫描内容找分隔符。
  • 在扫描缓冲区时:
    • 找到管道符|的位置:将分隔符之前的内容作为完整消息交付给上层,然后返回的消费长度为“分隔符位置 + 1”(把分隔符本身也从缓冲区移除)。
    • 没找到管道符:不消费任何字节,退出循环时返回1,意思是“只要再有1个新字节,就重新唤起Framer扫描一遍缓冲区”。
修正后的Framer代码
import Network

fileprivate let pipe = Character("|").asciiValue!

class PipeFramer: NWProtocolFramerImplementation {
    static let label = "Pipe framer"
    static let definition = NWProtocolFramer.Definition(implementation: PipeFramer.self)

    required init(framer: NWProtocolFramer.Instance) {}

    func start(framer: NWProtocolFramer.Instance) -> NWProtocolFramer.StartResult { .ready }

    func handleInput(framer: NWProtocolFramer.Instance) -> Int {
        while true {
            var delimiterFound = false
            
            // 只要缓冲区有至少1字节就触发回调扫描
            _ = framer.parseInput(minimumIncompleteLength: 1, maximumLength: 65535) { buffer, _ in
                guard let buffer = buffer else { return 0 }
                
                // 查找缓冲区中第一个分隔符的位置
                if let index = buffer.firstIndex(of: pipe) {
                    delimiterFound = true
                    
                    // 交付分隔符之前的内容作为完整消息
                    guard framer.deliverInputNoCopy(
                        length: index,
                        message: .init(instance: framer),
                        isComplete: true
                    ) else {
                        return 0
                    }
                    
                    // 返回消费长度:分隔符本身也需要标记为已消费
                    return index + 1
                } else {
                    // 未找到分隔符,不消费任何字节
                    return 0
                }
            }

            // 未找到可交付的完整消息,返回增量值1,新数据到达时立即唤起处理
            guard delimiterFound else {
                return 1
            }
        }
    }

    func handleOutput(framer: NWProtocolFramer.Instance, message: NWProtocolFramer.Message, messageLength: Int, isComplete: Bool) {
        try! framer.writeOutputNoCopy(length: messageLength)
        framer.writeOutput(data: [pipe])
    }

    func wakeup(framer: NWProtocolFramer.Instance) {}
    func stop(framer: NWProtocolFramer.Instance) -> Bool { return true }
    func cleanup(framer: NWProtocolFramer.Instance) { }
}

按照这个实现,不管TCP流的分块方式如何,只要有新数据到达,Framer就会立即扫描整个缓冲区,定位到管道符位置后拆分出完整消息,不会再出现卡死的问题。

内容的提问来源于stack exchange,提问作者Damiaan Dufaux

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.11.23 18:27:02