func ServerConnectGateCallback(conn *GxTcpConn, msg *GxMessage) error { //register server var req ServerConnectGateReq err := msg.UnpackagePbmsg(&req) if err != nil { SendPbMessage(conn, false, 0, msg.GetCmd(), msg.GetSeq(), RetFail, nil) return err } conn.Data = make([]interface{}, len(req.Cmds)) mutex.Lock() for i := 0; i < len(req.Cmds); i++ { //保存自己处理的消息cmd conn.Data[i] = req.Cmds[i] cmd := uint16(req.Cmds[i]) cmdinfo, ok := CmdServers[cmd] if !ok { CmdServers[cmd] = make(ServersInfo) cmdinfo, _ = CmdServers[cmd] } cmdinfo[req.GetId()] = conn } mutex.Unlock() SendPbMessage(conn, false, 0, msg.GetCmd(), msg.GetSeq(), RetSucc, nil) return nil }
func GetRoleListCallback(conn *GxTcpConn, info *LoginInfo, msg *GxMessage) { rdClient := PopRedisClient() defer PushRedisClient(rdClient) var req GetRoleListReq var rsp GetRoleListRsp err := msg.UnpackagePbmsg(&req) if err != nil { SendPbMessage(conn, false, msg.GetId(), msg.GetCmd(), msg.GetSeq(), RetFail, nil) return } if req.Info == nil || req.GetInfo().Token == nil || req.ServerId == nil { SendPbMessage(conn, false, msg.GetId(), msg.GetCmd(), msg.GetSeq(), RetMsgFormatError, nil) return } playerName := CheckToken(rdClient, req.GetInfo().GetToken()) if playerName == "" { SendPbMessage(conn, false, msg.GetId(), msg.GetCmd(), msg.GetSeq(), RetTokenError, nil) return } player := new(Player) err = player.Get(rdClient, playerName) if err != nil { SendPbMessage(conn, false, msg.GetId(), msg.GetCmd(), msg.GetSeq(), RetUserNotExists, nil) return } ids := GetRoleList(rdClient, playerName, req.GetServerId()) for i := 0; i < len(ids); i++ { id, _ := strconv.Atoi(ids[i]) role := new(Role) err = role.Get(rdClient, uint32(id)) if err != nil { Debug("role %d is not existst", id) continue } rsp.Roles = append(rsp.Roles, &RoleCommonInfo{ Id: proto.Uint32(role.Id), Name: proto.String(role.Name), Level: proto.Uint32(role.Level), VocationId: proto.Uint32(role.VocationId), }) } info.PlayerName = playerName info.BeginTs = time.Now().Unix() info.ServerId = req.GetServerId() SaveGateLoginInfo(rdClient, info.GateId, info.ConnId, playerName) info.Save(rdClient) SendPbMessage(conn, false, msg.GetId(), msg.GetCmd(), msg.GetSeq(), RetSucc, &rsp) }
func SelectRoleCallback(conn *GxTcpConn, info *LoginInfo, msg *GxMessage) { rdClient := PopRedisClient() defer PushRedisClient(rdClient) var req SelectRoleReq err := msg.UnpackagePbmsg(&req) if err != nil { SendPbMessage(conn, false, msg.GetId(), msg.GetCmd(), msg.GetSeq(), RetFail, nil) return } if req.RoleId == nil { SendPbMessage(conn, false, msg.GetId(), msg.GetCmd(), msg.GetSeq(), RetMsgFormatError, nil) return } if req.Info != nil && req.GetInfo().Token != nil { //重新重连 ret := DisconnLogin(rdClient, req.GetInfo().GetToken(), info) if ret != RetSucc { SendPbMessage(conn, false, msg.GetId(), msg.GetCmd(), msg.GetSeq(), ret, nil) return } } role := new(Role) err = role.Get(rdClient, req.GetRoleId()) if err != nil { Debug("role %d is not existst", req.GetRoleId()) SendPbMessage(conn, false, msg.GetId(), msg.GetCmd(), msg.GetSeq(), RetRoleNotExists, nil) return } info.RoleId = req.GetRoleId() info.Save(rdClient) SendPbMessage(conn, false, msg.GetId(), msg.GetCmd(), msg.GetSeq(), RetSucc, &SelectRoleRsp{ Role: &RoleCommonInfo{ Id: proto.Uint32(role.Id), Name: proto.String(role.Name), Level: proto.Uint32(role.Level), VocationId: proto.Uint32(role.VocationId), Expr: proto.Uint64(role.Expr), GodValue: proto.Uint64(role.GodValue), Prestige: proto.Uint64(role.Prestige), Gold: proto.Uint64(role.Gold), Crystal: proto.Uint64(role.Crystal), }, }) }
func ClientRawMessage(conn *GxTcpConn, msg *GxMessage) error { Debug("new client message, remote: %s %s", conn.Conn.RemoteAddr().String(), msg.String()) _, server := GetServerByCmd(msg.GetCmd()) if server == nil { Debug("msg is not register, remote: %s, msg: %s", conn.Conn.RemoteAddr().String(), msg.String()) return errors.New("close") } msg.SetId(conn.Id) server.Send(msg) return nil }
func (server *GxTcpServer) runConn(gxConn *GxTcpConn) { go gxConn.runHeartbeat() for { var msg *GxMessage = nil var err error empty := gxConn.GetProcessMsgNull() if empty { //没有消息正在处理,查找当前缓存的消息 msg = gxConn.GetUnprocessMsg() } if msg == nil { //如果没有缓存的消息,则读取新信息 msg, err = gxConn.Recv() if err != nil { server.closeConn(gxConn) return } if !empty { //如果有消息正在处理,缓存刚刚收到的消息 Debug("client has processing message, remote: %s, msg: %s", gxConn.Conn.RemoteAddr().String(), msg.String()) gxConn.SaveUnprocessMsg(msg) continue } } if msg.GetCmd() != CmdHeartbeat { Debug("recv buff msg, info: %s", msg.String()) } if cb, ok := server.clientCmds[msg.GetCmd()]; ok { //消息已经被注册 err = cb(gxConn, msg) if err != nil { //回调返回值不为空,则关闭连接 server.closeConn(gxConn) return } continue } //消息没有被注册 err = server.Rm(gxConn, msg) if err != nil { server.closeConn(gxConn) return } } }
func (conn *GxTcpConn) SaveProcessMsg(msg *GxMessage) { if !conn.MessageCtrl { return } conn.mutex.Lock() defer conn.mutex.Unlock() //服务端发送的通知seq为0 if msg.GetMask(MessageMaskNotify) { return } if conn.msg == nil { conn.msg = msg } else { if conn.msg.GetSeq() == msg.GetSeq() && conn.msg.GetCmd() == msg.GetCmd() { conn.msg = nil } } }
func CreateRoleCallback(conn *GxTcpConn, info *LoginInfo, msg *GxMessage) { rdClient := PopRedisClient() defer PushRedisClient(rdClient) var req CreateRoleReq err := msg.UnpackagePbmsg(&req) if err != nil { SendPbMessage(conn, false, msg.GetId(), msg.GetCmd(), msg.GetSeq(), RetFail, nil) return } if req.Name == nil || req.VocationId == nil { SendPbMessage(conn, false, msg.GetId(), msg.GetCmd(), msg.GetSeq(), RetMsgFormatError, nil) return } if req.Info != nil && req.GetInfo().Token != nil { //重新重连 ret := DisconnLogin(rdClient, req.GetInfo().GetToken(), info) if ret != RetSucc { SendPbMessage(conn, false, msg.GetId(), msg.GetCmd(), msg.GetSeq(), ret, nil) return } } ids := GetRoleList(rdClient, info.PlayerName, info.ServerId) if len(ids) > 0 { SendPbMessage(conn, false, msg.GetId(), msg.GetCmd(), msg.GetSeq(), RetRoleExists, nil) return } if CheckRoleNameConflict(rdClient, req.GetName()) { SendPbMessage(conn, false, msg.GetId(), msg.GetCmd(), msg.GetSeq(), RetRoleNameConflict, nil) return } role := &Role{ Id: NewRoleID(rdClient), PlayerName: info.PlayerName, GameServerId: info.ServerId, Name: req.GetName(), VocationId: req.GetVocationId(), Level: 0, Expr: 0, GodValue: 0, Prestige: 0, Gold: 10000, Crystal: 0, } role.Save(rdClient) SaveRoleName(rdClient, role.Name) // info.RoleId = role.Id info.Save(rdClient) // SendPbMessage(conn, false, msg.GetId(), msg.GetCmd(), msg.GetSeq(), RetSucc, &CreateRoleRsp{ Role: &RoleCommonInfo{ Id: proto.Uint32(role.Id), Name: proto.String(role.Name), Level: proto.Uint32(role.Level), VocationId: proto.Uint32(role.VocationId), Expr: proto.Uint64(role.Expr), GodValue: proto.Uint64(role.GodValue), Prestige: proto.Uint64(role.Prestige), Gold: proto.Uint64(role.Gold), Crystal: proto.Uint64(role.Crystal), }, }) }