Author Topic: [BNET/RB] Where to start...  (Read 9064 times)

0 Members and 1 Guest are viewing this topic.

Offline rabbit

  • x86
  • Hero Member
  • *****
  • Posts: 8092
  • I speak for the entire clan (except Joe)
    • View Profile
Re: [BNET/RB] Where to start...
« Reply #15 on: September 01, 2005, 05:59:49 pm »
This is the buffer I am currently using, hope it helps:
Code: [Select]
Option Explicit
Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" _
    (pDst As Any, pSrc As Any, ByVal ByteLen As Long)

Private databuff As String
Private datalen As Integer
Private epos As Integer
Private isOutbuff As Boolean

Public Event buffError(Number As Integer, Description As String, Routine As String)

Private Sub Class_Initialize()
    Call Me.Clear
End Sub

Public Sub SetOutBuff()
    isOutbuff = True
End Sub

Public Sub Clear()
    databuff = ""
    datalen = 0
    epos = 1
End Sub

Public Sub LoadBuffer(ByVal s_Data As String)
    On Error GoTo Handler
   
    Call Me.Clear
    databuff = s_Data
    datalen = Len(s_Data)
   
    On Error GoTo 0
    Exit Sub
   
Handler:
    If isOutbuff Then
        RaiseEvent buffError(Err.Number, Err.Description, "LoadBuffer()")
    End If
End Sub

Public Sub Setepos(place As Integer)
    If place > 0 And place < Len(databuff) Then epos = place
End Sub

Public Function Getepos() As Integer
    Getepos = epos
End Function

Public Sub SendPacket(socket As CSocket, PID As Byte)
    socket.SendData Chr(&HCF) & Chr(PID)
    socket.SendData MakeDBYTE(datalen + 4)
    socket.SendData databuff
   
    Call Clear
End Sub

Public Sub SendPacket2(socket As CSocket, PID As Byte, cbuff As clsBuffer)
    socket.SendData Chr(&HCF) & Chr(PID)
    socket.SendData MakeDBYTE(Len(cbuff.getBuffer()) + 4)
    socket.SendData cbuff.getBuffer()
   
    Call Clear
End Sub

Public Sub InsertSTRING(ByVal s_Data As String)
    On Error GoTo Handler
   
    datalen = datalen + Len(s_Data) + 1
    databuff = Left(databuff, datalen) & s_Data & Chr(0)
   
    On Error GoTo 0
    Exit Sub
   
Handler:
    If isOutbuff Then
        RaiseEvent buffError(Err.Number, Err.Description, "InsertSTRING()")
    End If
End Sub

Public Sub InsertBYTE(ByVal b_Data As Byte)
    On Error GoTo Handler
   
    databuff = databuff & Chr(b_Data)
    datalen = datalen + 1
   
    On Error GoTo 0
    Exit Sub
   
Handler:
    If isOutbuff Then
        RaiseEvent buffError(Err.Number, Err.Description, "InsertBYTE()")
    End If
End Sub

Public Sub InsertDBYTE(ByVal db_Data As Integer)
    On Error GoTo Handler
   
    databuff = databuff & MakeDBYTE(db_Data)
    datalen = datalen + 2
   
    On Error GoTo 0
    Exit Sub
   
Handler:
    If isOutbuff Then
        RaiseEvent buffError(Err.Number, Err.Description, "InsertDBYTE()")
    End If
End Sub

Public Sub InsertWORD(ByVal w_Data As Long)
    On Error GoTo Handler
   
    databuff = databuff & MakeWORD(w_Data)
    datalen = datalen + 4
   
    On Error GoTo 0
    Exit Sub
   
Handler:
    If isOutbuff Then
        RaiseEvent buffError(Err.Number, Err.Description, "InsertWORD()")
    End If
End Sub

Public Function getBuffer() As String
    On Error GoTo Handler
   
    If datalen = 0 Then Exit Function
   
    getBuffer = Left(databuff, datalen)
   
    On Error GoTo 0
    Exit Function
   
Handler:
    If isOutbuff Then
        RaiseEvent buffError(Err.Number, Err.Description, "getBuffer()")
    End If
End Function

Public Function ExtractSTRING() As String
    Dim ret As String
    ret = Mid(databuff, epos, InStr(epos, databuff, Chr(&H0)))
   
    ret = Left(ret, InStr(1, ret, Chr(&H0)))
    epos = epos + Len(ret)
   
    ExtractSTRING = ret
End Function

Public Function ExtractBYTE() As String
    Dim ret As String
    ret = Mid(databuff, epos, 1)
    epos = epos + 1
   
    ExtractBYTE = ret
End Function

Public Function ExtractDBYTE() As String
    Dim ret As String
    ret = Mid(databuff, epos, 2)
    epos = epos + 2
   
    ExtractDBYTE = ret
End Function

Public Function ExtractWORD() As String
    Dim ret As String
    ret = Mid(databuff, epos, 4)
    epos = epos + 4
   
    ExtractWORD = ret
End Function

Public Function ExtractDATA(LENGTH As Long) As String
    Dim ret As String
    ret = Mid(databuff, epos, LENGTH)
    epos = epos + LENGTH
   
    ExtractDATA = ret
End Function

Private Function MakeWORD(Value As Long) As String
    On Error GoTo Handler
   
    Dim Result As String * 4
    CopyMemory ByVal Result, Value, 4
    MakeWORD = Result
   
    On Error GoTo 0
    Exit Function
   
Handler:
    If isOutbuff Then
        RaiseEvent buffError(Err.Number, Err.Description, "MakeWORD()")
    End If
End Function

Private Function MakeDBYTE(Value As Integer) As String
    On Error GoTo Handler
   
    Dim Result As String * 2
    CopyMemory ByVal Result, Value, 2
    MakeDBYTE = Result
   
    On Error GoTo 0
    Exit Function
   
Handler:
    If isOutbuff Then
        RaiseEvent buffError(Err.Number, Err.Description, "MakeDBYTE()")
    End If
End Function

Public Function GetByteAsString(var As String) As String
    Dim k As Long
    Dim ret As String
   
    k = Asc(Left(var, 1))
    If k < 0 Or k > 255 Then
        GetByteAsString = "[error]"
        Exit Function
    End If
   
    ret = Hex(k)
    ret = IIf(Len(ret) = 1, "0", "") & ret
    GetByteAsString = "0x" & ret
End Function

Offline Joe

  • B&
  • Moderator
  • Hero Member
  • *****
  • Posts: 10319
  • In Soviet Russia, text read you!
    • View Profile
    • Github
Re: [BNET/RB] Where to start...
« Reply #16 on: September 02, 2005, 07:39:46 am »
Well, mine use copymemory or cpymem or whatever you want to call it, a Win32 API. You probably won't want to use that. Regaurdless, here they are.

These can easily be written without copymemory, but seeing as how it VB instead of C(++) or whatever they wrote the API in, its a *tad* slower. I've found not to mind it. I'll write these for you, but I'm off to school in a few minutes. I'll catch ya when I get home.

EDIT -
R.a.B.B.i.T, unless you go back into the buffer to dig something out again, I find it much quicker (not compiled, but to write) if you just kill the front of the buffer instead of moving along it with a position marker.
« Last Edit: September 02, 2005, 07:44:13 am by Joe[x86] »
I'd personally do as Joe suggests

You might be right about that, Joe.


Offline rabbit

  • x86
  • Hero Member
  • *****
  • Posts: 8092
  • I speak for the entire clan (except Joe)
    • View Profile
Re: [BNET/RB] Where to start...
« Reply #17 on: September 03, 2005, 02:23:29 am »
Well I use that so I can have multiple things use the same data without duplicating the buffer several times.  Do what you will.

Offline Eric

  • Full Member
  • ***
  • Posts: 304
  • I'm new here!
    • View Profile
Re: [BNET/RB] Where to start...
« Reply #18 on: September 03, 2005, 02:33:33 am »
Well, mine use copymemory or cpymem or whatever you want to call it, a Win32 API. You probably won't want to use that. Regaurdless, here they are.

These can easily be written without copymemory, but seeing as how it VB instead of C(++) or whatever they wrote the API in, its a *tad* slower. I've found not to mind it. I'll write these for you, but I'm off to school in a few minutes. I'll catch ya when I get home.

EDIT -
R.a.B.B.i.T, unless you go back into the buffer to dig something out again, I find it much quiker (not compiled, but to write) if you just kill the front of the buffer instead of moving along it with a position marker.

RtlMoveMemory, or CopyMemory as it's been called since Visual Basic supported the use of API calls, is much faster than any Visual Basic-made clone.
« Last Edit: September 03, 2005, 02:35:05 am by LoRd[nK] »

Offline Joe

  • B&
  • Moderator
  • Hero Member
  • *****
  • Posts: 10319
  • In Soviet Russia, text read you!
    • View Profile
    • Github
Re: [BNET/RB] Where to start...
« Reply #19 on: September 03, 2005, 04:28:17 pm »
I know that. Statistics are great. However, look on the reality side. What end user is really going to sit there and count the miliseconds used by your MakeDWORD method? Unless you do it about 1000 times over, nobody will notice.

Disclaimer:
I didn't actually test it 1000 times. It was an estimate.
I'd personally do as Joe suggests

You might be right about that, Joe.


Offline Eric

  • Full Member
  • ***
  • Posts: 304
  • I'm new here!
    • View Profile
Re: [BNET/RB] Where to start...
« Reply #20 on: September 03, 2005, 06:48:44 pm »
And that's exactly why you'll never become a good programmer.

Offline rabbit

  • x86
  • Hero Member
  • *****
  • Posts: 8092
  • I speak for the entire clan (except Joe)
    • View Profile
Re: [BNET/RB] Where to start...
« Reply #21 on: September 03, 2005, 07:32:30 pm »
I know that. Statistics are great. However, look on the reality side. What end user is really going to sit there and count the miliseconds used by your MakeDWORD method? Unless you do it about 1000 times over, nobody will notice.

Disclaimer:
I didn't actually test it 1000 times. It was an estimate.
API calls were added to VB for a reason, Joe.