the only thing that bothers me is that if tiberion.com, whatever that is, goes down, all porgrams based on this wont work anymore...
Code archives/Networking/TCP engine ULTRA FAST
This code has been declared by its author to be Public Domain code.
Download source code
| 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 (daniel@tiberion.com) ;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
@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
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
sure. but isnt there running a php script within the server which manages the TCP connections? i bet there is.
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.
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.
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!
relpaced:-
;window=CreateWindow("Key press me window",100,100,640,200,0,1)
with
Graphics 800,600,16,2
Thanks again!
@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.
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.
for sure..... post it.
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.....
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:
; TCP engine ULTRA FAST ; ; Written by Daniel Eriksson (daniel@...) ; ; save executable as : ; ; server_v2.exe ; ; for VB.net front end to locate. Also add a sub-directory ; called "file_dir" for the server to put files associated ; with clients ; added to by M Gormley, also incorporated ; a VB.net front end which can be used to start ; the server, start clients, monitor how many clients ; are running, kill the server and cleanup files. ;Public Domain free do use as wish including selling it. ;VARS max_bank=512 global_client_count=0 Dim file_name$(255); 255 possible clients - who'd need more? Global file_count=0 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) BBasic Graphics 400,700,16,2 ; B3D 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 global_client_count=global_client_count+1 Print "New stream detected. memory allocated." ses.session = New session ses\link=stream ses\tcp_sendbank = CreateBank(max_bank) ses\tcp_recbank = CreateBank(max_bank) ses\status =10 ; Simulate login. ;ses\client_count=global_client_count fn_create_file(Int ses\link) End If oldms=MilliSecs() ;Validate streams.. For ses.session = Each session If Eof(ses\link) Print "A session called "+ses\link+" has stop working." Print "Frees memory banks for session "+ses\link FreeBank ses\tcp_sendbank FreeBank ses\tcp_recbank Print "deletes sessions "+ses\link numberin=ses\link fn_delete_file(Int ses\link) Delete ses global_client_count=global_client_count-1 End If Next For ses.session = Each session Select ses\status Case 10 If ReadAvail(ses\link)>=max_bank ;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,max_bank) Print "for session "+ses\link+" who has a tcp recbank of:"+ses\tcp_recbank Print "read "+r+" bytes from stream" ;my attempt at accessing sent count rx_count=PeekByte(ses\tcp_recbank,0) Print"rx_count is:"+rx_count 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 0 For i=0 To (max_bank-1) ;PokeByte ses\tcp_sendbank,i,Rnd(255) PokeByte ses\tcp_sendbank,i,Rnd(255) Next rx_count=PeekByte(ses\tcp_recbank,0) ;client_count=PeekByte(ses\tcp_recbank,0) PokeByte ses\tcp_sendbank,0,global_client_count PokeByte ses\tcp_sendbank,1,rx_count Print " sent back a rx_count of:"+rx_count ;Secondly we reply it kindly. s=WriteBytes (ses\tcp_sendbank,ses\link,0,max_bank) ;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 ;--------------------------------------------------------------------- ; functions ;--------------------------------------------------------------------- Function fn_delete_file(numberin) Print "Deleting File: "+numberin del_file_name$="file_dir/"+numberin+".ini" DeleteFile (del_file_name$) Print "File Deleted" End Function Function fn_create_file(numberin) BestName$="Mark" ;BestScore$="123" ;BestLevel$="One and only" Print "Ganna create a text file with this:"+numberin+" identity." ;file_name$(file_count)="file_dir/"+file_count+"_"+numberin+".ini" ;got rid of file count prefix string manip hassle file_name$(file_count)="file_dir/"+numberin+".ini" Print file_name$(file_count) writethis$=file_name$(file_count) file_count=file_count+1 ; Open a file to write to fileout = WriteFile(writethis$) Print"-------------------------------------" Print"fileout is:"+writethis$ Print"-------------------------------------" ; Write the information to the file WriteString( fileout, BestName$ ) ;WriteInt( fileout, BestScore$ ) ;WriteByte( fileout, BestLevel$ ) Print "Wrote to:"+writethis$ ; Close the file CloseFile( fileout ) End Function
Client:
;------------ Client part ------------------------------ ; this file needs to be saved as: ; ; client_v2.exe ; ; for VB.net front end to locate ; this is a dumb client.. it only spams server :D max_bank = 512 sent_count=0 tcp_sendbank=CreateBank(max_bank) tcp_recbank=CreateBank(max_bank) ;link=OpenTCPStream("www.tiberion.com",7056) link=OpenTCPStream("127.0.0.1",7056) ;NOTE: might need to replace with your IP info If Not link Then Print "failed to create link." : Delay 3000 : End Print "Connected.. let the spam flood." status=12; we emulate a logged 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)>=max_bank ;time to read some ;) r=ReadBytes(tcp_recbank,link,0,max_bank) Print "have readin "+r+" bytes from server." Print "-----------------------------------------------------" ;my attempt at accessing sent count xth=PeekByte(tcp_recbank,0) serv_count=PeekByte(tcp_recbank,1) Print"client count is:"+xth Print "Server reckons there is "+xth+" clients connected......" Print "Server thinks this clients sent him: "+serv_count+" packets of 512 bytes" Print "------------------------------------------------------" 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 (max_bank-1) PokeByte tcp_sendbank,i,Rnd(255) ;Print "filled 512 bytes with randz, bar first one" If sentcount<255 PokeByte tcp_sendbank,0,sent_count Else sentcount=255 EndIf Next ;well we got our important randomnes.. time ti send the junk spam to server. s=WriteBytes (tcp_sendbank,link,0,max_bank) ;Print "Sending "+s+" bytes to server."+sent_count+" times" sent_count=sent_count+1 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. Delay 500 ; MG Delay Wend
Hope this helps someone.....
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......
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......