Register for your free account! | Forgot your password?

You last visited: Today at 18:46

  • Please register to post and access all features, it's quick, easy and FREE!


Hmm packet writer question.

Discussion on Hmm packet writer question. within the CO2 PServer - Discussions / Questions forum part of the Conquer Online 2 category.

Reply
 
Old   #1
 
elite*gold: 0
Join Date: May 2011
Posts: 1,769
Received Thanks: 755
Hmm packet writer question.

Why do everybody uses something like.
Code:
WriteByte(byte value);
WriteUInt32(uint value);
//....
When you could just do something like.
Code:
    public unsafe class ConquerPacket
    {
        public ConquerPacket(int Length)
        {
            if (Length > Kernel.MaxPacketSize)
                throw new Exception("Invalid Packet Length.");

            m_packet = new byte[Length];
        }
        public int Length { get { return m_packet.Length; } }
        private byte* m_ptr
        {
            get
            {
                fixed (byte* ptr = m_packet)
                    return ptr;
            }
        }
        private byte[] m_packet;
        private int m_offset;

        /// <summary>
        /// Writing a value to the packet.
        /// </summary>
        /// <param name="Value">The value to write.</param>
        /// <param name="DataType">The type of the value.</param>
        /// <param name="StringLength">The length of the string, should be 0, if it's not a string.</param>
        public void Write(object Value, Enums.DataTypes DataType, int StringLength)
        {
            switch (DataType)
            {
                #region Byte
                case Enums.DataTypes.Byte:
                    {
                        *((byte*)(m_ptr + m_offset)) = (byte)Value;
                        m_offset++;
                        break;
                    }
                #endregion
                #region Byte
                case Enums.DataTypes.Bytes:
                    {
                        byte[] value = (byte[])Value;
                        foreach (byte b in value)
                            Write(b, Enums.DataTypes.Byte, 0);

                        break;
                    }
                #endregion
                #region UInt16
                case Enums.DataTypes.UInt16:
                    {
                        *((ushort*)(m_ptr + m_offset)) = (ushort)Value;
                        m_offset += 2;
                        break;
                    }
                #endregion
                #region UInt32
                case Enums.DataTypes.UInt32:
                    {
                        *((uint*)(m_ptr + m_offset)) = (uint)Value;
                        m_offset += 4;
                        break;
                    }
                #endregion
                #region UInt64
                case Enums.DataTypes.UInt64:
                    {
                        *((uint*)(m_ptr + m_offset)) = (uint)Value;
                        m_offset += 8;
                        break;
                    }
                #endregion
                #region String
                case Enums.DataTypes.String:
                    {
                        string value = Value.ToString();
                        if (value.Length > Length)
                        {
                            for (int i = 0; i < Length; i++)
                            {
                                *((byte*)(m_ptr + m_offset)) = Convert.ToByte(value[i]);
                                m_offset++;
                            }
                        }
                        else
                        {
                            for (int i = 0; i < value.Length; i++)
                            {
                                *((byte*)(m_ptr + m_offset)) = Convert.ToByte(value[i]);
                                m_offset++;
                            }
                        }
                        break;
                    }
                #endregion
            }
        }
        public void Move(int AddOffset)
        {
            m_offset += AddOffset;
        }

        public static implicit operator byte[](ConquerPacket Packet)
        {
            return Packet.m_packet;
        }
    }
Example:
Code:
                        ConquerPacket Packet = new ConquerPacket(32);
                        Packet.Write(Packet.Length, Enums.DataTypes.UInt16, 0);
                        Packet.Write(0x41f, Enums.DataTypes.UInt16, 0);
                        Packet.Write(packet.IV, Enums.DataTypes.Bytes, 0);
                        Packet.Write(Kernel.ServerIP, Enums.DataTypes.String, 0);
                        Packet.Move(16 - Kernel.ServerIP.Length);
                        Packet.Write(Kernel.GamePort, Enums.DataTypes.UInt16, 0);



BaussHacker is offline  
Old 09/21/2011, 01:09   #2
 
elite*gold: 0
Join Date: Apr 2008
Posts: 752
Received Thanks: 276
is not it the same-thing but instead of separate methods u made one method which ur still separating "the types " inside it using the enum lol?


Mr_PoP is offline  
Old 09/21/2011, 01:09   #3
 
elite*gold: 0
Join Date: May 2011
Posts: 1,769
Received Thanks: 755
Quote:
Originally Posted by Mr_PoP View Post
is not it the same-thing but instead of separate methods u made one method which ur still separating inside it using the enum lol?
Yup. Just why make seperate methods, when you can do it all in one?

Perfomance-wise it will be the same basically.
BaussHacker is offline  
Old 09/21/2011, 01:14   #4
 
elite*gold: 0
Join Date: Apr 2008
Posts: 752
Received Thanks: 276
Quote:
Originally Posted by BaussHacker View Post
Yup. Just why make seperate methods, when you can do it all in one?

Perfomance-wise it will be the same basically.
well am against the idea of enuming methods , you should have separate methods based on it's functionality!


Mr_PoP is offline  
Old 09/21/2011, 01:17   #5
 
elite*gold: 0
Join Date: May 2011
Posts: 1,769
Received Thanks: 755
Quote:
Originally Posted by Mr_PoP View Post
well am against the idea of enuming methods , you should have separate methods based on it's functionality!
In the end it might just be personal. I like this way tho, but maybe it's because I'm used to do things like this working with parameters in MSSQL etc.?
BaussHacker is offline  
Old 09/21/2011, 01:20   #6
 
elite*gold: 0
Join Date: Apr 2008
Posts: 752
Received Thanks: 276
Quote:
Originally Posted by BaussHacker View Post
In the end it might just be personal. I like this way tho, but maybe it's because I'm used to do things like this working with parameters in MSSQL etc.?
yeah :P agreed
Mr_PoP is offline  
Thanks
1 User
Old 09/21/2011, 01:24   #7
 
elite*gold: 20
Join Date: Mar 2006
Posts: 6,124
Received Thanks: 2,504
Yours will be a tiny bit slower than just using individual methods, not just because of the switch but because you need to check the type of the value, it doesnt actually improve readability because you dont know if your writing ints, shorts or bytes, so that actually makes the code harder to read.

Perhaps those are some of the reasons why this isnt done...
Korvacs is offline  
Thanks
1 User
Old 09/21/2011, 01:29   #8
 
elite*gold: 0
Join Date: May 2011
Posts: 1,769
Received Thanks: 755
Sooooo, I should switch to methods again?
BaussHacker is offline  
Old 09/21/2011, 01:36   #9
 
elite*gold: 0
Join Date: Dec 2010
Posts: 341
Received Thanks: 254
Code:
void Write(Byte b) { }
void Write(UInt16 b) { }
void Write(UInt32 b) { }
... etc
Then you just call one function name, clean and sexy imo.
.Kinshi is offline  
Thanks
1 User
Old 09/21/2011, 01:44   #10
 
elite*gold: 0
Join Date: May 2011
Posts: 1,769
Received Thanks: 755
Quote:
Originally Posted by .Kinshi View Post
Code:
void Write(Byte b) { }
void Write(UInt16 b) { }
void Write(UInt32 b) { }
... etc
Then you just call one function name, clean and sexy imo.
I feel stupid now. Spank me.
BaussHacker is offline  
Old 09/21/2011, 02:08   #11
 
elite*gold: 0
Join Date: Dec 2010
Posts: 341
Received Thanks: 254
Quote:
Originally Posted by BaussHacker View Post
I feel stupid now. Spank me.
Gladly
.Kinshi is offline  
Thanks
1 User
Old 09/21/2011, 09:51   #12
 
elite*gold: 0
Join Date: May 2011
Posts: 1,769
Received Thanks: 755
Quote:
Originally Posted by .Kinshi View Post
Gladly


____
Alright, so I have switched it back to methods again.


BaussHacker is offline  
Reply



« iam need some advices | server error, please help »

Similar Threads
[Question] How do you know what packet what is?
11/30/2010 - CO2 PServer - Discussions / Questions - 8 Replies
Im still learning for C#. But how do i know if i get an Unknown packet error what i need to add? Thanks in advance! JobvdH
Play sound via Packet Send?? [Question String Packet]
07/14/2010 - CO2 PServer - Discussions / Questions - 5 Replies
Yow im trying to figure out why i cant play music with the string packet What im doin is; MyChar.Client.SendPacket(Game.Packet.String(MyChar .UID, 20, Splitter)); My Packet is: public byte String(long CharId, byte Type, string name)
[Question] Packet data , packet editing ??
10/13/2009 - 9Dragons - 2 Replies
I would like to know : What is packet data? How do i get the address for hacking a item in game? How to use it ??
packet bot [QUESTION]
08/11/2009 - Kal Online - 10 Replies
well...is packetbot written in c++ or c#? and if there is anyone who could help me with it pls post here....i will give somthing in exchange :D
packet question.
12/31/2007 - Archlord - 2 Replies
Alright i got this idea of for example sending a packet to the server, that says open storage ^^ Now i got the packets itself but everytime i try to send it, it just freezes me.. I still see everyone move but I cant move. I was thinking well a filter would work but it wont have any use to me because then i would need a replacement packet wich would be useless for example ur killing monsters and OOPS ur out of space in inventory or whatever reason i have :p I think I know what the problem...



All times are GMT +2. The time now is 18:46.


Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
SEO by vBSEO ©2011, Crawlability, Inc.

BTC: 3KeUpz52VCbhmLwuwydqxu6U1xsgbT8YT5
ETH: 0xc6ec801B7563A4376751F33b0573308aDa611E05

Support | Contact Us | FAQ | Advertising | Privacy Policy | Terms of Service | Abuse
Copyright ©2019 elitepvpers All Rights Reserved.