Home

Add

Edit

Without Linenumbers

Code in Textfield

Download

  1. '##############################################################################################################
    
  2. #DEFINE TSNE_SUBCALLBACK
    
  3. #INCLUDE Once "TSNE_V3.bi"
    
  4. 
    
  5. 
    
  6. 
    
  7. '##############################################################################################################
    
  8. Type Client_Type
    
  9.     V_Next              As Client_Type Ptr
    
  10.     V_Prev              As Client_Type Ptr
    
  11.     
    
  12.     V_TSNEID            As Uinteger
    
  13.     V_IPA               As String
    
  14.     V_Data              As String
    
  15. End Type
    
  16. '--------------------------------------------------------------------------------------------------------------
    
  17. Dim Shared G_ClientF    As Client_Type Ptr
    
  18. Dim Shared G_ClientL    As Client_Type Ptr
    
  19. Dim Shared G_ClientMux  As Any Ptr
    
  20. 
    
  21. 
    
  22. 
    
  23. '##############################################################################################################
    
  24. Sub SendToAll(V_Data As String)
    
  25. Mutexlock(G_ClientMux)
    
  26. Dim TPtr As Client_Type Ptr = G_ClientF
    
  27. Do Until TPtr = 0
    
  28.     TSNE_Data_Send(TPtr->V_TSNEID, V_Data)
    
  29.     TPtr = TPtr->V_Next
    
  30. Loop
    
  31. Mutexunlock(G_ClientMux)
    
  32. End Sub
    
  33. 
    
  34. 
    
  35. 
    
  36. '##############################################################################################################
    
  37. Sub TSNE_Disconnected(Byval V_TSNEID As Uinteger, Byval V_CallBackPtr As Client_Type Ptr)
    
  38. If V_CallBackPtr = 0 Then Exit Sub
    
  39. Print "[CLIENT] Disconnected: " & V_TSNEID & " " & V_CallBackPtr->V_IPA
    
  40. Mutexlock(G_ClientMux)
    
  41. Dim TPtr As Client_Type Ptr = G_ClientF
    
  42. Do Until TPtr = 0
    
  43.     If TPtr = V_CallBackPtr Then
    
  44.         If TPtr->V_Next <> 0 Then TPtr->V_Next->V_Prev = TPtr->V_Prev
    
  45.         If TPtr->V_Prev <> 0 Then TPtr->V_Prev->V_Next = TPtr->V_Next
    
  46.         If G_ClientF = TPtr Then G_ClientF = TPtr->V_Next
    
  47.         If G_ClientL = TPtr Then G_ClientL = TPtr->V_Prev
    
  48.         Exit Do
    
  49.     End If
    
  50.     TPtr = TPtr->V_Next
    
  51. Loop
    
  52. Mutexunlock(G_ClientMux)
    
  53. Deallocate(V_CallBackPtr)
    
  54. End Sub
    
  55. 
    
  56. 
    
  57. 
    
  58. '##############################################################################################################
    
  59. Sub TSNE_Connected(Byval V_TSNEID As Uinteger, Byval V_CallBackPtr As Client_Type Ptr)
    
  60. If V_CallBackPtr = 0 Then TSNE_Disconnect(V_TSNEID): Exit Sub
    
  61. Print "[CLIENT] Connected: " & V_TSNEID & " " & V_CallBackPtr->V_IPA
    
  62. End Sub
    
  63. 
    
  64. 
    
  65. 
    
  66. '##############################################################################################################
    
  67. Sub TSNE_NewData(Byval V_TSNEID As Uinteger, Byref V_Data As String, Byval V_CallBackPtr As Client_Type Ptr)
    
  68. If V_CallBackPtr = 0 Then TSNE_Disconnect(V_TSNEID): Exit Sub
    
  69. With *V_CallBackPtr
    
  70.     .V_Data += V_Data
    
  71.     Print "[CLIENT] DATA: " & V_TSNEID & " " & .V_IPA & "   DataLen:"; Len(.V_Data)
    
  72.     
    
  73.     'Nur zu dem clienten senden, vond em wir daten empfangen haben
    
  74.     TSNE_Data_Send(V_TSNEID, "Habe daten von dir empfangen!")
    
  75.     
    
  76.     'zu allen clienten senden
    
  77.     SendToAll("Hab von " & .V_IPA & " datenempfangen!")
    
  78.     
    
  79.     .V_Data = ""
    
  80. End With
    
  81. End Sub
    
  82. 
    
  83. 
    
  84. 
    
  85. '##############################################################################################################
    
  86. Sub TSNE_NewConnection(Byval V_TSNEID As Uinteger, Byval V_RequestID As Socket, Byval V_IPA As String)
    
  87. Dim TNewTSNEID As Uinteger
    
  88. Dim TClient As Client_Type Ptr = Callocate(Sizeof(Client_Type))
    
  89. TClient->V_IPA = V_IPA
    
  90. Mutexlock(G_ClientMux)
    
  91. Dim RV As Integer = TSNE_Create_Accept(V_RequestID, TNewTSNEID, , @TSNE_Disconnected, @TSNE_Connected, @TSNE_NewData, , , , Cast(Any Ptr, TClient))
    
  92. If RV <> TSNE_Const_NoError Then
    
  93.     Deallocate(TClient)
    
  94.     Print "[CLIENT] [FEHLER] " & TSNE_GetGURUCode(RV)
    
  95.     Exit Sub
    
  96. End If
    
  97. If G_ClientL <> 0 Then
    
  98.     G_ClientL->V_Next = TClient
    
  99.     G_ClientL->V_Next->V_Prev = G_ClientL
    
  100.     G_ClientL = G_ClientL->V_Next
    
  101. Else
    
  102.     G_ClientL = TClient
    
  103.     G_ClientF = G_ClientL
    
  104. End If
    
  105. With *G_ClientL
    
  106.     .V_TSNEID   = TNewTSNEID
    
  107.     .V_IPA      = V_IPA
    
  108. End With
    
  109. Mutexunlock(G_ClientMux)
    
  110. Print "[CLIENT] New Connect: " & TNewTSNEID & " " & V_IPA
    
  111. End Sub
    
  112. 
    
  113. 
    
  114. 
    
  115. '##############################################################################################################
    
  116. G_ClientMux = Mutexcreate()
    
  117. Dim TServerID As Uinteger
    
  118. Dim RV As Integer = TSNE_Create_Server(TServerID, 1234, 10, @TSNE_NewConnection)
    
  119. If RV <> TSNE_Const_NoError Then
    
  120.     Print "[SERVER] [FEHLER] " & TSNE_GetGURUCode(RV)
    
  121.     End 0
    
  122. End If
    
  123. Print "[SERVER] OK!"
    
  124. Do
    
  125.     Sleep 10, 1
    
  126. Loop Until Inkey() = Chr(27)
    
  127. RV = TSNE_Disconnect(TServerID)
    
  128. TSNE_WaitClose(TServerID)
    
  129. Mutexdestroy(G_ClientMux)
    
  130. End 0
    
  131.