Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

The corresponding read with write(WriteSize=true) #32

Open
geohuz opened this issue Mar 9, 2023 · 2 comments
Open

The corresponding read with write(WriteSize=true) #32

geohuz opened this issue Mar 9, 2023 · 2 comments

Comments

@geohuz
Copy link

geohuz commented Mar 9, 2023

I have a protobuf message server which requires me to put the size of the message on the wire, so the following

stream.write(msg, true)     

works on sending message (without WriteSize=true the server response error message). Now I have problem of reading the message received from server, apparently the server also encode the size in the message, can we have a flag to indicate the skip reading the size of bytes in the read proc?

BTW, as I have made it work on the nimpb by modifing the compiled nim files, as such:

proc writeXMessage(stream: Stream, message: XMessage) =
  stream.protoWriteUInt64(sizeOfXMessage(message))  # write the size 
  ....

proc readXMessage(stream: Stream): XMessage =
  result = new XMessage()
  discard stream.protoReadUInt64()  # we don't even specify the size
  while not atEnd(stream):
  ...
@geohuz
Copy link
Author

geohuz commented Mar 9, 2023

I found the solution, I didnt' realize the raw data from server is type seq[byte], so I just simply write:

let data = await client.receiveBinaryPacket()
var stream = newStringStream(cast[string](data))
var readMsg = stream.readXMesage()
....

@geohuz geohuz closed this as completed Mar 9, 2023
@geohuz
Copy link
Author

geohuz commented Mar 9, 2023

well, I think it is better to generate overloaded procs for different data type, likewise:

proc getXMessage*(data: string): XMessage =
    let
        ss = newStringStream(data)
    result = readXMessage(ss)

proc getXMessage*(data: seq[byte]): XMessage =
    let
        ss = newStringStream(cast[string](data))
    result = readXMessage(ss)

@geohuz geohuz reopened this Mar 9, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant