本文整理汇总了Golang中tcpserver.Conn.AsyncWriteData方法的典型用法代码示例。如果您正苦于以下问题:Golang Conn.AsyncWriteData方法的具体用法?Golang Conn.AsyncWriteData怎么用?Golang Conn.AsyncWriteData使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类tcpserver.Conn
的用法示例。
在下文中一共展示了Conn.AsyncWriteData方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: HandlerMessage
//数据包逻辑处理
func (this *handle) HandlerMessage(conn *tcpserver.Conn, data []byte) {
defer func() {
if r := recover(); r != nil {
slog.Error("Recovered in HandlerMessage", r)
}
}()
slog.Debug("receive data: %s|%s", PROTOCOLTYPE[data[0]], string(data[1:]))
switch data[0] {
case HOSTCONFIG: //get host config
req := &HostConfigReq{}
if err := json.Unmarshal(data[1:], &req); err != nil {
slog.Error("HandlerMessage() Parse HostConfigReq error(%s), data(%s)", err.Error(), string(data[1:]))
return
}
if len(req.UUID) == 0 {
return
}
host, err := mysql.GetHostConfigByUUID(req.UUID)
if err != nil {
if err == sql.ErrNoRows {
proxy := conn.GetLocalIp()
if req.Proxy == proxy {
req.Proxy = ""
}
err = mysql.CreateHost(req)
if err != nil {
slog.Error("add host failed uuid(%s) ip(%s) proxy(%s) error(%s) ", req.UUID, req.IP, req.Proxy, err.Error())
return
} else {
slog.Info("add host uuid(%s) proxy(%s) ip(%s) host(%v)", req.UUID, req.Proxy, req.IP, host)
}
}
slog.Error("get host config error uuid(%s) ip(%s) proxy(%s) error(%s)", req.UUID, req.IP, req.Proxy, err.Error())
return
}
if err := mysql.UpdateHostInfo(req); err != nil {
slog.Error("update host info error %s", req.IP)
}
host.Ip = req.IP
resp, err := json.Marshal(host)
if err != nil {
slog.Error("marshal host error %s", err.Error())
return
}
packet := NewPacket(HOSTCONFIGRESP, resp)
if err := conn.AsyncWriteData(packet); err != nil {
slog.Error("HandlerMessage() AsyncWriteData() error(%s)", err.Error())
}
case CLIENTVERSION: //获取当前最新版本
version := mysql.GetVersion()
if len(version) == 0 {
return
}
vp := map[string]string{"version": version}
resp, _ := json.Marshal(vp)
packet := NewPacket(CLIENTVERSIONRESP, resp)
if err := conn.AsyncWriteData(packet); err != nil {
slog.Error("AsyncWriteData error %s", err.Error())
}
case SERIESDATA:
sdata := &SeriesData{}
if err := json.Unmarshal(data[1:], &sdata); err != nil {
slog.Error("parse monior data error(%s) data(%s)", err.Error(), string(data))
return
}
//添加指标
if sdata.Flag == 1 {
host, err := mysql.GetHostConfigByUUID(sdata.UUID)
if err != nil {
return
}
for _, s := range host.Services {
if s.Name == sdata.ServiceName {
item := &Item{Key: sdata.Metric, DT: "GAUGE"}
if err := mysql.CreateItemByService(s, item); err != nil {
slog.Error("create item error(%s)", err.Error())
} else {
slog.Info("create item done service(%v) item(%v)", s.Name, sdata)
}
}
}
}
DataBuffer <- []byte(fmt.Sprintf("put %s.%s %v %v uuid=%s ip=%s", sdata.ServiceName, sdata.Metric, sdata.TimeStamp, sdata.Value, sdata.UUID, sdata.IP))
if cfg.ENABLE_REDIS {
RedisBuffer <- fmt.Sprintf(`{"ip":"%s","metric":"%s.%s","@timestamp":"%v","value":%v}`,
sdata.IP, sdata.ServiceName, sdata.Metric, time.Unix(sdata.TimeStamp, 0).Format(time.RFC3339), sdata.Value)
}
case PORTDISCOVERYR:
req := &PortDiscovery{}
if err := json.Unmarshal(data[1:], &req); err != nil {
slog.Error("HandlerMessage() Parse PortDiscovery error(%s), data(%s)", err.Error(), string(data[1:]))
return
}
host, err := mysql.GetHostByUUID(req.UUID)
if err != nil {
return
}
//.........这里部分代码省略.........