Jump to content



Photo

TCP P2P, Event driven!


  • Please log in to reply
14 replies to this topic

#1 twitchyliquid64

twitchyliquid64

    Peace. Always.

  • Active Members
  • PipPipPipPipPipPip
  • 519 posts

Posted 24 September 2010 - 08:40 AM

Hi all,

Firstly, considerable credit must go to Kip, as the event driven TCP is primarily based off his UDF.

Objective of this UDF:
To provide dead-easy functionality for internet/LAN applications that do not need/want a server. In simpler terms, an easy to use P2P engine.

If your confused, a simplistic way of thinking of this would be to think of it as a re-write of kip's TCP UDF, that allows a 'client and server in the same script'. =]

PLEASE READ THE ENTIRE POST IF YOU WANT TO UNDERSTAND HOW TO USE THIS.

How it works:
##################
When you start the UDF, it will start listening and accepting incoming connections. The UDF will, in the backround, continue to manage all your connections and initiate any new ones should you use the _P2P_Connect( IP) function. It will also manage the recieving and sending of data.
As a P2P node should, it will route data destined for others, and it will also maintain a healthy number of connections (the number which you define when you start the node).

This entire system is event driven, so you will register your functions with the UDF and the function will get called when the corresponding event happens. See the comments in the UDF for more information.
This works in the same way as Kip's TCP UDF.


Structure:
##################
The first thing you must do is start the Node:
_P2P_Start_Node( Node Identifier, Port, Max peers, Bootstrap mode, Bootstrap max, local/global, IP)

All the Parameters are pretty self expanatory. The Node identifier is the name of your computer in the network. It must not change across sessions and it must be unique. It is used to identify you in your network.

Bootstrapping means autoconnecting. if Bootstrap mode is 1, It will autoconnect you to others until you reach your Bootstrap Max.

Local/Global - Put 0 if working on a LAN, 1 if over internet.

IP - (Optional). You shouldn't need to change this. It is the IP of the listening port.

The second thing is declare your functions. This tells the UDF which functions to call when something happens. EG:
_P2P_Register_Event($P2P_RECEIVE, "my_recieve_function")
So, this will call the function my_recieve_function when data from a peer is recieved. See the comments at the beginning of the UDF for a complete list of Event values, and the parameters for your functions.

Then fire away!
_P2P_Send( $socket, "MY DATA!!)
_P2P_Connect( "127.0.0.1")
_P2P_Broadcast( "My data")

When your done,

_P2P_Stop_Node()
EDIT:

There is also a very powerful system I forgot to mention earlier called long range message routing. Basically, it allows you to send messages in your P2P network to peers you are not connected to, but someone else in the network is. Consider this:

P1 is connected to P2 and P3. P4 is connect to P3. P1 wants to send message to P4.
You would use my function _P2P_Send_Message. It will route your message from P1 -> P3 -> P4 automatically. The speed of processing for three nodes will be approx. 90ms, so it will be relatively fast.

UDF:
Attached File  P2PTCP.au3   36.21K   869 downloads
EXAMPLE:
Attached File  P2Ptest.au3   1.38K   793 downloads
Not the best example but it will do. Notice that the UDF by default prints to console.

Hope you enjoy it!

Hypoz.

Edited by hyperzap, 16 November 2010 - 11:47 PM.

ongoing projects:-firestorm: Largescale P2P Social NetworkCompleted Autoit Programs/Scripts: Variable Pickler | Networked Streaming Audio (in pure autoIT) | firenet p2p web messenger | Proxy Checker | Dynamic Execute() Code Generator | P2P UDF | Graph Theory Proof of Concept - Breadth First search





#2 ludocus

ludocus

    Possibly inventive crap going on right here

  • Active Members
  • PipPipPipPipPipPip
  • 664 posts

Posted 24 September 2010 - 01:07 PM

Sounds great man!

#3 Yorn

Yorn

    Adventurer

  • Active Members
  • PipPip
  • 113 posts

Posted 30 September 2010 - 02:10 PM

FAQ:
I can't use #8i8TERMINATE# in my code. Why?
It is a required string of the code itself. This is to prevent TCP runoff that plagues most TCP transmissions, including those of us who were using the event driven TCP script and ran into this problem. Having a terminate request solves this problem of runoff and makes the actual "events" work.

#4 Zibit

Zibit

    Universalist

  • Active Members
  • PipPipPipPipPip
  • 262 posts

Posted 30 September 2010 - 02:17 PM

Well done !;)

#5 Zibit

Zibit

    Universalist

  • Active Members
  • PipPipPipPipPip
  • 262 posts

Posted 30 September 2010 - 02:19 PM

;) #include <guicombo.au3> isnt working

#6 Manadar

Manadar

    Taking a REST.

  • MVPs
  • 10,714 posts

Posted 01 October 2010 - 11:36 AM

I've been going through the code and I have some comments. It seems like you've written this for yourself and created an awesome app around it (looking at the includes in the test), and before posting you decided it needed a short example. In my opinion, the example is far too short and doesn't nearly show the full potential of the UDF. Also, dare I say, that this makes network communication without a server more complicated instead of simpler. It does make working with P2P a lot simpler.

I made a shortlist of what I thought so far.
  • _Get_IP() is in the standard AutoIt functions. It's called _GetIP() there and is found in Inet.au3.
  • I'd really like to see a longer example. Possibly something with a GUI that runs directly on a standard AutoIt installation. (Maybe a GUI that shows connected clients, all the clients in the network (by IP), realtime traffic, traceroute functionality with #hops. And to top it all off, a network topology of connected subnets.)
  • You can allow people to use #8i8TERMINATE# in their message. For an example how that works check out HTTP multipart. (In practice, the header string is generated from the browser name and a random value. For example: --webkit9876987asd765786fg57sdf89fg.)
  • For situations like this:
    $arrayslot = Findfreearrayslot() if $arrayslot = "error" then ;No more spots for new peers. Connection dropped.

    you can use the @error macro manually with SetError
  • Instead of copy pasting
    if $console_out = True then ConsoleWrite
    everywhere I recommend using something like
    Func _DebugWrite($s) if $console_out = True then ConsoleWrite($s) EndFunc

And the UDF could have used a bit more comments. Certain parts were hard to read through.

Edited by Manadar, 01 October 2010 - 11:42 AM.


#7 twitchyliquid64

twitchyliquid64

    Peace. Always.

  • Active Members
  • PipPipPipPipPipPip
  • 519 posts

Posted 01 October 2010 - 09:04 PM

I've been going through the code and I have some comments. It seems like you've written this for yourself and created an awesome app around it (looking at the includes in the test), and before posting you decided it needed a short example. In my opinion, the example is far too short and doesn't nearly show the full potential of the UDF. Also, dare I say, that this makes network communication without a server more complicated instead of simpler. It does make working with P2P a lot simpler.

I made a shortlist of what I thought so far.

  • _Get_IP() is in the standard AutoIt functions. It's called _GetIP() there and is found in Inet.au3.
  • I'd really like to see a longer example. Possibly something with a GUI that runs directly on a standard AutoIt installation. (Maybe a GUI that shows connected clients, all the clients in the network (by IP), realtime traffic, traceroute functionality with #hops. And to top it all off, a network topology of connected subnets.)
  • You can allow people to use #8i8TERMINATE# in their message. For an example how that works check out HTTP multipart. (In practice, the header string is generated from the browser name and a random value. For example: --webkit9876987asd765786fg57sdf89fg.)
  • For situations like this:
    $arrayslot = Findfreearrayslot() if $arrayslot = "error" then ;No more spots for new peers. Connection dropped.

    you can use the @error macro manually with SetError
  • Instead of copy pasting
    if $console_out = True then ConsoleWrite
    everywhere I recommend using something like
    Func _DebugWrite($s) if $console_out = True then ConsoleWrite($s) EndFunc

And the UDF could have used a bit more comments. Certain parts were hard to read through.

Thankyou for your time.

I will get on to making a better example when I can. I have almost finished my P2P program, which is a massed hi-speed message and IRC based messenger. Once thats done, I will create a better example.

The Get_IP() Function as you said is from the Inet.au3 library. The reason I popped it in was because I thought UDF's were not allowed to have #Include thingies. Then I changed the Function name slightly to avoid collision.

I originally considered using a random number system followed by a delimiter as a header to a message (much like the Message ID system in the long range message functionality). The issue was that this would slow down the delievery and it seemed to be a waste of time. Honestly, Who would use the string #8i8TERMINATE#? A user writing messages would never. I would only be worried of Binary or random data and even then its a probability of (26*2+1)*14 which is a high number.

"
you can use the @error macro manually with SetError
"
What functional advantage would this hold?

"INstead of copying and pasting: if $console_out = True then ConsoleWrite"

Yer, I added the if statment at the last minute in case someone wanted to turn of the Console prints. Its dirty. If will fix it in the next release.

I took a bit more time to make sure this UDF was a bit cleaner than my normal code with aptly named Vars and all that. Looks like it still needs a bit of work ;)

Thanks sooo much. This is the feedback I was looking for. I will get round to it when I can.


Hypoz.
ongoing projects:-firestorm: Largescale P2P Social NetworkCompleted Autoit Programs/Scripts: Variable Pickler | Networked Streaming Audio (in pure autoIT) | firenet p2p web messenger | Proxy Checker | Dynamic Execute() Code Generator | P2P UDF | Graph Theory Proof of Concept - Breadth First search

#8 twitchyliquid64

twitchyliquid64

    Peace. Always.

  • Active Members
  • PipPipPipPipPipPip
  • 519 posts

Posted 01 October 2010 - 09:06 PM

;) #include <guicombo.au3> isnt working


My first guess would be a var clash, and not to mention this was written on an old version of Autoit. Please post your code so I can test more extensively.
ongoing projects:-firestorm: Largescale P2P Social NetworkCompleted Autoit Programs/Scripts: Variable Pickler | Networked Streaming Audio (in pure autoIT) | firenet p2p web messenger | Proxy Checker | Dynamic Execute() Code Generator | P2P UDF | Graph Theory Proof of Concept - Breadth First search

#9 twitchyliquid64

twitchyliquid64

    Peace. Always.

  • Active Members
  • PipPipPipPipPipPip
  • 519 posts

Posted 05 November 2010 - 06:49 AM

Guys if any of you who have used this in a program can posts your scripts (if you like), I would like to get some idea of how others use this. Also if anyone wants more functions, let me know.
ongoing projects:-firestorm: Largescale P2P Social NetworkCompleted Autoit Programs/Scripts: Variable Pickler | Networked Streaming Audio (in pure autoIT) | firenet p2p web messenger | Proxy Checker | Dynamic Execute() Code Generator | P2P UDF | Graph Theory Proof of Concept - Breadth First search

#10 lsakizada

lsakizada

    Universalist

  • Active Members
  • PipPipPipPipPipPip
  • 392 posts

Posted 14 February 2011 - 03:38 PM

[DELETED BY ME]

Edited by lsakizada, 14 February 2011 - 03:45 PM.

Be Green Now or Never (BGNN)!

#11 nullschritt

nullschritt

    Adventurer

  • Active Members
  • PipPip
  • 129 posts

Posted 30 August 2012 - 09:07 AM

I am currently considering a P2P solution for my Instant Messaging Client.

However I have some questions about your UDF.

So say each PC authenticates with a web server, and when it does it stores the IP of the computer connecting.

Then say I use your UDF to use the most recent connection as a node. (so each time a client connects, the last client connected is added as a node)

client 1 -> client 2

client 3 -> client 2

client 4 -> client 3

client 5 -> client 4

That your UDF could make connections between all of these nodes, even if one only connects to one other? (so say client 5 could send a message to client 2, even though they never directly connected)

And my last question, if my previous question is true, does your udf automatically connect more nodes when it discovers them (directly or indirectly) [so say if client 4 disconnects, would client 5 automatically connect to client 3 instead, or would I have to add client 3 as a new node manually?]

Sorry if the answers to my question are obvious! I've never used P2P with anything before.

Edit: Also does P2P require port forwarding behind a router? (if so I am sure I could make a non-p2p fallback system for users unable to port forward)

Edited by nullschritt, 30 August 2012 - 09:09 AM.


#12 DeltaRocked

DeltaRocked

    Prodigy

  • Active Members
  • PipPipPip
  • 166 posts

Posted 01 November 2012 - 06:55 PM

Hi Hypoz,

I have gone through the UDF and I needed your help in setting up Google App engine.

regards

#13 twitchyliquid64

twitchyliquid64

    Peace. Always.

  • Active Members
  • PipPipPipPipPipPip
  • 519 posts

Posted 02 November 2012 - 08:26 AM

hi,

is it possible to wait 4 days? i will be able to get this working for you after then.

sorry.
ongoing projects:-firestorm: Largescale P2P Social NetworkCompleted Autoit Programs/Scripts: Variable Pickler | Networked Streaming Audio (in pure autoIT) | firenet p2p web messenger | Proxy Checker | Dynamic Execute() Code Generator | P2P UDF | Graph Theory Proof of Concept - Breadth First search

#14 DeltaRocked

DeltaRocked

    Prodigy

  • Active Members
  • PipPipPip
  • 166 posts

Posted 06 November 2012 - 01:48 PM

sure. :)

#15 nullschritt

nullschritt

    Adventurer

  • Active Members
  • PipPip
  • 129 posts

Posted 30 March 2013 - 04:45 PM

I have a quick question, is the UDF set up so a user can still receive messages, even if they cannot act as a server(they for some reason cannot port forward), will the outbound tcp connections they make to other clients, provide them with data routed to them, or would I have to implement this manually?




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users