Greetings, I am trying to make one node send a struct marshalled to protobuf, and the other is supposed to receive it and send a response via the same stream. My problem is that the listener is stuck on on the ReadAll() function and the only way I can unstuck it is if I close the stream - which I don’t want to do before sending back a response.
Could someone recommend me an workaround?
Thank you kindly.
Here are my send and reader functions
//lots of imports and main() skipped
ggio “github.com/gogo/protobuf/io”
func send(s network.Stream) {
cntr := entities.Contract{
OwnerID: "id",
Size: 222,
Cid: "cidcid",
Expires: time.Now(),
Cardinality: 3,
}
req := messages.StorageRequest{Contract: cntr}
req_pb := req.MarshalToProtobuf()
//write to stream
writer := ggio.NewFullWriter(s)
if err := writer.WriteMsg(req_pb); err != nil{
s.Reset()
panic(err)
}
//receive response
in := genmsg.Message{}
buf, err := ioutil.ReadAll(s)
if err != nil{
s.Reset()
panic(err)
}
if err := proto.Unmarshal(buf, &in); err != nil{
panic(err)
}
fmt.Printf("Received %v from %s\n", in, s.ID())
}
func reader(s network.Stream) {
in := genmsg.Message{}
buf, err := ioutil.ReadAll(s) //<---- it gets stuck here
if err != nil{
s.Reset()
panic(err)
}
if err := proto.Unmarshal(buf, &in); err != nil{
panic(err)
}
fmt.Printf("Received %v from %s\n", in, s.ID())
var resp_pb *genmsg.Message
//generate response
switch in.Type{
case genmsg.Message_STORAGE_REQUEST:
resp := messages.StorageResponse{
Done: true,
}
resp_pb = resp.MarshalToProtobuf()
default:
}
writer := ggio.NewFullWriter(s)
if err := writer.WriteMsg(resp_pb); err != nil{
s.Reset()
panic(err)
}
s.Close()
}