forum.boolean.name

forum.boolean.name (http://forum.boolean.name/index.php)
-   BlitzMax (http://forum.boolean.name/forumdisplay.php?f=104)
-   -   GNet(сервер-клиент) (http://forum.boolean.name/showthread.php?t=6415)

snikers 29.09.2008 14:34

GNet(сервер-клиент)
 
Обясните на пальцах общую структуру построения кода.

Нашел на http://blitzmax.com/ старый пример, но там есть команды которых нет в новом блице, у кого есть старая справка чтоб посмотреть что они значили и заменить их новыми :) или может кто конвертнет код.

Сервер:
Код:

Strict
' GNET simple dedicated server

' Create a new host. We call it listen because that's what it does
Local listen:TGNetHost = CreateGNetHost()

' Make it listen to the connection. 8086 is the port we want to use but you can change this to whatever
' you want between 1024 and 65535 (if i remember correctly.)
If Not GNetListen( listen, 8086 ) Then
        Print "Unable to listen on port."
        End
EndIf


Print "Server started."
Local quit_server = False
While Not quit_server

        GNetSync(listen) ' Sync all created objects

        Local newpeer:TGNetPeer = GNetAccept( listen ) ' Is someone knocking on our port?
       
        If newpeer Then
                ' A new player wants to join us.
                Print "New player"
                Print "Players onnected at the moment:"
                Local peerlist:TList = GNetPeers( listen )
               
                For Local p:TGNetPeer = EachIn peerlist
                        Print "  - "+DottedIP( SocketRemoteIP(GNetPeerTCPSocket( p )) )
                Next
               
        EndIf
       

        ' The stuff below isn't needed in this short tutorial. It just prints some debug information so we can see
        ' that the server is actually working. :)       

        Local olist:TList = GNetObjects( listen ) ' Get all created objects in the game
       
        For Local o:TGnetObject = EachIn olist
               
                Local state = GNetObjectState( o ) ' Get the state of the object
                       
                Select state
                        Case GNET_CREATED 'Object has been created
                                print "New object created"
                        Case GNET_SYNCED 'Object is in sync
                                ' do something here if it is needed
                                ' But the object is in sync and everything is just fine.
                        Case GNET_MODIFIED 'Object has been modified
                                ' The object is modified! But no worries. It will be synced automagically.
                        Case GNET_CLOSED 'Object has been closed
                                Print "Connection closed to a peer"
                                ' The object is no longer valid. The player left the game
                        Case GNET_MESSAGE 'Object is a message Object
                                ' I haven't played around with this yet. So you need to read the manual
                                ' yourself about this. ;)
               
                EndSelect
               
        Next

Wend

Клиент:
Код:

strict
' Simple GNet client

Local server:TGNetHost = CreateGNetHost() ' Create a "host"

Local serverpeer:TGNetPeer = GNetConnect( server,"127.0.0.1",8086 ) ' Connect to the server

If Not serverpeer Then ' Aaaw crap! Start the server first! And check the ip and port!
        Print "Unable to connect to server."
        End
EndIf

Print "Sever contacted!" ' Yaaaay

' Now, create our player. Only needs to create a local one...
' NetObject is our own type. You can find it after the main loop.
Local localplayer:NetObject = NetObject.create(server,"DaBomb!",320,240)



' Some graphics...
Graphics 640,480,0


' The main loop
While Not KeyDown( KEY_ESCAPE )

        ' --------- Logic

                GNetSync(server) ' Sync objects

                ' Get objects
                Local olist:TList = GNetObjects( server )

                ' We use this later when we draw players
                Local plist:TList = CreateList() ' Active players
               
                For Local o:TGnetObject = EachIn olist
               
                        ' The state the object is in
                        Local state = GNetObjectState( o )
                       
                        Select state
                                Case GNET_CREATED 'Object has been created
                                        If GetGNetInt( o, 0 ) = O_AVATAR Then
                                                plist.addlast( o ) ' we want to draw this later
                                                Print "Joined: "+GetGNetString(o,1)
                                                ' Spawn some particles or something to show that
                                                ' The player has arrived...
                                        EndIf
                                Case GNET_SYNCED 'Object is in sync
                                        If GetGNetInt( o, 0 ) = O_AVATAR Then
                                                plist.addlast( o ) ' Draw it.
                                        endif
                                Case GNET_MODIFIED 'Object has been modified
                                        If GetGNetInt( o, 0 ) = O_AVATAR Then
                                                plist.addlast( o ) ' Draw it anyway
                                        endif
                                Case GNET_CLOSED 'Object has been closed
                                        ' Spawn some particles or a sound effect or something.
                                        ' This player is no more...
                       
                        EndSelect
               
                Next

                ' We want to controll our local player
                If KeyDown( KEY_LEFT ) Then LocalPlayer.move(-1,0)
                If KeyDown( KEY_RIGHT ) Then LocalPlayer.move(1,0)
                If KeyDown( KEY_UP ) Then LocalPlayer.move(0,-1)
                If KeyDown( KEY_DOWN ) Then LocalPlayer.move(0,1)


        ' --------- GFX
       
        Cls ' nice and clean

                ' Draw connected players       
                Local y = 10
                For Local dp:TGnetObject = EachIn plist
                        ' Player list at top left
                        Local tmp:String = GetGNetString( dp,1 ) ' Nickname
                        DrawText tmp, 10,y
                        y:+17
                       
                        ' Draw a rectangle to show where the player is
                        Local xpos = GetGNetInt(dp,2) ' get position
                        Local ypos = GetGNetInt(dp,3)
                        DrawRect xpos-3,ypos-12,7,12
                        drawtext tmp, xpos,ypos-30
                       
                Next
               
                ' How many players are there?               
                DrawText "Players: "+plist.count(), 5,y
       
               
                ' Some GNet statistics               
                DrawText "In/out: "+GNetTotalBytesIn()+"/"+GNetTotalBytesOut(),  0,465
       
        Flip ; FlushMem

Wend

End ' bye bye



' ----- our player TYPE

' So we know what type an object is...
Const O_AVATAR = 0

' Our type
Type NetObject

        Field nobj:TGNetObject ' The actual GNet object
        Field xpos:Int ' Position on the screen
        Field ypos:Int


        Function create:NetObject( connection:TGNetHost, name:String, x:Int, y:Int )
       
                Local no:NetObject = New NetObject
       
                no.nobj = CreateGNetObject( connection ) ' Create the GNet object
               
                SetGNetInt( no.nobj, 0, O_AVATAR ) ' We use slot 0 as an object type ID
                no.place(x,y) ' Place the object
                no.rename(name) ' rename it
       
                Return no ' Return it
        EndFunction


       

        Method place( x:Int, y:Int )
                xpos = x ' Save the position internally
                ypos = y
                SetGNetInt( nobj,2,x ) ' slot 2 and 3 are used for x and y position.
                SetGNetInt( nobj,3,y )       
        EndMethod

        Method Move( xoff:Int, yoff:Int )
                xpos:+xoff
                ypos:+yoff
                SetGNetInt( nobj,2,xpos )
                SetGNetInt( nobj,3,ypos )       
        EndMethod
       
        Method rename( name:String )
                SetGNetString( nobj,1,name ) ' slot 1 is used for the nickname
        EndMethod

EndType

Спасиб!

snikers 29.09.2008 19:35

Ответ: GNet(сервер-клиент)
 
а_УУ! подскажите а то запарился, нет внятного примера, тот что в семплах не катит, так как там нет сервера(отдельно).

dimanche13 30.09.2008 09:51

Ответ: GNet(сервер-клиент)
 
надо сторонние либы подключать

snikers 30.09.2008 19:13

Ответ: GNet(сервер-клиент)
 
Цитата:

надо сторонние либы подключать
а не подскажеж какая рулит, всмысле какую стор. либу использовать?
и еще напиши что ты используеш в добавку к БМ? кроме ХМЛ и ЛУА

jimon 30.09.2008 19:20

Ответ: GNet(сервер-клиент)
 
RakNet довольно хорошая

HolyDel 30.09.2008 21:20

Ответ: GNet(сервер-клиент)
 
jimon, дорого сцуко. :'(

jimon 30.09.2008 22:09

Ответ: GNet(сервер-клиент)
 
HolyDel
не в деньгах счастье :)

dimanche13 01.10.2008 09:21

Ответ: GNet(сервер-клиент)
 
кстати, где-то jimon выкладывал приложение клиент-сервер на какой-то либе, надо только поискать.

jimon 01.10.2008 14:59

Ответ: GNet(сервер-клиент)
 
dimanche13
на raknet'е было

Siarzhuk Piatrouski 04.10.2008 05:35

Ответ: GNet(сервер-клиент)
 
Вполне неплохая либа... Правда лучше юзать нечто более толковое, возможно RakNet. :super:
http://www.truplo.com/TNet/

Данил 06.11.2009 18:44

Ответ: GNet(сервер-клиент)
 
подниму тему, потому что с TNetом какие-то проблемы, бмакс в упор не хочет видеть это, а других либ больше не видел.

Все-таки, что посоветуете? GNet, говорят, вообще параша, так:?


Часовой пояс GMT +4, время: 16:15.

vBulletin® Version 3.6.5.
Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Перевод: zCarot