decoding
for real-time stream processing under persistent connection. Users don't have to wait to get the full packet before they can start decoding
.observe key
to get the data they concern.Y3
's scenario is to deal with real-time stream processing under persistent connection, so the user gives the raw stream to Y3
, and then tells Y3
to observe the key. Y3
starts the parsing operation after taking over the raw stream. When the key is observed, Y3
will decode the value in specified type and trigger a callback function.
Y3
describes the object as a set of TLV
structures. When decoding packets, it can know earlier whether the current T is the observed key, and determine whether to jump to the next set of TLV structures directly, without unnecessary decoding operations on unobserved packets, so Y3
can improve the decoding efficiency and resource utilization.
Core interfaces include:
Y3
's encoding rules.key
which is specified by user.Y3
.type SourceData struct {
Name string `y3:"0x10"`
Noise float32 `y3:"0x11"`
Therm Thermometer `y3:"0x12"`
}
type Thermometer struct {
Temperature float32 `y3:"0x13"`
Humidity float32 `y3:"0x14"`
}
func main() {
input := SourceData{
Name: "yomo",
Noise: float32(456),
Therm: Thermometer{Temperature: float32(30), Humidity: float32(40)},
}
// encode to the data in Y3-Codec format
codec := y3.NewCodec(0x20)
inputBuf, _ := codec.Marshal(input)
fmt.Printf("inputBuf=%#v\n", inputBuf)
}
func main() {
// define callback function to process the data being observed
callback := func(v []byte) (interface{}, error) {
return y3.ToFloat32(v)
}
// create the Observable interface
source := y3.FromStream(bytes.NewReader(inputBuf))
// subscribe the Key being observed and set the callback function
consumer := source.Subscribe(0x11).OnObserve(callback)
// checking data after it has been processed
for c := range consumer {
fmt.Printf("observed value=%v, type=%v\n", c, reflect.ValueOf(c).Kind())
}
}
The key being subscribed has a defined range:
0x10 ~ 0x3f
0x01 ~ 0x0f
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。