Valhalla Legends Forums Archive | Visual Basic Programming | New PacketBuffer Class

AuthorMessageTime
CupHead
The packetbuffer class that I'll be using in the development of the new CSB. It uses pseudo-overloading in order to make adding to the packet easily done in one function. See example at bottom. Make sure to note the CByte() conversion. VB assumes that all numbers in code are vbIntegers unless they're too large for vbInteger, in which case they're automatically turned into vbLongs (&H12345678). Thus, in order to add a DWORD with a value less than 32767 or whatever it is, you need to .Add CLng(1) or whatever your number is. Yes, this is much more strict type checking than you're normally forced to use with VB, but it does make things easier to follow (in my opinion anyway). Lastly, you don't really need all of those cases in the Add routine, it was just useful for debugging, and it's easy to add other types if you need them.

Sample code:
[code]
Private Sub Form_Load()
Dim PB As New cPacketBuffer

PB.Add &H12345678
PB.Add &H1234
PB.Add "Test"
PB.Add CByte(255)
PB.Add "Testing again", True
PB.Display
End Sub
[/code]

Sample output:
[code]
-- Current packet contents --
0000: 78 56 34 12 34 12 54 65 73 74 00 FF 54 65 73 74 xV44Test.˙Test
0010: 69 6E 67 20 61 67 61 69 6E ing again.......
-- End of packet contents --
[/code]

[code]
' cPacketBuffer.cls
' cuphead@valhallalegends.com

Option Explicit

Private Declare Sub CopyMemory Lib "KERNEL32" Alias "RtlMoveMemory" (ByRef Destination As Any, ByRef Source As Any, ByVal numbytes As Long)

Private strBuffer As String

Public Function Clear()
strBuffer = ""
End Function

Public Function Display()
' Requires Grok's DebugOutput function or you will get an error.
Debug.Print "-- Current packet contents --"
Debug.Print DebugOutput(strBuffer)
Debug.Print "-- End of packet contents --"
End Function


Public Function Add(ByRef Data As Variant, Optional ByVal NonNT As Boolean = False)
Select Case VarType(Data)
Case VbVarType.vbArray
Debug.Print "Array"

Case VbVarType.vbBoolean
Debug.Print "Boolean"

Case VbVarType.vbByte
Debug.Print "Byte"
strBuffer = strBuffer & Chr(Data)

Case VbVarType.vbCurrency
Debug.Print "Currency"

Case VbVarType.vbDataObject
Debug.Print "Data Object"

Case VbVarType.vbDate
Debug.Print "Date"

Case VbVarType.vbDecimal
Debug.Print "Decimal"

Case VbVarType.vbDouble
Debug.Print "Double"

Case VbVarType.vbEmpty
Debug.Print "Empty"

Case VbVarType.vbError
Debug.Print "Error"

Case VbVarType.vbInteger
Debug.Print "Integer"
strBuffer = strBuffer & MakeWORD(CInt(Data))

Case VbVarType.vbLong
Debug.Print "Long"
strBuffer = strBuffer & MakeDWORD(CLng(Data))

Case VbVarType.vbNull
Debug.Print "Null"

Case VbVarType.vbObject
Debug.Print "Object"

Case VbVarType.vbSingle
Debug.Print "Single"

Case VbVarType.vbString
Debug.Print "String"
If NonNT = True Then
strBuffer = strBuffer & Data
Else
strBuffer = strBuffer & Data & Chr(0)
End If

Case VbVarType.vbUserDefinedType
Debug.Print "User-Defined Type"

Case VbVarType.vbVariant
Debug.Print "Variant"
End Select
End Function

Private Function MakeDWORD(Value As Long) As String
Dim Result As String * 4
CopyMemory ByVal Result, Value, 4
MakeDWORD = Result
End Function

Private Function MakeWORD(Value As Integer) As String
Dim Result As String * 2
CopyMemory ByVal Result, Value, 2
MakeWORD = Result
End Function
[/code]
December 17, 2003, 7:36 PM
UserLoser.
Very nice
December 17, 2003, 9:43 PM
Grok
That sucks.

Not really, just thought it should have a different opinion.
December 18, 2003, 3:30 AM
kamakazie
Should add support for arrays of integers/longs/bytes/strings/etc.
December 18, 2003, 7:10 AM
Grok
[quote author=kamakazie link=board=31;threadid=4310;start=0#msg36147 date=1071731434]
Should add support for arrays of integers/longs/bytes/strings/etc.
[/quote]

As well as copying a structure onto the buffer.

[code]
'your function
Function BlockCopy( StartAddr As Long, CopyLength As Long) As Long
'copy a block of memory onto the buffer
'implement..
End Function
[/code]
[code]
'their usage of it
Type typWhatever
Var1 As Variant
Var2 As Long
Var3 As String
End Type
Dim MyStruct As typWhatever

If BlockCopy( VarPtr(MyStruct), Len(MyStruct) ) = 0 Then
'seems to have worked
Else
MsgBox "Didn't work copying struct to bufffer", vbExclamation
End If
[/code]
December 18, 2003, 7:18 AM
TheMinistered
When clearing the buffer, set the string equal to vbNullString.
December 20, 2003, 6:29 PM

Search