Envision, Create, Share

Welcome to HBGames, a leading amateur game development forum and Discord server. All are welcome, and amongst our ranks you will find experts in their field from all aspects of video game design and development.

[C# .net 2]My PacketBuilder for Writing/Reading Packets

[iurl=#ReadWrite]Write/Read[/iurl]

I've made my own PacketWriter/PacketReader to use for Sockets that send data with an Byte array.
The byte array doesn't got a fixed size so whenever you write it makes the array bigger.

Code:
PacketWriter writer = new PacketWriter(0);
writer.Write("Hello Server");
writer.Write(56);
ClientSocket.Send(writer.GetPacket);


Code:
PacketReader reader = new PacketReader(socketCollection[e.SourceIP].Get());

            switch (reader.PacketID)
            {
                case 0: //Greetings from Client
                    string message = reader.ReadString();
                    int RandomInt = reader.ReadInt();
                    break;
            }

Writer:
Code:
        private byte[] PacketBytes;

        private int offset;

        private ushort packetID;

        private Encoding encoding = Encoding.ASCII;

        public byte[] GetPacket
        {
            get { return PacketBytes; }
        }
        public int ByteLength
        {
            get { return PacketBytes.Length; }
        }

        public PacketWriter(ushort PacketID, Encoding Encoding)
        {
            PacketBytes = new byte[0];
            offset = 0;
            packetID = PacketID;
            this.encoding = Encoding;

            Write(packetID);
        }

        public PacketWriter(ushort PacketID)
        {
            PacketBytes = new byte[0];
            offset = 0;
            packetID = PacketID;

            Write(packetID);
        }

        public void Write(int value)
        {
            ResizeArray(4);
            byte[] bytes = BitConverter.GetBytes(value);
            Buffer.BlockCopy(bytes, 0, PacketBytes, offset, 4);
            offset += 4;
        }

        public void Write(string value)
        {
            byte[] bytes = encoding.GetBytes(value);
            ResizeArray(bytes.Length);
            Write(bytes.Length);
            Buffer.BlockCopy(bytes, 0, PacketBytes, offset, bytes.Length);
            offset += bytes.Length;
        }


Reader:
Code:
        private byte[] PacketStream;

        private int offset;

        private ushort packetID;

        private Encoding encoding = Encoding.ASCII;

        public int ByteLength
        {
            get { return PacketStream.Length; }
        }

        public override string ToString()
        {
            StringBuilder sb = new StringBuilder();
            foreach (byte b in PacketStream)
            {
                sb.Append(string.Format("{0:X2}", b) + " ");
            }

            return sb.ToString();
        }

        public ushort PacketID
        {
            get { return packetID;}
        }

        public PacketReader(byte[] Packet)
        {
            PacketStream = Packet;
            offset = 0;
            packetID = ReadUShort();
        }

        public PacketReader(byte[] Packet, Encoding Encoding)
        {
            PacketStream = Packet;
            offset = 0;
            packetID = ReadUShort();
            this.encoding = Encoding;
        }

        public int ReadInt()
        {
            int i = BitConverter.ToInt32(PacketStream, offset);
            offset += 4;
            return i;
        }

        public string ReadString()
        {
            int length = ReadInt();
            string s = encoding.GetString(PacketStream, offset, length);
            offset += length;
            return s;
        }
[Anchor=ReadWrite]
You can read/write:[/anchor]
  • long
  • ulong
  • double
  • int
  • uint
  • float
  • short
  • ushort
  • char
  • byte
  • bool
  • byte array
  • string

Download Here

I know i'm not so good with explaining...
 

e

Sponsor

Maybe you should explain why someone would use this. Alternatively, writing/reading packets isn't exactly a proper word. A packet is actually just a means to pack data into a portable format to send on some kind of network. More commonly, in the TCP/IP protocol, the packet is the router (or 3rd level) data format, the formats being: physical (+- 5 volt), frames (level 2, switches, hubs, etc., works at the MAC address level) and then packet (level 3, works at the IP level for routers and smart switches and stuff).

You usually don't bother packaging a packet as it's done automatically when you send data. Now, if your tool forges packets, then it is a useful thing (because you can mess with the CRC and other stuff).
 
it's kinda like the BinaryWriter/BinaryReader where you can write to a file except you write to an Byte[] and then you can get the bytes to send it over a socket and get the bytes on the reader that reads the first two bytes to get packetID then you can do Readint() to get an int from the next 4 bytes... the byte array isn't on a fixed size. it does increase every time you write something to it

this:

Code:
            PacketWriter writer = new PacketWriter(56);
            writer.Write(6); //Level int
            writer.Write(655445); //Exp int
            writer.Write(false); //Admin boolean

becomes this:
Code:
38 00    06 00 00 00    55 00 0A 00       00
PackID       Level              Exp            Admin
 
Cocoa":29763one said:
Is this mono project compatible?
I don't know, gonna check what you need to do for that.

seems kinda like it is because i do only use System and System.Text... but i may have to change .net version of it

updated download link to a .NET 2 version
 

Thank you for viewing

HBGames is a leading amateur video game development forum and Discord server open to all ability levels. Feel free to have a nosey around!

Discord

Join our growing and active Discord server to discuss all aspects of game making in a relaxed environment. Join Us

Content

  • Our Games
  • Games in Development
  • Emoji by Twemoji.
    Top