VERSION 1.0 CLASS
BEGIN
  MultiUse = -1  'True
  Persistable = 0  'NotPersistable
  DataBindingBehavior = 0  'vbNone
  DataSourceBehavior  = 0  'vbNone
  MTSTransactionMode  = 0  'NotAnMTSObject
END
Attribute VB_Name = "clsPacketBuffer"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = True
Attribute VB_PredeclaredId = False
Attribute VB_Exposed = False

Option Base 0
Option Explicit
Option Compare Text

Public pData As String, pLen As Long, Buffer As String
    
'CopyMemory Functions

Public Function MakeMemory(ByVal Data As Long, ByVal Length As Long) As String
Dim strOut As String
    strOut = String$(Length, vbNullChar)
    CopyMemory ByVal strOut, Data, Length
    MakeMemory = strOut
End Function

Public Function GetMemory(ByVal Data As String) As Long
    CopyMemory GetMemory, ByVal Data, Len(Data)
End Function

'Server > Client Parser

Public Function SetData(ByVal Data As String) As Long
    pData = Data
    pLen = 5
    SetData = GetMemory(Mid$(Data, 2, 1))
End Function


Public Sub Skip(ByVal Bytes As Long)
    pLen = pLen + Bytes
End Sub

Public Sub Back(ByVal Bytes As Long)
    pLen = pLen - Bytes
End Sub

Public Function GetDWORD() As Long
    GetDWORD = GetMemory(Mid$(pData, pLen, 4))
    Skip 4
End Function

Public Function GetWORD() As Long
    GetWORD = GetMemory(Mid$(pData, pLen, 2))
    Skip 2
End Function

Public Function GetBYTE() As Byte
    GetBYTE = CByte(GetMemory(Mid$(pData, pLen, 1)))
    Skip 1
End Function

Public Function GetSTRING() As String
Dim i As Long
    i = InStr(Mid$(pData, pLen), vbNullChar)
    If i Then GetSTRING = Mid$(pData, pLen, i - 1)
    Skip i
End Function

Public Function GetFixedSTRING(ByVal Data As Long) As String
    GetFixedSTRING = Mid$(pData, pLen, Data)
    Skip Data
End Function

'Client > Server Parser

Public Sub InsertDWORD(ByVal Data As Long)
    Buffer = Buffer & MakeMemory(Data, 4)
End Sub

Public Sub InsertWORD(ByVal Data As Long)
    Buffer = Buffer & MakeMemory(Data, 2)
End Sub

Public Sub InsertBYTE(ByVal Data As Long)
    Buffer = Buffer & MakeMemory(Data, 1)
End Sub

Public Sub InsertSTRING(ByVal Data As String)
    Buffer = Buffer & Data & vbNullChar
End Sub

Public Sub InsertVOID(ByVal Data As String)
    Buffer = Buffer & Data
End Sub

Public Sub Send(ByVal PacketID As Long)
    If frmMain.WS.State = sckConnected Then frmMain.WS.SendData Chr$(&HFF) & Chr$(PacketID) & MakeMemory(Len(Buffer) + 4, 2) & Buffer
    Buffer = vbNullString
End Sub
