Тема: [Point Blank] UDP3 answer
Показать сообщение отдельно
Непрочитано 30.06.2018, 18:28   #3
Пользователь

Автор темы (Топик Стартер) Re: UDP3 answer

Цитата:
Сообщение от ManuelDev Посмотреть сообщение
I've try shifted and unshifted, but he keep don't working!

This is the actual class, somebody understand what's the problem?

Код:
using Battle.Utils;
using System;
using System.IO;
using System.Text;
namespace Battle.Battle
{
    public class UdpPacket
    {
        public byte Id { get; set; }
        public byte Slot { get; set; }
        public float Time { get; set; }
        public byte Session { get; set; }
        public short Length { get; set; }
        public int Events { get; set; }
        public byte[] Data { get; set; }

        public BattleReceive Receive;
        public BattleSend send2;
        public BattleSend Event3;

        public UdpPacket(byte[] Buffer)
        {
            BinaryReader br = new BinaryReader(new MemoryStream(Buffer));
            Id = br.ReadByte();
            Slot = br.ReadByte();
            Time = br.ReadSingle();
            Session = br.ReadByte();
            Length = br.ReadInt16();
            Events = br.ReadInt32();

            send2 = new BattleSend();
            send2.writeC(4);
            send2.writeC(255);
            send2.writeFloat(Time);
            send2.writeC(Session);
            send2.writeH(Length);
            send2.writeD(0);

            if (Id == 3)
            {
                Data = br.ReadBytes(Length - 13);
                send2.writeB(Data); // <- This is working (just copying the received data)

                Event3 = HandlePacket(); // <- This isn't working (Just tried with PosRotation)
            }
        }


        public BattleSend HandlePacket()
        {
            byte[] events = Data;
            Unshift(events, Length % 6 + 1);
            BinaryReader read = new BinaryReader(new MemoryStream(events));
            byte opcode = read.ReadByte();
            ushort subSlot = read.ReadUInt16();
            ushort subLength = read.ReadUInt16();
            int flag = read.ReadInt32();
            int packetSize = 0;
            int packetFlag = 0;

            BattleSend eventData = new BattleSend();
            if (opcode == 0)
            {
                if ((flag & 0x01) > 0) int action = read.ReadInt16();
                if ((flag & 0x02) > 0)  int action = read.ReadInt16();

                if ((flag & 0x04) > 0) //Position rotation
                {
                    ushort posX = read.ReadUInt16();
                    ushort posY = read.ReadUInt16();
                    ushort posZ = read.ReadUInt16();
                    ushort camX = read.ReadUInt16();
                    ushort camY = read.ReadUInt16();
                    ushort zone = read.ReadUInt16();

                    eventData.writeUH(posX);
                    eventData.writeUH(posY);
                    eventData.writeUH(posZ);
                    eventData.writeUH(camX);
                    eventData.writeUH(camY;
                    eventData.writeUH(zone);

                    packetSize+= 12;
                    packetFlag += 4;
                     

                }

                if ((flag & 0x40) > 0) Log.Info("wep type: " + read.ReadByte() + ", wep: " + read.ReadInt32());
                if ((flag & 0x1000) > 0) Log.Info("HP: " + read.ReadUInt16()); //No working
                if ((flag & 0x80000) > 0) Log.Info("Hit a item!");
                if ((flag & 0x80000) > 0) Log.Info("Hit a player!");
            }

            BattleSend header = new BattleSend();
            header.writeC(4);
            header.writeC(255);
            header.writeFloat(Time);
            header.writeC(Session);
            header.writeH((short)(packetSize + 13));
            header.writeD(0);

            BattleSend body = new BattleSend();
            body.writeC(opcode);
            body.writeH(subSlot);
            body.writeH((short)packetSize);
            body.writeD(packetFlag);
            body.writeB(eventData.mstream.ToArray());

            byte[] toshift = body.mstream.ToArray();
            Shift(toshift, (packetSize + 13) % 6 + 1);
            header.writeB(toshift);
            return header;
        }

        public static void Shift(byte[] buffer, int bits)
        {
            int length = buffer.Length;
            byte first = buffer[0];
            byte current;

            for (int i = 0; i < length; i++)
            {
                if (i >= (length - 1))
                {
                    current = first;
                }
                else
                {
                    current = buffer[i + 1];
                }

                buffer[i] = (byte)(current >> (8 - bits) | (buffer[i] << bits));
            }
        }

        public static void Unshift(byte[] buffer, int bits)
        {
            int length = buffer.Length;
            byte last = buffer[length - 1];
            byte current;

            for (int i = length - 1; (i & 0x80000000) == 0; i--)
            {
                if (i <= 0)
                {
                    current = last;
                }
                else
                {
                    current = buffer[i - 1];
                }

                buffer[i] = (byte)(current << (8 - bits) | buffer[i] >> bits);
            }
        }
    }
}
How i call it
Код:
// ..

bool copyBuffer = false;
public void BeginReceive(IAsyncResult result){
    // ..
    byte[] buffer = client.EndReceive(result, ref remoteEP);
    UdpPacket Packet = new UdpPacket(buffer);

    switch(Packet.Id){
        case 65: // .. 
        case 66: // ..
        case 3:{
            foreach (Player pl in Room.Players.Values)
                        {
                            bool isUdp = Room.isUDP(pl.Slot, us.Slot == Room.leader);
                            if (isUdp)
                            {
                                // copyBuffer = true -> Working | copyBuffer = false -> Don't work, all players stopped
                                if(copyBuffer) client.Send(Packet.send2.mstream.ToArray(), Packet.send2.mstream.ToArray().Length, new IPEndPoint(IPAddress.Parse(pl.Ip), pl.Port));
                                else client.Send(Packet.Event3.mstream.ToArray(), Packet.Event3.mstream.ToArray().Length, new IPEndPoint(IPAddress.Parse(pl.Ip), pl.Port));
                            }
                        }
        }
        default: //..
    }
}
@PROGRAMMATOR
@Awiion
Anyone?
ManuelDev вне форума Ответить с цитированием