#1
  1. Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Dec 2004
    Location
    Meriden, Connecticut
    Posts
    1,797
    Rep Power
    155

    Help with DataArrival


    I'm not sure exactly how I need to do this. I need for my program to receive the following code. The following code is written in VB6.
    Code:
    Private Sub winsock_DataArrival(ByVal bytesTotal As Long)
        On Error Resume Next
        Static strBuffer As String
        Dim strTemp As String, lngLen As Long
        winsock.GetData strTemp, vbString
        strBuffer = strBuffer & strTemp
        While Len(strBuffer) > 4
          lngLen = Val("&H" & StrToHex(StrReverse(Mid(strBuffer, 3, 2))))
          If Len(strBuffer) < lngLen Then Exit Sub
           parsep (Left(strBuffer, lngLen))
           
          strBuffer = Mid(strBuffer, lngLen + 1)
        Wend
        
       
    End Sub
    Here is that code written in Python.
    Code:
        def DataArrival():
            s.recv(strTemp)
            strBuffer = strBuffer+strTemp
            while len(strBuffer) > 4:
                lngLen = int("0x"+StrToHex(StrReverse(strBuffer[3:2]))
                if len(strBuffer) < lngLen:
                    pass
                ParseP = strBuffer[:lngLen]
                strBuffer = strBuffer[lngLen+1:]
    I know that that code is incorrect. I just need help converting the code properly. Can anyone help me with this?
    Edit: The following code is for StrToHex. Someone in these forums showed me this code, I just can't remember their username.
    Code:
        def StrToHex(str1):
                strReturn = ""
                for i in range(len(str1)):
                        strTemp = hex(ord(str1[i:i + 1]))
                        if len(strTemp) == 1:
                                strTemp = "0" + strTemp
                                strReturn = strReturn +" "+strTemp
                return strReturn
  2. #2
  3. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69
    This may be a little off topic but I thought you might be interested in VB2Py: I haven't used it personally but still, may help and it looks interesting .

    http://vb2py.sourceforge.net/
    programming language development: www.netytan.com Hula

  4. #3
  5. Banned ;)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Nov 2001
    Location
    Woodland Hills, Los Angeles County, California, USA
    Posts
    9,645
    Rep Power
    4248
    Code:
    strBuffer[3:2]
    is not the equivalent of
    Code:
    Mid(strBuffer, 3, 2)
    In python, the 3rd param is the end character+1 position, not the length to read. Also, the start position is 0 based, unlike VB, where the first position is 1. Hence you need:
    strBuffer[2:5]
    Code:
    [mb@ironmaiden cpp]$ python
    Python 2.2.2 (#1, Feb 24 2003, 19:13:11)
    [GCC 3.2.2 20030222 (Red Hat Linux 3.2.2-4)] on linux2
    Type "help", "copyright", "credits" or "license" for more information.
    >>> str = "ABCDEF"
    >>> print str[3:2]
    
    >>> print str[2:5]
    CDE
    >>>
    As you can see, str[3:2] didn't print a thing. Also notice that str[2] = 'C'. In VB, this would have been 'B'.
    Up the Irons
    What Would Jimi Do? Smash amps. Burn guitar. Take the groupies home.
    "Death Before Dishonour, my Friends!!" - Bruce D ickinson, Iron Maiden Aug 20, 2005 @ OzzFest
    Down with Sharon Osbourne

    "I wouldn't hire a butcher to fix my car. I also wouldn't hire a marketing firm to build my website." - Nilpo
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Nov 2003
    Posts
    624
    Rep Power
    35
    I suppose the literal translation would be:
    Code:
    def dataArrival():
        strTemp = s.recv()
        strBuffer += strTemp
    
        while len(strBuffer) > 4:
            lngLen = int(StrToHex(strBuffer[5:3:-1]), 16)
            if len(strBuffer) < lngLen: return
            parsep(strBuffer[:lngLen])
           
            strBuffer = strBuffer[lngLen + 1:]
    If you really want to slice the buffer, convert it to hex, then convert it back again.

    (it wont work, though, because there's nothing in Python to call "dataArrival", and Python doesn't allow you to change non-global variables such as strBuffer inside a function. You need to either declare it as global or rework things into classes.)

    Code:
    def StrToHex(str1):
                strReturn = ""
                for i in range(len(str1)):
                        strTemp = hex(ord(str1[i:i + 1]))
                        if len(strTemp) == 1:
                                strTemp = "0" + strTemp
                                strReturn = strReturn +" "+strTemp
                return strReturn
    hint: this, er, doesn't work. The output of hex() always starts with "0x", so len(strTemp) is never == 1, and so strReturn never gets changed.

    Code:
    def strToHex(str1):
        # Convert characters to ASCII values, then to hex:
        hexlist = [hex(ord(char)) for char in str1]
    
        # (un)Tidy it into VB-esque format, so
        # 0xa becomes 0A, but 0x12 just goes to 12.
        hexlist = [hexnum[2:].zfill(2).upper() for hexnum in hexlist]
    
        # Put it back into a string and return it.
        return ''.join(hexlist)
  8. #5
  9. Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Dec 2004
    Location
    Meriden, Connecticut
    Posts
    1,797
    Rep Power
    155
    I'm sorry I didn't respond sooner. The phone lines and the internet were done for quite a few hours yesterday. I'm not sure if this would help but parsep is also a definition. I'd show you the code but from what Scorpions showed me, all my code is messaed up, I didn't realize that VB6 started at 1 and not 0. Also, sfb, the line of code in the dataArrival, strBuffer += strTemp, is the addition sign really necessary or was that just a typo? I left out a little too much info, because of what happened with the power I couldn't fix it, but I was wondering if I would need to do s.recv(dataArrival()) or something to that extent. So that the socket could receive all of the data under dataArrival(). Can anyone show me what to do with this?

    Edit: I posted the wrong dataArrival code. The one i posted is slightly different than the one I needed help on. The one below is the one I need help on, I think with your (both of you) help I can figure out the proper code. My only two problems are the parsep and the DispatchMessage.
    Code:
    Private Sub Winsock_DataArrival(ByVal bytesTotal As Long)
    On Error GoTo EndDA
    Dim SS1 As Long, sS2 As String, sS3 As String
    Dim i As Integer, i2 As Integer, strTemp As String, lngLen As Long
    Static strBuffer As String
    
    Winsock.GetData strTemp
    
    If vFreeze = 1 Then strTemp = "": Exit Sub
    
        strBuffer = strBuffer & strTemp
        While Len(strBuffer) > 4
        
    strTemp = StrReverse(Mid(strBuffer, 3, 2))
     
        lngLen = Asc(Right(strTemp, 1))
        i = Asc(Left(strTemp, 1)): i2 = 0
        
    If i > 0 Then
    i2 = i
      Do Until i = 0
        lngLen = lngLen + 255
        i = i - 1
      Loop
    End If
              
         If Len(strBuffer) < lngLen Then Exit Sub
         Data = Left(strBuffer, lngLen)
         PacketId = Asc(Mid(Data, 2, 1))
    
      If PacketId = 15 Then
        SS1 = Asc(Mid(Data, 5, 4))
           If LC = 1 Then
             If SS1 = "5" Or SS1 = "23" Then GoTo WSDEnd
           End If
    
            sS3 = Mid(Data, 9, 4): sS3 = Asc(sS3)
            sS2 = Mid(Data, 29): i = InStr(sS2, Chr(0)): If i > 0 Then sS2 = Left(sS2, i - 1)
              If SS1 = "2" And opTrue = 1 Then
                If sS3 = "32" Or sS3 = "48" Then If NoQue = 0 Then AQ "/ban " & sS2 & " IPB": RunData: GoTo WSDEnd Else: QQue = "/ban " & sS2 & " IPB": RunData: GoTo WSDEnd
                If LCase(sS2) = KillName Then If NoQue = 0 Then AQ "/ban " & sS2 & " YBQang!S": RunData: GoTo WSDEnd Else: QQue = "/ban " & sS2 & " YBQang!S": RunData: GoTo WSDEnd
                SS1 = Asc(Mid(Data, 13, 1))
                If PBan = SS1 Then If NoQue = 0 Then AQ "/ban " & sS2 & " PB: " & PBan: sS3 = "-555": RunData: GoTo WSDEnd Else: QQue = "/ban " & sS2 & " PB: " & PBan: sS3 = "-555": RunData: GoTo WSDEnd
                If PBan = -1 And SS1 = 255 Then If NoQue = 0 Then AQ "/ban " & sS2 & " PB: -1": sS3 = "-555": RunData: GoTo WSDEnd Else: QQue = "/ban " & sS2 & " PB: -1": sS3 = "-555": RunData: GoTo WSDEnd
                If PMode = 3 Then If NoQue = 0 Then AQ "/ignore " & sS2: RunData: GoTo WSDEnd Else: QQue = "/ignore " & sS2: RunData: GoTo WSDEnd
                If PMode = 2 Then If NoQue = 0 Then AQ "/ban " & sS2: RunData: GoTo WSDEnd Else: QQue = "/ban " & sS2: RunData: GoTo WSDEnd
                If PMode = 1 Then
                  UA (sS2)
                  If vFlagS = 0 Then If NoQue = 0 Then AQ "/ban " & sS2 & " P1": RunData: GoTo WSDEnd Else: QQue = "/ban " & sS2 & " P1": RunData: GoTo WSDEnd
                  If vFlagS > 0 Then Module2.DispatchMessage Data
                End If
              End If
        
         Module2.DispatchMessage Data: GoTo WSDEnd
      End If
      ParseBnet Data
    WSDEnd:
           If UseFreeze = 1 And vFreeze = 0 Then vFreeze = 1: TmrFreeze.Enabled = True
           strBuffer = Mid(strBuffer, lngLen + 1 + i2)
        Wend
    
    Exit Sub
    EndDA:
    strBuffer = ""
    
    End Sub
    This dataArrival has a lot of useless code, I just pulled it out of some Warcraft3 bot. but where it has DispatchMessage Data, my DispatchMessage is a definition, (def DispatchMessage(), I don't understand where the Data gets put. And also with ParseBnet, that is also a definition (def ParseBnet(), I dont understand where the Data goes for that code either. Here are both definitions:

    Code:
    def DispatchMessage():
                StartEID()
            while EID == 0x07: # Joined channel
                channel = message
                userlist = []
            while EID == 0x08: # stuff
    Well the code would go on and on, one would be userjoin, userleave, usertalk, battle.net info, and so on.
    StartEID code:
    Code:
        def StartEID():
            EID = struct.pack("< I", databuf[5:4])
            flags = struct.pack("< I", databuf[9:4])
            ping = struct.pack("< I", databuf[13:4])
            username = databuf[:29]
            username = username.replace(chr(0), "")
            message = databuf[:len(username)+30]
            message = message.replace(chr(0), "")
    Is it possible that back in dataArrival that DispatchMessage Data makes Data = databuf?
    ParseBnet is a lot like DispatchMessage. I would just like to know where this Data gets put. Thanks for all the help.
  10. #6
  11. Banned ;)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Nov 2001
    Location
    Woodland Hills, Los Angeles County, California, USA
    Posts
    9,645
    Rep Power
    4248
    BTW, the original VB code is a little convoluted by itself:
    Code:
    lngLen = Val("&H" & StrToHex(StrReverse(Mid(strBuffer, 3, 2))))
    What this is doing is:
    1. Taking two characters from position 3.
    2. Reversing them
    3. Converting the number to hex digits.
    4. Tacking on an "&H" in front, to make it like a hex constant (In python, the equiv would be '0x')
    5. Converting the hex constant number back to integer using Val()

    As some of you reading this might have noted... steps 3, 4 and 5 are entirely bogus. Why convert a number to hex, tack on the hex constant identifier and then convert to int again, when you can convert it to int directly with CInt() in VB.

    The python equiv. would be:
    Code:
    To take a substring of a string: 
    str2 = str1[2:4]  # Takes two characters.
    
    To reverse a string:
    str3 = str2[::-1]  # Works on newer versions of python
    
    To convert to an integer:
    str4 = int(str3)
    The remaining operations to convert to hex, tack on a '0x', and convert that hex constant to int are totally unnecessary. You can always combine the three operations into one expression and get the equivalent python for the VB expression:
    Code:
    lngLen = int(strBuffer[2:4][::-1])
    if (len(strBuffer) < lngLen): 
       # Do something here
    Up the Irons
    What Would Jimi Do? Smash amps. Burn guitar. Take the groupies home.
    "Death Before Dishonour, my Friends!!" - Bruce D ickinson, Iron Maiden Aug 20, 2005 @ OzzFest
    Down with Sharon Osbourne

    "I wouldn't hire a butcher to fix my car. I also wouldn't hire a marketing firm to build my website." - Nilpo
  12. #7
  13. Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Dec 2004
    Location
    Meriden, Connecticut
    Posts
    1,797
    Rep Power
    155
    I'm not sure exactly why I lot of the code is like that, but it originally started from some of the best programmers that I've ever seen, they created the code that many programs/bots needed for battle.net. I'm guessing it has to do with battle.net re-checking data or something to that extent.
  14. #8
  15. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Nov 2003
    Posts
    624
    Rep Power
    35
    Also, sfb, the line of code in the dataArrival, strBuffer += strTemp, is the addition sign really necessary or was that just a typo?
    It's necessary - it does the same as strBuffer = strBuffer & strTemp in the VB code. It's a shortcut from having to type strBuffer twice.

    but where it has DispatchMessage Data, my DispatchMessage is a definition, (def DispatchMessage(), I don't understand where the Data gets put.
    I have no idea what a lot of that DataArrival code is doing. I suspect it's passing data off to various other areas of the VB code, like:

    Code:
    If sS3 = "32" Or sS3 = "48" Then If NoQue = 0 Then AQ "/ban " & sS2 & " IPB": RunData: GoTo WSDEnd Else: QQue = "/ban " & sS2 & " IPB": RunData: GoTo WSDEnd
    I don't know what RunData is or what it means to put it there.

    Code:
    Module2.DispatchMessage Data
    It's passing something to Module2.DispatchMessage...

    "I would just like to know where this Data gets put."

    Wherever Module2.DispatchMessage puts it, I guess.


    Eww:
    Code:
    lngLen = Asc(Right(strTemp, 1))
    i = Asc(Left(strTemp, 1)): i2 = 0
        
    If i > 0 Then
    i2 = i
      Do Until i = 0
        lngLen = lngLen + 255
        i = i - 1
      Loop
    End If
    -->

    Code:
    i = Asc(Left(strTemp, 1))
    lngLen = (255 * i) + Asc(Right(strTemp, 1))
    o_O

IMN logo majestic logo threadwatch logo seochat tools logo