Example #1
0
func (p *Packet) Decode(buf []byte) ([]int, error) {
	var (
		header PacketHeader    = PacketHeader{}
		netes  *network.Parser = network.NewParser(buf)
	)

	netes.SetEndian(network.LittleEndian).ReadUint16(&header.Len)
	netes.SetEndian(network.BigEndian).ReadUint32(&header.UniqueId)
	netes.SetEndian(network.LittleEndian).ReadUint16(&header.Opcode)

	pck, err := p.GetPck(int(header.Opcode))
	if err != nil {
		return nil, err
	}

	handler, process := pck.Packet(p.Database)

	handler(netes)
	err = netes.Error()
	if err != nil {
		return nil, err
	}

	opcodes := process(p.Player)

	return opcodes, nil
}
Example #2
0
func (p *Packet) Encode(opcode int) ([]byte, error) {
	start := time.Now()
	netes := network.NewParser([]byte{})

	pck, err := p.GetPck(opcode)
	if err != nil {
		return nil, err
	}

	handler, process := pck.Packet(p.Database)

	process(p.Player)

	handler(netes)
	err = netes.Error()
	if err != nil {
		return nil, err
	}

	data := string(netes.Bytes())
	netes.Reset()

	header := PacketHeader{Len: uint16(len(data) + 8), UniqueId: 128, Opcode: uint16(opcode)}

	netes.SetEndian(network.LittleEndian).WriteUint16(header.Len)
	netes.SetEndian(network.BigEndian).WriteUint32(header.UniqueId)
	netes.SetEndian(network.LittleEndian).WriteUint16(header.Opcode)
	netes.WriteBytes([]byte(data))

	log.Info("Elapled time in Encode function: ", time.Since(start))

	return netes.Bytes(), nil
}
Example #3
0
func (p *Pill) Decrypt(buf []byte, player *entitie.Player) ([]int, error) {
	var (
		header Header          = Header{}
		netes  *network.Parser = network.NewParser(buf)
	)

	netes.SetEndian(network.LittleEndian).ReadUint16(&header.Len)
	netes.SetEndian(network.BigEndian).ReadUint32(&header.UniqueId)
	netes.SetEndian(network.LittleEndian).ReadUint16(&header.Opcode)

	pill := p.GetPill(int(header.Opcode))
	if pill == nil {
		return nil, errors.New("Pill is not found")
	}

	process := pill.Handler(netes).Process(player)
	if process.Error() != nil {
		return nil, process.Error()
	}

	err := netes.Error()
	if err != nil {
		return nil, err
	}

	return process.Opcodes(), nil
}
Example #4
0
func (p *Pill) Decrypt(buf []byte, player entitie.Player) ([]int, error) {
	var (
		header Header          = Header{}
		netes  *network.Parser = network.NewParser(buf)
	)

	netes.SetEndian(network.LittleEndian).ReadUint16(&header.Len)
	netes.SetEndian(network.BigEndian).ReadUint32(&header.UniqueId)
	netes.SetEndian(network.LittleEndian).ReadUint16(&header.Opcode)

	crumb := p.SetOpcode(int(header.Opcode)).GetIncomingCrumb()
	if crumb == nil {
		return nil, errors.New("Crumb is not found")
	}

	crumbProcess, err := crumb.PreHandler(netes).Process(player)
	if err != nil {
		return nil, err
	}

	err = netes.Error()
	if err != nil {
		return nil, err
	}

	return crumbProcess, nil
}
Example #5
0
func (p *Pill) Encrypt(pe interfaces.PillEncoder) ([]byte, error) {
	netes := network.NewParser([]byte{})

	data := pe.Process().PostHandler(netes)
	netes.Reset()

	header := Header{Len: uint16(len(data) + 8), UniqueId: 128, Opcode: uint16(p.opcode)}

	netes.SetEndian(network.LittleEndian).WriteUint16(header.Len)
	netes.SetEndian(network.BigEndian).WriteUint32(header.UniqueId)
	netes.SetEndian(network.LittleEndian).WriteUint16(header.Opcode)
	netes.WriteBytes([]byte(data))

	err := netes.Error()
	if err != nil {
		return nil, err
	}

	return netes.Bytes(), nil
}
Example #6
0
func (p *Pill) Encrypt(pf PillFactory, player *entitie.Player) ([]byte, error) {
	netes := network.NewParser([]byte{})

	pf.Process(player).Handler(netes)
	data := string(netes.Bytes())
	netes.Reset()

	header := Header{Len: uint16(len(data) + 8), UniqueId: 128, Opcode: uint16(p.opcode)}

	netes.SetEndian(network.LittleEndian).WriteUint16(header.Len)
	netes.SetEndian(network.BigEndian).WriteUint32(header.UniqueId)
	netes.SetEndian(network.LittleEndian).WriteUint16(header.Opcode)
	netes.WriteBytes([]byte(data))

	err := netes.Error()
	if err != nil {
		return nil, err
	}

	return netes.Bytes(), nil
}