Accompanying files

This section includes many references to a byte array packing class module I made for my engine. This is not the only way to approach it, but it is easier to explain through only one approach, especially when I have the code for it.

You can find the code here.

Byte array packing

To send binary packets, they must be first converted into an array of bytes before being sent. For example, a Visual Basic 6 long (4-bytes) has to be converted into 4 individual bytes before being sent, and converted back into a long after being sent. Though, with the CopyMemory API, this is quite easy:

	Dim i As Long
	Dim b(0 To 3) As Byte
	i = 1203981	'Set a random value
	CopyMemory b(0), i, 4
	

It is quite easy to make your code overly-bulky with redundant typing keeping all of that in-line with the rest of the code, though. There are many ways to ease this process, but my personal approach was to create a class module, and for every individual buffer I needed, just create a new instance of it.

	Dim Buffer As New DataBuffer
	

Packing variables into a byte array, as you could imagine, adds a bit of overhead to every packet sent, just like combining a bunch of strings together adds overhead. For the most part, it isn't anything you need to worry about, as long as you use it correctly. Most online games require you to send packets to certain groups of players, whether it is all the players in the game, on the map, or to a certain index. One approach to holding their packets would be to specify the group to send to, and the variable to put in.

	Public Sub SendLongToAll(ByVal Value As Long)
	Dim i As Long
	For i = 1 To NumberOfPlayers
	   '.Buffer is a DataBuffer
	   Player(i).Buffer.Put_Long Value
	Next i
	End Sub
	

As you can see, though, this requires every player putting the long into their buffer. Doing this for every variable can get quite CPU-intensive. A better approach is to create a buffer for doing conversions to byte arrays, then put the byte arrays into the user's buffer (as explained in the Packet Buffering section).

	Dim ConBuf As New DataBuffer
	ConBuf.Pre_Allocate 6 'Allocate 6 bytes of memory in the buffer
	ConBuf.Put_Byte 123 'Random values
	ConBuf.Put_Long 123098123
	ConBuf.Put_Byte 123
	SendToAll ConBuf.Get_Buffer
	
	Public Sub SendToAll(ByRef Data() As Byte)
	Dim DataSize As Long
	Dim WritePos As Long
	Dim i As Long
	DataSize = UBound(Data) + 1 '+ 1 because we have to include array index 0
	For i = 1 To NumberOfPlayers
	    'Get the position to write to
		WritePos = UBound(Player(i).Buffer)
	    'Allocate the memory in the buffer
		ReDim Preserve Player(i).Buffer(0 to WritePos + DataSize)
		'.Buffer is a byte array
		CopyMemory Player(i).Buffer(WritePos + 1), Data(0), DataSize
	Next i
	

This way, all we have to do is allocate the memory once, and copy the memory from the data passed into the player's buffer. It may require a lot of playing with the data buffer to get used to how it works if you are used to sending strings, but you will quickly learn to love it and all the flexibility it gives you.