lux/host/lux_state.go
2025-01-11 02:28:46 +02:00

50 lines
871 B
Go

package host
import "lux/proto"
type LuxState struct {
Hostname string
Options map[LuxOptionType]LuxOption
}
func NewLuxState(hostname string) LuxState {
return LuxState{
Hostname: hostname,
Options: make(map[LuxOptionType]LuxOption),
}
}
func (state *LuxState) Read(rd *proto.LuxBuffer) error {
// hostname
hostname, err := rd.ReadString()
if err != nil {
return err
}
state.Hostname = hostname
// option count
count, err := rd.ReadUint16()
if err != nil {
return nil
}
for i := 0; i < int(count); i++ {
// read options
opt, err := ReadLuxOption(rd)
if err != nil {
return err
}
state.Options[opt.Type()] = opt
}
return nil
}
func (state *LuxState) Write(wd *proto.LuxBuffer) {
wd.WriteString(state.Hostname)
wd.WriteUint16(uint16(len(state.Options)))
for _, opt := range state.Options {
WriteLuxOption(wd, opt)
}
}