Gnutella Forums

Gnutella Forums (https://www.gnutellaforums.com/)
-   General Gnutella Development Discussion (https://www.gnutellaforums.com/general-gnutella-development-discussion/)
-   -   Gnutella Code (https://www.gnutellaforums.com/general-gnutella-development-discussion/8153-gnutella-code.html)

Joetella February 14th, 2002 02:51 PM

Gnutella Code
 
I never see any Code posted here ... just a lot of questions.. so here is a my VB Timer Event that handles connected peers and messages:

Any Questions?

Code:


Private Sub Peer_Timer(Index As Integer)
 
  Dim lbufferlength As Long
  Dim dPayloadlength As Double
  Dim bPacket() As Byte
  Dim bNothing() As Byte
  Dim bMessageHeader(0 To 22) As Byte
  Dim lBytesRead As Long
  Dim lSize As Long
  Dim lPos As Long
 
  On Error GoTo HandleError

  msPeerInfo(Index).Pinger = msPeerInfo(Index).Pinger + 1
   
  If msPeerInfo(Index).Connected = True Then
      If msPeerInfo(Index).Payload = 0 Then
        If Peer(Index).IsReadable = True And Peer(Index).RecvNext > 0 Then
           
' Read Message Header First
            lSize = msPeerInfo(Index).BytesNeeded
            ReDim bPacket(0 To lSize - 1)
            lBytesRead = Peer(Index).ReadBytes(bPacket, lSize)
            If lBytesRead > 1 Then
              msPeerInfo(Index).bytes = msPeerInfo(Index).bytes + lBytesRead
              mlBandwithIN = mlBandwithIN + lBytesRead
              If lBytesRead = 23 Then
                  If bPacket(MESSAGE_FUNCTION) = 0 Then
       
' Ping Message, go process asap
                    Call ProcessMessage(Index, bPacket, bNothing)
                    If msPeerInfo(Index).Pinger Mod 50 = 0 Then
                        Call UpdateCount(Index)
                    End If
                    msPeerInfo(Index).BytesNeeded = 23
                    Exit Sub
                  Else
                    msPeerInfo(Index).MessageHeader = bPacket
                  End If
              End If
              If lBytesRead < 23 Then
                  If msPeerInfo(Index).BytesNeeded = 23 Then
                    ReDim msPeerInfo(Index).MessageHeader(0 To 22)
                  End If
                 
                  For lPos = 0 To lBytesRead - 1
                    msPeerInfo(Index).MessageHeader(23 - msPeerInfo(Index).BytesNeeded + lPos) = bPacket(lPos)
                  Next lPos
                 
                  msPeerInfo(Index).BytesNeeded = msPeerInfo(Index).BytesNeeded - lBytesRead
                  If msPeerInfo(Index).BytesNeeded > 0 Then
                    If msPeerInfo(Index).Pinger Mod 50 = 0 Then
                        Call UpdateCount(Index)
                    End If
                    Exit Sub
                  Else
                    If msPeerInfo(Index).MessageHeader(MESSAGE_FUNCTION) = 0 Then
       
' Ping Message, go process asap
                        Call ProcessMessage(Index, msPeerInfo(Index).MessageHeader, bNothing)
                        If msPeerInfo(Index).Pinger Mod 50 = 0 Then
                          Call UpdateCount(Index)
                        End If
                        msPeerInfo(Index).BytesNeeded = 23
                        Exit Sub
                    End If
                  End If
              End If
            End If
                   
            dPayloadlength = ByteToSingle(msPeerInfo(Index).MessageHeader, PAYLOAD_START, 4)
            If dPayloadlength >= 65534 Then
              Call LogError("OverSize Error " & ByteToString(msPeerInfo(Index).MessageHeader, 0, UBound(msPeerInfo(Index).MessageHeader)) & " " & Peer(Index).Tag)
              Peer(Index).Flush
              msPeerInfo(Index).BytesNeeded = 23
              msPeerInfo(Index).Payload = 0
              Exit Sub
            End If
            msPeerInfo(Index).Payload = dPayloadlength
            msPeerInfo(Index).BytesNeeded = dPayloadlength
        End If
      End If
       
' read Payload of Message
      If msPeerInfo(Index).Payload > 0 Then
        If Peer(Index).IsReadable = True And Peer(Index).RecvNext > 0 Then
            lSize = msPeerInfo(Index).BytesNeeded
            ReDim bPacket(0 To lSize - 1)
            lBytesRead = Peer(Index).ReadBytes(bPacket, lSize)
            If lBytesRead > 0 Then
              msPeerInfo(Index).bytes = msPeerInfo(Index).bytes + lBytesRead
              mlBandwithIN = mlBandwithIN + lBytesRead
              If lBytesRead = msPeerInfo(Index).Payload Then
                  msPeerInfo(Index).MessagePayload = bPacket
                  Call ProcessMessage(Index, msPeerInfo(Index).MessageHeader, msPeerInfo(Index).MessagePayload)
                  msPeerInfo(Index).BytesNeeded = 23
                  msPeerInfo(Index).Payload = 0
              Else
                  If msPeerInfo(Index).Payload = msPeerInfo(Index).BytesNeeded Then
                    ReDim msPeerInfo(Index).MessagePayload(0 To msPeerInfo(Index).Payload - 1)
                  End If
                 
                  For lPos = 0 To lBytesRead - 1
                    msPeerInfo(Index).MessagePayload(msPeerInfo(Index).Payload - msPeerInfo(Index).BytesNeeded + lPos) = bPacket(lPos)
                  Next lPos
                  msPeerInfo(Index).BytesNeeded = msPeerInfo(Index).BytesNeeded - lBytesRead
                   
                  If msPeerInfo(Index).BytesNeeded = 0 Then
                    Call ProcessMessage(Index, msPeerInfo(Index).MessageHeader, msPeerInfo(Index).MessagePayload)
                    msPeerInfo(Index).BytesNeeded = 23
                    msPeerInfo(Index).Payload = 0
                  End If
              End If
            End If
        End If
      End If
  Else
     
' if we did not handshake connection string yet, we only need 13 bytes to get connect string
      lbufferlength = Peer(Index).RecvNext
      If lbufferlength >= 13 And msPeerInfo(Index).ConnectType = 2 Then
        If Peer(Index).IsReadable = True Then
            lSize = lbufferlength
            ReDim bPacket(0 To lSize - 1)
            lBytesRead = Peer(Index).ReadBytes(bPacket, lSize)
            mlBandwithIN = mlBandwithIN + lBytesRead
            If ByteCompare(mbGnutellaok, bPacket, 13) = True Then
              msPeerInfo(Index).Connected = True
              msPeerInfo(Index).In = 0
              Call UpdateStatus(Index, "Out")
              miOkHosts = miOkHosts + 1
              msPeerInfo(Index).Idle = 0
              msPeerInfo(Index).BytesNeeded = 23
              msPeerInfo(Index).Payload = 0
              Call Ping(Index, miTTL)
            Else
     
' if our 13 bytes did not match, must be protocol .6 i hope !
              msPeerInfo(Index).Protocol = msPeerInfo(Index).Protocol & ByteToString(bPacket, 0)
              If Right$(msPeerInfo(Index).Protocol, 4) = (vbCrLf & vbCrLf) Then
                  msPeerInfo(Index).Connected = True
                  msPeerInfo(Index).In = 0
                  Call UpdateStatus(Index, "Out")
                  Call CheckUserAgent(Index, msPeerInfo(Index).Protocol)
                  miOkHosts = miOkHosts + 1
                  msPeerInfo(Index).Idle = 0
                  msPeerInfo(Index).BytesNeeded = 23
                  msPeerInfo(Index).Payload = 0
                 
                  Peer(Index).WriteBytes mbNewOk
                  Peer(Index).WriteBytes mbUserAgent
                  Peer(Index).WriteBytes mbCRLF
                 
                  Call Ping(Index, miTTL)
              End If
            End If
        End If
      Else
        If lbufferlength >= 17 And msPeerInfo(Index).ConnectType = 1 Then
            If Peer(Index).IsReadable = True And Peer(Index).IsWritable = True Then
              Peer(Index).ReadBytes bMessageHeader, 17
              mlBandwithIN = mlBandwithIN + 17
              If ByteCompare(bMessageHeader, mbNewConnect) = True Then
                  Call UpdateStatus(Index, "Protocol")
                  msPeerInfo(Index).ConnectType = 3
                  Exit Sub
              Else
                  Call PullThePlug(Index)
              End If
            End If
        End If
       
        If lbufferlength >= 1 And msPeerInfo(Index).ConnectType = 3 Then
            If Peer(Index).IsReadable = True Then
              lSize = lbufferlength
              ReDim bPacket(0 To lSize - 1)
              lBytesRead = Peer(Index).ReadBytes(bPacket, lSize)
              msPeerInfo(Index).Protocol = msPeerInfo(Index).Protocol & ByteToString(bPacket, 0, lBytesRead)
              If Right$(msPeerInfo(Index).Protocol, 2) = (vbLf & vbLf) Then
                 
' old style Connect .4
                  msPeerInfo(Index).Protocol = "GNUTELLA CONNECT/" & msPeerInfo(Index).Protocol
                  msPeerInfo(Index).Connected = True
                  msPeerInfo(Index).In = 0
                  msPeerInfo(Index).Pinger = 1
                  msPeerInfo(Index).BytesNeeded = 23
                  msPeerInfo(Index).Payload = 0
                  Call UpdateStatus(Index, "In")
                  Peer(Index).WriteBytes mbGnutellaok
                  miOkHosts = miOkHosts + 1
                  msPeerInfo(Index).Idle = 0
                  Call Ping(Index, miTTL)
                  Exit Sub
              End If
              If Right$(msPeerInfo(Index).Protocol, 4) = (vbCrLf & vbCrLf) Then
                 
' new style Connect .6
                  msPeerInfo(Index).Connected = True
                  msPeerInfo(Index).In = 0
                  msPeerInfo(Index).Pinger = 1
                  msPeerInfo(Index).BytesNeeded = 23
                  msPeerInfo(Index).Payload = 0
                  Call UpdateStatus(Index, "In")
                  Peer(Index).WriteBytes mbNewOk
                  Peer(Index).WriteBytes mbUserAgent
                  Peer(Index).WriteBytes mbCRLF
                  miOkHosts = miOkHosts + 1
                  msPeerInfo(Index).Idle = 0
                  Call Ping(Index, miTTL)
                 
' Add Host name to Peer Listview
                  Call CheckUserAgent(Index, msPeerInfo(Index).Protocol)
              End If
            End If
        End If
      End If
  End If
   
HandleError:

  Err.Clear
  On Error Resume Next
  If msPeerInfo(Index).Pinger Mod 50 = 0 Then
      Call UpdateCount(Index)
  End If
 
End Sub


Unregistered February 16th, 2002 11:16 AM

my god, why not just write it in C ? it looks like "almost C" or "wanta-be C"
it would run faster too.

Joetella February 19th, 2002 07:38 AM

is trying to write structured code a bad thing? ;)

GregorK February 19th, 2002 08:12 AM

This dosent look structured at all...

Morgwen February 20th, 2002 02:53 AM

Quote:

Originally posted by GregorK
This dosent look structured at all...
Do you have some tips for him?

Morgwen

GregorK February 20th, 2002 05:05 AM

The code is some times nested up to 8 levels or maybe more. In almost every style guide you will read that you should not use more then 3 or maybe 4 levels. Instead try to use sub method calls. This makes things much easier to read and understand.

Also you can sometimes limit nesting with reversing the condition.

Instead of saying:

If example = True Then
If example2 = True Then
...... VERY LONG CODE LIST.....
end if
end if

you can say:

If example = False Then
return
end if
If example2 = False Then
return
end if
...... VERY LONG CODE LIST.....


Don't know the exact syntax but I hope you understand my intention.

Joetella February 20th, 2002 05:20 AM

Quote:

Originally posted by GregorK
The code is some times nested up to 8 levels or maybe more. In almost every style guide you will read that you should not use more then 3 or maybe 4 levels. Instead try to use sub method calls. This makes things much easier to read and understand.

Also you can sometimes limit nesting with reversing the condition.

Instead of saying:

If example = True Then
If example2 = True Then
...... VERY LONG CODE LIST.....
end if
end if

you can say:

If example = False Then
return
end if
If example2 = False Then
return
end if
...... VERY LONG CODE LIST.....


Don't know the exact syntax but I hope you understand my intention.

Just a matter of style I guess .. I actually dont like multiple returns/exit points (however i do have some!) ... and normally would write code like the first example to avoid it !

However I do have to many nested If/Thens .. or flying ducks

Joe

yoselin January 15th, 2012 12:22 AM

I need it code of protocol gnutella. urgent

yoselin January 15th, 2012 12:24 AM

I need it code of protocol gnutella, urgent in c++ or other language of programation.

Lord of the Rings January 23rd, 2012 07:19 AM

I only recently returned from vacation. Perhaps my answer here is helpful? http://www.gnutellaforums.com/genera...tocol-dht.html


All times are GMT -7. The time now is 03:19 AM.

Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2024, vBulletin Solutions, Inc.
SEO by vBSEO 3.6.0 ©2011, Crawlability, Inc.

Copyright © 2020 Gnutella Forums.
All Rights Reserved.