func (self *Manager) handleMsgServerClient(msc *link.Session) { msc.ReadLoop(func(msg link.InBuffer) { glog.Info("msg_server", msc.Conn().RemoteAddr().String(), "say:", string(msg.Get())) self.parseProtocol(msg.Get(), msc) }) }
//其他客户端连接TransferServer处理 func connectTransferServer(session *link.Session, protoMsg systemProto.ProtoMsg) { rev_msg := protoMsg.Body.(*systemProto.System_ConnectTransferServerC2S) serverName := rev_msg.GetServerName() INFO(serverName + " Connect TransferServer") useServerName := strings.Split(serverName, "[")[0] sessions, exists := servers[useServerName] if !exists { sessions = make([]*link.Session, 0, 10) } sessions = append(sessions, session) servers[useServerName] = sessions //GameServer可以有多个 if useServerName == "GameServer" { addr := strings.Split(session.Conn().RemoteAddr().String(), ":") addrIp := addr[0] addrPort, _ := strconv.Atoi(addr[1]) gameConsistent.Add(hashs.NewNode(len(sessions), addrIp, addrPort, serverName, 1)) } send_msg := systemProto.MarshalProtoMsg(&systemProto.System_ConnectTransferServerS2C{}) systemProto.Send(send_msg, session) startDealReceiveMsgC2S(session) }
///============================ ///消费者 获取图片 func getFile(session *link.Session, req map[string]string) error { seq := req["seq"] //死等 vf := QueueInstance.DeChan() if vf == nil { ULogger.Error("getfile time out,sessioninfo is %s", session.State.(*User).Id) ret := map[string]string{ "action": "res_getfile", "seq": seq, } by, _ := json.Marshal(ret) session.Send(link.Bytes(by)) ULogger.Info("send to client", session.Conn().RemoteAddr().String(), "say:", string(by)) return nil } vf.C = session vf.CInfo = session.State.(*User).Id vf.Status = 2 vf.CGetUnix = time.Now().Unix() ret := map[string]string{ "action": "res_getfile", "seq": seq, "id": vf.Id, "file": vf.File, } by, _ := json.Marshal(ret) VFMapInstance.Update("c_getfile", vf) session.Send(link.Bytes(by)) ULogger.Info("send to client", session.Conn().RemoteAddr().String(), "say:", string(by)) return nil }
func (self *Router) handleMsgServerClient(msc *link.Session) { msc.ReadLoop(func(msg link.InBuffer) { glog.Info("msg_server", msc.Conn().RemoteAddr().String(), " say: ", string(msg.Get())) var c protocol.CmdInternal pp := NewProtoProc(self) err := json.Unmarshal(msg.Get(), &c) if err != nil { glog.Error("error:", err) } switch c.GetCmdName() { case protocol.SEND_MESSAGE_P2P_CMD: err := pp.procSendMsgP2P(c, msc) if err != nil { glog.Warning(err.Error()) } case protocol.CREATE_TOPIC_CMD: err := pp.procCreateTopic(c, msc) if err != nil { glog.Warning(err.Error()) } case protocol.JOIN_TOPIC_CMD: err := pp.procJoinTopic(c, msc) if err != nil { glog.Warning(err.Error()) } case protocol.SEND_MESSAGE_TOPIC_CMD: err := pp.procSendMsgTopic(c, msc) if err != nil { glog.Warning(err.Error()) } } }) }
//LoginServer用户上线 func SetClientSessionOnline(userSession *link.Session) { //发送用户上线消息到serverName protoMsg := &systemProto.System_ClientSessionOnlineC2S{ SessionID: protos.Uint64(userSession.Id()), Network: protos.String(userSession.Conn().RemoteAddr().Network()), Addr: protos.String(userSession.Conn().RemoteAddr().String()), } send_msg := protos.MarshalProtoMsg(protoMsg) sendSystemMsg2("LoginServer", 0, send_msg) }
//通知GameServer用户登录成功 func SetClientLoginSuccess(userName string, userID uint64, session *link.Session) { send_msg := protos.MarshalProtoMsg(&systemProto.System_ClientLoginSuccessC2S{ UserID: protos.Uint64(userID), UserName: protos.String(userName), SessionID: protos.Uint64(session.Id()), GameServerID: protos.Uint32(0), Network: protos.String(session.Conn().RemoteAddr().Network()), Addr: protos.String(session.Conn().RemoteAddr().String()), }) sendSystemMsgToServer(send_msg) }
//其他客户端连接TransferServer处理 func connectTransferServer(session *link.Session, protoMsg protos.ProtoMsg) { rev_msg := protoMsg.Body.(*systemProto.System_ConnectTransferServerC2S) serverName := rev_msg.GetServerName() serverID := rev_msg.GetServerID() useServerName := strings.Split(serverName, "[")[0] serverList, exists := servers[useServerName] if !exists { serverList = make([]Server, 0, 10) } server := Server{ session: session, serverID: serverID, serverIndex: len(serverList), } serverList = append(serverList, server) servers[useServerName] = serverList //服务器断开连接处理 session.AddCloseCallback(session, func() { serverList = append(serverList[:server.serverIndex], serverList[server.serverIndex+1:]...) servers[useServerName] = serverList ERR(serverName + " Disconnect At " + global.ServerName) }) //GameServer可以有多个 if useServerName == "GameServer" { addr := strings.Split(session.Conn().RemoteAddr().String(), ":") addrIp := addr[0] addrPort, _ := strconv.Atoi(addr[1]) gameNode := hashs.NewNode(server.serverIndex, addrIp, addrPort, serverName, 1) gameConsistent.Add(gameNode) //GameServer断开连接处理 session.AddCloseCallback(session, func() { //移除此Node gameConsistent.Remove(gameNode) //将此Node的所有用户断开连接 for clientSessionID, gameNodeIndex := range gameUserSessions { if server.serverIndex == gameNodeIndex { clientSession := global.GetSession(clientSessionID) if clientSession != nil { clientSession.Close() } } } }) } //发送连接成功消息 send_msg := protos.MarshalProtoMsg(&systemProto.System_ConnectTransferServerS2C{}) protos.Send(session, send_msg) }
func (flink *frontendLink) AddClient(session *link.Session) { flink.clientMutex.Lock() defer flink.clientMutex.Unlock() flink.clientWaitId += 1 flink.clientWaits[flink.clientWaitId] = session addr := session.Conn().RemoteAddr() flink.session.Send(&gatewayMsg{ Command: CMD_NEW_1, ClientId: flink.clientWaitId, Data: []byte(addr.Network() + "," + addr.String()), }) }
///====================================== ///生产者 ,产生图验图片 func putFile(session *link.Session, req map[string]string) error { file := req["file"] seq := req["seq"] fileid := GetMd5String(file) vf := &VerifyObj{Id: getId(), P: session, C: nil, FileId: fileid, File: file, Status: 1, Result: "0", Seq: seq, PPutUnix: time.Now().Unix()} QueueInstance.Enqueue(vf) VFMapInstance.Put(vf) ULogger.Infof("putfile 进队列 %v\n", vf) //记录p端的操作 if session.State == nil { userId := session.Conn().RemoteAddr().String() user := &User{UserType: "P", Id: userId, WorkTime: time.Now().Format("2006-01-02 15:04:05")} session.State = user Exec(`insert into user_activities(user_id,active_time,active_type,user_type,other_info) values(?,now(),'begin','production',?)`, userId, userId) } return nil }
func Process(session *link.Session, req map[string]string) error { command, ok := req["action"] if !ok { ULogger.Error("client", session.Conn().RemoteAddr().String(), "bad request ,not found action") session.Close() return nil } if (command == "getfile" || command == "answer") && session.State == nil { ULogger.Error("client", session.Conn().RemoteAddr().String(), "c must login frist") session.Close() return nil } switch command { //p case "putfile": return putFile(session, req) case "reportanswer": return reportAnswer(session, req) //c case "getfile": return getFile(session, req) case "cstart": return cStart(session, req) case "answer": return answer(session, req) default: ULogger.Error("client", session.Conn().RemoteAddr().String(), "not support command") session.Close() //ULogger.Info("sssss") } return nil }
///c端开始答题 func cStart(session *link.Session, req map[string]string) error { userid := req["userid"] password := req["password"] seq := req["seq"] ULogger.Infof("user %s start answer", userid) ret := map[string]string{ "action": "res_cstart", "seq": seq, "result": "0", } if session.State != nil && session.State.(*User).UserType == "C" { ULogger.Error("have logined ", userid) session.Close() } if b := Login(userid, password); !b { ULogger.Errorf("cstart failed ,userid is %s password is %s", userid, password) by, _ := json.Marshal(ret) session.Send(link.Bytes(by)) ULogger.Info("send to client", session.Conn().RemoteAddr().String(), "say:", string(by)) session.Close() return nil } else { user := &User{UserType: "C", Id: userid, WorkTime: time.Now().Format("2006-01-02 15:04:05")} session.State = user ret["result"] = "1" by, _ := json.Marshal(ret) session.Send(link.Bytes(by)) ULogger.Info("send to client", session.Conn().RemoteAddr().String(), "say:", string(by)) //c端开始答题 Exec(`insert into user_activities(user_id,active_time,active_type,user_type,other_info) values(?,now(),'begin','customer',?)`, userid, session.Conn().RemoteAddr().String()) VFMapInstance.AddSession(session) } return nil }