func (user *ChatUser) sendMsgReq_Handle(req *chat.SendmsgReq) { if !user.verify() { user.Close("not login") return } fmt.Println("user:"******" msg:", req.GetMsg()) if !user.userMgr.SendMsg(user.getAcc(), req.GetMsg()) { //发送数据失败 user.Close("SendMsg Fail") return } protoRsp := &chat.SendmsgRsp{ State: proto.Int32(0), } rspData, err := proto.Marshal(protoRsp) util.CheckError(err, "proto.Marshal SendmsgRsp: ") packetData := user.encodePacket(rspData, cmd_sendmsg_rsp) user.sendPacket(packetData) }
func (user *ChatUser) handle_exit_notify(data *notifyExitData) { protoRsp := &chat.ExitNotify{ Acc: proto.String(data.acc), } rspData, err := proto.Marshal(protoRsp) //编码失败了 util.CheckError(err, "proto.Marshal ExitNotify: ") packetData := user.encodePacket(rspData, cmd_exit_notify) user.sendPacket(packetData) }
func (user *ChatUser) handle_sendmsg_notify(data *notifySendMsgData) { protoRsp := &chat.MsgPush{ SourceAcc: proto.String(data.acc), Msg: proto.String(data.msg), } rspData, err := proto.Marshal(protoRsp) //编码失败了 util.CheckError(err, "proto.Marshal MsgPush: ") packetData := user.encodePacket(rspData, cmd_msg_push) user.sendPacket(packetData) }
func (cp ChatPacket) encodePacket(rspData []byte, cmd uint32) []byte { chatPacket := &chat.ChatPacket{ Cmd: proto.Uint32(cmd), Data: rspData, } chatPacketData, err := proto.Marshal(chatPacket) util.CheckError(err, "proto.Marshal ChatPacket: ") var packetDataLen uint16 = uint16(len(chatPacketData)) log.Println("packetDataLen:", packetDataLen) //分配发送缓冲区(head(2) + Data长度) var packetData []byte = make([]byte, 2, packetDataLen+2) //设置包头的值 cp.writePacketLen(packetData, packetDataLen) //拷贝数据到缓冲区 packetData = append(packetData, chatPacketData...) return packetData }
func (user *ChatUser) loginReq_Handle(req *chat.LoginReq) { fmt.Println("user:"******" pwd:", req.GetPwd()) var rsp *chat.LoginRsp if !user.verify() { fmt.Println("setVerify") selfAcc := req.GetAcc() joinState, userAarray := user.userMgr.Join(selfAcc, user) if !joinState { //进入失败了,关闭连接 user.Close("join fail") return } user.setAcc(selfAcc) rsp = &chat.LoginRsp{ State: proto.Int32(0), AccArray: userAarray, } } else { rsp = &chat.LoginRsp{ State: proto.Int32(1), } } rspData, err := proto.Marshal(rsp) //编码失败了 util.CheckError(err, "proto.Marshal LoginRsp: ") packetData := user.encodePacket(rspData, cmd_login_rsp) user.sendPacket(packetData) }