當前位置: 首頁>>代碼示例>>Golang>>正文


Golang Conn.AsyncWriteData方法代碼示例

本文整理匯總了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
		}
//.........這裏部分代碼省略.........
開發者ID:y8y,項目名稱:OWL-v3,代碼行數:101,代碼來源:handle.go


注:本文中的tcpserver.Conn.AsyncWriteData方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。