Skip to content

Commit 432c910

Browse files
committed
[DataBuffer] Scripting can now use .SendData and .GetDataAndAppend
The bot has been refactored such that the interfaces for sending and receiving packets through the DataBuffer can now be used through scripting in the same way the bot does. API: Buffer.SendData(Socket, [PacketID], [ServerType], [HeaderType]) - By default, this sends the buffer over the socket unchanged. - If the HeaderType supports it, PacketID will be used in a pre-pended header. HeaderType values: 0 = phtNONE: no header 3 = phtMCP: MCP/BNLS-like 3-byte header 4 = phtBNCS: BNCS-like 4-byte header - If ServerType is non-zero, the packet will be logged to your packet log and cache. Buffer.GetDataAndAppend(Socket, Length) - Reads up to Length bytes off the socket as a Byte(). This is locale-independent. - Appends them to the end of Buffer. Packet.GetPacketLength(HeaderLenStart) - Returns an Integer (16-bit value) from position 0, 1, or 2 from the start of the packet. Returns 0 for any other HeaderLenStart. Does not change the value of the Buffer.Position property. For the use of several Battle.net-like binary protocols. Buffer.IsFullPacket(HeaderLenStart) - Returns True if this is a full packet, False if not, if invalid HeaderLenStart, or if empty buffer. Does not change the value of the Buffer.Position property. For the use of several Battle.net-like binary protocols. Buffer.TakePacket(HeaderLenStart) - Returns a new DataBuffer with a completed packet. The DataBuffer is empty if IsFullPacket() would have failed. For the use of several Battle.net-like binary protocols. Suggested usage: ' Example: Data buffering in a script made to connect to vL's BotNet service ' Headers are of the form: ' (UINT8) Protocol version (0x01) ' (UINT8) Packet ID ' (UINT16) Packet length Sub BotNetSock_DataArrival(Length) Dim Packet RecvBuffer.GetDataAndAppend BotNetSock, Length Do While RecvBuffer.IsFullPacket(2) Set Packet = RecvBuffer.TakePacket(2) Call Recv_PacketSwitch(Packet) Set Packet = Nothing Loop End Sub
1 parent 00c3657 commit 432c910

File tree

5 files changed

+92
-98
lines changed

5 files changed

+92
-98
lines changed

trunk/clsDataBuffer.cls

Lines changed: 55 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -380,7 +380,7 @@ Public Function DebugOutput(Optional ByVal Start As Long = 0, Optional ByVal Len
380380
DebugOutput = modPacketBuffer.DebugOutput(Data, Start, Length)
381381
End Function
382382

383-
Public Function SendData(ByRef Socket As Winsock, Optional ByVal PacketID As Byte, Optional ByVal ServerType As enuServerTypes = stBNCS, Optional ByVal HeaderType As enuPacketHeaderTypes = phtBNCS) As Boolean
383+
Public Function SendData(ByVal Socket As Winsock, Optional ByVal PacketID As Byte, Optional ByVal ServerType As enuServerTypes = stGEN, Optional ByVal HeaderType As enuPacketHeaderTypes = phtNONE) As Boolean
384384
If (Socket Is frmChat.sckBNet) Then
385385
' catch packets being sent with their header
386386
If HeaderType = phtNONE And m_bufsize >= 4 Then PacketID = m_buf(1)
@@ -426,6 +426,14 @@ Public Function vLSendPacket(Optional ByVal PacketID As Byte) As Boolean
426426
vLSendPacket = Me.SendData(frmChat.sckBNLS, PacketID, stBNLS, phtMCP)
427427
End Function
428428

429+
Public Sub GetDataAndAppend(ByVal Socket As Winsock, Optional ByVal Length As Long)
430+
Dim buf() As Byte
431+
' read buffer as Byte()
432+
Socket.GetData buf(), vbArray + vbByte, Length
433+
' add data to buffer
434+
Me.InsertByteArr buf()
435+
End Sub
436+
429437
Public Function HandleRecvData(Optional ByRef PacketID As Byte, Optional ByRef PacketLength As Long, Optional ByVal ServerType As enuServerTypes = stBNCS, Optional ByVal HeaderType As enuPacketHeaderTypes = phtBNCS, Optional ByVal ScriptSource As Boolean = False) As Boolean
430438
Dim HasPacketID As Boolean
431439

@@ -459,76 +467,74 @@ Public Function FindByte(ByVal Value As Integer) As Long
459467
FindByte = 0
460468
End Function
461469

462-
Public Function IsFullPacket(Optional ByVal st As enuServerTypes = stBNCS) As Boolean
470+
Public Function IsFullPacket(ByVal HeaderLenStart As Integer) As Boolean
463471
Dim lngPacketLen As Long, ByteIndex As Long
464472

465473
IsFullPacket = False
466474

467475
If m_bufsize > 0 Then
468-
ByteIndex = FindByte(&HFF)
476+
lngPacketLen = GetPacketLength(HeaderLenStart)
469477

470-
If st <> stBNCS Or ByteIndex = 1 Then
471-
lngPacketLen = GetPacketLength(st)
472-
473-
If (lngPacketLen = 0) Then
474-
Exit Function
475-
End If
478+
If (lngPacketLen = 0) Then
479+
Exit Function
480+
End If
476481

477-
If (m_bufsize >= lngPacketLen) Then
478-
If lngPacketLen < 10000 Then
479-
IsFullPacket = True
480-
Else
481-
frmChat.AddChat g_Color.ErrorMessageText, "Error: Packet Length of unusually high Length detected! Packet " & _
482-
"dropped. Buffer content at this time: " & vbCrLf & DebugOutput()
483-
484-
Call Clear
485-
End If
486-
End If
487-
Else
488-
frmChat.AddChat g_Color.ErrorMessageText, "Error: The front of the buffer is not a valid packet!"
489-
490-
If MDebug("showdrops") Then
491-
frmChat.AddChat g_Color.ErrorMessageText, "Error: The front of the buffer is not " & _
492-
"a valid packet!"
493-
frmChat.AddChat g_Color.ErrorMessageText, "The following data is being purged:"
494-
495-
If ByteIndex > 0 Then
496-
frmChat.AddChat g_Color.ErrorMessageText, Space$(1) & DebugOutput()
497-
Else
498-
frmChat.AddChat g_Color.ErrorMessageText, Space$(1) & DebugOutput()
499-
End If
500-
End If
501-
502-
If ByteIndex > 0 Then
503-
m_bufpos = ByteIndex
504-
Call Clear
482+
If (m_bufsize >= lngPacketLen) Then
483+
If lngPacketLen < 10000 Then
484+
IsFullPacket = True
505485
Else
486+
frmChat.AddChat g_Color.ErrorMessageText, "Error: Packet Length of unusually high Length detected! Packet " & _
487+
"dropped. Buffer content at this time: " & vbCrLf & DebugOutput()
488+
506489
Call Clear
507490
End If
508491
End If
492+
'Else
493+
' frmChat.AddChat g_Color.ErrorMessageText, "Error: The front of the buffer is not a valid packet!"
494+
'
495+
' If MDebug("showdrops") Then
496+
' frmChat.AddChat g_Color.ErrorMessageText, "Error: The front of the buffer is not " & _
497+
' "a valid packet!"
498+
' frmChat.AddChat g_Color.ErrorMessageText, "The following data is being purged:"
499+
'
500+
' If ByteIndex > 0 Then
501+
' frmChat.AddChat g_Color.ErrorMessageText, Space$(1) & DebugOutput()
502+
' Else
503+
' frmChat.AddChat g_Color.ErrorMessageText, Space$(1) & DebugOutput()
504+
' End If
505+
' End If
506+
'
507+
' If ByteIndex > 0 Then
508+
' m_bufpos = ByteIndex
509+
' Call Clear
510+
' Else
511+
' Call Clear
512+
' End If
513+
'End If
509514
End If
510515
End Function
511516

512-
Public Function GetPacketLength(Optional ByVal st As enuServerTypes = stBNCS) As Long
517+
Public Function GetPacketLength(ByVal HeaderLenStart As Integer) As Long
513518
Dim Value As Long
514-
515-
Select Case st
516-
Case stBNCS
517-
Value = GetDWord(True)
518-
Value = CLng(Value \ 65536)
519-
Case stBNLS, stMCP
520-
Value = GetWord(True)
521-
End Select
522-
519+
If HeaderLenStart = 0 Then
520+
Value = GetWord(True)
521+
ElseIf HeaderLenStart = 2 Then
522+
Value = GetDWord(True)
523+
Value = CLng(Value \ &H10000)
524+
ElseIf HeaderLenStart = 1 Then
525+
' this one's unlikely but...
526+
Value = GetDWord(True)
527+
Value = CLng(Value \ &H100) And &HFFFF&
528+
End If
523529
GetPacketLength = Value
524530
End Function
525531

526-
Public Function TakePacket(Optional ByVal st As enuServerTypes = stBNCS) As clsDataBuffer
532+
Public Function TakePacket(ByVal HeaderLenStart As Integer) As clsDataBuffer
527533
Dim tmpbuf() As Byte
528534
Dim lngPacketLen As Long
529535
Dim pBuff As clsDataBuffer
530536

531-
lngPacketLen = GetPacketLength(st)
537+
lngPacketLen = GetPacketLength(HeaderLenStart)
532538

533539
' returns new buffer
534540
Set TakePacket = New clsDataBuffer

trunk/frmChat.frm

Lines changed: 22 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -5966,23 +5966,19 @@ End Sub
59665966

59675967
Private Sub sckMCP_DataArrival(ByVal bytesTotal As Long)
59685968
On Error GoTo ERROR_HANDLER
5969-
5970-
Dim buf() As Byte
5969+
59715970
Dim pBuff As clsDataBuffer
5972-
5971+
59735972
If bytesTotal = 0 Then Exit Sub
5974-
5975-
' read buffer as Byte()
5976-
sckMCP.GetData buf(), vbArray + vbByte, bytesTotal
5977-
' add data to buffer
5978-
ReceiveBuffer(stMCP).InsertByteArr buf()
5973+
5974+
ReceiveBuffer(stMCP).GetDataAndAppend sckMCP, bytesTotal
59795975

59805976
If ProxyConnInfo(stMCP).IsUsingProxy And ProxyConnInfo(stMCP).Status <> psOnline Then
59815977
Call modProxySupport.ProxyRecvPacket(sckMCP, ProxyConnInfo(stMCP), ReceiveBuffer(stMCP))
59825978
Else
5983-
Do While ReceiveBuffer(stMCP).IsFullPacket(stMCP)
5979+
Do While ReceiveBuffer(stMCP).IsFullPacket(0)
59845980
' retrieve MCP packet
5985-
Set pBuff = ReceiveBuffer(stMCP).TakePacket(stMCP)
5981+
Set pBuff = ReceiveBuffer(stMCP).TakePacket(0)
59865982
' if MCP handler exists, parse
59875983
If Not ds.MCPHandler Is Nothing Then
59885984
Call ds.MCPHandler.MCPRecvPacket(pBuff)
@@ -5991,7 +5987,7 @@ Private Sub sckMCP_DataArrival(ByVal bytesTotal As Long)
59915987
Set pBuff = Nothing
59925988
Loop
59935989
End If
5994-
5990+
59955991
Exit Sub
59965992

59975993
ERROR_HANDLER:
@@ -6431,7 +6427,7 @@ Sub Connect()
64316427
Exit Sub
64326428
End If
64336429

6434-
For i = 0 To 2
6430+
For i = LBound(ProxyConnInfo) To UBound(ProxyConnInfo)
64356431
ProxyConnInfo(i).IsUsingProxy = ProxyConnInfo(i).UseProxy
64366432
If ProxyConnInfo(i).IsUsingProxy And (ProxyConnInfo(i).ProxyPort = 0 Or LenB(ProxyConnInfo(i).ProxyIP) = 0) Then
64376433
MsgBox "You have selected to use a proxy for one or more connections, but no proxy is configured. Please set one up in the Advanced " & _
@@ -7239,7 +7235,7 @@ Sub ReloadConfig(Optional Mode As Byte = 0)
72397235
Call UpdateListviewTabs
72407236
End If
72417237

7242-
For i = 0 To 2
7238+
For i = LBound(ProxyConnInfo) To UBound(ProxyConnInfo)
72437239
With ProxyConnInfo(i)
72447240
.ServerType = i
72457241
Select Case i
@@ -7429,17 +7425,13 @@ Private Sub sckBNet_DataArrival(ByVal bytesTotal As Long)
74297425
#If (COMPILE_DEBUG <> 1) Then
74307426
On Error GoTo ERROR_HANDLER
74317427
#End If
7432-
7433-
Dim buf() As Byte
7428+
74347429
Dim pBuff As clsDataBuffer
7435-
7430+
74367431
If bytesTotal = 0 Then Exit Sub
74377432

7438-
' read buffer as Byte()
7439-
sckBNet.GetData buf(), vbArray + vbByte, bytesTotal
7440-
' add data to buffer
7441-
ReceiveBuffer(stBNCS).InsertByteArr buf()
7442-
7433+
ReceiveBuffer(stBNCS).GetDataAndAppend sckBNet, bytesTotal
7434+
74437435
If ProxyConnInfo(stBNCS).IsUsingProxy And ProxyConnInfo(stBNCS).Status <> psOnline Then
74447436
Call modProxySupport.ProxyRecvPacket(sckBNet, ProxyConnInfo(stBNCS), ReceiveBuffer(stBNCS))
74457437
Else
@@ -7449,16 +7441,16 @@ Private Sub sckBNet_DataArrival(ByVal bytesTotal As Long)
74497441
AutoReconnectTry = 0
74507442
End If
74517443

7452-
Do While ReceiveBuffer(stBNCS).IsFullPacket(stBNCS)
7444+
Do While ReceiveBuffer(stBNCS).IsFullPacket(2)
74537445
' retrieve BNLS packet
7454-
Set pBuff = ReceiveBuffer(stBNCS).TakePacket(stBNCS)
7446+
Set pBuff = ReceiveBuffer(stBNCS).TakePacket(2)
74557447
' parse
74567448
Call modBNCS.BNCSRecvPacket(pBuff)
74577449
' clean up
74587450
Set pBuff = Nothing
74597451
Loop
74607452
End If
7461-
7453+
74627454
Exit Sub
74637455

74647456
ERROR_HANDLER:
@@ -7573,30 +7565,26 @@ End Sub
75737565

75747566
Private Sub sckBNLS_DataArrival(ByVal bytesTotal As Long)
75757567
On Error GoTo ERROR_HANDLER
7576-
7577-
Dim buf() As Byte
7568+
75787569
Dim pBuff As clsDataBuffer
75797570

75807571
If bytesTotal = 0 Then Exit Sub
7581-
7582-
' read buffer as Byte()
7583-
sckBNLS.GetData buf(), vbArray + vbByte, bytesTotal
7584-
' add data to buffer
7585-
ReceiveBuffer(stBNLS).InsertByteArr buf()
7572+
7573+
ReceiveBuffer(stBNLS).GetDataAndAppend sckBNLS, bytesTotal
75867574

75877575
If ProxyConnInfo(stBNLS).IsUsingProxy And ProxyConnInfo(stBNLS).Status <> psOnline Then
75887576
Call modProxySupport.ProxyRecvPacket(sckBNLS, ProxyConnInfo(stBNLS), ReceiveBuffer(stBNLS))
75897577
Else
7590-
Do While ReceiveBuffer(stBNLS).IsFullPacket(stBNLS)
7578+
Do While ReceiveBuffer(stBNLS).IsFullPacket(0)
75917579
' retrieve BNLS packet
7592-
Set pBuff = ReceiveBuffer(stBNLS).TakePacket(stBNLS)
7580+
Set pBuff = ReceiveBuffer(stBNLS).TakePacket(0)
75937581
' parse
75947582
Call modBNLS.BNLSRecvPacket(pBuff)
75957583
' clean up
75967584
Set pBuff = Nothing
75977585
Loop
75987586
End If
7599-
7587+
76007588
Exit Sub
76017589

76027590
ERROR_HANDLER:

trunk/modGlobals.bas

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@ Public ProductList(12) As udtProductInfo
2121
Public g_Queue As New clsQueue
2222
Public g_OSVersion As New clsOSVersion
2323
Public SharedScriptSupport As New clsScriptSupportClass
24-
Public ReceiveBuffer(0 To 2) As clsDataBuffer
25-
Public ProxyConnInfo(0 To 2) As udtProxyConnectionInfo
24+
Public ReceiveBuffer(1 To 3) As clsDataBuffer
25+
Public ProxyConnInfo(1 To 3) As udtProxyConnectionInfo
2626

2727
Public ConfigOverride As String
2828
Public CommandLine As String

trunk/modPacketBuffer.bas

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,12 @@ Option Explicit
77
Private Const MAX_PACKET_CACHE_SIZE = 100
88

99
Public Enum enuServerTypes
10-
stBNCS = 0
11-
stBNLS = 1
12-
stMCP = 2
13-
stBNFTP = 3
14-
stPROXY = 4
10+
stGEN = 0
11+
stBNCS = 1
12+
stBNLS = 2
13+
stMCP = 3
14+
stBNFTP = 4
15+
stPROXY = 5
1516
End Enum
1617

1718
Public Enum enuPacketHeaderTypes
@@ -63,6 +64,7 @@ End Function
6364

6465
Public Function NamePacketType(ByVal PktType As enuServerTypes) As String
6566
Select Case PktType
67+
Case stGEN: NamePacketType = "SCRIPTING"
6668
Case stBNCS: NamePacketType = "BNCS"
6769
Case stBNLS: NamePacketType = "BNLS"
6870
Case stMCP: NamePacketType = "MCP"
@@ -225,7 +227,7 @@ End Function
225227
' PacketType: value sent to NamePacketType() shown in packet logs
226228
' HeaderType: what kind of header to prepend
227229
Public Function SendData(ByRef Data() As Byte, ByVal DataLen As Long, _
228-
ByVal HasPktID As Boolean, Optional ByVal PktID As Byte, Optional ByRef Socket As Winsock, _
230+
ByVal HasPktID As Boolean, Optional ByVal PktID As Byte, Optional ByVal Socket As Winsock, _
229231
Optional ByVal PktType As enuServerTypes, Optional ByVal HeaderType As enuPacketHeaderTypes) As Boolean
230232
Dim buf() As Byte
231233
Dim HLen As Byte
@@ -285,9 +287,11 @@ Public Function SendData(ByRef Data() As Byte, ByVal DataLen As Long, _
285287
Socket.SendData buf
286288

287289
' only log if sent
288-
Pkt = MakePacket(buf, PktLen, HasPktID, PktID, PktType, CtoS)
289-
Call CachePacket(Pkt)
290-
Call WritePacketData(Pkt)
290+
If PktType <> stGEN Then
291+
Pkt = MakePacket(buf, PktLen, HasPktID, PktID, PktType, CtoS)
292+
Call CachePacket(Pkt)
293+
Call WritePacketData(Pkt)
294+
End If
291295
End If
292296
End Function
293297

trunk/modParsing.bas

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,6 @@ Option Explicit
33

44
Public Const COLOR_BLUE2 = 12092001
55

6-
Public Sub SendHeader()
7-
frmChat.sckBNet.SendData ChrW(1)
8-
End Sub
9-
106
Public Function StrToHex(ByVal String1 As String, Optional ByVal NoSpaces As Boolean = False) As String
117
Dim strTemp As String, strReturn As String, i As Long
128

0 commit comments

Comments
 (0)