Skip to content

Commit

Permalink
added broken packat data handling.
Browse files Browse the repository at this point in the history
  • Loading branch information
ryogrid committed Jun 9, 2024
1 parent 1a66615 commit a3ba11d
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 9 deletions.
20 changes: 13 additions & 7 deletions core/np2p_peer.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,11 +68,16 @@ func (p *Np2pPeer) stop() {
}

func (p *Np2pPeer) OnRecvBroadcast(src uint64, buf []byte) (received schema.EncodableAndMergeable, err error) {
//var pkt schema.Np2pPacket
///if err_ := gob.NewDecoder(bytes.NewReader(buf)).Decode(&pkt); err_ != nil {
tmpEvts := make([]*schema.Np2pEvent, 0)
tmpReqs := make([]*schema.Np2pReq, 0)
retPkt := schema.NewNp2pPacket(&tmpEvts, &tmpReqs)

pkt, err_ := schema.NewNp2pPacketFromBytes(buf)
if err_ != nil {
return nil, err_
// returns NP2pPacket having zero length fields
// this means received data is already known to mesh library...
fmt.Println("received strange packet. decoding failed. err = ", err_)
return retPkt, nil
}
if pkt.PktVer != np2p_const.PacketStructureVersion {
return nil, errors.New("Invalid packet version")
Expand All @@ -81,9 +86,6 @@ func (p *Np2pPeer) OnRecvBroadcast(src uint64, buf []byte) (received schema.Enco
fmt.Println("received packat from newer version of server")
}

tmpEvts := make([]*schema.Np2pEvent, 0)
tmpReqs := make([]*schema.Np2pReq, 0)
retPkt := schema.NewNp2pPacket(&tmpEvts, &tmpReqs)
if pkt.Events != nil {
for _, evt := range pkt.Events {
if _, ok := p.recvedEvtReqMap[np2p_util.ExtractUint64FromBytes(evt.Id[:])]; !ok {
Expand Down Expand Up @@ -119,11 +121,15 @@ func (p *Np2pPeer) OnRecvBroadcast(src uint64, buf []byte) (received schema.Enco
}
}
} else {
fmt.Println("received empty packet")
return pkt, nil
}

if len(retPkt.Events) == 0 && len(retPkt.Reqs) == 0 {
return nil, nil
fmt.Println("received strange packet")
// returns NP2pPacket having zero length fields
// this means received data is already known to mesh library...
return pkt, nil
} else {
return retPkt, nil
}
Expand Down
6 changes: 4 additions & 2 deletions transport/mesh_transport.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,10 @@ func (mt *MeshTransport) OnGossipUnicast(src mesh.PeerName, buf []byte) error {
// Return the state information that was modified.
func (mt *MeshTransport) OnGossipBroadcast(src mesh.PeerName, buf []byte) (received mesh.GossipData, err error) {
np2p_util.Np2pDbgPrintln("OnGossipBroadcast called")
recved, err_ := mt.peer.OnRecvBroadcast(uint64(src), buf)
return recved.(mesh.GossipData), err_
recved, _ := mt.peer.OnRecvBroadcast(uint64(src), buf)
// this method does not return error
// for avoiding mesh library's TCP disconnection
return recved.(mesh.GossipData), nil
}

// Register the result of a mesh.Router.NewGossip.
Expand Down

0 comments on commit a3ba11d

Please sign in to comment.