pb正反序列化替换新的方法
This commit is contained in:
parent
4da4c572e5
commit
c77f36ca3a
@ -41,7 +41,7 @@ func (r *Robot) FriendList() {
|
||||
|
||||
func (r *Robot) handleFriendList(msg *pb.UserMessage) {
|
||||
rsp := &pb.Friend_List_Rsp{}
|
||||
if !comm.ProtoDecode(msg, rsp) {
|
||||
if !comm.ProtoUnmarshal(msg, rsp) {
|
||||
return
|
||||
}
|
||||
printReply(msg, rsp)
|
||||
@ -62,7 +62,7 @@ func (r *Robot) FriendSearch(nickName string) {
|
||||
|
||||
func (r *Robot) handleFriendSearch(msg *pb.UserMessage) {
|
||||
rsp := &pb.Friend_Search_Rsp{}
|
||||
if !comm.ProtoDecode(msg, rsp) {
|
||||
if !comm.ProtoUnmarshal(msg, rsp) {
|
||||
return
|
||||
}
|
||||
printReply(msg, rsp)
|
||||
@ -83,7 +83,7 @@ func (r *Robot) FriendApply(friendId string) {
|
||||
|
||||
func (r *Robot) handleFriendApply(msg *pb.UserMessage) {
|
||||
rsp := &pb.Friend_Apply_Rsp{}
|
||||
if !comm.ProtoDecode(msg, rsp) {
|
||||
if !comm.ProtoUnmarshal(msg, rsp) {
|
||||
return
|
||||
}
|
||||
printReply(msg, rsp)
|
||||
@ -102,7 +102,7 @@ func (r *Robot) FriendApplyList() {
|
||||
|
||||
func (r *Robot) handleFriendApplyList(msg *pb.UserMessage) {
|
||||
rsp := &pb.Friend_ApplyList_Rsp{}
|
||||
if !comm.ProtoDecode(msg, rsp) {
|
||||
if !comm.ProtoUnmarshal(msg, rsp) {
|
||||
return
|
||||
}
|
||||
printReply(msg, rsp)
|
||||
@ -123,7 +123,7 @@ func (r *Robot) FriendAgree(friendIds []string) {
|
||||
|
||||
func (r *Robot) handleFriendAgree(msg *pb.UserMessage) {
|
||||
rsp := &pb.Friend_Agree_Rsp{}
|
||||
if !comm.ProtoDecode(msg, rsp) {
|
||||
if !comm.ProtoUnmarshal(msg, rsp) {
|
||||
return
|
||||
}
|
||||
printReply(msg, rsp)
|
||||
@ -144,7 +144,7 @@ func (r *Robot) FriendRefuse(friendIds []string) {
|
||||
|
||||
func (r *Robot) handleFriendRefuse(msg *pb.UserMessage) {
|
||||
rsp := &pb.Friend_Refuse_Rsp{}
|
||||
if !comm.ProtoDecode(msg, rsp) {
|
||||
if !comm.ProtoUnmarshal(msg, rsp) {
|
||||
return
|
||||
}
|
||||
printReply(msg, rsp)
|
||||
@ -163,7 +163,7 @@ func (r *Robot) FriendBlacklist() {
|
||||
|
||||
func (r *Robot) handleFriendBlacklist(msg *pb.UserMessage) {
|
||||
rsp := &pb.Friend_BlackList_Rsp{}
|
||||
if !comm.ProtoDecode(msg, rsp) {
|
||||
if !comm.ProtoUnmarshal(msg, rsp) {
|
||||
return
|
||||
}
|
||||
printReply(msg, rsp)
|
||||
@ -182,7 +182,7 @@ func (r *Robot) FriendAddBlack() {
|
||||
|
||||
func (r *Robot) handleFriendAddBlack(msg *pb.UserMessage) {
|
||||
rsp := &pb.Friend_BlackAdd_Rsp{}
|
||||
if !comm.ProtoDecode(msg, rsp) {
|
||||
if !comm.ProtoUnmarshal(msg, rsp) {
|
||||
return
|
||||
}
|
||||
printReply(msg, rsp)
|
||||
@ -203,7 +203,7 @@ func (r *Robot) FriendDelBlack(friendId string) {
|
||||
|
||||
func (r *Robot) handleFriendDelBlack(msg *pb.UserMessage) {
|
||||
rsp := &pb.Friend_DelBlack_Rsp{}
|
||||
if !comm.ProtoDecode(msg, rsp) {
|
||||
if !comm.ProtoUnmarshal(msg, rsp) {
|
||||
return
|
||||
}
|
||||
printReply(msg, rsp)
|
||||
|
@ -26,7 +26,7 @@ func (r *Robot) QueryUserPack() {
|
||||
|
||||
func (r *Robot) handleQueryUserPack(msg *pb.UserMessage) {
|
||||
rsp := &pb.Pack_Getlist_Resp{}
|
||||
if !comm.ProtoDecode(msg, rsp) {
|
||||
if !comm.ProtoUnmarshal(msg, rsp) {
|
||||
return
|
||||
}
|
||||
printReply(msg, rsp)
|
||||
|
@ -10,9 +10,9 @@ import (
|
||||
"log"
|
||||
"net/http"
|
||||
|
||||
"github.com/golang/protobuf/proto"
|
||||
"github.com/gorilla/websocket"
|
||||
jsoniter "github.com/json-iterator/go"
|
||||
"google.golang.org/protobuf/proto"
|
||||
)
|
||||
|
||||
type Robot struct {
|
||||
@ -88,7 +88,7 @@ func (r *Robot) onUserLoaded() {
|
||||
//friend
|
||||
// r.FriendApply("1_62aa8f30d25fb8c1a7d90b50")
|
||||
// r.FriendAgree([]string{"0_62a9afd994fe03b7aaee6773"})
|
||||
r.FriendRefuse([]string{"0_62aa9427e2979698b080ec78"})
|
||||
// r.FriendRefuse([]string{"0_62aa9427e2979698b080ec78"})
|
||||
// r.FriendApplyList()
|
||||
// r.FriendList()
|
||||
// r.FriendBlacklist()
|
||||
@ -102,7 +102,7 @@ func (r *Robot) onUserLoaded() {
|
||||
}
|
||||
|
||||
func (r *Robot) SendToClient(msg *pb.UserMessage, rsp proto.Message) error {
|
||||
if comm.ProtoEncode(rsp, msg) {
|
||||
if comm.ProtoMarshal(rsp, msg) {
|
||||
data, _ := proto.Marshal(msg)
|
||||
return r.ws.WriteMessage(websocket.BinaryMessage, data)
|
||||
}
|
||||
|
@ -20,7 +20,7 @@ func (r *Robot) handleUserMsg(msg *pb.UserMessage) {
|
||||
//处理登录响应数据
|
||||
func (r *Robot) handleLogin(msg *pb.UserMessage) {
|
||||
rsp := &pb.UserLoginResp{}
|
||||
if !comm.ProtoDecode(msg, rsp) {
|
||||
if !comm.ProtoUnmarshal(msg, rsp) {
|
||||
return
|
||||
}
|
||||
printReply(msg, rsp)
|
||||
@ -54,7 +54,7 @@ func (r *Robot) CreateUser(NickName string) {
|
||||
|
||||
func (r *Robot) handleCreateUser(msg *pb.UserMessage) {
|
||||
rsp := &pb.UserCreateRsp{}
|
||||
if !comm.ProtoDecode(msg, rsp) {
|
||||
if !comm.ProtoUnmarshal(msg, rsp) {
|
||||
return
|
||||
}
|
||||
printReply(msg, rsp)
|
||||
|
22
comm/core.go
22
comm/core.go
@ -8,8 +8,9 @@ import (
|
||||
"go_dreamfactory/lego/core"
|
||||
"go_dreamfactory/lego/sys/log"
|
||||
|
||||
"github.com/golang/protobuf/proto"
|
||||
"github.com/golang/protobuf/ptypes"
|
||||
|
||||
"google.golang.org/protobuf/proto"
|
||||
"google.golang.org/protobuf/types/known/anypb"
|
||||
)
|
||||
|
||||
const (
|
||||
@ -93,21 +94,24 @@ type IUserSession interface {
|
||||
ToString() string
|
||||
}
|
||||
|
||||
func ProtoDecode(msg *pb.UserMessage, req proto.Message) (ok bool) {
|
||||
err := ptypes.UnmarshalAny(msg.Data, req)
|
||||
//对protobuf格式的数据进行反序列化
|
||||
func ProtoUnmarshal(msg *pb.UserMessage, req proto.Message) (ok bool) {
|
||||
any := msg.Data
|
||||
err := any.UnmarshalTo(req)
|
||||
if err != nil {
|
||||
log.Errorf("%s.%s %v", msg.MainType, msg.SubType, err)
|
||||
log.Errorf("UnmarshalTo %s.%s %v", msg.MainType, msg.SubType, err)
|
||||
return
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
func ProtoEncode(rsp proto.Message, msg *pb.UserMessage) (ok bool) {
|
||||
data, err := ptypes.MarshalAny(rsp)
|
||||
//对protobuf格式的数据进行序列化
|
||||
func ProtoMarshal(rsp proto.Message, msg *pb.UserMessage) (ok bool) {
|
||||
any, err := anypb.New(rsp)
|
||||
if err != nil {
|
||||
log.Errorf("%s.%s %v", msg.MainType, msg.SubType, err)
|
||||
log.Errorf("Any New %s.%s %v", msg.MainType, msg.SubType, err)
|
||||
return
|
||||
}
|
||||
msg.Data = data
|
||||
msg.Data = any
|
||||
return true
|
||||
}
|
||||
|
@ -8,8 +8,8 @@ import (
|
||||
"go_dreamfactory/lego/base"
|
||||
"go_dreamfactory/lego/sys/log"
|
||||
|
||||
"github.com/golang/protobuf/proto"
|
||||
"github.com/golang/protobuf/ptypes"
|
||||
"google.golang.org/protobuf/proto"
|
||||
"google.golang.org/protobuf/types/known/anypb"
|
||||
)
|
||||
|
||||
/*
|
||||
@ -88,7 +88,7 @@ func (this *UserSession) UnBind() (err error) {
|
||||
//向用户发送消息
|
||||
func (this *UserSession) SendMsg(mainType, subType string, msg proto.Message) (err error) {
|
||||
reply := &pb.RPCMessageReply{}
|
||||
data, _ := ptypes.MarshalAny(msg)
|
||||
data, _ := anypb.New(msg)
|
||||
log.Debugf("SendMsg to SessionId:[%s] UserId:[%s] Data: %v", this.UserId, msg)
|
||||
if err := this.service.RpcCall(context.Background(), fmt.Sprintf("%s/%s", Service_Gateway, this.GatewayServiceId), string(Rpc_GatewayAgentSendMsg), &pb.AgentSendMessageReq{
|
||||
UserSessionId: this.SessionId,
|
||||
|
Loading…
Reference in New Issue
Block a user