New announcementPacket structure to report back values up the chain

This commit is contained in:
Kleissner
2021-04-20 05:25:56 +02:00
parent 8bb1a3bdf7
commit 62c4b370fc
4 changed files with 41 additions and 23 deletions

View File

@@ -453,13 +453,24 @@ func selfFeatureSupport() (feature byte) {
return feature
}
// announcementPacket contains information about a single announcement message
type announcementPacket struct {
raw []byte // The raw packet
hashes [][]byte // List of hashes that are being searched for
sequence uint32 // Sequence number once sent
err error // Sending error, if any
}
// msgEncodeAnnouncement encodes an announcement message. It may return multiple messages if the input does not fit into one.
// findPeer is a list of node IDs (blake3 hash of peer ID compressed form)
// findValue is a list of hashes
// files is a list of files stored to inform about
func msgEncodeAnnouncement(sendUA, findSelf bool, findPeer []KeyHash, findValue []KeyHash, files []InfoStore) (packetsRaw [][]byte, err error) {
func msgEncodeAnnouncement(sendUA, findSelf bool, findPeer []KeyHash, findValue []KeyHash, files []InfoStore) (packets []*announcementPacket) {
createPacketLoop:
for {
packet := &announcementPacket{}
packets = append(packets, packet)
raw := make([]byte, 64*1024) // max UDP packet size
packetSize := announcementPayloadHeaderSize
@@ -484,13 +495,15 @@ createPacketLoop:
// FIND_SELF
if findSelf {
raw[2] |= 1 << ActionFindSelf
packet.hashes = append(packet.hashes, nodeID)
}
// FIND_PEER
if len(findPeer) > 0 {
// check if there is enough space for at least the header and 1 record
if isPacketSizeExceed(packetSize, 2+32) {
packetsRaw = append(packetsRaw, raw[:packetSize])
packet.raw = raw[:packetSize]
continue createPacketLoop
}
@@ -501,7 +514,7 @@ createPacketLoop:
for n, find := range findPeer {
// check if minimum length is available in packet
if isPacketSizeExceed(packetSize, 32) {
packetsRaw = append(packetsRaw, raw[:packetSize])
packet.raw = raw[:packetSize]
findPeer = findPeer[n:]
continue createPacketLoop
}
@@ -509,6 +522,8 @@ createPacketLoop:
binary.LittleEndian.PutUint16(raw[index:index+2], uint16(n+1))
copy(raw[index+2+32*n:index+2+32*n+32], find.Hash)
packetSize += 32
packet.hashes = append(packet.hashes, find.Hash)
}
findPeer = nil
@@ -518,7 +533,7 @@ createPacketLoop:
if len(findValue) > 0 {
// check if there is enough space for at least the header and 1 record
if isPacketSizeExceed(packetSize, 2+32) {
packetsRaw = append(packetsRaw, raw[:packetSize])
packet.raw = raw[:packetSize]
continue createPacketLoop
}
@@ -529,7 +544,7 @@ createPacketLoop:
for n, find := range findValue {
// check if minimum length is available in packet
if isPacketSizeExceed(packetSize, 32) {
packetsRaw = append(packetsRaw, raw[:packetSize])
packet.raw = raw[:packetSize]
findValue = findValue[n:]
continue createPacketLoop
}
@@ -537,6 +552,8 @@ createPacketLoop:
binary.LittleEndian.PutUint16(raw[index:index+2], uint16(n+1))
copy(raw[index+2+32*n:index+2+32*n+32], find.Hash)
packetSize += 32
packet.hashes = append(packet.hashes, find.Hash)
}
findValue = nil
@@ -546,7 +563,7 @@ createPacketLoop:
if len(files) > 0 {
// check if there is enough space for at least the header and 1 record
if isPacketSizeExceed(packetSize, 2+41) {
packetsRaw = append(packetsRaw, raw[:packetSize])
packet.raw = raw[:packetSize]
continue createPacketLoop
}
@@ -557,7 +574,7 @@ createPacketLoop:
for n, file := range files {
// check if minimum length is available in packet
if isPacketSizeExceed(packetSize, 41) {
packetsRaw = append(packetsRaw, raw[:packetSize])
packet.raw = raw[:packetSize]
files = files[n:]
continue createPacketLoop
}
@@ -574,7 +591,7 @@ createPacketLoop:
files = nil
}
packetsRaw = append(packetsRaw, raw[:packetSize])
packet.raw = raw[:packetSize]
if len(findPeer) == 0 && len(findValue) == 0 && len(files) == 0 {
return
@@ -758,14 +775,15 @@ func (peer *PeerInfo) Chat(text string) {
}
// sendAnnouncement sends the announcement message
func (peer *PeerInfo) sendAnnouncement(sendUA, findSelf bool, findPeer []KeyHash, findValue []KeyHash, files []InfoStore) (err error) {
packets, err := msgEncodeAnnouncement(sendUA, findSelf, findPeer, findValue, files)
func (peer *PeerInfo) sendAnnouncement(sendUA, findSelf bool, findPeer []KeyHash, findValue []KeyHash, files []InfoStore) (packets []*announcementPacket) {
packets = msgEncodeAnnouncement(sendUA, findSelf, findPeer, findValue, files)
for _, packet := range packets {
peer.send(&PacketRaw{Command: CommandAnnouncement, Payload: packet, Sequence: peer.msgNewSequence()})
packet.sequence = peer.msgNewSequence()
packet.err = peer.send(&PacketRaw{Command: CommandAnnouncement, Payload: packet.raw, Sequence: packet.sequence})
}
return err
return
}
// sendResponse sends the response message