• Welcome to Valhalla Legends Archive.
 

[VB] IncommingPacket Class

Started by Joe[x86], November 19, 2005, 09:34 PM

Previous topic - Next topic

Joe[x86]

Yeah, I was having trouble splitting packets apart so I wrote this for JBBE. Basically, add to it when you recieve data. Then check for a packet, and if theres a whole one, parse it. Then check again, and again, until it returns false, when you exit the sub.

Option Explicit

Private m_sBuffer As String

Public Sub Add(sData As String)
    'Debug.Print "added data: " & sData
    m_sBuffer = m_sBuffer & sData
End Sub

Public Function HasPacket() As Boolean
    'Debug.Print "HasPacket(). Current buffer: " & m_sBuffer
    If Len(m_sBuffer) < 4 Then HasPacket = False: Exit Function
    Dim Length As Integer: Length = GetWORD(Mid(m_sBuffer, 3, 2))
    If Len(m_sBuffer) >= Length Then
        HasPacket = True
    Else
        HasPacket = False
    End If
End Function

Public Function GetPacket() As String
    'Debug.Print "GetPacket(). HasPacket: " & HasPacket
    If HasPacket Then
        Dim Length As Integer: Length = GetWORD(Mid(m_sBuffer, 3, 2))
        GetPacket = Mid(m_sBuffer, 1, Length)
        m_sBuffer = Mid(m_sBuffer, Length + 1)
    End If
End Function


Usage:
Public Sub PParse(sData As String)
    IncommingPacket.Add sData
Start:
    If IncommingPacket.HasPacket Then Call Parse(IncommingPacket.GetPacket) Else Exit Sub
    GoTo Start
End Sub


Enjoy!
Quote from: brew on April 25, 2007, 07:33 PM
that made me feel like a total idiot. this entire thing was useless.