Home

Add

Edit

Without Linenumbers

Code in Textfield

Download

  1. '##############################################################################################################
    
  2. '##############################################################################################################
    
  3. ' TSNEplay_V3 - TCP Socket Networking [Eventing] Play (Gaming Extension) Version: 1.0 for TSNE_V3
    
  4. '##############################################################################################################
    
  5. '##############################################################################################################
    
  6. ' 2009 By.: /_\ DeltaLab's Germany - Experimental Computing
    
  7. ' Autor: Martin Wiemann
    
  8. ' Version: 2013.03.11
    
  9. '##############################################################################################################
    
  10. 
    
  11. 
    
  12. 
    
  13. 
    
  14. 
    
  15. #IFNDEF _TSNEplay_
    
  16.     #DEFINE _TSNEplay_
    
  17. '>...
    
  18. 
    
  19. '##############################################################################################################
    
  20. #DEFINE TSNE_DEF_REUSER
    
  21. #INCLUDE Once "TSNE_V3.bi"
    
  22. 
    
  23. 
    
  24. 
    
  25. '##############################################################################################################
    
  26. Enum TSNEPlay_GURUCode
    
  27.     TSNEPlay_NoError                            = 1
    
  28.     TSNEPlay_Unknown                            = 0
    
  29.     TSNEPlay_NotReadyForNewConnection           = -1000
    
  30.     TSNEPlay_PlayerIDNotFound                   = -1001
    
  31.     TSNEPlay_MessageToLong                      = -1002
    
  32. End Enum
    
  33. 
    
  34. 
    
  35. 
    
  36. '##############################################################################################################
    
  37. Enum TSNEPlay_State_Enum
    
  38.     TSNEPlay_State_Unknown                      = 0
    
  39.     TSNEPlay_State_Disconnected                 = 1
    
  40.     TSNEPlay_State_Connecting                   = 2
    
  41.     TSNEPlay_State_Connected                    = 3
    
  42.     TSNEPlay_State_Login                        = 4
    
  43.     TSNEPlay_State_Ready                        = 5
    
  44. End Enum
    
  45. 
    
  46. '--------------------------------------------------------------------------------------------------------------
    
  47. Enum TSNEPlay_MessageType_Enum
    
  48.     TSNEPlay_MSGType_Regular                    = 0
    
  49.     TSNEPlay_MSGType_Private                    = 1
    
  50.     TSNEPlay_MSGType_Notice                     = 2
    
  51.     TSNEPlay_MSGType_Hightlighted               = 3
    
  52. End Enum
    
  53. 
    
  54. 
    
  55. 
    
  56. '##############################################################################################################
    
  57. Enum TSNEPlay_INT_CMDType_Enum
    
  58.     TSNEPlay_INT_MSGT_Unknown                   = 0
    
  59.     TSNEPlay_INT_MSGT_NeedPassword              = 1
    
  60.     TSNEPlay_INT_MSGT_Password                  = 2
    
  61.     TSNEPlay_INT_MSGT_PasswordWrong             = 3
    
  62.     TSNEPlay_INT_MSGT_GetInfo                   = 4
    
  63.     TSNEPlay_INT_MSGT_PutInfo                   = 5
    
  64.     TSNEPlay_INT_MSGT_Ready                     = 10
    
  65.     TSNEPlay_INT_MSGT_StreamError               = 999999
    
  66.     TSNEPlay_INT_MSGT_Ping                      = 1000
    
  67.     TSNEPlay_INT_MSGT_Pong                      = 1001
    
  68.     TSNEPlay_INT_MSGT_MSG                       = 1002
    
  69.     TSNEPlay_INT_MSGT_Move                      = 1003
    
  70.     TSNEPlay_INT_MSGT_Dat                       = 1004
    
  71.     TSNEPlay_INT_MSGT_Con                       = 1005
    
  72.     TSNEPlay_INT_MSGT_Dis                       = 1006
    
  73.     TSNEPlay_INT_MSGT_Full                      = 1007
    
  74.     TSNEPlay_INT_MSGT_MoveDbl                   = 1008
    
  75. End Enum
    
  76. 
    
  77. 
    
  78. 
    
  79. '##############################################################################################################
    
  80. Type TSNEPlay_INT_CommandQue_Type
    
  81.     V_Next                                          As TSNEPlay_INT_CommandQue_Type Ptr
    
  82.     V_Prev                                          As TSNEPlay_INT_CommandQue_Type Ptr
    
  83.     
    
  84.     V_RAW                                           As String
    
  85.     V_CMDType                                       As TSNEPlay_INT_CMDType_Enum
    
  86.     V_Data                                          As String
    
  87. End Type
    
  88. 
    
  89. 
    
  90. 
    
  91. '##############################################################################################################
    
  92. Type TSNEPlay_INT_Client_Type
    
  93.     V_Next                                          As TSNEPlay_INT_Client_Type Ptr
    
  94.     V_Prev                                          As TSNEPlay_INT_Client_Type Ptr
    
  95.     
    
  96.     V_TSNEID                                        As Uinteger
    
  97.     V_IPA                                           As String
    
  98.     V_Data                                          As String
    
  99.     V_State                                         As TSNEPlay_State_Enum
    
  100.     
    
  101.     V_CMDQueF                                       As TSNEPlay_INT_CommandQue_Type Ptr
    
  102.     V_CMDQueL                                       As TSNEPlay_INT_CommandQue_Type Ptr
    
  103.     V_CMDQueC                                       As Ushort
    
  104.     
    
  105.     V_PlayerID                                      As Uinteger
    
  106.     V_Nickname                                      As String
    
  107.     
    
  108.     T_LastPing                                      As Double
    
  109.     T_PingT                                         As Double
    
  110. End Type
    
  111. 
    
  112. '--------------------------------------------------------------------------------------------------------------
    
  113. Dim Shared TSNEPlay_INT_ClientF                     As TSNEPlay_INT_Client_Type Ptr
    
  114. Dim Shared TSNEPlay_INT_ClientL                     As TSNEPlay_INT_Client_Type Ptr
    
  115. Dim Shared TSNEPlay_INT_ClientC                     As Uinteger
    
  116. Dim Shared TSNEPlay_INT_PlayerIDC                   As Uinteger
    
  117. 
    
  118. 
    
  119. 
    
  120. '##############################################################################################################
    
  121. Dim Shared TSNEPlay_INT_Mutex                       As Any Ptr
    
  122. '--------------------------------------------------------------------------------------------------------------
    
  123. Dim Shared TSNEPlay_INT_State                       As TSNEPlay_State_Enum
    
  124. Dim Shared TSNEPlay_INT_ClientMode                  As Ubyte
    
  125. Dim Shared TSNEPlay_INT_ServerPassword              As String
    
  126. Dim Shared TSNEPlay_INT_Nickname                    As String
    
  127. '--------------------------------------------------------------------------------------------------------------
    
  128. Dim Shared TSNEPlay_INT_Server_TSNEID               As Uinteger
    
  129. Dim Shared TSNEPlay_INT_Server_PlayerID             As Uinteger
    
  130. Dim Shared TSNEPlay_INT_Server_MaxPlayer            As Ushort
    
  131. '--------------------------------------------------------------------------------------------------------------
    
  132. Dim Shared TSNEPlay_INT_Client_TSNEID               As Uinteger
    
  133. Dim Shared TSNEPlay_INT_Client_Data                 As String
    
  134. Dim Shared TSNEPlay_INT_Client_PlayerID             As Uinteger
    
  135. '--------------------------------------------------------------------------------------------------------------
    
  136. Dim Shared TSNEPlay_INT_Event_ConnectionState       As Sub (Byval V_FromPlayerID As Uinteger, Byval V_State As TSNEPlay_State_Enum)
    
  137. Dim Shared TSNEPlay_INT_Event_Player_Connected      As Sub (Byval V_PlayerID As Uinteger, V_IPA As String, V_Nickname As String)
    
  138. Dim Shared TSNEPlay_INT_Event_Player_Disconnected   As Sub (Byval V_PlayerID As Uinteger)
    
  139. Dim Shared TSNEPlay_INT_Event_Message               As Sub (Byval V_FromPlayerID As Uinteger, Byval V_ToPlayerID As Uinteger, Byval V_Message As String, Byval V_MessageType As TSNEPlay_MessageType_Enum)
    
  140. Dim Shared TSNEPlay_INT_Event_Move                  As Sub (Byval V_FromPlayerID As Uinteger, Byval V_ToPlayerID As Uinteger, Byval V_NewPositionX As Integer, Byval V_NewPositionY As Integer, Byval V_NewPositionZ As Integer, Byval V_SubData As Uinteger)
    
  141. Dim Shared TSNEPlay_INT_Event_MoveDbl               As Sub (Byval V_FromPlayerID As Uinteger, Byval V_ToPlayerID As Uinteger, Byval V_NewPositionX As Double, Byval V_NewPositionY As Double, Byval V_NewPositionZ As Double, Byval V_SubData As Uinteger)
    
  142. Dim Shared TSNEPlay_INT_Event_Data                  As Sub (Byval V_FromPlayerID As Uinteger, Byval V_ToPlayerID As Uinteger, Byref V_Data As String)
    
  143. 
    
  144. 
    
  145. 
    
  146. '##############################################################################################################
    
  147. Sub TSNEPlay_INT_Construct() Constructor
    
  148. TSNEPlay_INT_Mutex = Mutexcreate()
    
  149. End Sub
    
  150. 
    
  151. '--------------------------------------------------------------------------------------------------------------
    
  152. Sub TSNEPlay_INT_Destruct() Destructor
    
  153. Mutexdestroy(TSNEPlay_INT_Mutex)
    
  154. TSNEPlay_INT_Mutex = 0
    
  155. End Sub
    
  156. 
    
  157. 
    
  158. 
    
  159. '##############################################################################################################
    
  160. Function TSNEPlay_INT_ClientGetPID(V_PlayerID As Uinteger) As TSNEPlay_INT_Client_Type Ptr
    
  161. Dim TPtr As TSNEPlay_INT_Client_Type Ptr = TSNEPlay_INT_ClientF
    
  162. Do Until TPtr = 0
    
  163.     If TPtr->V_PlayerID = V_PlayerID Then Return TPtr
    
  164.     TPtr = TPtr->V_Next
    
  165. Loop
    
  166. Return 0
    
  167. End Function
    
  168. 
    
  169. '--------------------------------------------------------------------------------------------------------------
    
  170. Function TSNEPlay_INT_ClientGetTSNEID(V_TSNEID As Uinteger) As TSNEPlay_INT_Client_Type Ptr
    
  171. Dim TPtr As TSNEPlay_INT_Client_Type Ptr = TSNEPlay_INT_ClientF
    
  172. Do Until TPtr = 0
    
  173.     If TPtr->V_TSNEID = V_TSNEID Then Return TPtr
    
  174.     TPtr = TPtr->V_Next
    
  175. Loop
    
  176. Return 0
    
  177. End Function
    
  178. 
    
  179. '--------------------------------------------------------------------------------------------------------------
    
  180. Function TSNEPlay_INT_ClientAdd() As TSNEPlay_INT_Client_Type Ptr
    
  181. Dim TPtr As TSNEPlay_INT_Client_Type Ptr
    
  182. TSNEPlay_INT_PlayerIDC += 1
    
  183. Do Until TSNEPlay_INT_ClientGetPID(TSNEPlay_INT_PlayerIDC) = 0
    
  184.     TSNEPlay_INT_PlayerIDC += 1
    
  185.     If TSNEPlay_INT_PlayerIDC = 0 Then TSNEPlay_INT_PlayerIDC = 1
    
  186. Loop
    
  187. If TSNEPlay_INT_ClientL <> 0 Then
    
  188.     TSNEPlay_INT_ClientL->V_Next = Callocate(Sizeof(TSNEPlay_INT_Client_Type))
    
  189.     TSNEPlay_INT_ClientL->V_Next->V_Prev = TSNEPlay_INT_ClientL
    
  190.     TSNEPlay_INT_ClientL = TSNEPlay_INT_ClientL->V_Next
    
  191. Else
    
  192.     TSNEPlay_INT_ClientL = Callocate(Sizeof(TSNEPlay_INT_Client_Type))
    
  193.     TSNEPlay_INT_ClientF = TSNEPlay_INT_ClientL
    
  194. End If
    
  195. With *TSNEPlay_INT_ClientL
    
  196.     .V_PlayerID = TSNEPlay_INT_PlayerIDC
    
  197. End With
    
  198. TSNEPlay_INT_ClientC += 1
    
  199. Return TSNEPlay_INT_ClientL
    
  200. End Function
    
  201. 
    
  202. '--------------------------------------------------------------------------------------------------------------
    
  203. Sub TSNEPlay_INT_ClientDel(V_Client As TSNEPlay_INT_Client_Type Ptr)
    
  204. If V_Client->V_Next <> 0 Then V_Client->V_Next->V_Prev = V_Client->V_Prev
    
  205. If V_Client->V_Prev <> 0 Then V_Client->V_Prev->V_Next = V_Client->V_Next
    
  206. If TSNEPlay_INT_ClientF = V_Client Then TSNEPlay_INT_ClientF = V_Client->V_Next
    
  207. If TSNEPlay_INT_ClientL = V_Client Then TSNEPlay_INT_ClientL = V_Client->V_Prev
    
  208. With *V_Client
    
  209.     Do Until .V_CMDQueF = 0
    
  210.         .V_CMDQueL = .V_CMDQueF->V_Next
    
  211.         Deallocate(.V_CMDQueF)
    
  212.         .V_CMDQueF = .V_CMDQueL
    
  213.     Loop
    
  214. End With
    
  215. Deallocate(V_Client)
    
  216. If TSNEPlay_INT_ClientC > 0 Then TSNEPlay_INT_ClientC -= 1
    
  217. End Sub
    
  218. 
    
  219. 
    
  220. 
    
  221. '##############################################################################################################
    
  222. Function TSNEPlay_Desc_GetGuruCode(V_GuruCode As TSNEPlay_GURUCode) As String
    
  223. Select Case V_GuruCode
    
  224.     Case TSNEPlay_NoError                           : Return "No error!"
    
  225.     Case TSNEPlay_Unknown                           : Return "Unknown error!"
    
  226.     Case TSNEPlay_NotReadyForNewConnection          : Return "Not Ready! Conection Already Exist! Close all connections!"
    
  227.     Case TSNEPlay_MessageToLong                     : Return "Message text is too long!"
    
  228.     Case Else                                       : Return TSNE_GetGURUCode(V_GuruCode)
    
  229. End Select
    
  230. End Function
    
  231. 
    
  232. '--------------------------------------------------------------------------------------------------------------
    
  233. Function TSNEPlay_Desc_GetStateCode(V_State As TSNEPlay_State_Enum) As String
    
  234. Select Case V_State
    
  235.     Case TSNEPlay_State_Unknown                     : Return "Unknown"
    
  236.     Case TSNEPlay_State_Disconnected                : Return "Disconnected!"
    
  237.     Case TSNEPlay_State_Connecting                  : Return "Connecting..."
    
  238.     Case TSNEPlay_State_Connected                   : Return "Connected!"
    
  239.     Case TSNEPlay_State_Login                       : Return "Login..."
    
  240.     Case TSNEPlay_State_Ready                       : Return "Ready!"
    
  241.     Case Else                                       : Return "[Unknown State-Code]"
    
  242. End Select
    
  243. End Function
    
  244. 
    
  245. 
    
  246. 
    
  247. '##############################################################################################################
    
  248. Function TSNEPlay_INT_CreateStreamCommand(V_Command As TSNEPlay_INT_CMDType_Enum, V_Data As String = "") As String
    
  249. 'Print "OUT_CMD: >"; Str(V_Command); "<___>"; Str(Len(V_Data)) & "<"
    
  250. Dim MX As Uinteger = 4 + Len(V_Data)
    
  251. Dim T As String
    
  252. T += Chr((MX Shr 24) And 255) & Chr((MX Shr 16) And 255) & Chr((MX Shr 8) And 255) & Chr(MX And 255)
    
  253. T += Chr((V_Command Shr 24) And 255) & Chr((V_Command Shr 16) And 255) & Chr((V_Command Shr 8) And 255) & Chr(V_Command And 255)
    
  254. Return T & V_Data
    
  255. End Function
    
  256. 
    
  257. '--------------------------------------------------------------------------------------------------------------
    
  258. Sub TSNEPlay_INT_ParseStreamCommand(Byref V_Data As String, Byref R_CMDQueF As TSNEPlay_INT_CommandQue_Type Ptr, Byref R_CMDQueL As TSNEPlay_INT_CommandQue_Type Ptr)
    
  259. Dim MX As Uinteger
    
  260. Dim XRAW As String
    
  261. Dim XCMD As TSNEPlay_INT_CMDType_Enum
    
  262. Dim XData As String
    
  263. Do
    
  264.     If Len(V_Data) < 8 Then Exit Sub
    
  265.     MX = (V_Data[0] Shl 24) Or (V_Data[1] Shl 16) Or (V_Data[2] Shl 8) Or V_Data[3]
    
  266.     If (Len(V_Data) - 4) < MX Then Exit Sub
    
  267.     XCMD = (V_Data[4] Shl 24) Or (V_Data[5] Shl 16) Or (V_Data[6] Shl 8) Or V_Data[7]
    
  268.     XRAW = Left(V_Data, MX + 4)
    
  269.     XData = Mid(V_Data, 9, MX - 4)
    
  270.     V_Data = Mid(V_Data, MX + 5)
    
  271.     If R_CMDQueL <> 0 Then
    
  272.         R_CMDQueL->V_Next = Callocate(Sizeof(TSNEPlay_INT_CommandQue_Type))
    
  273.         R_CMDQueL = R_CMDQueL->V_Next
    
  274.     Else
    
  275.         R_CMDQueL = Callocate(Sizeof(TSNEPlay_INT_CommandQue_Type))
    
  276.         R_CMDQueF = R_CMDQueL
    
  277.     End If
    
  278.     With *R_CMDQueL
    
  279.         .V_RAW = XRAW
    
  280.         .V_CMDType = XCMD
    
  281.         .V_Data = XData
    
  282.     End With
    
  283. Loop
    
  284. End Sub
    
  285. 
    
  286. '--------------------------------------------------------------------------------------------------------------
    
  287. Function TSNEPlay_INT_SendData(V_ToPlayerID As Uinteger, V_Data As String, Byref R_LocalSend As Ubyte) As TSNEPlay_GURUCode
    
  288. 'Print "OUT:"; V_ToPlayerID; " "; Len(V_Data); "  ";: For XXX as UInteger = 1 to Len(V_Data): Print V_Data[XXX - 1]; " ";: Next: Print
    
  289. R_LocalSend = 0
    
  290. If TSNEPlay_INT_ClientMode = 1 Then
    
  291.     TSNE_Data_Send(TSNEPlay_INT_Client_TSNEID, V_Data)
    
  292.     Return TSNEPlay_NoError
    
  293. End If
    
  294. Dim TPtr As TSNEPlay_INT_Client_Type Ptr
    
  295. Mutexlock(TSNEPlay_INT_Mutex)
    
  296. If V_ToPlayerID > 0 Then
    
  297.     Dim TSID As Uinteger
    
  298.     TPtr = TSNEPlay_INT_ClientGetPID(V_ToPlayerID)
    
  299.     If TPtr = 0 Then Mutexunlock(TSNEPlay_INT_Mutex): Return TSNEPlay_PlayerIDNotFound
    
  300.     TSID = TPtr->V_TSNEID
    
  301.     Mutexunlock(TSNEPlay_INT_Mutex)
    
  302.     If TSID > 0 Then
    
  303.         TSNE_Data_Send(TSID, V_Data)
    
  304.     Else: R_LocalSend = 1
    
  305.     End If
    
  306. Else
    
  307.     Dim DD() As Uinteger
    
  308.     Dim DC As Uinteger
    
  309.     Dim DX As Uinteger
    
  310.     TPtr = TSNEPlay_INT_ClientF
    
  311.     Do Until TPtr = 0
    
  312.         If TPtr->V_State = TSNEPlay_State_Ready Then
    
  313.             If TPtr->V_TSNEID > 0 Then
    
  314.                 DC += 1
    
  315.                 If DC > DX Then
    
  316.                     DX += 4
    
  317.                     Redim Preserve DD(DX) As Uinteger
    
  318.                 End If
    
  319.                 DD(DC) = TPtr->V_TSNEID
    
  320.             Else: R_LocalSend = 1
    
  321.             End If
    
  322.         End If
    
  323.         TPtr = TPtr->V_Next
    
  324.     Loop
    
  325.     Mutexunlock(TSNEPlay_INT_Mutex)
    
  326.     For X As Uinteger = 1 To DC
    
  327.         TSNE_Data_Send(DD(X), V_Data)
    
  328.     Next
    
  329. End If
    
  330. Return TSNEPlay_NoError
    
  331. End Function
    
  332. 
    
  333. 
    
  334. 
    
  335. '##############################################################################################################
    
  336. Sub TSNEPlay_INT_SendPlayerTable(V_TSNEID As Uinteger)
    
  337. Dim D As String
    
  338. Dim TMS0 As String
    
  339. Dim TMS1 As String
    
  340. Dim TMV0 As Uinteger
    
  341. Mutexlock(TSNEPlay_INT_Mutex)
    
  342. Dim TPtr As TSNEPlay_INT_Client_Type Ptr = TSNEPlay_INT_ClientF
    
  343. Do Until TPtr = 0
    
  344.     If TPtr->V_TSNEID <> V_TSNEID Then
    
  345.         With *TPtr
    
  346.             TMS1 = Chr((.V_PlayerID Shr 24) And 255) & Chr((.V_PlayerID Shr 16) And 255) & Chr((.V_PlayerID Shr 8) And 255) & Chr(.V_PlayerID And 255)
    
  347.             TMV0 = 0: TMS1 += Chr((TMV0 Shr 24) And 255) & Chr((TMV0 Shr 16) And 255) & Chr((TMV0 Shr 8) And 255) & Chr(TMV0 And 255)
    
  348.             TMS1 += ""
    
  349.             TMV0 = Len(.V_Nickname): TMS1 += Chr((TMV0 Shr 24) And 255) & Chr((TMV0 Shr 16) And 255) & Chr((TMV0 Shr 8) And 255) & Chr(TMV0 And 255)
    
  350.             TMS1 += .V_Nickname
    
  351.             TMS1 = TSNEPlay_INT_CreateStreamCommand(TSNEPlay_INT_MSGT_Con, TMS1)
    
  352.             D += TMS1
    
  353.         End With
    
  354.     End If
    
  355.     TPtr = TPtr->V_Next
    
  356. Loop
    
  357. Mutexunlock(TSNEPlay_INT_Mutex)
    
  358. TSNE_Data_Send(V_TSNEID, D)
    
  359. End Sub
    
  360. 
    
  361. 
    
  362. 
    
  363. '##############################################################################################################
    
  364. Sub TSNEPlay_INT_Disconnected(Byval V_TSNEID As Uinteger)
    
  365. 'Print "DIS:"; V_TSNEID
    
  366. If TSNEPlay_INT_ClientMode = 1 Then
    
  367.     TSNEPlay_INT_State = TSNEPlay_State_Disconnected
    
  368.     If TSNEPlay_INT_Event_ConnectionState <> 0 Then TSNEPlay_INT_Event_ConnectionState(TSNEPlay_INT_Client_PlayerID, TSNEPlay_INT_State)
    
  369.     Exit Sub
    
  370. End If
    
  371. Mutexlock(TSNEPlay_INT_Mutex)
    
  372. Dim TPtr As TSNEPlay_INT_Client_Type Ptr = TSNEPlay_INT_ClientGetTSNEID(V_TSNEID)
    
  373. If TPtr = 0 Then Mutexunlock(TSNEPlay_INT_Mutex): TSNE_Disconnect(V_TSNEID): Exit Sub
    
  374. Dim TPID As Uinteger = TPtr->V_PlayerID
    
  375. Dim TAOK As TSNEPlay_State_Enum = TPtr->V_State
    
  376. TSNEPlay_INT_ClientDel(TPtr)
    
  377. Mutexunlock(TSNEPlay_INT_Mutex)
    
  378. If TSNEPlay_INT_Event_Player_Disconnected <> 0 Then TSNEPlay_INT_Event_Player_Disconnected(TPID)
    
  379. If TAOK <> TSNEPlay_State_Ready Then Exit Sub
    
  380. Dim T As String = Chr((TPID Shr 24) And 255) & Chr((TPID Shr 16) And 255) & Chr((TPID Shr 8) And 255) & Chr(TPID And 255)
    
  381. T = TSNEPlay_INT_CreateStreamCommand(TSNEPlay_INT_MSGT_Dis, T)
    
  382. Dim TLocalSend As Ubyte
    
  383. Dim RV As TSNEPlay_GURUCode = TSNEPlay_INT_SendData(0, T, TLocalSend)
    
  384. End Sub
    
  385. 
    
  386. '--------------------------------------------------------------------------------------------------------------
    
  387. Sub TSNEPlay_INT_Connected(Byval V_TSNEID As Uinteger)
    
  388. 'Print "CON:"; V_TSNEID
    
  389. If TSNEPlay_INT_ClientMode = 1 Then
    
  390.     TSNEPlay_INT_State = TSNEPlay_State_Connected
    
  391.     If TSNEPlay_INT_Event_ConnectionState <> 0 Then TSNEPlay_INT_Event_ConnectionState(TSNEPlay_INT_Client_PlayerID, TSNEPlay_INT_State)
    
  392.     Exit Sub
    
  393. End If
    
  394. Mutexlock(TSNEPlay_INT_Mutex)
    
  395. Dim TPtr As TSNEPlay_INT_Client_Type Ptr = TSNEPlay_INT_ClientGetTSNEID(V_TSNEID)
    
  396. If TPtr = 0 Then Mutexunlock(TSNEPlay_INT_Mutex): TSNE_Disconnect(V_TSNEID): Exit Sub
    
  397. Dim TPID As Uinteger
    
  398. With *TPtr
    
  399.     If TSNEPlay_INT_ServerPassword = "" Then
    
  400.         .V_State        = TSNEPlay_State_Connected
    
  401.     Else: .V_State      = TSNEPlay_State_Login
    
  402.     End If
    
  403.     TPID = .V_PlayerID
    
  404. End With
    
  405. Mutexunlock(TSNEPlay_INT_Mutex)
    
  406. If TSNEPlay_INT_ServerPassword <> "" Then
    
  407.     TSNE_Data_Send(V_TSNEID, TSNEPlay_INT_CreateStreamCommand(TSNEPlay_INT_MSGT_NeedPassword))
    
  408. Else: TSNE_Data_Send(V_TSNEID, TSNEPlay_INT_CreateStreamCommand(TSNEPlay_INT_MSGT_GetInfo, Str(TPID)))
    
  409. End If
    
  410. End Sub
    
  411. 
    
  412. '--------------------------------------------------------------------------------------------------------------
    
  413. Sub TSNEPlay_INT_NewData(Byval V_TSNEID As Uinteger, Byref V_Data As String)
    
  414. 'Print "DAT:"; V_TSNEID; " "; Len(V_Data); "  ";: For XXX as UInteger = 1 to Len(V_Data): Print V_Data[XXX - 1]; " ";: Next: Print
    
  415. Dim TCMDQueF As TSNEPlay_INT_CommandQue_Type Ptr
    
  416. Dim TCMDQueL As TSNEPlay_INT_CommandQue_Type Ptr
    
  417. Dim TCInfo As TSNEPlay_INT_Client_Type
    
  418. Dim TPtr As TSNEPlay_INT_Client_Type Ptr
    
  419. If TSNEPlay_INT_ClientMode = 1 Then
    
  420.     If Len(TSNEPlay_INT_Client_Data) > 10000 Then
    
  421.         TSNE_Data_Send(V_TSNEID, TSNEPlay_INT_CreateStreamCommand(TSNEPlay_INT_MSGT_StreamError))
    
  422.         TSNE_Disconnect(V_TSNEID)
    
  423.         Exit Sub
    
  424.     End If
    
  425.     TSNEPlay_INT_Client_Data += V_Data
    
  426.     TSNEPlay_INT_ParseStreamCommand(TSNEPlay_INT_Client_Data, TCMDQueF, TCMDQueL)
    
  427. Else
    
  428.     Mutexlock(TSNEPlay_INT_Mutex)
    
  429.     TPtr = TSNEPlay_INT_ClientGetTSNEID(V_TSNEID)
    
  430.     If TPtr = 0 Then Mutexunlock(TSNEPlay_INT_Mutex): TSNE_Disconnect(V_TSNEID): Exit Sub
    
  431.     Dim TData As String = TPtr->V_Data & V_Data
    
  432.     Mutexunlock(TSNEPlay_INT_Mutex)
    
  433.     If Len(TData) > 10000 Then
    
  434.         TSNE_Data_Send(V_TSNEID, TSNEPlay_INT_CreateStreamCommand(TSNEPlay_INT_MSGT_StreamError))
    
  435.         TSNE_Disconnect(V_TSNEID)
    
  436.         Exit Sub
    
  437.     End If
    
  438.     TSNEPlay_INT_ParseStreamCommand(TData, TCMDQueF, TCMDQueL)
    
  439.     Mutexlock(TSNEPlay_INT_Mutex)
    
  440.     TPtr = TSNEPlay_INT_ClientGetTSNEID(V_TSNEID)
    
  441.     If TPtr = 0 Then Mutexunlock(TSNEPlay_INT_Mutex): TSNE_Disconnect(V_TSNEID): Exit Sub
    
  442.     TPtr->V_Data = TData
    
  443.     TCInfo = *TPtr
    
  444.     Mutexunlock(TSNEPlay_INT_Mutex)
    
  445. End If
    
  446. Dim TLocalSend As Ubyte
    
  447. Dim RV As TSNEPlay_GURUCode
    
  448. Dim XErrExit As Ubyte
    
  449. Dim TMV0 As Uinteger
    
  450. Dim TMV1 As Uinteger
    
  451. Dim TMV2 As Double
    
  452. Dim TMV3 As Double
    
  453. Dim TMV4 As Double
    
  454. Dim TMV5 As Uinteger
    
  455. Dim TMV6 As Uinteger
    
  456. Dim TMV7 As Uinteger
    
  457. Dim TMVI1 As Integer
    
  458. Dim TMVI2 As Integer
    
  459. Dim TMVI3 As Integer
    
  460. Dim TMS0 As String
    
  461. Dim TMS1 As String
    
  462. Do Until TCMDQueF = 0
    
  463.     TCMDQueL = TCMDQueF->V_Next
    
  464.     If XErrExit = 0 Then
    
  465.         With *TCMDQueF
    
  466.             TMV0 = 0
    
  467.             TMV1 = 0
    
  468.             TMV2 = 0
    
  469.             TMV3 = 0
    
  470.             TMV4 = 0
    
  471.             TMV5 = 0
    
  472.             TMS0 = ""
    
  473.             TMS1 = ""
    
  474. '           Print "CMD: >"; Str(.V_CMDType); "<___>"; .V_Data; "<"
    
  475. '           Print #1, "IN_CMD: >"; Str(.V_CMDType); "<___>"; Str(Len(.V_Data)); "<"
    
  476.             If TSNEPlay_INT_ClientMode = 1 Then
    
  477.                 Select Case .V_CMDType
    
  478.                     Case TSNEPlay_INT_MSGT_Unknown
    
  479.                     
    
  480.                     Case TSNEPlay_INT_MSGT_Full
    
  481.                         
    
  482.                     Case TSNEPlay_INT_MSGT_NeedPassword
    
  483.                         If TSNEPlay_INT_State = TSNEPlay_State_Connected Then
    
  484.                             TSNEPlay_INT_State = TSNEPlay_State_Login
    
  485.                             If TSNEPlay_INT_Event_ConnectionState <> 0 Then TSNEPlay_INT_Event_ConnectionState(TSNEPlay_INT_Client_PlayerID, TSNEPlay_INT_State)
    
  486.                             TSNE_Data_Send(V_TSNEID, TSNEPlay_INT_CreateStreamCommand(TSNEPlay_INT_MSGT_Password, TSNEPlay_INT_ServerPassword))
    
  487.                         End If
    
  488.                         
    
  489.                     Case TSNEPlay_INT_MSGT_PasswordWrong
    
  490.                         XErrExit = 1
    
  491.                         
    
  492.                     Case TSNEPlay_INT_MSGT_GetInfo
    
  493.                         TSNEPlay_INT_Client_PlayerID = Valuint(.V_Data)
    
  494.                         TMV0 = Len(TSNEPlay_INT_Nickname): TMS0 += Chr((TMV0 Shr 24) And 255) & Chr((TMV0 Shr 16) And 255) & Chr((TMV0 Shr 8) And 255) & Chr(TMV0 And 255)
    
  495.                         TMS0 += TSNEPlay_INT_Nickname
    
  496.                         TSNE_Data_Send(V_TSNEID, TSNEPlay_INT_CreateStreamCommand(TSNEPlay_INT_MSGT_PutInfo, TMS0))
    
  497.                         
    
  498.                     Case TSNEPlay_INT_MSGT_PutInfo
    
  499.                         
    
  500.                     Case TSNEPlay_INT_MSGT_Ready
    
  501.                         TSNEPlay_INT_State = TSNEPlay_State_Ready
    
  502.                         If TSNEPlay_INT_Event_ConnectionState <> 0 Then TSNEPlay_INT_Event_ConnectionState(TSNEPlay_INT_Client_PlayerID, TSNEPlay_INT_State)
    
  503.                         
    
  504.                     Case TSNEPlay_INT_MSGT_StreamError
    
  505.                         XErrExit = 1
    
  506.                         
    
  507.                     Case TSNEPlay_INT_MSGT_Ping
    
  508.                         
    
  509.                     Case TSNEPlay_INT_MSGT_MSG
    
  510.                         If TSNEPlay_INT_Event_Message <> 0 Then
    
  511.                             If Len(.V_Data) >= 16 Then
    
  512.                                 TMV0 = (.V_Data[0] Shl 24) Or (.V_Data[1] Shl 16) Or (.V_Data[2] Shl 8) Or .V_Data[3]
    
  513.                                 TMV1 = (.V_Data[4] Shl 24) Or (.V_Data[5] Shl 16) Or (.V_Data[6] Shl 8) Or .V_Data[7]
    
  514.                                 TMV6 = (.V_Data[8] Shl 24) Or (.V_Data[9] Shl 16) Or (.V_Data[10] Shl 8) Or .V_Data[11]
    
  515.                                 TMV7 = (.V_Data[12] Shl 24) Or (.V_Data[13] Shl 16) Or (.V_Data[14] Shl 8) Or .V_Data[15]
    
  516.                                 If (Len(.V_Data) - 16) >= TMV7 Then
    
  517.                                     TMS0 = Mid(.V_Data, 17, TMV7)
    
  518.                                     TSNEPlay_INT_Event_Message(TMV0, TMV1, TMS0, Cast(TSNEPlay_MessageType_Enum, TMV6))
    
  519.                                 End If
    
  520.                             End If
    
  521.                         End If
    
  522.                         
    
  523.                     Case TSNEPlay_INT_MSGT_Move
    
  524.                         If TSNEPlay_INT_Event_Move <> 0 Then
    
  525.                             If Len(.V_Data) >= 36 Then
    
  526.                                 TMV0 = (.V_Data[0] Shl 24) Or (.V_Data[1] Shl 16) Or (.V_Data[2] Shl 8) Or .V_Data[3]
    
  527.                                 TMV1 = (.V_Data[4] Shl 24) Or (.V_Data[5] Shl 16) Or (.V_Data[6] Shl 8) Or .V_Data[7]
    
  528.                                 TMVI1 = *Cast(Integer Ptr, @.V_Data[8])
    
  529.                                 TMVI2 = *Cast(Integer Ptr, @.V_Data[16])
    
  530.                                 TMVI3 = *Cast(Integer Ptr, @.V_Data[24])
    
  531.                                 TMV5 = (.V_Data[32] Shl 24) Or (.V_Data[33] Shl 16) Or (.V_Data[34] Shl 8) Or .V_Data[35]
    
  532.                                 TSNEPlay_INT_Event_Move(TMV0, TMV1, TMVI1, TMVI2, TMVI3, TMV5)
    
  533.                             End If
    
  534.                         End If
    
  535.                         
    
  536.                     Case TSNEPlay_INT_MSGT_MoveDbl
    
  537.                         If TSNEPlay_INT_Event_Move <> 0 Then
    
  538.                             If Len(.V_Data) >= 36 Then
    
  539.                                 TMV0 = (.V_Data[0] Shl 24) Or (.V_Data[1] Shl 16) Or (.V_Data[2] Shl 8) Or .V_Data[3]
    
  540.                                 TMV1 = (.V_Data[4] Shl 24) Or (.V_Data[5] Shl 16) Or (.V_Data[6] Shl 8) Or .V_Data[7]
    
  541.                                 TMV2 = *Cast(Double Ptr, @.V_Data[8])
    
  542.                                 TMV3 = *Cast(Double Ptr, @.V_Data[16])
    
  543.                                 TMV4 = *Cast(Double Ptr, @.V_Data[24])
    
  544.                                 TMV5 = (.V_Data[32] Shl 24) Or (.V_Data[33] Shl 16) Or (.V_Data[34] Shl 8) Or .V_Data[35]
    
  545.                                 TSNEPlay_INT_Event_Move(TMV0, TMV1, TMV2, TMV3, TMV4, TMV5)
    
  546.                             End If
    
  547.                         End If
    
  548.                     
    
  549.                     Case TSNEPlay_INT_MSGT_Dat
    
  550.                         If TSNEPlay_INT_Event_Data <> 0 Then
    
  551.                             If Len(.V_Data) >= 12 Then
    
  552.                                 TMV0 = (.V_Data[0] Shl 24) Or (.V_Data[1] Shl 16) Or (.V_Data[2] Shl 8) Or .V_Data[3]
    
  553.                                 TMV1 = (.V_Data[4] Shl 24) Or (.V_Data[5] Shl 16) Or (.V_Data[6] Shl 8) Or .V_Data[7]
    
  554.                                 TMV6 = (.V_Data[8] Shl 24) Or (.V_Data[9] Shl 16) Or (.V_Data[10] Shl 8) Or .V_Data[11]
    
  555.                                 If (Len(.V_Data) - 12) >= TMV6 Then
    
  556.                                     TMS0 = Mid(.V_Data, 13, TMV6)
    
  557.                                     TSNEPlay_INT_Event_Data(TMV0, TMV1, TMS0)
    
  558.                                 End If
    
  559.                             End If
    
  560.                         End If
    
  561.                         
    
  562.                     Case TSNEPlay_INT_MSGT_Con
    
  563.                         If TSNEPlay_INT_Event_Player_Connected <> 0 Then
    
  564.                             If Len(.V_Data) >= 8 Then
    
  565.                                 TMV0 = (.V_Data[0] Shl 24) Or (.V_Data[1] Shl 16) Or (.V_Data[2] Shl 8) Or .V_Data[3]
    
  566.                                 TMV1 = (.V_Data[4] Shl 24) Or (.V_Data[5] Shl 16) Or (.V_Data[6] Shl 8) Or .V_Data[7]
    
  567.                                 If (Len(.V_Data) - 8) >= TMV1 Then
    
  568.                                     TMS0 = Mid(.V_Data, 9, TMV1)
    
  569.                                     .V_Data = Mid(.V_Data, 9 + TMV1)
    
  570.                                     TMV1 = (.V_Data[0] Shl 24) Or (.V_Data[1] Shl 16) Or (.V_Data[2] Shl 8) Or .V_Data[3]
    
  571.                                     If (Len(.V_Data) - 4) >= TMV1 Then
    
  572.                                         TMS1 = Mid(.V_Data, 5, TMV1)
    
  573.                                         TSNEPlay_INT_Event_Player_Connected(TMV0, TMS0, TMS1)
    
  574.                                     End If
    
  575.                                 End If
    
  576.                             End If
    
  577.                         End If
    
  578.                         
    
  579.                     Case TSNEPlay_INT_MSGT_Dis
    
  580.                         If TSNEPlay_INT_Event_Player_Disconnected <> 0 Then
    
  581.                             If Len(.V_Data) >= 4 Then
    
  582.                                 TMV0 = (.V_Data[0] Shl 24) Or (.V_Data[1] Shl 16) Or (.V_Data[2] Shl 8) Or .V_Data[3]
    
  583.                                 TSNEPlay_INT_Event_Player_Disconnected(TMV0)
    
  584.                             End If
    
  585.                         End If
    
  586.                         
    
  587.                 End Select
    
  588.             Else
    
  589.                 Select Case .V_CMDType
    
  590.                     Case TSNEPlay_INT_MSGT_Unknown
    
  591.                     Case TSNEPlay_INT_MSGT_Password
    
  592.                         If .V_Data <> TSNEPlay_INT_ServerPassword Then
    
  593.                             XErrExit = 1
    
  594.                             TSNE_Data_Send(V_TSNEID, TSNEPlay_INT_CreateStreamCommand(TSNEPlay_INT_MSGT_Ready))
    
  595.                         Else: TSNE_Data_Send(V_TSNEID, TSNEPlay_INT_CreateStreamCommand(TSNEPlay_INT_MSGT_GetInfo, Str(TCInfo.V_PlayerID)))
    
  596.                         End If
    
  597.                         
    
  598.                     Case TSNEPlay_INT_MSGT_GetInfo
    
  599.                         
    
  600.                     Case TSNEPlay_INT_MSGT_PutInfo
    
  601.                         If (TCInfo.V_State = TSNEPlay_State_Connected) Or (TCInfo.V_State = TSNEPlay_State_Login) Then
    
  602.                             If Len(.V_Data) >= 4 Then
    
  603.                                 TMV0 = (.V_Data[0] Shl 24) Or (.V_Data[1] Shl 16) Or (.V_Data[2] Shl 8) Or .V_Data[3]
    
  604.                                 TMS0 = Mid(.V_Data, 5, TMV0)
    
  605.                                 Mutexlock(TSNEPlay_INT_Mutex)
    
  606.                                 TPtr = TSNEPlay_INT_ClientGetTSNEID(V_TSNEID)
    
  607.                                 If TPtr > 0 Then
    
  608.                                     TPtr->V_Nickname = TMS0
    
  609.                                     TPtr->V_State = TSNEPlay_State_Ready
    
  610.                                 End If
    
  611.                                 Mutexunlock(TSNEPlay_INT_Mutex)
    
  612.                                 TSNE_Data_Send(V_TSNEID, TSNEPlay_INT_CreateStreamCommand(TSNEPlay_INT_MSGT_Ready))
    
  613.                                 TSNEPlay_INT_Event_Player_Connected(TCInfo.V_PlayerID, TCInfo.V_IPA, TMS0)
    
  614.                                 TMS1 += Chr((TCInfo.V_PlayerID Shr 24) And 255) & Chr((TCInfo.V_PlayerID Shr 16) And 255) & Chr((TCInfo.V_PlayerID Shr 8) And 255) & Chr(TCInfo.V_PlayerID And 255)
    
  615.                                 TMV0 = 0: TMS1 += Chr((TMV0 Shr 24) And 255) & Chr((TMV0 Shr 16) And 255) & Chr((TMV0 Shr 8) And 255) & Chr(TMV0 And 255)
    
  616.                                 TMS1 += ""
    
  617.                                 TMV0 = Len(TMS0): TMS1 += Chr((TMV0 Shr 24) And 255) & Chr((TMV0 Shr 16) And 255) & Chr((TMV0 Shr 8) And 255) & Chr(TMV0 And 255)
    
  618.                                 TMS1 += TMS0
    
  619.                                 TMS1 = TSNEPlay_INT_CreateStreamCommand(TSNEPlay_INT_MSGT_Con, TMS1)
    
  620.                                 RV = TSNEPlay_INT_SendData(0, TMS1, TLocalSend)
    
  621.                                 TSNEPlay_INT_SendPlayerTable(V_TSNEID)
    
  622.                             End If
    
  623.                         End If
    
  624.                         
    
  625.                     Case TSNEPlay_INT_MSGT_Ready
    
  626.                         
    
  627.                     Case TSNEPlay_INT_MSGT_StreamError
    
  628.                         XErrExit = 1
    
  629.                         
    
  630.                     Case TSNEPlay_INT_MSGT_Pong
    
  631.                         
    
  632.                     Case TSNEPlay_INT_MSGT_MSG
    
  633.                         If Len(.V_Data) >= 16 Then
    
  634.                             TMV1 = (.V_Data[4] Shl 24) Or (.V_Data[5] Shl 16) Or (.V_Data[6] Shl 8) Or .V_Data[7]
    
  635.                             RV = TSNEPlay_INT_SendData(TMV1, .V_RAW, TLocalSend)
    
  636.                             If TLocalSend = 1 Then
    
  637.                                 If TSNEPlay_INT_Event_Message <> 0 Then
    
  638.                                     TMV0 = (.V_Data[0] Shl 24) Or (.V_Data[1] Shl 16) Or (.V_Data[2] Shl 8) Or .V_Data[3]
    
  639.                                     TMV6 = (.V_Data[8] Shl 24) Or (.V_Data[9] Shl 16) Or (.V_Data[10] Shl 8) Or .V_Data[11]
    
  640.                                     TMV7 = (.V_Data[12] Shl 24) Or (.V_Data[13] Shl 16) Or (.V_Data[14] Shl 8) Or .V_Data[15]
    
  641.                                     If (Len(.V_Data) - 16) >= TMV7 Then
    
  642.                                         TMS0 = Mid(.V_Data, 17, TMV7)
    
  643.                                         TSNEPlay_INT_Event_Message(TMV0, TMV1, TMS0, Cast(TSNEPlay_MessageType_Enum, TMV6))
    
  644.                                     End If
    
  645.                                 End If
    
  646.                             End If
    
  647.                         End If
    
  648.                         
    
  649.                     Case TSNEPlay_INT_MSGT_Move
    
  650.                         If Len(.V_Data) >= 36 Then
    
  651.                             TMV1 = (.V_Data[4] Shl 24) Or (.V_Data[5] Shl 16) Or (.V_Data[6] Shl 8) Or .V_Data[7]
    
  652.                             RV = TSNEPlay_INT_SendData(TMV1, .V_RAW, TLocalSend)
    
  653.                             If TLocalSend = 1 Then
    
  654.                                 If TSNEPlay_INT_Event_Move <> 0 Then
    
  655.                                     TMV0 = (.V_Data[0] Shl 24) Or (.V_Data[1] Shl 16) Or (.V_Data[2] Shl 8) Or .V_Data[3]
    
  656.                                     TMV1 = (.V_Data[4] Shl 24) Or (.V_Data[5] Shl 16) Or (.V_Data[6] Shl 8) Or .V_Data[7]
    
  657.                                     TMVI1 = *Cast(Integer Ptr, @.V_Data[8])
    
  658.                                     TMVI2 = *Cast(Integer Ptr, @.V_Data[16])
    
  659.                                     TMVI3 = *Cast(Integer Ptr, @.V_Data[24])
    
  660.                                     TMV5 = (.V_Data[32] Shl 24) Or (.V_Data[33] Shl 16) Or (.V_Data[34] Shl 8) Or .V_Data[35]
    
  661.                                     TSNEPlay_INT_Event_Move(TMV0, TMV1, TMVI1, TMVI2, TMVI3, TMV5)
    
  662.                                 End If
    
  663.                             End If
    
  664.                         End If
    
  665.                         
    
  666.                     Case TSNEPlay_INT_MSGT_MoveDbl
    
  667.                         If Len(.V_Data) >= 36 Then
    
  668.                             TMV1 = (.V_Data[4] Shl 24) Or (.V_Data[5] Shl 16) Or (.V_Data[6] Shl 8) Or .V_Data[7]
    
  669.                             RV = TSNEPlay_INT_SendData(TMV1, .V_RAW, TLocalSend)
    
  670.                             If TLocalSend = 1 Then
    
  671.                                 If TSNEPlay_INT_Event_Move <> 0 Then
    
  672.                                     TMV0 = (.V_Data[0] Shl 24) Or (.V_Data[1] Shl 16) Or (.V_Data[2] Shl 8) Or .V_Data[3]
    
  673.                                     TMV1 = (.V_Data[4] Shl 24) Or (.V_Data[5] Shl 16) Or (.V_Data[6] Shl 8) Or .V_Data[7]
    
  674.                                     TMV2 = *Cast(Double Ptr, @.V_Data[8])
    
  675.                                     TMV3 = *Cast(Double Ptr, @.V_Data[16])
    
  676.                                     TMV4 = *Cast(Double Ptr, @.V_Data[24])
    
  677.                                     TMV5 = (.V_Data[32] Shl 24) Or (.V_Data[33] Shl 16) Or (.V_Data[34] Shl 8) Or .V_Data[35]
    
  678.                                     TSNEPlay_INT_Event_Move(TMV0, TMV1, TMV2, TMV3, TMV4, TMV5)
    
  679.                                 End If
    
  680.                             End If
    
  681.                         End If
    
  682.                         
    
  683.                     Case TSNEPlay_INT_MSGT_Dat
    
  684.                         If Len(.V_Data) >= 12 Then
    
  685.                             TMV1 = (.V_Data[4] Shl 24) Or (.V_Data[5] Shl 16) Or (.V_Data[6] Shl 8) Or .V_Data[7]
    
  686.                             RV = TSNEPlay_INT_SendData(TMV1, .V_RAW, TLocalSend)
    
  687.                             If TLocalSend = 1 Then
    
  688.                                 If TSNEPlay_INT_Event_Data <> 0 Then
    
  689.                                     TMV0 = (.V_Data[0] Shl 24) Or (.V_Data[1] Shl 16) Or (.V_Data[2] Shl 8) Or .V_Data[3]
    
  690.                                     TMV6 = (.V_Data[8] Shl 24) Or (.V_Data[9] Shl 16) Or (.V_Data[10] Shl 8) Or .V_Data[11]
    
  691.                                     If (Len(.V_Data) - 12) >= TMV6 Then
    
  692.                                         TMS0 = Mid(.V_Data, 13, TMV6)
    
  693.                                         TSNEPlay_INT_Event_Data(TMV0, TMV1, TMS0)
    
  694.                                     End If
    
  695.                                 End If
    
  696.                             End If
    
  697.                         End If
    
  698.                         
    
  699.                 End Select
    
  700.             End If
    
  701.         End With
    
  702.     End If
    
  703.     Deallocate(TCMDQueF)
    
  704.     TCMDQueF = TCMDQueL
    
  705. Loop
    
  706. If XErrExit = 1 Then TSNE_Disconnect(V_TSNEID)
    
  707. End Sub
    
  708. 
    
  709. '--------------------------------------------------------------------------------------------------------------
    
  710. Sub TSNEPlay_INT_NewConnection(Byval V_TSNEID As Uinteger, Byval V_RequestID As Socket, Byval V_IPA As String)
    
  711. 'Print "NEW:"; V_TSNEID; " "; V_IPA
    
  712. Dim RV As Integer
    
  713. Dim TNewTSNEID As Uinteger
    
  714. Mutexlock(TSNEPlay_INT_Mutex)
    
  715. If TSNEPlay_INT_ClientC >= TSNEPlay_INT_Server_MaxPlayer Then
    
  716.     Mutexunlock(TSNEPlay_INT_Mutex)
    
  717.     RV = TSNE_Create_Accept(V_RequestID, TNewTSNEID, , @TSNEPlay_INT_Disconnected, @TSNEPlay_INT_Connected, 0)
    
  718.     RV = TSNE_Data_Send(TNewTSNEID, TSNEPlay_INT_CreateStreamCommand(TSNEPlay_INT_MSGT_Full))
    
  719.     RV = TSNE_Disconnect(TNewTSNEID)
    
  720.     Exit Sub
    
  721. End If
    
  722. RV = TSNE_Create_Accept(V_RequestID, TNewTSNEID, , @TSNEPlay_INT_Disconnected, @TSNEPlay_INT_Connected, @TSNEPlay_INT_NewData)
    
  723. Dim TPtr As TSNEPlay_INT_Client_Type Ptr = TSNEPlay_INT_ClientAdd()
    
  724. With *TPtr
    
  725.     .V_TSNEID       = TNewTSNEID
    
  726.     .V_IPA          = V_IPA
    
  727.     .V_State        = TSNEPlay_State_Connecting
    
  728. End With
    
  729. Mutexunlock(TSNEPlay_INT_Mutex)
    
  730. End Sub
    
  731. 
    
  732. 
    
  733. 
    
  734. '##############################################################################################################
    
  735. Function TSNEPlay_CreateServer(V_MaxPlayer As Ushort, V_Port As Ushort, V_Nickname As String, V_Password As String = "", Byval V_EventPtr_ConnectionState As Any Ptr = 0, Byval V_EventPtr_Player_Connected As Any Ptr, Byval V_EventPtr_Player_Disconnected As Any Ptr, Byval V_EventPtr_Message As Any Ptr, Byval V_EventPtr_Move As Any Ptr, Byval V_EventPtr_MoveDbl As Any Ptr, Byval V_EventPtr_Data As Any Ptr, V_NoSelfPlayer As Ubyte = 1) As TSNEPlay_GURUCode
    
  736. If TSNEPlay_INT_State <> TSNEPlay_State_Unknown Then Return TSNEPlay_NotReadyForNewConnection
    
  737. TSNEPlay_INT_Client_PlayerID = 0
    
  738. TSNEPlay_INT_Server_MaxPlayer = V_MaxPlayer
    
  739. TSNEPlay_INT_ServerPassword = V_Password
    
  740. TSNEPlay_INT_Nickname = V_Nickname
    
  741. TSNEPlay_INT_ClientMode = 0
    
  742. TSNEPlay_INT_Event_ConnectionState      = V_EventPtr_ConnectionState
    
  743. TSNEPlay_INT_Event_Player_Connected     = V_EventPtr_Player_Connected
    
  744. TSNEPlay_INT_Event_Player_Disconnected  = V_EventPtr_Player_Disconnected
    
  745. TSNEPlay_INT_Event_Message              = V_EventPtr_Message
    
  746. TSNEPlay_INT_Event_Move                 = V_EventPtr_Move
    
  747. TSNEPlay_INT_Event_Data                 = V_EventPtr_Data
    
  748. TSNEPlay_INT_State = TSNEPlay_State_Connecting
    
  749. If TSNEPlay_INT_Event_ConnectionState <> 0 Then TSNEPlay_INT_Event_ConnectionState(TSNEPlay_INT_Client_PlayerID, TSNEPlay_INT_State)
    
  750. Dim RV As Integer = TSNE_Create_Server(TSNEPlay_INT_Server_TSNEID, V_Port, 100, @TSNEPlay_INT_NewConnection)
    
  751. If RV <> TSNE_Const_NoError Then TSNEPlay_INT_State = TSNEPlay_State_Unknown: Return RV
    
  752. If V_NoSelfPlayer = 0 Then
    
  753.     Dim TPtr As TSNEPlay_INT_Client_Type Ptr = TSNEPlay_INT_ClientAdd()
    
  754.     With *TPtr
    
  755.         TSNEPlay_INT_Server_PlayerID = .V_PlayerID
    
  756.         .V_Nickname     = V_Nickname
    
  757.         .V_TSNEID       = 0
    
  758.         .V_IPA          = "127.0.0.1"
    
  759.         .V_State        = TSNEPlay_State_Ready
    
  760.     End With
    
  761. End If
    
  762. TSNEPlay_INT_State = TSNEPlay_State_Ready
    
  763. If TSNEPlay_INT_Event_ConnectionState <> 0 Then TSNEPlay_INT_Event_ConnectionState(TSNEPlay_INT_Client_PlayerID, TSNEPlay_INT_State)
    
  764. Return TSNEPlay_NoError
    
  765. End Function
    
  766. 
    
  767. '--------------------------------------------------------------------------------------------------------------
    
  768. Function TSNEPlay_ConnectToServer(V_Host As String, V_Port As Ushort, V_Nickname As String, V_Password As String = "", Byval V_EventPtr_ConnectionState As Any Ptr = 0, Byval V_EventPtr_Player_Connected As Any Ptr, Byval V_EventPtr_Player_Disconnected As Any Ptr, Byval V_EventPtr_Message As Any Ptr, Byval V_EventPtr_Move As Any Ptr, Byval V_EventPtr_MoveDbl As Any Ptr, Byval V_EventPtr_Data As Any Ptr) As TSNEPlay_GURUCode
    
  769. If TSNEPlay_INT_State <> TSNEPlay_State_Unknown Then Return TSNEPlay_NotReadyForNewConnection
    
  770. TSNEPlay_INT_Client_PlayerID = 0
    
  771. TSNEPlay_INT_ServerPassword = V_Password
    
  772. TSNEPlay_INT_Nickname = V_Nickname
    
  773. TSNEPlay_INT_ClientMode = 1
    
  774. TSNEPlay_INT_Event_ConnectionState      = V_EventPtr_ConnectionState
    
  775. TSNEPlay_INT_Event_Player_Connected     = V_EventPtr_Player_Connected
    
  776. TSNEPlay_INT_Event_Player_Disconnected  = V_EventPtr_Player_Disconnected
    
  777. TSNEPlay_INT_Event_Message              = V_EventPtr_Message
    
  778. TSNEPlay_INT_Event_Move                 = V_EventPtr_Move
    
  779. TSNEPlay_INT_Event_Data                 = V_EventPtr_Data
    
  780. TSNEPlay_INT_State = TSNEPlay_State_Connecting
    
  781. If TSNEPlay_INT_Event_ConnectionState <> 0 Then TSNEPlay_INT_Event_ConnectionState(TSNEPlay_INT_Client_PlayerID, TSNEPlay_INT_State)
    
  782. Dim RV As Integer = TSNE_Create_Client(TSNEPlay_INT_Client_TSNEID, V_Host, V_Port, @TSNEPlay_INT_Disconnected, @TSNEPlay_INT_Connected, @TSNEPlay_INT_NewData, 60)
    
  783. If RV <> TSNE_Const_NoError Then TSNEPlay_INT_State = TSNEPlay_State_Unknown: Return RV
    
  784. Return TSNEPlay_NoError
    
  785. End Function
    
  786. 
    
  787. '--------------------------------------------------------------------------------------------------------------
    
  788. Function TSNEPlay_CloseAll() As TSNEPlay_GURUCode
    
  789. If TSNEPlay_INT_Server_TSNEID <> 0 Then
    
  790.     TSNE_Disconnect(TSNEPlay_INT_Server_TSNEID)
    
  791.     TSNE_WaitClose(TSNEPlay_INT_Server_TSNEID)
    
  792.     TSNEPlay_INT_Server_TSNEID = 0
    
  793.     Mutexlock(TSNEPlay_INT_Mutex)
    
  794.     Dim TPtr1 As TSNEPlay_INT_Client_Type Ptr = TSNEPlay_INT_ClientF
    
  795.     Dim TPtr2 As TSNEPlay_INT_Client_Type Ptr
    
  796.     Dim TSID As Uinteger
    
  797.     Do Until TPtr1 = 0
    
  798.         TPtr2 = TPtr1->V_Next
    
  799.         TSID = TPtr1->V_TSNEID
    
  800.         If TSID > 0 Then TSNE_Disconnect(TSID)
    
  801.         TPtr1 = TPtr2
    
  802.     Loop
    
  803.     Mutexunlock(TSNEPlay_INT_Mutex)
    
  804.     Mutexlock(TSNEPlay_INT_Mutex)
    
  805.     TPtr1 = TSNEPlay_INT_ClientF
    
  806.     Do Until TPtr1 = 0
    
  807.         TPtr2 = TPtr1->V_Next
    
  808.         TSID = TPtr1->V_TSNEID
    
  809.         If TSID > 0 Then
    
  810.             Mutexunlock(TSNEPlay_INT_Mutex)
    
  811.             TSNE_WaitClose(TSID)
    
  812.             Mutexlock(TSNEPlay_INT_Mutex)
    
  813.         End If
    
  814.         TPtr1 = TPtr2
    
  815.     Loop
    
  816.     TSNEPlay_INT_ClientF = 0
    
  817.     TSNEPlay_INT_ClientL = 0
    
  818.     Mutexunlock(TSNEPlay_INT_Mutex)
    
  819. End If
    
  820. If TSNEPlay_INT_Client_TSNEID <> 0 Then
    
  821.     TSNE_Disconnect(TSNEPlay_INT_Client_TSNEID)
    
  822.     TSNE_WaitClose(TSNEPlay_INT_Client_TSNEID)
    
  823.     TSNEPlay_INT_Client_TSNEID = 0
    
  824. End If
    
  825. TSNEPlay_INT_State = TSNEPlay_State_Unknown
    
  826. TSNEPlay_INT_Server_MaxPlayer = 0
    
  827. TSNEPlay_INT_ServerPassword = ""
    
  828. TSNEPlay_INT_Nickname = ""
    
  829. TSNEPlay_INT_ClientMode = 0
    
  830. TSNEPlay_INT_Event_ConnectionState      = 0
    
  831. TSNEPlay_INT_Event_Player_Connected     = 0
    
  832. TSNEPlay_INT_Event_Player_Disconnected  = 0
    
  833. TSNEPlay_INT_Event_Message              = 0
    
  834. TSNEPlay_INT_Event_Move                 = 0
    
  835. TSNEPlay_INT_Event_Data                 = 0
    
  836. Return TSNEPlay_NoError
    
  837. End Function
    
  838. 
    
  839. 
    
  840. 
    
  841. '##############################################################################################################
    
  842. Function TSNEPlay_Connection_GetState() As TSNEPlay_State_Enum
    
  843. Return TSNEPlay_INT_State
    
  844. End Function
    
  845. 
    
  846. 
    
  847. 
    
  848. '##############################################################################################################
    
  849. Function TSNEPlay_SendMSG(V_ToPlayerID As Uinteger, V_Message As String, V_MessageType As TSNEPlay_MessageType_Enum = TSNEPlay_MSGType_Regular) As TSNEPlay_GURUCode
    
  850. '<FromPlayerID><ToPlayerID><MSGType><MSG>
    
  851. If Len(V_Message) > 4096 Then Return TSNEPlay_MessageToLong
    
  852. If TSNEPlay_INT_State <> TSNEPlay_State_Ready Then Return TSNEPlay_NotReadyForNewConnection
    
  853. Dim T As String
    
  854. Dim FPID As Uinteger
    
  855. If TSNEPlay_INT_ClientMode = 1 Then
    
  856.     FPID = TSNEPlay_INT_Client_PlayerID
    
  857. Else: FPID = TSNEPlay_INT_Server_PlayerID
    
  858. End If
    
  859. T += Chr((FPID Shr 24) And 255) & Chr((FPID Shr 16) And 255) & Chr((FPID Shr 8) And 255) & Chr(FPID And 255)
    
  860. T += Chr((V_ToPlayerID Shr 24) And 255) & Chr((V_ToPlayerID Shr 16) And 255) & Chr((V_ToPlayerID Shr 8) And 255) & Chr(V_ToPlayerID And 255)
    
  861. T += Chr((V_MessageType Shr 24) And 255) & Chr((V_MessageType Shr 16) And 255) & Chr((V_MessageType Shr 8) And 255) & Chr(V_MessageType And 255)
    
  862. Dim MX As Uinteger = Len(V_Message)
    
  863. T += Chr((MX Shr 24) And 255) & Chr((MX Shr 16) And 255) & Chr((MX Shr 8) And 255) & Chr(MX And 255)
    
  864. T += V_Message
    
  865. T = TSNEPlay_INT_CreateStreamCommand(TSNEPlay_INT_MSGT_MSG, T)
    
  866. Dim TLocalSend As Ubyte
    
  867. Dim RV As TSNEPlay_GURUCode = TSNEPlay_INT_SendData(V_ToPlayerID, T, TLocalSend)
    
  868. If TLocalSend = 1 Then If TSNEPlay_INT_Event_Message <> 0 Then TSNEPlay_INT_Event_Message(FPID, V_ToPlayerID, V_Message, V_MessageType)
    
  869. Return RV
    
  870. End Function
    
  871. 
    
  872. '--------------------------------------------------------------------------------------------------------------
    
  873. Function TSNEPlay_SendMove(V_ToPlayerID As Uinteger, V_NewPositonX As Double = 0, V_NewPositonY As Double = 0, V_NewPositonZ As Double = 0, Byval V_SubData As Uinteger = 0) As TSNEPlay_GURUCode
    
  874. '<FromPlayerID><ToPlayerID><PosX><PosY><PosZ><SubData>
    
  875. If TSNEPlay_INT_State <> TSNEPlay_State_Ready Then Return TSNEPlay_NotReadyForNewConnection
    
  876. Dim T As String
    
  877. Dim FPID As Uinteger
    
  878. If TSNEPlay_INT_ClientMode = 1 Then
    
  879.     FPID = TSNEPlay_INT_Client_PlayerID
    
  880. Else: FPID = TSNEPlay_INT_Server_PlayerID
    
  881. End If
    
  882. T += Chr((FPID Shr 24) And 255) & Chr((FPID Shr 16) And 255) & Chr((FPID Shr 8) And 255) & Chr(FPID And 255)
    
  883. T += Chr((V_ToPlayerID Shr 24) And 255) & Chr((V_ToPlayerID Shr 16) And 255) & Chr((V_ToPlayerID Shr 8) And 255) & Chr(V_ToPlayerID And 255)
    
  884. T += Space(8): *Cast(Integer Ptr, @T[Len(T) - 8]) = V_NewPositonX
    
  885. T += Space(8): *Cast(Integer Ptr, @T[Len(T) - 8]) = V_NewPositonY
    
  886. T += Space(8): *Cast(Integer Ptr, @T[Len(T) - 8]) = V_NewPositonZ
    
  887. T += Chr((V_SubData Shr 24) And 255) & Chr((V_SubData Shr 16) And 255) & Chr((V_SubData Shr 8) And 255) & Chr(V_SubData And 255)
    
  888. T = TSNEPlay_INT_CreateStreamCommand(TSNEPlay_INT_MSGT_Move, T)
    
  889. Dim TLocalSend As Ubyte
    
  890. Dim RV As TSNEPlay_GURUCode = TSNEPlay_INT_SendData(V_ToPlayerID, T, TLocalSend)
    
  891. If TLocalSend = 1 Then If TSNEPlay_INT_Event_Move <> 0 Then TSNEPlay_INT_Event_Move(FPID, V_ToPlayerID, V_NewPositonX, V_NewPositonY, V_NewPositonZ, V_SubData)
    
  892. Return RV
    
  893. End Function
    
  894. 
    
  895. '--------------------------------------------------------------------------------------------------------------
    
  896. Function TSNEPlay_SendMoveDbl(V_ToPlayerID As Uinteger, V_NewPositonX As Double = 0, V_NewPositonY As Double = 0, V_NewPositonZ As Double = 0, Byval V_SubData As Uinteger = 0) As TSNEPlay_GURUCode
    
  897. '<FromPlayerID><ToPlayerID><PosX><PosY><PosZ><SubData>
    
  898. If TSNEPlay_INT_State <> TSNEPlay_State_Ready Then Return TSNEPlay_NotReadyForNewConnection
    
  899. Dim T As String
    
  900. Dim FPID As Uinteger
    
  901. If TSNEPlay_INT_ClientMode = 1 Then
    
  902.     FPID = TSNEPlay_INT_Client_PlayerID
    
  903. Else: FPID = TSNEPlay_INT_Server_PlayerID
    
  904. End If
    
  905. T += Chr((FPID Shr 24) And 255) & Chr((FPID Shr 16) And 255) & Chr((FPID Shr 8) And 255) & Chr(FPID And 255)
    
  906. T += Chr((V_ToPlayerID Shr 24) And 255) & Chr((V_ToPlayerID Shr 16) And 255) & Chr((V_ToPlayerID Shr 8) And 255) & Chr(V_ToPlayerID And 255)
    
  907. T += Space(8): *Cast(Double Ptr, @T[Len(T) - 8]) = V_NewPositonX
    
  908. T += Space(8): *Cast(Double Ptr, @T[Len(T) - 8]) = V_NewPositonY
    
  909. T += Space(8): *Cast(Double Ptr, @T[Len(T) - 8]) = V_NewPositonZ
    
  910. T += Chr((V_SubData Shr 24) And 255) & Chr((V_SubData Shr 16) And 255) & Chr((V_SubData Shr 8) And 255) & Chr(V_SubData And 255)
    
  911. T = TSNEPlay_INT_CreateStreamCommand(TSNEPlay_INT_MSGT_MoveDbl, T)
    
  912. Dim TLocalSend As Ubyte
    
  913. Dim RV As TSNEPlay_GURUCode = TSNEPlay_INT_SendData(V_ToPlayerID, T, TLocalSend)
    
  914. If TLocalSend = 1 Then If TSNEPlay_INT_Event_MoveDbl <> 0 Then TSNEPlay_INT_Event_MoveDbl(FPID, V_ToPlayerID, V_NewPositonX, V_NewPositonY, V_NewPositonZ, V_SubData)
    
  915. Return RV
    
  916. End Function
    
  917. 
    
  918. '--------------------------------------------------------------------------------------------------------------
    
  919. Function TSNEPlay_SendData(V_ToPlayerID As Uinteger, Byref V_Data As String) As TSNEPlay_GURUCode
    
  920. '<FromPlayerID><ToPlayerID><Data>
    
  921. If Len(V_Data) > (TSNE_INT_BufferSize - 24) Then Return TSNEPlay_MessageToLong
    
  922. If TSNEPlay_INT_State <> TSNEPlay_State_Ready Then Return TSNEPlay_NotReadyForNewConnection
    
  923. Dim T As String
    
  924. Dim FPID As Uinteger
    
  925. If TSNEPlay_INT_ClientMode = 1 Then
    
  926.     FPID = TSNEPlay_INT_Client_PlayerID
    
  927. Else: FPID = TSNEPlay_INT_Server_PlayerID
    
  928. End If
    
  929. T += Chr((FPID Shr 24) And 255) & Chr((FPID Shr 16) And 255) & Chr((FPID Shr 8) And 255) & Chr(FPID And 255)
    
  930. T += Chr((V_ToPlayerID Shr 24) And 255) & Chr((V_ToPlayerID Shr 16) And 255) & Chr((V_ToPlayerID Shr 8) And 255) & Chr(V_ToPlayerID And 255)
    
  931. Dim MX As Uinteger = Len(V_Data)
    
  932. T += Chr((MX Shr 24) And 255) & Chr((MX Shr 16) And 255) & Chr((MX Shr 8) And 255) & Chr(MX And 255)
    
  933. T += V_Data
    
  934. T = TSNEPlay_INT_CreateStreamCommand(TSNEPlay_INT_MSGT_Dat, T)
    
  935. Dim TLocalSend As Ubyte
    
  936. Dim RV As TSNEPlay_GURUCode = TSNEPlay_INT_SendData(V_ToPlayerID, T, TLocalSend)
    
  937. If TLocalSend = 1 Then If TSNEPlay_INT_Event_Data <> 0 Then TSNEPlay_INT_Event_Data(FPID, V_ToPlayerID, V_Data)
    
  938. Return RV
    
  939. End Function
    
  940. 
    
  941. 
    
  942. 
    
  943. '##############################################################################################################
    
  944. '...<
    
  945. #ENDIF
    
  946. 
    
  947.