Beispiel #1
0
        static void Main(string[] args)
        {
            const int packetSizeS = 1;
            const int packetSizeM = 5;
            const int packetSizeL = 15;

            float value = 60.21F;
            int   numberOfLargePackets  = (int)(value / packetSizeL);
            int   numberOfMediumPackets = 0;
            int   numberOfSmallPackets  = 1;

            PacketSizes usedPacketSizes = 0;

            if (numberOfLargePackets > 0)
            {
                usedPacketSizes = usedPacketSizes | PacketSizes.Large;
            }

            if (numberOfMediumPackets > 0)
            {
                usedPacketSizes = usedPacketSizes | PacketSizes.Medium;
            }

            if (numberOfSmallPackets > 0)
            {
                usedPacketSizes = usedPacketSizes | PacketSizes.Small;
            }

            Console.WriteLine("Hello World!");

            if ((usedPacketSizes & PacketSizes.Large) > 0)
            {
                // numberOfLargePackets of 15-litters packets
            }

            if (numberOfMediumPackets > 0)
            {
                // numberOfMediumPackets of 5-litters packets
            }

            if (numberOfSmallPackets > 0)
            {
                // numberOfSmallPackets of 1-litters packets
            }
        }
Beispiel #2
0
        private void Receive()
        {
            if (!handShakeSendToServer)
            {
                PacketGenerator gen = new PacketGenerator();
                gen.Add((byte)PacketBytes._0x02_Handshake_0x02);
                gen.Add(Name);
                client.Client.Send(gen.ToByteArray());
                handShakeSendToServer = true;
            }

            extBuffer = ReceiveBytes(client.Client, 1);

            PacketBytes firstByte = (PacketBytes)extBuffer[0];
            PacketSizes size      = MinecraftEnums.GetPacketSize(firstByte);

            if (size != PacketSizes.Minus_1)
            {
                if (firstByte == PacketBytes._0x0D_PlayerMoveAndLook_0x0D)
                {
                    size = PacketSizes.Minus_1;
                }
                if (firstByte == PacketBytes._0x03_Chat_0x03)
                {
                    size = PacketSizes.Minus_1;
                }

                if (size != PacketSizes.Minus_1)
                {
                    ReceiveBytes((int)size);
                }
            }

            if (size == PacketSizes.Minus_1)
            {
                switch (firstByte)
                {
                case PacketBytes.Minues_1:
                    break;

                case PacketBytes._0x00_KeepAlive_0x00:
                    break;

                case PacketBytes._0x01_Login_0x01:
                    if (!loginRequestReceived)
                    {
                        LoginRequest();
                    }
                    break;

                case PacketBytes._0x02_Handshake_0x02:
                    if (!handShakeReceived)
                    {
                        Handshake();
                    }
                    break;

                case PacketBytes._0x03_Chat_0x03:
                    String chat = ReceiveString(client.Client);
                    AddOutput("Chat: " + chat);
                    break;

                case PacketBytes._0x04_UpdateTime_0x04:
                    ReceiveInt64();
                    break;

                case PacketBytes._0x05_EntityEquipment_01_0x05:
                    ReceiveInt32();
                    ReceiveInt16();
                    ReceiveInt16();
                    ReceiveInt16();
                    break;

                case PacketBytes._0x06_SpawnPosition_02_0x06:
                    ReceiveInt32();
                    ReceiveInt32();
                    ReceiveInt32();
                    break;

                case PacketBytes._0x07_Use_Entity_0x07:
                    ReceiveInt32();
                    ReceiveInt32();
                    ReceiveBoolean();
                    break;

                case PacketBytes._0x08_Health_0x08:
                    short health = ReceiveInt16();
                    if (health <= 0)
                    {
                        PacketGenerator gen = new PacketGenerator();
                        gen.Add(PacketBytes._0x08_Health_0x08);
                        client.Client.Send(gen.ToByteArray());
                    }
                    break;

                case PacketBytes._0x09_Respawn_0x09:
                    break;

                case PacketBytes._0x0A_Player_0x0A:
                    break;

                case PacketBytes._0x0B_PlayerPosition_0x0B:
                    break;

                case PacketBytes._0x0C_PlayerLook_0x0C:

                    break;

                case PacketBytes._0x0D_PlayerMoveAndLook_0x0D:
                    position.X          = Util.AtoD(ReceiveBytes(8), 0);
                    position.Y          = Util.AtoD(ReceiveBytes(8), 0);
                    position.Stance     = Util.AtoD(ReceiveBytes(8), 0);
                    position.Z          = Util.AtoD(ReceiveBytes(8), 0);
                    position.Rotation   = Util.AtoF(ReceiveBytes(4), 0);
                    position.Pitch      = Util.AtoF(ReceiveBytes(4), 0);
                    position.Unkown     = ByteArythmetic.GetBoolean(ReceiveBytes(1), 0);
                    moveAndLookReceived = true;
                    Move(0);
                    break;

                case PacketBytes._0x0E_BlockDig_0x0E:
                    break;

                case PacketBytes._0x0F_PlaceBlock_0x0F:
                    break;

                case PacketBytes._0x10_BlockItemSwitch_0x10:
                    ReceiveBytes(2);
                    break;

                case PacketBytes._0x11_UseBed_0x11:
                    ReceiveInt32();
                    ReceiveByte();
                    ReceiveInt32();
                    ReceiveByte();
                    ReceiveInt32();
                    break;

                case PacketBytes._0x12_ArmAnimation_0x12:
                    ReceiveInt32();
                    ReceiveByte();
                    break;

                case PacketBytes._0x13_NewPacket_0x13:
                    ReceiveBytes(5);
                    break;

                case PacketBytes._0x14_EntitySpawnName_0x14:
                    ReceiveInt32();
                    ReceiveString(client.Client);
                    ReceiveInt32();
                    ReceiveInt32();
                    ReceiveInt32();
                    ReceiveByte();
                    ReceiveByte();
                    ReceiveInt16();
                    break;

                case PacketBytes._0x15_EntitySpawn_0x15:
                    ReceiveInt32();
                    ReceiveInt16();
                    ReceiveByte();
                    ReceiveInt16();
                    ReceiveInt32();
                    ReceiveInt32();
                    ReceiveInt32();
                    ReceiveByte();
                    ReceiveByte();
                    ReceiveByte();
                    break;

                case PacketBytes._0x16_CollectItem_0x16:
                    ReceiveInt32();
                    ReceiveInt32();
                    break;

                case PacketBytes._0x17_AddObject_0x17:
                    ReceiveBytes(client.Client, 17);
                    if (Util.AtoI(ReceiveBytes(client.Client, 4), 0) > 0)
                    {
                        ReceiveBytes(client.Client, 6);
                    }
                    break;

                case PacketBytes._0x18_MobSpawn_0x18:
                    ReceiveInt32();
                    ReceiveByte();
                    ReceiveInt32();
                    ReceiveInt32();
                    ReceiveInt32();
                    ReceiveByte();
                    ReceiveByte();
                    HandleInsanity(client.Client);
                    break;

                case PacketBytes._0x1B_NewPacket:
                    ReceiveFloat();
                    ReceiveFloat();
                    ReceiveFloat();
                    ReceiveFloat();
                    ReceiveBoolean();
                    ReceiveBoolean();
                    break;

                case PacketBytes._0x1C_EntityVelocity_0x1C:
                    ReceiveInt32();
                    ReceiveInt16();
                    ReceiveInt16();
                    ReceiveInt16();
                    break;

                case PacketBytes._0x1D_DestroyEntity_0x1D:
                    ReceiveInt32();
                    break;

                case PacketBytes._0x1E_Entity_0x1E:
                    ReceiveInt32();
                    break;

                case PacketBytes._0x1F_RelativeEntityMove_0x1F:
                    ReceiveInt32();
                    ReceiveByte();
                    ReceiveByte();
                    ReceiveByte();
                    break;

                case PacketBytes._0x19_NewPacket_0x19:
                    ReceiveInt32();
                    ReceiveString(client.Client);
                    ReceiveInt32();
                    ReceiveInt32();
                    ReceiveInt32();
                    ReceiveInt32();
                    break;

                case PacketBytes._0x20_EntityLook_0x20:
                    ReceiveInt32();
                    ReceiveByte();
                    ReceiveByte();
                    break;

                case PacketBytes._0x21_RelativeEntityLookAndMove_0x21:
                    ReceiveInt32();
                    ReceiveByte();
                    ReceiveByte();
                    ReceiveByte();
                    ReceiveByte();
                    ReceiveByte();
                    break;

                case PacketBytes._0x22_EntityTeleport_0x22:
                    ReceiveInt32();
                    ReceiveInt32();
                    ReceiveInt32();
                    ReceiveInt32();
                    ReceiveByte();
                    ReceiveByte();
                    break;

                case PacketBytes._0x26_HitEntity_0x26:
                    ReceiveInt32();
                    ReceiveByte();
                    break;

                case PacketBytes._0x27_AttachEntity_0x27:
                    ReceiveInt32();
                    ReceiveInt32();
                    break;

                case PacketBytes._0x28_NewPacket_0x28:
                    ReceiveInt32();
                    HandleInsanity(client.Client);
                    break;

                case PacketBytes._0x32_ChunkPre_0x32:
                    ReceiveInt32();
                    ReceiveInt32();
                    ReceiveBoolean();
                    break;

                case PacketBytes._0x33_ChunkMap_0x33:
                    ReceiveInt32();
                    ReceiveInt16();
                    ReceiveInt32();
                    ReceiveByte();
                    ReceiveByte();
                    ReceiveByte();
                    int chunkSize = ReceiveInt32();
                    ReceiveBytes(chunkSize);
                    break;

                case PacketBytes._0x34_MultiBlockChange_0x34:
                    this.ReceiveBytes(8);
                    this.ReceiveBytes(ByteArythmetic.GetInt16(this.ReceiveBytes(2), 0) * 4);
                    break;

                case PacketBytes._0x35_BlockChange_0x35:
                    ReceiveBytes(11);
                    break;

                case PacketBytes._0x36_BlockAction_0x36:
                    ReceiveBytes(12);
                    break;

                case PacketBytes._0x3C_Explosion_0x3C:
                    ReceiveBytes(28);     //8 + 8 + 8 + 4
                    ReceiveBytes(ByteArythmetic.GetInt32(ReceiveBytes(4), 0) * 3);
                    break;

                case PacketBytes._0x46_InvalidBed_0x46:
                    ReceiveByte();
                    break;

                case PacketBytes._0x47_Weather_0x47:
                    ReceiveBytes(17);
                    break;

                case PacketBytes._0x64_OpenWindow_0x64:
                    ReceiveBytes(2);
                    ReceiveStringUtf8(client.Client);     // UTF8
                    ReceiveBytes(1);
                    break;

                case PacketBytes._0x65_CloseWindow_0x65:
                    ReceiveBytes(1);
                    break;

                case PacketBytes._0x66_WindowClick_0x66:
                    ReceiveBytes(7);     // Update 1.5 + 1 bool
                    if (Util.AtoN(ReceiveBytes(2), 0) >= 0)
                    {
                        ReceiveBytes(3);
                    }
                    break;

                case PacketBytes._0x67_SetSlot_0x67:
                    ReceiveBytes(3);
                    if (Util.AtoN(ReceiveBytes(2), 0) >= 0)
                    {
                        ReceiveBytes(3);
                    }
                    break;

                case PacketBytes._0x68_WindowItems_0x68:
                    short xmax = Util.AtoN(ReceiveBytes(3), 1);
                    for (short cx = 0; cx < xmax; cx++)
                    {
                        if (Util.AtoN(ReceiveBytes(2), 0) >= 0)
                        {
                            ReceiveBytes(3);
                        }
                    }
                    break;

                case PacketBytes._0x69_UpdateProgressBar_0x69:
                    ReceiveBytes(5);
                    break;

                case PacketBytes._0x6A_Transaction_0x6A:
                    ReceiveBytes(4);
                    break;

                case PacketBytes._0x82_UpdateSign_0x82:
                    ReceiveBytes(10);
                    ReceiveString(client.Client);
                    ReceiveString(client.Client);
                    ReceiveString(client.Client);
                    ReceiveString(client.Client);
                    break;

                case PacketBytes._0x83_MapData_0x83:
                {
                    ReceiveBytes(client.Client, 4);
                    ReceiveBytes(client.Client, Util.AtoI(ReceiveBytes(client.Client, 1), 0));
                }
                break;

                case PacketBytes._0xC8_IncrementStatistics_0xC8:
                    ReceiveBytes(5);
                    break;

                case PacketBytes._0xFF_Disconnect_0xFF:
                    String str = ReceiveString(client.Client);
                    AddOutput("NPC Disconnected: " + str);
                    break;

                default:
                    break;
                }
            }
        }
Beispiel #3
0
        static void Main(string[] args)
        {
            const int packetSizeS = 1;
            const int packetSizeM = 5;
            const int packetSizeL = 20;

            int numberOfLargePackets  = 0; //переменная для записи количества больших пакетов
            int numberOfMediumPackets = 0; //переменная для записи количества средних пакетов
            int numberOfSmallPackets  = 0; //переменная для записи количества маленьких пакетов

            double input = 0;              //Запись ввода пользователя

            PacketSizes usedPacketSizes = 0;

            int remainderDivision = 0; //переменная для записи остатка от деления

            Console.WriteLine("Данная программа позволит расчитать, какое количество упаковок обьемом 1л, 5л, 20л \nпотребуется для упаковки вашего сока");
            Console.WriteLine("Какой объем сока (в литрах) требуется упаковать?");

            while (!double.TryParse(Console.ReadLine(), out input))  //ввод пользователя
            {
                Console.WriteLine("Неправильный ввод повторите попытку");
            }

            int value = (int)Math.Ceiling(input); //округление

            numberOfLargePackets = value / packetSizeL;
            remainderDivision    = value % packetSizeL;

            if (remainderDivision > 0)
            {
                numberOfMediumPackets = remainderDivision / packetSizeM;
                remainderDivision     = remainderDivision % packetSizeM;

                if (remainderDivision > 0)
                {
                    numberOfSmallPackets = remainderDivision;
                }
            }

            if (numberOfLargePackets > 0)
            {
                usedPacketSizes = usedPacketSizes | PacketSizes.Large;
            }

            if (numberOfMediumPackets > 0)
            {
                usedPacketSizes = usedPacketSizes | PacketSizes.Medium;
            }

            if (numberOfSmallPackets > 0)
            {
                usedPacketSizes = usedPacketSizes | PacketSizes.Small;
            }

            if ((usedPacketSizes & PacketSizes.Large) > 0)
            {
                Console.WriteLine("Вам потребуются следующие контейнеры: ");
                Console.WriteLine($"{packetSizeL} л: {numberOfLargePackets} шт.");
            }

            if ((usedPacketSizes & PacketSizes.Medium) > 0)
            {
                Console.WriteLine($"{packetSizeM} л: {numberOfMediumPackets} шт.");
            }

            if ((usedPacketSizes & PacketSizes.Small) > 0)
            {
                Console.WriteLine($"{packetSizeS} л: {numberOfSmallPackets} шт.");
            }

            Console.WriteLine("Для продолжения нажмите любую клавишу");
            Console.ReadKey();
        }
Beispiel #4
0
        public void ReadCallback(IAsyncResult ar)
        {
            String      content = String.Empty;
            ServerState state   = (ServerState)ar.AsyncState;
            Socket      handler = state.workSocket;

            int bytesRead = 0;

            try
            {
                bytesRead = handler.EndReceive(ar);
            }
            catch (SocketException)
            {
                // Connection closed by peer
                if (OnDisconnect != null)
                {
                    OnDisconnect(this, state.endPoint);
                }
                return;
            }
            catch (ObjectDisposedException)
            {
                // Connection closed by client
                if (OnDisconnect != null)
                {
                    OnDisconnect(this, state.endPoint);
                }
                return;
            }
            catch (Exception e)
            {
                if (OnError != null)
                {
                    OnError(this, e.ToString());
                }
                return;
            }

            if (bytesRead > 0)
            {
                byte[]      data      = state.dynamicBuffer;
                PacketBytes firstByte = (PacketBytes)data[0];
                PacketSizes size      = PacketSizes.Null;

                if (state.result == null)
                {
                    switch (firstByte)
                    {
                    case PacketBytes.KeepAlive_0x00:
                        size = PacketSizes.KeepAlive_0x00;
                        break;

                    case PacketBytes.Login_0x01:
                        size = PacketSizes.Login_0x01;
                        break;

                    case PacketBytes.Handshake_0x02:
                        size = PacketSizes.Handshake_0x02;
                        break;

                    case PacketBytes.Chat_0x03:
                        size = PacketSizes.Chat_0x03;
                        break;

                    case PacketBytes.UpdateTime_0x04:
                        size = PacketSizes.UpdateTime_0x04;
                        break;

                    case PacketBytes.Unkown01_0x0A:
                        size = PacketSizes.Unkown01_0x0A;
                        break;

                    case PacketBytes.PlayerPosition_0x0B:
                        size = PacketSizes.PlayerPosition_0x0B;
                        break;

                    case PacketBytes.PlayerLook_0x0C:
                        size = PacketSizes.PlayerLook_0x0C;
                        break;

                    case PacketBytes.PlayerMoveAndLook_0x0D:
                        size = PacketSizes.PlayerMoveAndLook_0x0D;
                        break;

                    case PacketBytes.PlaceBlock_0x0F:
                        size = PacketSizes.PlaceBlock_0x0F;
                        break;

                    case PacketBytes.BlockItemSwitch_0x10:
                        size = PacketSizes.BlockItemSwitch_0x10;
                        break;

                    case PacketBytes.AddToInventory_0x11:
                        size = PacketSizes.AddToInventory_0x11;
                        break;

                    case PacketBytes.ArmAnimation_0x12:
                        size = PacketSizes.ArmAnimation_0x12;
                        break;

                    case PacketBytes.EntitySpawnName_0x14:
                        size = PacketSizes.EntitySpawnName_0x14;
                        break;

                    case PacketBytes.EntitySpawn_0x15:
                        size = PacketSizes.EntitySpawn_0x15;
                        break;

                    case PacketBytes.CollectItem_0x16:
                        size = PacketSizes.CollectItem_0x16;
                        break;

                    case PacketBytes.Unkown02_0x17:
                        size = PacketSizes.Unkown02_0x17;
                        break;

                    case PacketBytes.MobSpawn_0x18:
                        size = PacketSizes.MobSpawn_0x18;
                        break;

                    case PacketBytes.DestroyEntity_0x1D:
                        size = PacketSizes.DestroyEntity_0x1D;
                        break;

                    case PacketBytes.Entity_0x1E:
                        size = PacketSizes.Entity_0x1E;
                        break;

                    case PacketBytes.RelativeEntityMove_0x1F:
                        size = PacketSizes.RelativeEntityMove_0x1F;
                        break;

                    case PacketBytes.EntityLook_0x20:
                        size = PacketSizes.EntityLook_0x20;
                        break;

                    case PacketBytes.RelativeEntityLookAndMove_0x21:
                        size = PacketSizes.RelativeEntityLookAndMove_0x21;
                        break;

                    case PacketBytes.EntityTeleport_0x22:
                        size = PacketSizes.EntityTeleport_0x22;
                        break;

                    case PacketBytes.ChunkPre_0x32:
                        size = PacketSizes.ChunkPre_0x32;
                        break;

                    case PacketBytes.ChunkMap_0x33:
                        size = PacketSizes.ChunkMap_0x33;
                        break;

                    case PacketBytes.MultiBlockChange_0x34:
                        size = PacketSizes.MultiBlockChange_0x34;
                        break;

                    case PacketBytes.BlockChange_0x35:
                        size = PacketSizes.BlockChange_0x35;
                        break;

                    case PacketBytes.Disconnect_0xFF:
                        size = PacketSizes.Disconnect_0xFF;
                        break;
                    }

                    if (size == PacketSizes.String)
                    {
                        if (state.result == null)
                        {
                            state.result = new StringResult(this, handler, state, firstByte, data, 2);
                            state.result.Receive(); // erster einsprung
                        }
                        else
                        {
                            if (state.result.HasResult)
                            {
                                String result = (state.result as StringResult).ResultString;
                                state.result = null;
                            }
                            else
                            {
                                state.result.Receive(); // nächster einsprung
                            }
                        }
                    }

                    if (size == PacketSizes.String)
                    {
                        if (state.result == null)
                        {
                            state.result = new StringResult(this, handler, state, firstByte, data, bytesRead);
                            state.result.Receive(); // erster einsprung
                        }
                        else
                        {
                            if (state.result.HasResult)
                            {
                                String result = (state.result as StringResult).ResultString;
                                state.result = null;
                            }
                            else
                            {
                                state.result.Receive(); // nächster einsprung
                            }
                        }
                    }
                }

                if (OnByteDataAvailable != null)
                {
                    OnByteDataAvailable(this, handler, firstByte, data, bytesRead);
                }
            }
            else
            {
                // Connection closed by peer
                if (OnDisconnect != null)
                {
                    OnDisconnect(this, state.endPoint);
                }
            }
        }