Messaging service, adapters, refactoring
This commit is contained in:
26
pkg/discovery/packet.go
Normal file
26
pkg/discovery/packet.go
Normal file
@@ -0,0 +1,26 @@
|
||||
package discovery
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"net"
|
||||
)
|
||||
|
||||
var magicBytes = []byte{0x01, 0x02, 0x01}
|
||||
|
||||
func NewPacket() Packet {
|
||||
return make(Packet, 7) // 7 = 3 bytes of magic + 4 bytes for ipv4 address
|
||||
}
|
||||
|
||||
func NewPacketWithIP(ip net.IP) Packet {
|
||||
return append(magicBytes, []byte(ip.To4())...)
|
||||
}
|
||||
|
||||
type Packet []byte
|
||||
|
||||
func (p Packet) IP(n int) net.IP {
|
||||
return net.IP(p[3:n])
|
||||
}
|
||||
|
||||
func (p Packet) MagicOk() bool {
|
||||
return bytes.Equal(p[:3], magicBytes)
|
||||
}
|
||||
Reference in New Issue
Block a user