Code archives/Networking/TCP engine ULTRA FAST

This code has been declared by its author to be Public Domain code.

Download source code

TCP engine ULTRA FAST by Wings2008
There have ben a war that some ones claims tcp in blitz basic is useles for gamming. this code is prof of oposite.

This code is the Core thinking of my new Tiberion engine.

Please fell free to expand it to somthing usefull.

I have tryed to make it so smal and easy to understand as possible.
;CP engine ULTRA FAST
;
; Written by Daniel Eriksson  (wings@tiberion.eu)
;Public Domain free do use as wish including selling it.


Type session

	Field link
	Field tcp_sendbank
	Field tcp_recbank
	Field status

End Type

window=CreateWindow("Key press me window",100,100,640,200,0,1)

server=CreateTCPServer(7056)
Print "server is running.."

msmin=9999999 ; minimums millisecs :D

While Not KeyHit(1)   ;Hehe this command dosent work in BlitzPluss with the console window.

	stream = AcceptTCPStream(server)
	If stream
		
		Print "New stream detected. memmory allocated."
		ses.session = New session
		ses\link=stream
		ses\tcp_sendbank = CreateBank(512)	
		ses\tcp_recbank = CreateBank(512)
		ses\status =10 ; Simulate login.
	
	End If


	oldms=MilliSecs()
	;Validate streams.. 
	For ses.session = Each session
	
		If Eof(ses\link) 
			Print "A session caled "+ses\link+" has stop working."
			Print "Frees memmory banks for session "+ses\link
			FreeBank ses\tcp_sendbank 
			FreeBank ses\tcp_recbank
			Print "deletes sessions "+ses\link
			Delete ses
		End If
		
	Next
	
	
	For ses.session = Each session
	
	
		Select ses\status
		Case 10
		
			If ReadAvail(ses\link)>=512
			
				;so we got a new packet :D and its all 100% secure... so we can go on live our lifes
				r=ReadBytes (ses\tcp_recbank,ses\link,0,512)
				;Print "read "+r+" bytes from stream"
			
				ses\status=12 ; well time to reply this packet next round :D
			
			End If
		
		Case 12 ; Well time to reply a packet.
		
			;First we fill it upp with randomnezz
			For i=0 To 511
				PokeByte ses\tcp_sendbank,i,Rnd(255)
			Next
			
			;Secondly we reply it kindly.
			s=WriteBytes (ses\tcp_sendbank,ses\link,0,512)
			;Print "Succesfull sending "+s+" bytes of data"
			 packetssent= packetssent+1
	
	
			ses\status=10 ; Well we cant wait for next coll packet to arive :D"
			
		End Select
	
	
	Next	
	
	ms=MilliSecs()-oldms
	If ms>msmax Then msmax=ms
	If ms<msmin Then msmin=ms
	count=count+1
	cmsmed=cmsmed+ms
	msmed=cmsmed/count   ; medium millisecs
	
	
	tick=tick+1
	If tick>=500 Then Print "Ticks: "+count+" MS="+ms+" MIN="+msmin+" MED="+msmed+" MAX="+msmax+ "Packets sent "+ packetssent : tick=0

	
	If KeyHit(19) Then MSMIN=99999 : msmed=0 : count=0 : msmax=0 : packetssent=0 :cmsmed=0: Print "Counter resets."

	Delay 10 ; this gives cpu time to rest of OS in windows. as server is not so cpu intensive.

Wend
;--------------------------------------------------------------------




;------------ Client part ------------------------------
; this is a dumb client.. it only spams server :D


tcp_sendbank=CreateBank(512)
tcp_recbank=CreateBank(512)

link=OpenTCPStream("www.tiberion.com",7056)

If Not link Then Print "failed to create link." : Delay 3000 : End

Print "Connected.. let the spam flood."

status=12; we emulate a loged in client

While Not KeyHit(1)

	If Eof(link) Then Print "What server died ??? why   WHYYY !!?? " : Delay 5000 : End 


	Select status
	
	Case 10
	
		If ReadAvail(link)>=512
		
			;time to read some ;)
			r=ReadBytes(tcp_recbank,link,0,512)
			;Print "have readin "+r+" bytes from server."
			status=12 ; Yeppp time to respond... 
		End If
			
	
	Case 12
	
		;haha client time to spam server first we make a cool tcp packet with random nunbers.
		For i=0 To 511
			PokeByte tcp_sendbank,i,Rnd(255)
		Next
		
		;well we got our important randomnes.. time ti send the junk spam to server.
		s=WriteBytes (tcp_sendbank,link,0,512)
		;Print "Sending "+s+" bytes to server."
		
		status=10  ; Well we go out and wait for packet :D
		
	
	End Select
	
		tick=tick+1
	If tick>=500 Then Print CurrentTime()+"...tick..." : tick=0



	Delay 10 ; yep we have to delay some here to emulate many clients.. or cpu dies.

Wend

Comments

bytecode772008
the only thing that bothers me is that if tiberion.com, whatever that is, goes down, all porgrams based on this wont work anymore...


Destroyer2008
@Devils Child
I think you can just change :
link=OpenTCPStream("www.tiberion.com",7056)
to your own ip address.

Eg:link=OpenTCPStream("127.0.0.1",7056)

I tried this and was able to run both client and server on the same machine


bytecode772008
sure. but isnt there running a php script within the server which manages the TCP connections? i bet there is.


Dreamora2008
nope, he is using server and client which he both posted above ... not some kind of meta server to talk to each other.

Makes sense as it seems to be targeted to be the networking of an online gaming technology, not a matchmaking tech :)

Thank you for providing this :)
Surely interesting, the main problem I see with it is that the receive buffer is restricted to a single message. Does that work "usefully" when the server receives multiple messages from that user within the given timeframe? I mean towards the consistency of the data between players if you only process a single message of a player during one go, instead of all messages from that player for each player within that tick.


Blitzplotter2008
Thankyou for this code, it works great, I did one extra change to get it working in Blitz3D on top of Destroyer's suggestion:-

relpaced:-

;window=CreateWindow("Key press me window",100,100,640,200,0,1)

with
Graphics 800,600,16,2

Thanks again!


Blitzplotter2008
@Wings, I've expanded your code a little and am very impressed with it's speed. Fresh post lower down, hope you don't mind me adding to your code.

I'd to stick a hefty delay of half a second in too slow it down enough - like you say - it's ultrafast!...but if anyone wants to see the expanded code I'll post it.... The first two bytes from each packet for the client to send how many packets it had sent, and used the second byte for the Server to let the client know how many clients the server reckoned were connected.

Since the version that is on here I have implemented a system whereby each new client will result in the server writing a new file to a directory containing details of each new client, with 512 bytes per packet there's plenty storage.


dmaz2008
for sure..... post it.


Blitzplotter2008
Have moved it forward a little more now, It now writes a unique file name to a sub directory that you need to create in the same directory as server_v2 and client_v2 executables, called file_dir.
This sub directory is used by the server to maintain a list of the clients that are running.

I have developed a VB.net front end for the two executables server_v2 and client_v2 that keeps a running count of the clients running,
will kill the server and subsequently clean up the file_dir sub directory of historic client files. You do not want
to put any of your own files within the file_dir directory, this directory gets cleaned very well upon Kill Server!

Link to VB.net front end:
http://myweb.tiscali.co.uk/diecastcars/File_Counter.exe

NOTE: Do Not try to run the File_Counter.exe file before you have created a subdirectory call file_dir at the same path. Nothing BAAaaad will happen, it just wont work (;-)
So: You might want to put your server_v2.exe and client_v2.exe
at the same path as File_Counter.exe, else the start server and client buttons won't find what they need.

Image removed

Server:



Client:




Hope this helps someone.....


Blitzplotter2008
Posted new version of the code here, client requests x and y co-ordinate movement now to the server.

See here:

http://www.blitzbasic.com/Community/posts.php?topic=76772


Many thanks to Wings for posting his excellent code that enabled me to get so far......


Code Archives Forum