首先,值得一提的是,在單個F#解決方案中,序列化和反序列化Bond消息工作正常。但是,我無法正確處理通過ZeroMQ發送和/或接收消息。ZeroMQ上的MS Bond序列化中的運行時「EndOfStreamException」錯誤
以下程序的用戶端存在運行時錯誤。一個.bond文件是用bond編譯器定義和編譯的。然後從C#創建一個dll,從F#調用。然後我有兩個F#程序。一個通過tcp套接字發佈序列化數據,另一個是訂閱者。在sub上收到消息時,嘗試解組原始數據的行是導致運行時錯誤的行。任何人都可以看到這個原因嗎?
[編輯]根據Fyodor的評論,我對發佈方進行了修改,改變了用戶端的錯誤。所以這個錯誤可能與我打包和解包信息有關。
這是.bond文件
namespace Examples
struct Record
{
0: map<string, double> payload;
}
這裏是出版商:
// publisher
open System
open Bond
open Bond.Protocols
open Bond.IO.Safe
open ZeroMQ
let ctx = new ZContext()
let publisher = new ZSocket(ctx, ZSocketType.PUB)
publisher.Bind("tcp://*:5556")
let src = new Examples.Record()
src.payload.Add("a", 1.)
src.payload.Add("b", 2.)
let output = new OutputBuffer()
let writer = new CompactBinaryWriter<OutputBuffer>(output)
while true do
Marshal.To(writer, src)
//let input = new InputBuffer(output.Data)
//let byteArr = input.ReadBytes(int(input.Length - 1L))
let updateFrame = new ZFrame(System.Text.Encoding.ASCII.GetString output.Data.Array)
publisher.Send(updateFrame)
這裏是用戶:
// subscriber
open Bond
open Bond.Protocols
open Bond.IO.Safe
open System
open System.Text
open ZeroMQ
let ctx = new ZContext()
let subscriber = new ZSocket(ctx, ZSocketType.SUB)
subscriber.Connect("tcp://127.0.0.1:5556")
subscriber.SubscribeAll()
let output = new OutputBuffer()
while true do
let received = subscriber.ReceiveFrame()
let byteArr = Encoding.ASCII.GetBytes (received.ReadString())
let arrSeg = ArraySegment<byte>(byteArr)
let input = new InputBuffer(arrSeg)
let dst = Unmarshal<Examples.Record>.From(input)
for KeyValue(k, v) in dst.payload do
printfn "%A %A" k v
我看到你正在'byteArr.ToString()'中創建'ZFrame'。這不符合你的想法。嘗試打印出'byteArr.ToString()'的結果來查看實際發送的內容。 –