MonkeyGNet


Tweet gnet monkey-x networking code-archives
(Posted 5 months ago) RonTek

A simple module conversion of the Blitz3d GNet server thing that is over at blitzbasic.com

Author: EdzUp

MonkeyGNet.monkey

'
'   MonkeyGNet.monkey - Copyright (c) Mark Sibly and EdzUp (conversion)
'   Programmed by Mark Sibly, Converted to monkey by Ed 'EdzUp' Upton
'
Strict

Import brl.httprequest

Const GNET_HOST:String  = "gnet.blitzcoder.org"
Const GNET_PORT:Int     = 80
Const GNET_GET:String   = ""

Class GNET_Server
    Field game:String
    Field server:String
    Field ip:String
End
'the list for the servers
Global GNET_List:List<GNET_Server> = New List<GNET_Server>

Class GNetClass
    Method GNET_Esc:String( t:String )
        t.Replace( "&", "" )
        t.Replace( "%","" )
        t.Replace( "'","" )
        t.Replace( String.FromChar( 34 ), "" )
        t.Replace( " ", "_" )

        Return t
    End

    Method GNET_Open:TcpStream( opt:String )
        Local GNET_Stream:TcpStream = New TcpStream

        If Not GNET_Stream.Connect( GNET_HOST, GNET_PORT )
            Return Null
        Endif

        GNET_Stream.WriteLine "GET "+GNET_GET+"?opt="+opt+" HTTP/1.0"
        GNET_Stream.WriteLine "HOST:"+GNET_HOST
        GNET_Stream.WriteLine ""

        While GNET_Stream.ReadLine()<>""
        Wend
        Return GNET_Stream
    End

    Method GNET_Exec:Int( opt:String, game:String, server:String )
        Local ok:Bool = True
        Local GNET_Stream:TcpStream = New TcpStream

        opt += "&game="+GNET_Esc( game )
        If server <>"" opt += "&server=" +GNET_Esc( server )
        GNET_Stream =GNET_Open( opt )
        If Not GNET_Stream Then  Return 0

        ok=False
        If( GNET_Stream.ReadLine()="OK" ) Then ok=True

        GNET_Stream.Close()
        Return ok
    End

    Method Ping:String()
        Local ip:String = ""
        Local GNET_Stream:TcpStream = New TcpStream

        GNET_Stream =GNET_Open( "ping" )
        If Not GNET_Stream Then Return ""

        ip = GNET_Stream.ReadLine()

        GNET_Stream.Close()

        Return ip
    End

    Method AddServer:Int( game:String, server:String ="" )
        Return GNET_Exec( "add", game, server )
    End

    Method RefreshServer:Int( game:String, server:String ="" )
        Return GNET_Exec( "ref", game, server )
    End

    Method RemoveServer:Int( game:String )
        Return GNET_Exec( "rem", game, "" )
    End

    Method ListServers:Int( game:String ="" )
        Local GNET_Stream:TcpStream = New TcpStream
        Local Temp:GNET_Server = New GNET_Server            'server entry for list
        Local NewGame:String
        Local NewServer:String
        Local NewIP:String

        GNET_List.Clear()               'clear old list

        GNET_Stream = GNET_Open( "list" )
        If Not GNET_Stream Then Return 0    'failed

        Repeat
            NewGame = GNET_Stream.ReadLine()
            If NewGame="" Then Exit

            NewServer = GNET_Stream.ReadLine()
            NewIP = GNET_Stream.ReadLine()

            If game="" Or game = NewGame
                Temp = New GNET_Server
                Temp.game = NewGame
                Temp.server = NewServer
                Temp.ip = NewIP
                GNET_List.AddLast( Temp )
            Endif
        Forever

        GNET_Stream.Close()             'close it afterwards

        Return 1
    End
End

Global GNet:GNetClass = New GNetClass

Example.monkey

'Simple gnet demo
'
'Check out www.blitzbasic.com/gnet/gnet_servers.php while running!
'

'converted by Ed 'EdzUp' Upton

Import mojo
Import MonkeyGNet

Global my_game:String = "My_Game"
Global Timer:Int                        'so we can see whats going on :D

Function Main:Int()
    New GNetTest
    Return 0
End

Class GNetTest Extends App
    Method OnCreate:Int()
        SetUpdateRate 30

        Timer =0

        Return 0
    End

    Method OnUpdate:Int()
        If Timer=0
            'ping!
            Print "Ping:"+GNet.Ping()
            Print ""
        Endif

        If Timer=25
            'list active servers
            '
            ListServers()
        Endif

        If Timer=50
            'add a server!
            '
            Print "Adding server..."
            Print ""

            If Not GNet.AddServer( my_game )
                Print "FAILED"
                Error "Failed to add server"
            Endif
        Endif

        If Timer=75
            'list active servers
            '
            ListServers()
        Endif

        If Timer=100
            'refresh server!
            '
            Print ""

            If Not GNet.RefreshServer( my_game,"New Server Name!" )
                Error "Failed to refresh server"
            Endif
        Endif

        If Timer=125
            'list active servers
            '
            ListServers()
        Endif

        If Timer=150
            'remove ourselves!
            '
            Print ""

            If Not GNet.RemoveServer( my_game )
                Error "Failed to remove server"
            Endif
        Endif

        If KeyDown( KEY_ESCAPE )=1 Or Timer=200 Then Error ""

        Timer+=1

        Return 0
    End

    Method OnRender:Int()
        Cls 0, 0, 0
        Return 0
    End
End

Function ListServers()
    Local Temp:GNET_Server = New GNET_Server
    If Not GNet.ListServers()
        Error "Failed to list servers"
    Endif

    Print "Game, Server, IP"
    Print "-----------------------------------"
    For Temp = Eachin GNET_List
        Print Temp.game+", "+Temp.server+", "+Temp.ip
    Next
    Print ""
End

Reply To Topic (minimum 10 characters)

Please log in to reply