October 28, 2021, 11:50:14

Author Topic: [bmx] gnet conversion by boomboommax [ 1+ years ago ]  (Read 1634 times)

Offline BlitzBot

  • Jr. Member
  • **
  • Posts: 1
[bmx] gnet conversion by boomboommax [ 1+ years ago ]
« on: June 29, 2017, 00:28:43 »
Title : gnet conversion
Author : boomboommax
Posted : 1+ years ago

Description : er yup

Code :
Code: BlitzMax
  1. Type networkgnet
  2.         Const host:String = "www.blitzbasic.com"
  3.         Const hostget:String = "/gnet/gnet.php"
  4.        
  5.         Const port:Int = 80
  6.        
  7.         Global socket:tsocket
  8.         Global stream:tsocketstream
  9.        
  10.         Function gnet_esc:String(t:String)
  11.                
  12.                 t = Replace(t,"&","")
  13.                 t = Replace(t,"%","")
  14.                 t = Replace(t,"'","")
  15.                 t = Replace(t,Chr(34),"")
  16.                 t = Replace(t," ","_")
  17.                
  18.                 Return t
  19.                
  20.         End Function
  21.        
  22.         Function gnet_open:tsocketstream(opt:String)
  23.                
  24.                 networkgnet.socket = CreateTCPSocket()
  25.                
  26.                 ConnectSocket(networkgnet.socket,HostIp(networkgnet.host),networkgnet.port)
  27.                 networkgnet.stream = CreateSocketStream(networkgnet.socket,True)
  28.                
  29.                 WriteLine networkgnet.stream,"GET "+networkgnet.hostget+"?opt="+opt+" HTTP/1.0"
  30.                 WriteLine networkgnet.stream,"HOST: "+networkgnet.host
  31.                 WriteLine networkgnet.stream,""
  32.                
  33.                 FlushStream(networkgnet.stream)
  34.                
  35.                 While ReadLine(networkgnet.stream) <> ""
  36.                 Wend
  37.                
  38.                 Return networkgnet.stream
  39.                
  40.         End Function
  41.        
  42.         Function gnet_exec(opt:String,game:String,server:String)
  43.                
  44.                 opt = opt+"&game="+networkgnet.gnet_esc(game)
  45.                
  46.                 If server <> "" opt = opt+"&server="+networkgnet.gnet_esc(server)
  47.                
  48.                 Local t:tsocketstream = networkgnet.gnet_open(opt)
  49.                 If Not t Then Return False
  50.                
  51.                 Local ok = False
  52.                 If ReadLine(t) = "OK" Then ok = True
  53.                
  54.                 CloseSocket(networkgnet.socket)
  55.                 Return ok
  56.                
  57.         End Function
  58.        
  59.         Function gnet_ping:String()
  60.                
  61.                 Local t:tsocketstream = networkgnet.gnet_open("ping")
  62.                 If Not t Then Return False
  63.                
  64.                 Local ip:String = ReadLine(t)
  65.                
  66.                 CloseSocket(networkgnet.socket)
  67.                 Return ip
  68.                
  69.         End Function
  70.        
  71.         Function gnet_addserver(game:String,server:String="")
  72.                
  73.                 Return networkgnet.gnet_exec("add",game,server)
  74.                
  75.         End Function
  76.        
  77.         Function gnet_refreshserver(game:String,server:String="")
  78.                
  79.                 Return networkgnet.gnet_exec("ref",game,server)
  80.                
  81.         End Function
  82.        
  83.         Function gnet_removeserver(game:String)
  84.                
  85.                 Return networkgnet.gnet_exec("rem",game,"")
  86.                
  87.         End Function
  88.        
  89.         Function gnet_listservers(game:String="")
  90.                
  91.                 For Local n:networkgnet_server = EachIn networkserverlist
  92.                         networkserverlist.remove(n)
  93.                         n = Null
  94.                 Next
  95.                
  96.                 FlushMem
  97.                
  98.                 Local t:tsocketstream = networkgnet.gnet_open("list")
  99.                 If Not t Then Return False
  100.                
  101.                 Local t_game:String
  102.                 Local t_server:String
  103.                 Local t_ip:String
  104.                
  105.                 Repeat
  106.                         t_game = ReadLine(t)
  107.                         If t_game = "" Then Exit
  108.                        
  109.                         t_server = ReadLine(t)
  110.                         t_ip = ReadLine(t)
  111.                        
  112.                         If game = "" Or game = t_game
  113.                                 Local p:networkgnet_server = New networkgnet_server
  114.                                
  115.                                 p.game = t_game
  116.                                 p.server = t_server
  117.                                 p.ip = t_ip
  118.                                
  119.                                 networkserverlist.addfirst(p)
  120.                         EndIf
  121.                 Forever
  122.                
  123.                 CloseSocket(networkgnet.socket)
  124.                 Return 1
  125.                
  126.         End Function
  127. End Type
  128.  
  129.  
  130. Global networkserverlist:TList = New TList
  131.  
  132. Type networkgnet_server
  133.         Field game:String
  134.         Field server:String
  135.         Field ip:String
  136. End Type


Comments :


necky(Posted 1+ years ago)

 spot on! Thanks again!:O)


Regular K(Posted 1+ years ago)

 I fixed some problems (warning about header and problems recongizing games).Heres my fixed gnet_listservers function:
Code: [Select]
Function gnet_listservers:Int(game:String="")
For Local n:TNetworkGNetServer=EachIn GNetServerList
GNetServerList.remove(n)
n = Null
Next

Local t:tsocketstream = TNetworkGNet.gnet_open("list")
If Not t Then Return False

Local t_game:String
Local t_server:String
Local t_ip:String

Repeat
t_game = ReadLine(t)
If Left(t_game,2)<>"<b" And t_game<>""
t_server = ReadLine(t)
t_ip = ReadLine(t)

If game = "" Or gnet_esc(game) = t_game
Local p:TNetworkGNetServer=New TNetworkGNetServer

p.game = t_game
p.server = t_server
p.ip = t_ip

GNetServerList.addfirst(p)
EndIf
EndIf
Until Eof(t)

CloseSocket(TNetworkGNet.socket)
Return 1
End Function
Except for that, good job :D


markcw(Posted 1+ years ago)

 Regular K's function is better but he changed the variable names so here's the faithful update to "gnet_listservers".
Code: [Select]
Function gnet_listservers:Int(game:String="")
For Local n:networkgnet_server=EachIn networkserverlist
networkserverlist.remove(n)
n = Null
Next

Local t:tsocketstream = networkgnet.gnet_open("list")
If Not t Then Return False

Local t_game:String
Local t_server:String
Local t_ip:String

Repeat
t_game = ReadLine(t)
If Left(t_game,2)<>"<b" And t_game<>""
t_server = ReadLine(t)
t_ip = ReadLine(t)

If game = "" Or gnet_esc(game) = t_game
Local p:networkgnet_server=New networkgnet_server

p.game = t_game
p.server = t_server
p.ip = t_ip

networkserverlist.addfirst(p)
EndIf
EndIf
Until Eof(t)

CloseSocket(networkgnet.socket)
Return 1
End Function



markcw(Posted 1+ years ago)

 And here's a test based on the b3d gnet_v1 demo. This includes a working ping. See <a href="http://www.blitzmax.com/toolbox/toolbox.php?tool=61" target="_blank">here[/url] for gnet_v1 demo and doc.
Code: [Select]
' gnet conversion test

SuperStrict

Import "gnet conversion.bmx"

Global gnet:networkgnet = New networkgnet
Global gnet_server:networkgnet_server = Null

Local my_game$="my_gnet_game",my_server$="my_gnet_server"

Local p$=gnet.gnet_ping()
Print "~nPing:"+p

gnet.gnet_listservers()
print_servers()

Print "~nAdding server..."
gnet.gnet_addserver( my_game$ )

gnet.gnet_listservers()
print_servers()

Input "~nHit return to refresh server..."

gnet.gnet_refreshserver( my_game$,my_server$ )

gnet.gnet_listservers( my_game$ ) 'list just my game
print_servers()

Input "~nHit return to remove server and exit..."

gnet.gnet_removeserver( my_game$ )

gnet.gnet_listservers()
print_servers()

Function print_servers()
Print ""
For gnet_server:networkgnet_server = EachIn networkserverlist
If gnet_server<>Null
Print "Game:"+gnet_server.game+" Server:"+gnet_server.server+" IP:"+gnet_server.ip
EndIf
Next
EndFunction


 

SimplePortal 2.3.6 © 2008-2014, SimplePortal