package node import ( "lux/proto" ipnet "net" ) type LuxNodeSync struct { Synced map[proto.LuxID]struct{} Neighbors map[proto.LuxID]*ipnet.UDPAddr SyncState LuxNodeState } func NewLuxNodeSync() LuxNodeSync { return LuxNodeSync{ Synced: make(map[proto.LuxID]struct{}), Neighbors: make(map[proto.LuxID]*ipnet.UDPAddr), SyncState: NewLuxNodeState(), } } func (sync *LuxNodeSync) Read(rd *proto.LuxBuffer) error { // read synced list syncNum, err := rd.ReadUint16() if err != nil { return err } for i := 0; i < int(syncNum); i++ { id := proto.LuxID{} if err := id.Read(rd); err != nil { return err } sync.Synced[id] = struct{}{} } // read neighbors neighborNum, err := rd.ReadUint16() if err != nil { return err } for i := 0; i < int(neighborNum); i++ { id := proto.LuxID{} if err := id.Read(rd); err != nil { return err } addrPort, err := rd.ReadIPPort() if err != nil { return err } sync.Neighbors[id] = ipnet.UDPAddrFromAddrPort(addrPort) } // read sync state err = sync.SyncState.Read(rd) if err != nil { return err } return nil } func (sync *LuxNodeSync) Write(wd *proto.LuxBuffer) { // write synced list wd.WriteUint16(uint16(len(sync.Synced))) for id := range sync.Synced { id.Write(wd) } // write neighbors wd.WriteUint16(uint16(len(sync.Neighbors))) for id, udpAddr := range sync.Neighbors { id.Write(wd) wd.WriteIPPort(udpAddr.AddrPort()) } // write sync state sync.SyncState.Write(wd) }