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 }
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) } else if m.Proto == proto.TcpProtoIDFbAddGagAccount { str := `{"message":"success"}` m := makeNoticeMsg(str, m.Proto) c.Send(m) } else if m.Proto == proto.TcpProtoIDFbUpdateGagAccount { str := `{"message":"success"}` m := makeNoticeMsg(str, m.Proto) c.Send(m) } else if m.Proto == proto.TcpProtoIDFbDelGagAccountById { str := `{"message":"success"}` m := makeNoticeMsg(str, m.Proto) c.Send(m) } else if m.Proto == proto.TcpProtoIDFbGagGetTotalByServerZoneIdAndGameId { str := `{"num":1}` m := makeNoticeMsg(str, m.Proto) c.Send(m) } if m.Proto == proto.TcpProtoIDFbGetAllSealAccount { str := `[{"id":1,"serverZoneId":"1","gameId":"1","serverId":"fb_server_1","guid":"111","name":"adc","account":"1","platForm":"1","sealTime":"1234","sealStart":"2014-12-11 16:55:15","sealEnd":"2014-12-11 16:55:15"}]` m := makeNoticeMsg(str, m.Proto) c.Send(m) } else if m.Proto == proto.TcpProtoIDFbAddSealAccount { str := `{"message":"success"}` m := makeNoticeMsg(str, m.Proto) c.Send(m) } else if m.Proto == proto.TcpProtoIDFbUpdateSealAccount { str := `{"message":"success"}` m := makeNoticeMsg(str, m.Proto) c.Send(m) } else if m.Proto == proto.TcpProtoIDFbDelSealAccount { str := `{"message":"success"}` m := makeNoticeMsg(str, m.Proto) c.Send(m) } else if m.Proto == proto.TcpProtoIDFbSealGetTotalByServerZoneIdAndGameId { str := `{"num":1}` m := makeNoticeMsg(str, m.Proto) c.Send(m) } if m.Proto == proto.TcpProtoIDFbGetAllProducts { str := `[{"id":1,"serverZoneId":"1","gameId":"1","serverId":"fb_server_1","itemId":"1","num":"1","prodcutStoreId":"1","storeLocation":"1","isRandom":"1","randomProbability":"1","comsumeType":"1","comsumeNum":"1","discount":"1","levelLimit":"1","levelCap":"1","discountStartDate":"2014-12-11 16:55:15","discountContinueDate":"2014-12-11 16:55:15","discountCycleDate":"2014-12-11 16:55:15","productPostDate":"2014-12-11 16:55:15","productDownDate":"2014-12-11 16:55:15","showLevel":"1"}]` m := makeNoticeMsg(str, m.Proto) c.Send(m) } else if m.Proto == proto.TcpProtoIDFbAddProduct { str := `{"choose":1,"success":1,"objFail":[],"fail":"0"}` m := makeNoticeMsg(str, m.Proto) c.Send(m) } else if m.Proto == proto.TcpProtoIDFbUpdateProduct { str := `{"choose":"1","success":"1","objFail":[],"fail":"0"}` m := makeNoticeMsg(str, m.Proto) c.Send(m) } else if m.Proto == proto.TcpProtoIDFbDelProductById { str := `{"message":"success"}` m := makeNoticeMsg(str, m.Proto) c.Send(m) } else if m.Proto == proto.TcpProtoIDFbProductGetTotalByServerZoneIdAndGameId { str := `{"num":1}` m := makeNoticeMsg(str, m.Proto) c.Send(m) } }
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 } }
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) c.Send(m) } else if m.Proto == proto.TcpProtoIDXyjAddGagAccount { str := `{"message":"success"}` m := makeNoticeMsg(str, m.Proto) c.Send(m) } else if m.Proto == proto.TcpProtoIDXyjUpdateGagAccount { str := `{"message":"success"}` m := makeNoticeMsg(str, m.Proto) c.Send(m) } else if m.Proto == proto.TcpProtoIDXyjDelGagAccountById { str := `{"message":"success"}` m := makeNoticeMsg(str, m.Proto) c.Send(m) } else if m.Proto == proto.TcpProtoIDXyjGagGetTotalByServerZoneIdAndGameId { str := `{"num":1}` m := makeNoticeMsg(str, m.Proto) c.Send(m) } if m.Proto == proto.TcpProtoIDXyjGetAllSealAccount { str := `[{"guid":"111","name":"adc","account":"1","platForm":"1","sealTime":"1234","sealStart":"1467871730","sealEnd":"1467871730"},{"guid":"111","name":"adc","account":"1","platForm":"1","sealTime":"1234","sealStart":"1467871730","sealEnd":"1467871730"},{"guid":"111","name":"adc","account":"1","platForm":"1","sealTime":"1234","sealStart":"1467871730","sealEnd":"1467871730"}]` m := makeNoticeMsg(str, m.Proto) c.Send(m) } else if m.Proto == proto.TcpProtoIDXyjAddSealAccount { str := `{"message":"success"}` m := makeNoticeMsg(str, m.Proto) c.Send(m) } else if m.Proto == proto.TcpProtoIDXyjUpdateSealAccount { str := `{"message":"success"}` m := makeNoticeMsg(str, m.Proto) c.Send(m) } else if m.Proto == proto.TcpProtoIDXyjDelSealAccount { str := `{"message":"success"}` m := makeNoticeMsg(str, m.Proto) c.Send(m) } else if m.Proto == proto.TcpProtoIDXyjSealGetTotalByServerZoneIdAndGameId { str := `{"num":3}` m := makeNoticeMsg(str, m.Proto) c.Send(m) } if m.Proto == proto.TcpProtoIDXyjAddEventPrototype { // 从消息payload部分获取正文内容 s := proto.GetRootAsNotice(t.Payload, 0) fmt.Println(" 测试1111 ", string(s.Content())) str := `{"choose":"1","success":"1","objFail":[],"fail":"0"}` m := makeNoticeMsg(str, m.Proto) c.Send(m) } else if m.Proto == proto.TcpProtoIDXyjUpdateEventPrototype { // 从消息payload部分获取正文内容 s := proto.GetRootAsNotice(t.Payload, 0) fmt.Println(" 测试2222 ", string(s.Content())) str := `{"choose":"1","success":"1","objFail":[],"fail":"0"}` m := makeNoticeMsg(str, m.Proto) c.Send(m) } else if m.Proto == proto.TcpProtoIDXyjAddEventDataPrototype { // 从消息payload部分获取正文内容 s := proto.GetRootAsNotice(t.Payload, 0) fmt.Println(" 测试3333 ", string(s.Content())) str := `{"choose":"1","success":"1","objFail":[],"fail":"0"}` m := makeNoticeMsg(str, m.Proto) c.Send(m) } else if m.Proto == proto.TcpProtoIDXyjUpdateEventDataPrototype { // 从消息payload部分获取正文内容 s := proto.GetRootAsNotice(t.Payload, 0) fmt.Println(" 测试4444 ", string(s.Content())) str := `{"choose":"1","success":"1","objFail":[],"fail":"0"}` m := makeNoticeMsg(str, m.Proto) c.Send(m) } else if m.Proto == proto.TcpProtoIDXyjCloseEventPrototype { // 从消息payload部分获取正文内容 s := proto.GetRootAsNotice(t.Payload, 0) fmt.Println(" 测试5555 ", string(s.Content())) str := `{"message":"success"}` m := makeNoticeMsg(str, m.Proto) c.Send(m) } if m.Proto == proto.TcpProtoIDGmStatus { //获取 gomiddle 返回的活动列表 // 从消息payload部分获取正文内容 s := proto.GetRootAsNotice(t.Payload, 0) fmt.Println(" 测试 ", string(s.Content())) } }