func (this *ParentHandler) handleAllocServer(packet *structure.Packet) { //ALLOC_SERVER_RESPONSE = 0x0a packet.Number = define.ALLOC_SERVER_RESPONSE packet.Data = make([]byte, 6) //PushServerPort:9005 binary.BigEndian.PutUint16(packet.Data[4:6], this.PushServerPort) //返回一个个字段都为空值的STree stree := GetSTree() if pConn, ok := stree.UserMap.UserMap[packet.Uuid]; ok { ip := net.ParseIP(strings.Split((*pConn).RemoteAddr().String(), ":")[0]).To4() copy(packet.Data[0:4], ip) } else { conn := stree.GetMinLoadConn() ip := net.ParseIP(strings.Split((*conn).RemoteAddr().String(), ":")[0]).To4() copy(packet.Data[0:4], ip) } this.SendToParent(packet) }
func (this *STree) SendHeartBeat() { for { packet := structure.Packet{} packet.Number = define.HEART_BEAT packet.Uuid = define.P_UID packet.DataLen = 12 packet.Data = make([]byte, packet.DataLen) this.SelfInfo.Lock.RLock() userNumber := this.SelfInfo.UserNumber maxConn := this.SelfInfo.MaxConnNumber currConn := this.SelfInfo.CurrentConnNumber this.SelfInfo.Lock.RUnlock() binary.BigEndian.PutUint32(packet.Data[0:4], uint32(userNumber)) binary.BigEndian.PutUint32(packet.Data[4:8], uint32(maxConn)) binary.BigEndian.PutUint32(packet.Data[8:12], uint32(currConn)) GetParentHandler().SendToParent(&packet) time.Sleep(time.Duration(this.Seconds * 1000 * 1000 * 1000)) } }
func (this *ClientHandler) sendAllocReq(conn *net.Conn) (*structure.Packet, error) { buf := make([]byte, 19) _, err := (*conn).Read(buf) if err != nil { err = errors.New("component: sendAllocReq() failed, can not read a log in message from conn") fmt.Println(err.Error()) (*conn).Close() return nil, err } req := structure.Packet{} err = req.Decode(buf) if err != nil { err = errors.New("component: sendAllReq() failed, can not get message from received buf") fmt.Println(err.Error()) (*conn).Close() return nil, err } req.Number = define.ALLOC_SERVER req.DataLen = 0 req.Data = nil GetDaughterHandler().PushToDaughter(&req) return &req, nil }