为什么在使用网络管道时等待总是返回Nothing?

时间:2015-07-22 06:24:20

标签: haskell conduit network-conduit

我试图学习如何使用导管。我有一个管道,它接受字节串​​并将它们分组为表示发送到服务器的操作的数据包。然后我有一个管道,它接收这些数据包,对它们起作用并产生响应数据包。最后,我有一个管道,它接收响应数据包并将它们转换为字节串流。问题是数据包处理管道中的yield总是返回Nothing。我的代码看起来像这样

processingConduit :: ServerState -> Conduit BS.ByteString IO BS.ByteString
processingConduit state = getPacketConduit
    =$= processPacketConduit state
    =$= putPacketConduit

processPacketConduit :: ServerState -> Conduit ClientPacket IO ServerPacket
processPacketConduit state = awaitForever $ \packet -> do
    liftIO $ putStrLn "got something :)"
    let function = case packet of
            (LoginPacket _ _) -> login
            _                 -> ugh
    result <- liftIO $ function state packet
    yield result

getPacketConduit :: Conduit BS.ByteString IO ClientPacket
getPacketConduit = conduitGet (get :: Get ClientPacket)

putPacketConduit :: Conduit ServerPacket IO BS.ByteString
putPacketConduit = conduitPut (put :: ServerPacket -> Put)

我使用runTCPServer运行管道并且它能够接受连接并且getPacketConduit能够将字节字符串转换为有效数据包,但awaitForever从不调用其函数。如果我修改processPacketConduit来使用await,我可以看到它总是返回Nothing。

1 个答案:

答案 0 :(得分:0)

我弄清楚了我的错误,我忘记了谷物通过发送length :: Int后跟实际数据来序列化字节串。我只发送一个字节长度。