• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

Golang packet.Packet类代码示例

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

本文整理汇总了Golang中github.com/pdf/golifx/protocol/v2/packet.Packet的典型用法代码示例。如果您正苦于以下问题:Golang Packet类的具体用法?Golang Packet怎么用?Golang Packet使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。



在下文中一共展示了Packet类的11个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Golang代码示例。

示例1: SetStateLabel

func (d *Device) SetStateLabel(pkt *packet.Packet) error {
	l := stateLabel{}
	if err := pkt.DecodePayload(&l); err != nil {
		return err
	}
	common.Log.Debugf("Got label (%v): %+v\n", d.id, l.Label)
	newLabel := stripNull(string(l.Label[:]))
	if newLabel != d.label {
		d.label = newLabel
		if err := d.publish(common.EventUpdateLabel{Label: d.label}); err != nil {
			return err
		}
	}

	return nil
}
开发者ID:chendo,项目名称:golifx,代码行数:16,代码来源:device.go


示例2: SetStateHostFirmware

func (d *Device) SetStateHostFirmware(pkt *packet.Packet) error {
	f := stateHostFirmware{}
	if err := pkt.DecodePayload(&f); err != nil {
		return err
	}
	common.Log.Debugf("Got firmware version (%d): %d", d.id, f.Version)
	d.RLock()
	version := d.firmwareVersion
	d.RUnlock()
	if f.Version != version {
		d.Lock()
		d.firmwareVersion = f.Version
		d.firmwareVersionString = f.String()
		d.Unlock()
	}

	return nil
}
开发者ID:pdf,项目名称:golifx,代码行数:18,代码来源:device.go


示例3: SetStateLabel

func (d *Device) SetStateLabel(pkt *packet.Packet) error {
	l := stateLabel{}
	if err := pkt.DecodePayload(&l); err != nil {
		return err
	}
	common.Log.Debugf("Got label (%d): %v", d.id, string(l.Label[:]))
	newLabel := stripNull(string(l.Label[:]))
	if newLabel != d.CachedLabel() {
		d.Lock()
		d.label = newLabel
		d.Unlock()
		if err := d.publish(common.EventUpdateLabel{Label: newLabel}); err != nil {
			return err
		}
	}

	return nil
}
开发者ID:pdf,项目名称:golifx,代码行数:18,代码来源:device.go


示例4: New

func New(addr *net.UDPAddr, requestSocket *net.UDPConn, timeout *time.Duration, retryInterval *time.Duration, reliable bool, pkt *packet.Packet) (*Device, error) {
	d := new(Device)
	d.init(addr, requestSocket, timeout, retryInterval, reliable)

	if pkt != nil {
		d.id = pkt.Target
		service := new(stateService)

		if err := pkt.DecodePayload(service); err != nil {
			return nil, err
		}

		d.address.Port = int(service.Port)
	}
	go d.handler()

	return d, nil
}
开发者ID:chendo,项目名称:golifx,代码行数:18,代码来源:device.go


示例5: SetStatePower

func (d *Device) SetStatePower(pkt *packet.Packet) error {
	p := statePower{}
	if err := pkt.DecodePayload(&p); err != nil {
		return err
	}
	common.Log.Debugf("Got power (%v): %+v\n", d.id, d.power)

	if d.power != p.Level {
		d.Lock()
		d.power = p.Level
		d.Unlock()
		if err := d.publish(common.EventUpdatePower{Power: d.power > 0}); err != nil {
			return err
		}
	}

	return nil
}
开发者ID:chendo,项目名称:golifx,代码行数:18,代码来源:device.go


示例6: SetStatePower

func (d *Device) SetStatePower(pkt *packet.Packet) error {
	p := statePower{}
	if err := pkt.DecodePayload(&p); err != nil {
		return err
	}
	common.Log.Debugf("Got power (%d): %d", d.id, d.power)

	state := p.Level > 0
	if d.CachedPower() != state {
		d.Lock()
		d.power = p.Level
		d.Unlock()
		if err := d.publish(common.EventUpdatePower{Power: state}); err != nil {
			return err
		}
	}

	return nil
}
开发者ID:pdf,项目名称:golifx,代码行数:19,代码来源:device.go


示例7: Parse

func (g *Group) Parse(pkt *packet.Packet) error {
	var shouldUpdate, labelUpdate bool

	s := stateGroup{}
	if err := pkt.DecodePayload(&s); err != nil {
		return err
	}

	g.RLock()
	if s.UpdatedAt > g.updatedAt {
		shouldUpdate = true
	}
	g.RUnlock()

	if shouldUpdate {
		g.Lock()
		g.id = s.ID
		g.idEncoded = strings.Replace(
			base64.URLEncoding.EncodeToString(s.ID[:]),
			`=`, ``, -1,
		)
		g.updatedAt = s.UpdatedAt
		if g.label != s.Label {
			g.label = s.Label
			labelUpdate = true
		}
		g.Unlock()

		if labelUpdate {
			if err := g.publish(common.EventUpdateLabel{Label: g.GetLabel()}); err != nil {
				return err
			}
		}
	}

	return nil
}
开发者ID:chendo,项目名称:golifx,代码行数:37,代码来源:group.go


示例8: SetState

func (l *Light) SetState(pkt *packet.Packet) error {
	s := &state{}

	if err := pkt.DecodePayload(s); err != nil {
		return err
	}
	common.Log.Debugf("Got light state (%v): %+v\n", l.id, s)

	if l.color != s.Color {
		l.Lock()
		l.color = s.Color
		l.Unlock()
		if err := l.publish(common.EventUpdateColor{Color: l.color}); err != nil {
			return err
		}
	}
	if l.power != s.Power {
		l.Lock()
		l.power = s.Power
		l.Unlock()
		if err := l.publish(common.EventUpdatePower{Power: l.power > 0}); err != nil {
			return err
		}
	}
	newLabel := stripNull(string(s.Label[:]))
	if newLabel != l.label {
		l.Lock()
		l.label = newLabel
		l.Unlock()
		if err := l.publish(common.EventUpdateLabel{Label: l.label}); err != nil {
			return err
		}
	}

	return nil
}
开发者ID:chendo,项目名称:golifx,代码行数:36,代码来源:light.go


示例9: process

func (p *V2) process(pkt *packet.Packet, addr *net.UDPAddr) {
	common.Log.Debugf("Processing packet from %v: source %v, type %v, sequence %v, target %v, tagged %v, resRequired %v, ackRequired %v: %+v\n", addr.IP, pkt.GetSource(), pkt.GetType(), pkt.GetSequence(), pkt.GetTarget(), pkt.GetTagged(), pkt.GetResRequired(), pkt.GetAckRequired(), *pkt)

	// Update device seen time for any targeted packets
	if pkt.Target != 0 {
		dev, err := p.getDevice(pkt.Target)
		if err == nil {
			dev.SetSeen(time.Now())
		}
	}

	// Broadcast packets, or packets generated by other clients
	if pkt.GetSource() != packet.ClientID {
		switch pkt.GetType() {
		case device.StatePower:
			dev, err := p.getDevice(pkt.GetTarget())
			if err != nil {
				common.Log.Debugf("Skipping StatePower packet for unknown device: source %v, type %v, sequence %v, target %v, tagged %v, resRequired %v, ackRequired %v: %+v\n", pkt.GetSource(), pkt.GetType(), pkt.GetSequence(), pkt.GetTarget(), pkt.GetTagged(), pkt.GetResRequired(), pkt.GetAckRequired(), *pkt)
				return
			}
			err = dev.SetStatePower(pkt)
			if err != nil {
				common.Log.Debugf("Failed setting StatePower on device: source %v, type %v, sequence %v, target %v, tagged %v, resRequired %v, ackRequired %v: %+v\n", pkt.GetSource(), pkt.GetType(), pkt.GetSequence(), pkt.GetTarget(), pkt.GetTagged(), pkt.GetResRequired(), pkt.GetAckRequired(), *pkt)
				return
			}
		case device.StateLabel:
			dev, err := p.getDevice(pkt.GetTarget())
			if err != nil {
				common.Log.Debugf("Skipping StateLabel packet for unknown device: source %v, type %v, sequence %v, target %v, tagged %v, resRequired %v, ackRequired %v: %+v\n", pkt.GetSource(), pkt.GetType(), pkt.GetSequence(), pkt.GetTarget(), pkt.GetTagged(), pkt.GetResRequired(), pkt.GetAckRequired(), *pkt)
				return
			}
			err = dev.SetStateLabel(pkt)
			if err != nil {
				common.Log.Debugf("Failed setting StatePower on device: source %v, type %v, sequence %v, target %v, tagged %v, resRequired %v, ackRequired %v: %+v\n", pkt.GetSource(), pkt.GetType(), pkt.GetSequence(), pkt.GetTarget(), pkt.GetTagged(), pkt.GetResRequired(), pkt.GetAckRequired(), *pkt)
				return
			}
		case device.State:
			dev, err := p.getDevice(pkt.GetTarget())
			if err != nil {
				common.Log.Debugf("Skipping State packet for unknown device: source %v, type %v, sequence %v, target %v, tagged %v, resRequired %v, ackRequired %v: %+v\n", pkt.GetSource(), pkt.GetType(), pkt.GetSequence(), pkt.GetTarget(), pkt.GetTagged(), pkt.GetResRequired(), pkt.GetAckRequired(), *pkt)
				return
			}
			light, ok := dev.(*device.Light)
			if !ok {
				common.Log.Debugf("Skipping State packet for non-light device: source %v, type %v, sequence %v, target %v, tagged %v, resRequired %v, ackRequired %v: %+v\n", pkt.GetSource(), pkt.GetType(), pkt.GetSequence(), pkt.GetTarget(), pkt.GetTagged(), pkt.GetResRequired(), pkt.GetAckRequired(), *pkt)
				return
			}
			err = light.SetState(pkt)
			if err != nil {
				common.Log.Debugf("Error setting State on device: source %v, type %v, sequence %v, target %v, tagged %v, resRequired %v, ackRequired %v: %+v\n", pkt.GetSource(), pkt.GetType(), pkt.GetSequence(), pkt.GetTarget(), pkt.GetTagged(), pkt.GetResRequired(), pkt.GetAckRequired(), *pkt)
				return
			}
		default:
			common.Log.Debugf("Skipping packet with non-local source: source %v, type %v, sequence %v, target %v, tagged %v, resRequired %v, ackRequired %v: %+v\n", pkt.GetSource(), pkt.GetType(), pkt.GetSequence(), pkt.GetTarget(), pkt.GetTagged(), pkt.GetResRequired(), pkt.GetAckRequired(), *pkt)
		}
		return
	}

	// Packets processed at the protocol level regardless of target
	switch pkt.GetType() {
	case device.StateLocation:
		p.addLocation(pkt)
	case device.StateGroup:
		p.addGroup(pkt)
	}

	// Packets processed at the protocol level or returned to target
	switch pkt.GetType() {
	case device.StateService:
		dev, err := p.getDevice(pkt.Target)
		if err != nil {
			dev, err := device.New(addr, p.socket, p.timeout, p.retryInterval, p.Reliable, pkt)
			if err != nil {
				common.Log.Errorf("Failed creating device: %v\n", err)
				return
			}
			p.addDevice(dev)
			return
		}
		// Perform state discovery on lights
		if l, ok := dev.(*device.Light); ok {
			if err := l.Get(); err != nil {
				common.Log.Debugf("Failed getting light state: %v\n", err)
			}
		}
	default:
		if pkt.GetTarget() == 0 {
			common.Log.Debugf("Skipping packet without target: %+v\n", *pkt)
			return
		}
		dev, err := p.getDevice(pkt.GetTarget())
		if err != nil {
			common.Log.Errorf("No known device with ID %v\n", pkt.GetTarget())
			return
		}
		common.Log.Debugf("Returning packet to device %v: %+v\n", dev.ID(), *pkt)
		dev.Handle(pkt)
	}
}
开发者ID:chendo,项目名称:golifx,代码行数:99,代码来源:v2.go


示例10: Send

func (d *Device) Send(pkt *packet.Packet, ackRequired, responseRequired bool) (packet.Chan, error) {
	proxyChan := make(packet.Chan)

	// Rate limiter
	<-d.limiter.C

	// Broadcast vs direct
	broadcast := d.id == 0
	if broadcast {
		// Broadcast can't be reliable
		ackRequired = false
		pkt.SetTagged(true)
	} else {
		pkt.SetTarget(d.id)
		if ackRequired {
			pkt.SetAckRequired(true)
		}
		if responseRequired {
			pkt.SetResRequired(true)
		}
		if ackRequired || responseRequired {
			seq, res := d.addSeq()
			pkt.SetSequence(seq)

			go func() {
				defer func() {
					close(res.done)
					close(proxyChan)
				}()

				var (
					timeout <-chan time.Time
					ticker  = time.NewTicker(*d.retryInterval)
				)

				if d.timeout == nil || *d.timeout == 0 {
					timeout = make(<-chan time.Time)
				} else {
					timeout = time.After(*d.timeout)
				}

				for {
					select {
					case pktResponse, ok := <-res.ch:
						if !ok {
							return
						}
						if pktResponse.Result.GetType() == Acknowledgement {
							common.Log.Debugf("Got ACK for seq %d on device %d, cancelling retries", seq, d.ID())
							ticker.Stop()
							// Ack does not resolve outstanding request,
							// continue waiting for response
							if responseRequired {
								continue
							}
						}
						proxyChan <- pktResponse
						return
					case <-ticker.C:
						common.Log.Debugf("Retrying send for seq %d on device %d after %d milliseconds", seq, d.ID(), *d.retryInterval/time.Millisecond)
						if err := pkt.Write(); err != nil {
							proxyChan <- &packet.Response{
								Error: err,
							}
							return
						}
					case <-timeout:
						proxyChan <- &packet.Response{
							Error: common.ErrTimeout,
						}
						return
					}
				}
			}()
		}
	}

	err := pkt.Write()
	d.resetLimiter(broadcast)

	return proxyChan, err
}
开发者ID:pdf,项目名称:golifx,代码行数:82,代码来源:device.go


示例11: Send

func (d *Device) Send(pkt *packet.Packet, ackRequired, responseRequired bool) (packet.Chan, error) {
	proxyChan := make(packet.Chan)

	// Rate limiter
	<-d.limiter.C

	// Broadcast vs direct
	broadcast := d.id == 0
	if broadcast {
		// Broadcast can't be reliable
		ackRequired = false
		pkt.SetTagged(true)
	} else {
		pkt.SetTarget(d.id)
		if ackRequired {
			pkt.SetAckRequired(true)
		}
		if responseRequired {
			pkt.SetResRequired(true)
		}
		if ackRequired || responseRequired {
			inputChan := make(packet.Chan)
			doneChan := make(chan struct{})

			d.Lock()
			d.sequence++
			if d.sequence == 0 {
				d.sequence++
			}
			seq := d.sequence
			d.responseMap[seq] = inputChan
			d.doneMap[seq] = doneChan
			pkt.SetSequence(seq)
			d.Unlock()

			go func() {
				defer func() {
					close(doneChan)
				}()

				var (
					ok          bool
					timeout     <-chan time.Time
					pktResponse = packet.Response{}
					ticker      = time.NewTicker(*d.retryInterval)
				)

				if d.timeout == nil || *d.timeout == 0 {
					timeout = make(<-chan time.Time)
				} else {
					timeout = time.After(*d.timeout)
				}

				for {
					select {
					case pktResponse, ok = <-inputChan:
						if !ok {
							close(proxyChan)
							return
						}
						if pktResponse.Result.GetType() == Acknowledgement {
							common.Log.Debugf("Got ACK for seq %d on device %d, cancelling retries\n", seq, d.ID())
							ticker.Stop()
							if responseRequired {
								continue
							}
						}
						proxyChan <- pktResponse
						return
					case <-ticker.C:
						common.Log.Debugf("Retrying send after %d milliseconds: %+v\n", *d.retryInterval/time.Millisecond, *pkt)
						if err := pkt.Write(); err != nil {
							pktResponse.Error = err
							proxyChan <- pktResponse
							return
						}
					case <-timeout:
						pktResponse.Error = common.ErrTimeout
						proxyChan <- pktResponse
						return
					}
				}
			}()
		}
	}

	err := pkt.Write()
	d.resetLimiter(broadcast)

	return proxyChan, err
}
开发者ID:chendo,项目名称:golifx,代码行数:91,代码来源:device.go



注:本文中的github.com/pdf/golifx/protocol/v2/packet.Packet类示例由纯净天空整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
Golang middleware.Context类代码示例发布时间:2022-05-28
下一篇:
Golang common.Subscription类代码示例发布时间:2022-05-28
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap