Рейтинг темы:
  • 0 Голос(ов) - 0 в среднем
  • 1
  • 2
  • 3
  • 4
  • 5
UDP3 answer
#1
Hi,
i've a quickly question.

I've writted the header of the packet 4, than I've write the body.
The body have to been shifted again before send back to the client?

Thanks in advanca kappa
Ответ
#2
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
Ответ
#3
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? :confused:
Ответ
#4
@PROGRAMMATOR Help
Ответ


Возможно похожие темы ...
Тема Автор Ответы Просмотры Последний пост
  UDP3 MomzGames - Auth Protected (v42) vinne 0 3,430 01-08-2020, 03:02 AM
Последний пост: vinne
  [Source] UDP3 MoMz Auth Protected vinne 0 3,022 01-08-2020, 03:01 AM
Последний пост: vinne
  UDP3 Helicopter animation nikolen 0 2,090 09-01-2019, 08:46 PM
Последний пост: nikolen
  [Help] Point blank UDP3 ON indows 0 1,902 07-04-2019, 03:41 AM
Последний пост: indows
  UDP3 Grenade problem zOne62 5 2,107 12-16-2018, 12:03 PM
Последний пост: zOne62
  UDP3 POSITION ERROR Comandante9901 13 5,331 10-10-2018, 09:20 AM
Последний пост: ChunkyHunt
  help me UDP3 DeathDataForClient Event Id 2048 bmzproject 2 1,691 07-20-2018, 07:04 AM
Последний пост: battleBugado
  UDP3 Send-back packet ManuelDev 3 1,903 06-29-2018, 12:18 PM
Последний пост: ManuelDev
  pay for udp3 Light 0 1,527 06-29-2018, 02:04 AM
Последний пост: Light
  Some infos about UDP3? ManuelDev 2 1,769 06-26-2018, 09:38 AM
Последний пост: ManuelDev

Перейти к форуму:


Пользователи, просматривающие эту тему: 1 Гость(ей)