本文整理汇总了Golang中enlightgame/transport.TcpMessage类的典型用法代码示例。如果您正苦于以下问题:Golang TcpMessage类的具体用法?Golang TcpMessage怎么用?Golang TcpMessage使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了TcpMessage类的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: makeNoticeMsg
func makeNoticeMsg(str string, p uint16) []byte {
t := transport.TcpMessage{}
builder := flatbuffers.NewBuilder(0)
ct := builder.CreateString(str)
proto.NoticeStart(builder)
proto.NoticeAddContent(builder, ct)
payload := proto.NoticeEnd(builder)
builder.Finish(payload)
t.Payload = builder.Bytes[builder.Head():]
// 填充协议头信息
t.Header.Proto = p
t.Header.Flag = 0xdcba
t.Header.Size = uint16(len(t.Payload))
ret, err := t.Pack()
if err != nil {
log.Fatal(err.Error())
return nil
}
return ret
}
示例2: clientHandleMessage
func clientHandleMessage(id uint32, b []byte) {
t := transport.TcpMessage{}
err := t.Unpack(b)
if err != nil {
log.Fatal(err.Error())
}
n := proto.GetRootAsNotice(t.Payload, 0)
m := t.Header
log.Printf("recv notice! %v", n.Content())
log.Printf("recv notice! %v", string(n.Content()))
log.Printf("recv notice! %v %v %v", m.Flag, m.Proto, m.Size)
if m.Proto == proto.TcpProtoIDFbUpdateServer {
str := `{"choose":"1","success":"1","objFail":[],"fail":"0","status":"3"}`
m := makeNoticeMsg(str, m.Proto)
c.Send(m)
}
if m.Proto == proto.TcpProtoIDFbSaveGrayAccount {
str := `{"choose":"1","success":"1","objFail":[],"fail":"0"}`
m := makeNoticeMsg(str, m.Proto)
c.Send(m)
} else if m.Proto == proto.TcpProtoIDFbGetGrayAccountById {
str := `{"id":1,"serverZoneId":"1","gameId":"1","serverId":"fb_server_1","platForm":"2","account":"2"}`
m := makeNoticeMsg(str, m.Proto)
c.Send(m)
} else if m.Proto == proto.TcpProtoIDFbGetAllGrayAccount {
str := `[{"account": "88888888","platForm": "qq"},{"account": "555","platForm": "3434"}]`
m := makeNoticeMsg(str, m.Proto)
c.Send(m)
} else if m.Proto == proto.TcpProtoIDFbDelGrayAccountById {
str := `{"message":"success"}`
m := makeNoticeMsg(str, m.Proto)
c.Send(m)
} else if m.Proto == proto.TcpProtoIDFbUpdateGrayAccount {
str := `{"choose":"1","success":"1","objFail":[],"fail":"0"}`
m := makeNoticeMsg(str, m.Proto)
c.Send(m)
} else if m.Proto == proto.TcpProtoIDFbGrayGetTotalByServerZoneIdAndGameId {
str := `{"num":1}`
m := makeNoticeMsg(str, m.Proto)
c.Send(m)
}
if m.Proto == proto.TcpProtoIDFbSavePlacard {
str := `{"choose":"1","success":"1","objFail":[],"fail":"0"}`
m := makeNoticeMsg(str, m.Proto)
c.Send(m)
} else if m.Proto == proto.TcpProtoIDFbPlacardGetTotalByServerZoneIdAndGameId {
str := `{"num":1}`
m := makeNoticeMsg(str, m.Proto)
c.Send(m)
} else if m.Proto == proto.TcpProtoIDFbGetAllPlacards {
str := `[{"id":1,"serverZoneId":"1","gameId":"1","serverId":"xyj_server_test","version":"1","contents":"1"}]`
m := makeNoticeMsg(str, m.Proto)
c.Send(m)
} else if m.Proto == proto.TcpProtoIDFbDelPlacardById {
str := `{"message":"success"}`
m := makeNoticeMsg(str, m.Proto)
c.Send(m)
} else if m.Proto == proto.TcpProtoIDFbUpdatePlacards {
str := `{"choose":"1","success":"1","objFail":[],"fail":"0"}`
m := makeNoticeMsg(str, m.Proto)
c.Send(m)
} else if m.Proto == proto.TcpProtoIDXyjGetPlacardById {
str := `{"id":1,"serverZoneId":"1","gameId":"1","serverId":"xyj_server_test","version":"1","contents":"我们来了"}`
m := makeNoticeMsg(str, m.Proto)
c.Send(m)
}
if m.Proto == proto.TcpProtoIDFbGetAllEmails {
str := `[{"id":1,"serverZoneId":"1","gameId":"1","serverId":"fb_server_1","sender":"1","title":"1","contents":"1","annex":[{"itemId":"1","itemNum":1},{"itemId":"1","itemNum":1},{"itemId":"112","itemNum":1123}] }]`
m := makeNoticeMsg(str, m.Proto)
c.Send(m)
} else if m.Proto == proto.TcpProtoIDFbAddEmail {
str := `{"choose":"1","success":"1","objFail":[],"fail":"0"}`
m := makeNoticeMsg(str, m.Proto)
c.Send(m)
} else if m.Proto == proto.TcpProtoIDFbUpdateEmail {
str := `{"choose":"1","success":"1","objFail":[],"fail":"0"}`
m := makeNoticeMsg(str, m.Proto)
c.Send(m)
} else if m.Proto == proto.TcpProtoIDFbDelEmailById {
str := `{"message":"success"}`
m := makeNoticeMsg(str, m.Proto)
c.Send(m)
} else if m.Proto == proto.TcpProtoIDFbGetEmailById {
str := `{"id":1,"serverZoneId":"1","gameId":"1","serverId":"fb_server_1","sender":"1","title":"1","contents":"1","annex":[{"itemId":"1","itemNum":1},{"itemId":"1","itemNum":1},{"itemId":"112","itemNum":11234444}] }`
m := makeNoticeMsg(str, m.Proto)
c.Send(m)
} else if m.Proto == proto.TcpProtoIDFbEmailGetTotalByServerZoneIdAndGameId {
str := `{"num":1}`
m := makeNoticeMsg(str, m.Proto)
c.Send(m)
}
if m.Proto == proto.TcpProtoIDFbGetAllGagAccount {
str := `[{"guid":"1173993848902","name":"player_2","account":"sk2","platForm":"win32","gagTime":"-1","gagStart":"1467874079","gagEnd":"-1"},{"guid":"1173992487619","name":"player_6","account":"xc2203","platForm":"win32","gagTime":"43200","gagStart":"1467874200","gagEnd":"1467917400"}]`
m := makeNoticeMsg(str, m.Proto)
c.Send(m)
//.........这里部分代码省略.........
示例3: handleMessage
func handleMessage(id uint32, b []byte) {
defer func() { //必须要先声明defer,否则不能捕获到panic异常
if err := recover(); err != nil {
fmt.Println("TcpProtoIDGmStatus 号协议,未知异常捕获")
fmt.Println(err) //这里的err其实就是panic传入的内容,"bug"
}
}()
log.Println("on message: ", b)
t := transport.TcpMessage{}
// 从缓冲区获取消息内容
err := t.Unpack(b)
if err != nil {
log.Fatal(err.Error())
}
m := t.Header
fmt.Println("m := t.Header -----------------------")
fmt.Println(m)
//唯一游戏服务器发送的消息,服务器状态路由 TcpProtoIDStatus
if m.Proto == proto.TcpProtoIDGmStatus {
// 从消息payload部分获取正文内容
s := proto.GetRootAsNotice(t.Payload, 0)
var jsonServer ServerInfoJson
//如果JSON中的字段在go的目标类型中不存在,json.Unmarshal()在解码时会丢弃该字段
if err := json.Unmarshal(s.Content(), &jsonServer); err == nil {
zoneIdCvt, _ := strconv.ParseInt(jsonServer.ServerZoneId, 10, 32)
gameIdCvt, _ := strconv.ParseInt(jsonServer.GameId, 10, 32)
zoneId := int(zoneIdCvt)
gameId := int(gameIdCvt)
if zoneId == 0 && gameId == 0 {
fmt.Println("TcpProtoIDGmStatus 号协议收到了非服务器状态的 json 字符串,return", zoneId, gameId)
return
}
// 新连接加入map
hql.ConnMap[jsonServer.ServerId] = a
hql.ConnMa[jsonServer.ServerId] = id
hql.ConnM[id] = jsonServer.ServerId
sip := strings.Split(a.RemoteAddr(id), ":")
fmt.Printf("-->运营大区:%s 渠道:%s 服务器:%s 游戏:%s ip:%s 端口:%s 状态:%s\n", zoneId, jsonServer.PlatForm, jsonServer.ServerId, gameId, sip[0], sip[1], jsonServer.Status)
hql.Insert_serverZone(db, zoneId, gameId)
hql.Insert_gameId(db, gameId)
for i := 0; i < len(jsonServer.PlatForm); i++ {
hql.Insert_all_platform(db, zoneId, gameId, jsonServer.PlatForm[i], jsonServer.ServerId, sip[0], sip[1])
}
hql.Select_all_server(db, zoneId, gameId, jsonServer.ServerId, sip[0], sip[1], jsonServer.Status)
str1, err1 := hql.GetEventJSON(db, zoneId, gameId)
if err1 != nil {
fmt.Printf(err1.Error())
}
var event []xyj.EventPrototype
err2 := json.Unmarshal([]byte(str1), &event)
if err2 != nil {
fmt.Printf(err2.Error())
}
fmt.Println("------------------------------------------------------------------------------------>活动初始化..分包发送开始 ", jsonServer.ServerId)
for i, key := range event {
str2, err3 := hql.GetEventDataJSON(db, key.Id)
if err3 != nil {
fmt.Printf(err3.Error())
}
jsonData, _ := json.Marshal(key)
e := `{"eventPrototype":[` + string(jsonData) + `],"eventDataPrototype":` + str2 + `}`
fmt.Println("-->活动初始化..分包发送第 ", i, " 条活动:", e)
a.Send(id, makeNoticeMsg(e, proto.TcpProtoIDGmStatus))
}
fmt.Println("------------------------------------------------------------------------------------>活动初始化..分包发送结束 ", jsonServer.ServerId)
} else {
fmt.Println("111111", err)
}
} else {
// 从消息payload部分获取正文内容
s := proto.GetRootAsNotice(t.Payload, 0)
// 1_2 {"choose":1,"success":1,"objFail":["我是返回来的消息"],"fail":1}
hql.ResponseMap[string(id)+"_"+string(m.Proto)] = string(s.Content())
fmt.Println("1111 " + string(s.Content()))
hql.Channel_c <- hql.ResponseMap
}
}
示例4: clientHandleMessage
func clientHandleMessage(id uint32, b []byte) {
t := transport.TcpMessage{}
err := t.Unpack(b)
if err != nil {
log.Fatal(err.Error())
}
n := proto.GetRootAsNotice(t.Payload, 0)
m := t.Header
log.Printf("recv notice! %v", n.Content())
log.Printf("recv notice! %v", string(n.Content()))
log.Printf("recv notice! %v %v %v", m.Flag, m.Proto, m.Size)
if m.Proto == proto.TcpProtoIDXyjUpdateServer {
str := `{"choose":"1","success":"1","objFail":[],"fail":"0","status":"3"}`
m := makeNoticeMsg(str, m.Proto)
c.Send(m)
}
if m.Proto == proto.TcpProtoIDXyjSaveGrayAccount {
str := `{"choose":"1","success":"1","objFail":[],"fail":"0"}`
m := makeNoticeMsg(str, m.Proto)
c.Send(m)
} else if m.Proto == proto.TcpProtoIDXyjGetGrayAccountById {
str := `{"id":1,"serverZoneId":"1","gameId":"1","serverId":"kds_server_1","platForm":"2","account":"2"}`
m := makeNoticeMsg(str, m.Proto)
c.Send(m)
} else if m.Proto == proto.TcpProtoIDXyjGetAllGrayAccount {
str := `[{"account": "88888888","platForm": "qq"},{"account": "555","platForm": "3434"}]`
m := makeNoticeMsg(str, m.Proto)
c.Send(m)
} else if m.Proto == proto.TcpProtoIDXyjDelGrayAccountById {
str := `{"message":"success"}`
m := makeNoticeMsg(str, m.Proto)
c.Send(m)
} else if m.Proto == proto.TcpProtoIDXyjUpdateGrayAccount {
str := `{"choose":"1","success":"1","objFail":[],"fail":"0"}`
m := makeNoticeMsg(str, m.Proto)
c.Send(m)
} else if m.Proto == proto.TcpProtoIDXyjGrayGetTotalByServerZoneIdAndGameId {
str := `{"num":1}`
m := makeNoticeMsg(str, m.Proto)
c.Send(m)
}
if m.Proto == proto.TcpProtoIDXyjSavePlacard {
str := `{"choose":"1","success":"1","objFail":[],"fail":"0"}`
m := makeNoticeMsg(str, m.Proto)
c.Send(m)
} else if m.Proto == proto.TcpProtoIDXyjPlacardGetTotalByServerZoneIdAndGameId {
str := `{"num":1}`
m := makeNoticeMsg(str, m.Proto)
c.Send(m)
} else if m.Proto == proto.TcpProtoIDXyjGetAllPlacards {
str := `[{"id":1,"serverZoneId":"1","gameId":"1","serverId":"xyj_server_test","version":"1","contents":"1"}]`
m := makeNoticeMsg(str, m.Proto)
c.Send(m)
} else if m.Proto == proto.TcpProtoIDXyjDelPlacardById {
str := `{"message":"success"}`
m := makeNoticeMsg(str, m.Proto)
c.Send(m)
} else if m.Proto == proto.TcpProtoIDXyjUpdatePlacards {
str := `{"choose":"1","success":"1","objFail":[],"fail":"0"}`
m := makeNoticeMsg(str, m.Proto)
c.Send(m)
} else if m.Proto == proto.TcpProtoIDXyjGetPlacardById {
str := `{"id":1,"serverZoneId":"1","gameId":"1","serverId":"xyj_server_test","version":"1","contents":"我们来了"}`
m := makeNoticeMsg(str, m.Proto)
c.Send(m)
}
if m.Proto == proto.TcpProtoIDXyjGetAllEmails {
str := `[{"id":1,"serverZoneId":"1","gameId":"1","serverId":"xyj_server_test","sender":"1","title":"1","contents":"1","annex":[{"itemId":"1","itemNum":1},{"itemId":"1","itemNum":1},{"itemId":"112","itemNum":1123}] }]`
m := makeNoticeMsg(str, m.Proto)
c.Send(m)
} else if m.Proto == proto.TcpProtoIDXyjAddEmail {
str := `{"choose":"1","success":"0","objFail":["我是一个测试"],"fail":"1"}`
m := makeNoticeMsg(str, m.Proto)
c.Send(m)
} else if m.Proto == proto.TcpProtoIDXyjUpdateEmail {
str := `{"choose":"1","success":"1","objFail":[],"fail":"0"}`
m := makeNoticeMsg(str, m.Proto)
c.Send(m)
} else if m.Proto == proto.TcpProtoIDXyjDelEmailById {
str := `{"message":"success"}`
m := makeNoticeMsg(str, m.Proto)
c.Send(m)
} else if m.Proto == proto.TcpProtoIDXyjGetEmailById {
str := `{"id":1,"serverZoneId":"1","gameId":"1","serverId":"xyj_server_1","sender":"1","title":"1","contents":"1","annex":[{"itemId":"1","itemNum":1},{"itemId":"1","itemNum":1},{"itemId":"112","itemNum":11234444}] }`
m := makeNoticeMsg(str, m.Proto)
c.Send(m)
} else if m.Proto == proto.TcpProtoIDXyjEmailGetTotalByServerZoneIdAndGameId {
str := `{"num":1}`
m := makeNoticeMsg(str, m.Proto)
c.Send(m)
}
if m.Proto == proto.TcpProtoIDXyjGetAllGagAccount {
str := `[{"guid":"1173993848902","name":"player_2","account":"sk2","platForm":"win32","gagTime":"-1","gagStart":"1467874079","gagEnd":"-1"},{"guid":"1173992487619","name":"player_6","account":"xc2203","platForm":"win32","gagTime":"43200","gagStart":"1467874200","gagEnd":"1467917400"}]`
m := makeNoticeMsg(str, m.Proto)
//.........这里部分代码省略.........