• Welcome to Valhalla Legends Archive.
 

New PacketBuffer Class

Started by CupHead, December 17, 2003, 01:36 PM

Previous topic - Next topic

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:

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


Sample output:

-- 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 --



' cPacketBuffer.cls
' [email protected]

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

UserLoser.


Grok

That sucks.

Not really, just thought it should have a different opinion.

dxoigmn

Should add support for arrays of integers/longs/bytes/strings/etc.  

Grok

Quote from: kamakazie on December 18, 2003, 01:10 AM
Should add support for arrays of integers/longs/bytes/strings/etc.  

As well as copying a structure onto the buffer.


'your function
Function BlockCopy( StartAddr As Long, CopyLength As Long) As Long
   'copy a block of memory onto the buffer
   'implement..
End Function


'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

TheMinistered

When clearing the buffer, set the string equal to vbNullString.