Advertisement
Guest User

Untitled

a guest
Jun 25th, 2017
50
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.67 KB | None | 0 0
  1. Import vertex.bnetex
  2. SuperStrict
  3.  
  4. Const MSG_JOIN:Short=0001
  5.  
  6. Type TClient
  7. Field IP:Int
  8. Field Name:String
  9.  
  10. Method Set()
  11. TMsgManager.GetStream().SetRemoteIP(IP)
  12. TMsgManager.GetStream().SetRemotePort(TMSGManager.PORT)
  13. EndMethod
  14. Method Create(IP:Int)
  15. Self.IP=IP
  16. TMSGManager.Clients.AddLast(Self)
  17. EndMethod
  18. EndType
  19.  
  20. Type TMSG
  21. Const HEADERSIZE:Int=2*2
  22. Global MaxID:Int=0
  23. Field ID:Int
  24. Field Content:TBank '0=was es ist,2=die id, 4=der rest
  25.  
  26. Method _SendMe()
  27. For Local C:TClient=EachIn TMSGManager.Clients
  28. C.Set()
  29. TMSGManager.GetStream().WriteInt(Content.Size())
  30. For Local i:Int=0 To Content.Size()-1
  31. TMSGManager.GetStream().WriteByte(Content.PeekByte(i))
  32. Next
  33. TMsgManager.GetStream().SendMsg()
  34. Next
  35. EndMethod
  36.  
  37. Method Recv()
  38. Local Bank:TBank=CreateBank(TMSGManager.GetStream().ReadInt())
  39. For Local i:Int=0 To Bank.Size()-1
  40. Bank.PokeByte(i,TMSGManager.GetStream().ReadInt())
  41. Next
  42. Print "- "+GetTyp()
  43. EndMethod
  44.  
  45. Method Send(Secure:Byte)
  46. If Secure
  47.  
  48. EndIf
  49. _SendMe()
  50. EndMethod
  51.  
  52. Method New()
  53. ID=MaxID+1
  54. MaxID:+1
  55. Content=CreateBank(HEADERSIZE)
  56. Content.PokeShort(0,0)
  57. Content.PokeShort(2,ID)
  58. EndMethod
  59.  
  60. Method SetBank(B:TBank)
  61. 'Header hinzufügen
  62. Local Bank:TBank=CreateBank(B.Size()+HeaderSize)
  63. For Local i:Int=HeaderSize To B.size()-1
  64. Bank.PokeByte(i-HeaderSize, B.PeekByte(i))
  65. Next
  66.  
  67. Content=Bank
  68. EndMethod
  69. Method GetBank:TBank()
  70. Local Bank:TBank=CreateBank(Content.size()-HEADERSIZE)
  71. For Local i:Int=HeaderSize To Bank.size()-1
  72. Bank.PokeByte(i-HeaderSize, Content.PeekByte(i))
  73. Next
  74. Return Bank
  75. EndMethod
  76.  
  77. Method SetTyp(Typ:Int)
  78. Content.PokeShort(0,Typ)
  79. EndMethod
  80. Method GetTyp:Int()
  81. Return Content.PeekShort(0)
  82. EndMethod
  83.  
  84. Function CreateVoidMessage:TMSG(Typ:Int)
  85. Local MSG:TMSG=New TMSG
  86. MSG.SetTyp(TYP)
  87. Return MSG
  88. EndFunction
  89. EndType
  90.  
  91. Type TMSGManager
  92. Global _Stream:TUDPStream
  93. Global Clients:TList=New TList
  94. Global Host:Byte=False
  95. Global Recv:TList=New TList
  96. Global Port:Int
  97. Function Update()
  98. Try
  99. If _Stream.RecvAvail() Then
  100.  
  101. Recv.Clear()
  102. While _Stream.RecvMsg(); Wend
  103.  
  104.  
  105. If _Stream.Size() > 0 Then
  106.  
  107. WriteStdout("Message from:~n" + ..
  108. " - IP = " + TNetwork.StringIP(_Stream.GetMsgIP()) + "~n" + ..
  109. " - Port = " + _Stream.GetMsgPort() + "~n")
  110.  
  111. While Not _Stream.Eof()
  112. Local MSG:TMSG=New TMSG
  113. MSG.Recv()
  114. Select MSG.GetTyp()
  115. Case MSG_JOIN
  116. End
  117. Default
  118. Recv.AddLast(MSG)
  119. EndSelect
  120.  
  121. Wend
  122. EndIf
  123. EndIf
  124. Catch Exception:Object
  125. WriteStdout("Error~n " + Exception.ToString())
  126. End Try
  127. EndFunction
  128.  
  129. Function GetStream:TUDPStream()
  130. Return _Stream
  131. EndFunction
  132. Function Init(IP:String="", Port:Int=1234)
  133. Self.Port=Port
  134. _Stream = New TUDPStream
  135. If Not _Stream.Init() Then Throw("Can't create socket")
  136.  
  137.  
  138. _Stream.SetLocalPort()
  139.  
  140.  
  141. If Not _Stream.SetBroadcast(True) RuntimeError("Cannot create broadcast")
  142.  
  143. If IP<>""
  144. Local RealIP:Int=TNetwork.IntIP(IP)
  145. 'Client hinzufügen (ist in wirklichkeit der server)
  146. New TClient.Create(RealIP)
  147. 'nun an den Server die Willkommensnachricht senden
  148. TMSG.CreateVoidMessage(MSG_JOIN).Send(True)
  149. Else
  150. Host=True
  151. EndIf
  152. EndFunction
  153. EndType
  154. If Int(Input("Host(0)/Client(1): "))=0
  155. TMSGManager.Init()
  156. Else
  157. TMSGManager.Init("127.0.0.1")
  158. EndIf
  159.  
  160.  
  161. Repeat
  162. Local Bank:TBank=CreateBank(1)
  163. Bank.PokeByte(0,100)
  164. Local MSG:TMSG=New TMSG
  165. MSG.SetBank(Bank)
  166. MSG.Send(False)
  167. Print "Mainloop"
  168. Delay 100
  169. TMSGManager.Update()
  170. Until AppTerminate()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement