Packet structure

From VbGORE Visual Basic Online RPG Engine

[edit] String Packets

This section on string packets is here for your understanding only - it is NOT part of vbGORE!

Most ORPG engines in Visual Basic use String packets, so it will be no surprise if you find the packet architecture very weird and confusing at first since vbGORE does not use string packets, but rather binary packets.

First, lets clarify what a string packet is. A string packet is simply a packet sent by a string. Breaking those two up, a string is a series of ASCII characters. For example, this text right here is a string of text. ASCII characters come in the values of 0-255, most of which you can not read. More information on ASCII characters can be found here. A packet is simply a chunk of information sent from one computer to another (in this case over the internet). Packets can have a complex structure, but we don't need to cover that here.

Now sending a string packet, when you piece the above information together, you can see is when you send a bunch of characters. This is fine for words, such as "The brown dog". Though when you send numbers, things get worse. As a powerful engine, vbGORE concentrates one of it's main focuses on bandwidth usage. The less data sent for the same effect, the better. To send the number 12,345 as a string, you will have to send "12345" - a 5 character long string. Each character in a string counts as one byte, so the above is 5 bytes long. Instead, it would be more productive to send it as an Integer, which is a variable that is 2 bytes long.

[edit] Binary Packets

The packets for vbGORE are written in a byte array. A byte is the smallest sized variable you can have. A byte array simply means a series of bytes put together. In a way, a string is like a byte array. Each value of a byte can be 0 to 255, while each character in a string can be an ASCII character with the ID of 0 to 255.

vbGORE makes creating the byte arrays easy for you with the DataBuffer class module. All you have to do is get information out the same way you put it in. Each packet is structured, broadly, in the following manner: <DataCode><DataInformation>

Your DataCode is a value from 0 to 255 which states what kind of function will take place. You can recognize these functions by their Sub name, which start with "Data_" and end with the Datacode's name. These functions can be anything - walking, casting spells, talking, etc. They simply just tell the other computer what kind of packet it is.

According to the packet type, you have to get the data out accordingly. For example, if you have a Move packet received on the server (that is, the DataCode = User_Move), your DataInformation is and always will be one byte which states the direction the user is moving.

It is important you sync up the information of the Client and Server. You must never read data out of the order you send it. I have made things easier for you by writing how every packet is structured under the packet's recieve module (ie Data_Comm_Talk). The information looks like this:

'********************************************* 
'Send data to chat buffer 
'<Text(S)><FontColorID(B)> 
'*********************************************

The first line tells what the sub does. In this case, we are using Data_Comm_Talk. As it says above, this sends the recieved data to the chat buffer. The arguments the client sends are Text which is a String and FontColorID which is a byte. You can tell if it is a string or byte by the (S) and (B). Below is the list of the different identifiers:

(B) = Byte

(I) = Integer

(L) = Long

(S) = String

(S-EX) = StringEX

Note that you do not see the DataCode in here - this has already been removed in the Sox_OnDataArrival sub since you do not need it anymore.

As for packing data, it is as simple as using the following commands:

Put_Byte - packs a byte into the buffer

Put_Integer - packs an integer

Put_Long - packs a long

Put_String - packs a string 1 to 255 characters long

Put_StringEX - packs a string 1 to 65,535 characters long

These are all self-explanatory besides String and StringEX. The difference between these two is their identifier size. Strings are variable-length, unlike a byte or long, so you have to pack an extra variable to say how long the string is. Put_String packs a byte before the string to say how long the string is (thus why a max of 255 characters long) while StringEX packs an integer (thus why a max of 65535 characters long) to identify how long the string is. This is all handled by the Databuffer, though, so you only have to remember that String is short strings, StringEX is long strings.

To receive the data you send, just use Get in the same order as you used Put for that DataCode. Make sure you always place the DataCode in the buffer before you place in any other information.

This is an article I wrote for the Game Programming Wiki which covers this topic, along with other topics about packet optimization, called Optimizing Your Online Game Bandwidth.

Personal tools