本文整理匯總了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)
//.........這裏部分代碼省略.........