Example #1
0
func (c *Client) BeginLoop() *Client {
	buf := make([]byte, 4096)
	for {
		ByteRead, err := c.Conn.Read(buf)
		fmt.Println("ByteRead", ByteRead)
		if err != nil {
			log.Fatal("xx")
		}

		var size uint32
		var protoType monica.ProtoCode

		bufReader := bytes.NewReader(buf[:4])
		binary.Read(bufReader, binary.LittleEndian, &size)

		abufReader := bytes.NewReader(buf[4:8])
		binary.Read(abufReader, binary.LittleEndian, &protoType)

		switch protoType {
		case monica.ProtoPing:
			pingProto := new(common.Ping)
			proto.Unmarshal(buf[8:ByteRead], pingProto)
			fmt.Println("size", size)
			fmt.Println("protoType", protoType)
			fmt.Println("timeStamp", pingProto.GetTimestamp())
		case monica.ProtoBuildingsGds:
			gds := new(gds.BuildingGds)
			proto.Unmarshal(buf[8:ByteRead], gds)
			log.Println(len(gds.GetBuildings()))
			for _, building := range gds.GetBuildings() {
				fmt.Println("name ", building.GetName())
				fmt.Println("level ", building.GetLevel())
			}
		}

	}
	return c
}