Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Import vertex.bnetex
- SuperStrict
- Const MSG_JOIN:Short=0001
- Type TClient
- Field IP:Int
- Field Name:String
- Method Set()
- TMsgManager.GetStream().SetRemoteIP(IP)
- TMsgManager.GetStream().SetRemotePort(TMSGManager.PORT)
- EndMethod
- Method Create(IP:Int)
- Self.IP=IP
- TMSGManager.Clients.AddLast(Self)
- EndMethod
- EndType
- Type TMSG
- Const HEADERSIZE:Int=2*2
- Global MaxID:Int=0
- Field ID:Int
- Field Content:TBank '0=was es ist,2=die id, 4=der rest
- Method _SendMe()
- For Local C:TClient=EachIn TMSGManager.Clients
- C.Set()
- TMSGManager.GetStream().WriteInt(Content.Size())
- For Local i:Int=0 To Content.Size()-1
- TMSGManager.GetStream().WriteByte(Content.PeekByte(i))
- Next
- TMsgManager.GetStream().SendMsg()
- Next
- EndMethod
- Method Recv()
- Local Bank:TBank=CreateBank(TMSGManager.GetStream().ReadInt())
- For Local i:Int=0 To Bank.Size()-1
- Bank.PokeByte(i,TMSGManager.GetStream().ReadInt())
- Next
- Print "- "+GetTyp()
- EndMethod
- Method Send(Secure:Byte)
- If Secure
- EndIf
- _SendMe()
- EndMethod
- Method New()
- ID=MaxID+1
- MaxID:+1
- Content=CreateBank(HEADERSIZE)
- Content.PokeShort(0,0)
- Content.PokeShort(2,ID)
- EndMethod
- Method SetBank(B:TBank)
- 'Header hinzufügen
- Local Bank:TBank=CreateBank(B.Size()+HeaderSize)
- For Local i:Int=HeaderSize To B.size()-1
- Bank.PokeByte(i-HeaderSize, B.PeekByte(i))
- Next
- Content=Bank
- EndMethod
- Method GetBank:TBank()
- Local Bank:TBank=CreateBank(Content.size()-HEADERSIZE)
- For Local i:Int=HeaderSize To Bank.size()-1
- Bank.PokeByte(i-HeaderSize, Content.PeekByte(i))
- Next
- Return Bank
- EndMethod
- Method SetTyp(Typ:Int)
- Content.PokeShort(0,Typ)
- EndMethod
- Method GetTyp:Int()
- Return Content.PeekShort(0)
- EndMethod
- Function CreateVoidMessage:TMSG(Typ:Int)
- Local MSG:TMSG=New TMSG
- MSG.SetTyp(TYP)
- Return MSG
- EndFunction
- EndType
- Type TMSGManager
- Global _Stream:TUDPStream
- Global Clients:TList=New TList
- Global Host:Byte=False
- Global Recv:TList=New TList
- Global Port:Int
- Function Update()
- Try
- If _Stream.RecvAvail() Then
- Recv.Clear()
- While _Stream.RecvMsg(); Wend
- If _Stream.Size() > 0 Then
- WriteStdout("Message from:~n" + ..
- " - IP = " + TNetwork.StringIP(_Stream.GetMsgIP()) + "~n" + ..
- " - Port = " + _Stream.GetMsgPort() + "~n")
- While Not _Stream.Eof()
- Local MSG:TMSG=New TMSG
- MSG.Recv()
- Select MSG.GetTyp()
- Case MSG_JOIN
- End
- Default
- Recv.AddLast(MSG)
- EndSelect
- Wend
- EndIf
- EndIf
- Catch Exception:Object
- WriteStdout("Error~n " + Exception.ToString())
- End Try
- EndFunction
- Function GetStream:TUDPStream()
- Return _Stream
- EndFunction
- Function Init(IP:String="", Port:Int=1234)
- Self.Port=Port
- _Stream = New TUDPStream
- If Not _Stream.Init() Then Throw("Can't create socket")
- _Stream.SetLocalPort()
- If Not _Stream.SetBroadcast(True) RuntimeError("Cannot create broadcast")
- If IP<>""
- Local RealIP:Int=TNetwork.IntIP(IP)
- 'Client hinzufügen (ist in wirklichkeit der server)
- New TClient.Create(RealIP)
- 'nun an den Server die Willkommensnachricht senden
- TMSG.CreateVoidMessage(MSG_JOIN).Send(True)
- Else
- Host=True
- EndIf
- EndFunction
- EndType
- If Int(Input("Host(0)/Client(1): "))=0
- TMSGManager.Init()
- Else
- TMSGManager.Init("127.0.0.1")
- EndIf
- Repeat
- Local Bank:TBank=CreateBank(1)
- Bank.PokeByte(0,100)
- Local MSG:TMSG=New TMSG
- MSG.SetBank(Bank)
- MSG.Send(False)
- Print "Mainloop"
- Delay 100
- TMSGManager.Update()
- Until AppTerminate()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement