GNet for Blitzmax


(Posted 1 year ago) RonTek

GNet master server query for BlitzMax by boomboommax and example by markcwm. Reposted from BlitzBasic Code Archives

Important Note: To display your games to the GNet section and use the blitzcoder gnet master server, use host:String="gnet.blitzcoder.org"

Type networkgnet
    Const host:String = "localhost"   'enter gnet.blitzcoder.org to connect to BlitzCoder GNet MasterServer
    Const hostget:String = ""

    Const port:Int = 80

    Global socket:tsocket
    Global stream:tsocketstream

    Function gnet_esc:String(t:String)

        t = Replace(t,"&","")
        t = Replace(t,"%","")
        t = Replace(t,"'","")
        t = Replace(t,Chr(34),"")
        t = Replace(t," ","_")

        Return t

    End Function

    Function gnet_open:tsocketstream(opt:String)

        networkgnet.socket = CreateTCPSocket()

        ConnectSocket(networkgnet.socket,HostIp(networkgnet.host),networkgnet.port)
        networkgnet.stream = CreateSocketStream(networkgnet.socket,True)

        WriteLine networkgnet.stream,"GET "+networkgnet.hostget+"?opt="+opt+" HTTP/1.0"
        WriteLine networkgnet.stream,"HOST: "+networkgnet.host
        WriteLine networkgnet.stream,""

        FlushStream(networkgnet.stream)

        While ReadLine(networkgnet.stream) <> ""
        Wend

        Return networkgnet.stream

    End Function

    Function gnet_exec(opt:String,game:String,server:String)

        opt = opt+"&game="+networkgnet.gnet_esc(game)

        If server <> "" opt = opt+"&server="+networkgnet.gnet_esc(server)

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

        Local ok = False
        If ReadLine(t) = "OK" Then ok = True

        CloseSocket(networkgnet.socket)
        Return ok

    End Function

    Function gnet_ping:String()

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

        Local ip:String = ReadLine(t)

        CloseSocket(networkgnet.socket)
        Return ip

    End Function

    Function gnet_addserver(game:String,server:String="")

        Return networkgnet.gnet_exec("add",game,server)

    End Function

    Function gnet_refreshserver(game:String,server:String="")

        Return networkgnet.gnet_exec("ref",game,server)

    End Function

    Function gnet_removeserver(game:String)

        Return networkgnet.gnet_exec("rem",game,"")

    End Function

    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)<>"""
                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    

End Type

Global networkserverlist:TList = New TList

Type networkgnet_server
    Field game:String
    Field server:String
    Field ip:String
End Type

Demo / Example

'   gnet conversion test

SuperStrict

Import "gnet_inc.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
(Posted 1 year ago) markcwm commented:

Thanks Ron, but you shouldn't be crediting me, all I did was write the example code, the gnet conversion functions were by boomboommax (formerly cyanide) except for gnet_listservers which was fixed by Regular K but is identical to ListGNetServers in GNetServerList by Oddball. Oddball's code also works fine.

One function I did write is GetLocalIP but I can't remember if gnet_ping does this.

(Posted 1 year ago) RonTek commented:

Hey Mark, thanks! Ok just updated it GetLocalIP would be a good addition..

(Posted 1 year ago) hardcoal commented:

Excuse me for asking.. but what exactly is GNet? I mean whats the difference betwin this and the regular blitzmax network commands? Or Am I confusing stuff up?

(Posted 1 year ago) RonTek commented:

Yes, it is a bit confusing having both this masterserver and the networking commands with the same name.

Reply To Topic

Please log in to reply