Home

Add

Edit

Without Linenumbers

Code in Textfield

Download

  1. '##############################################################################################################
    
  2. '##############################################################################################################
    
  3. ' TSNE_V3 - TCP Socket Networking [Eventing] Version: 3.5_20111215 (0.18.5 -> 0.20.0 [08-10-2008])
    
  4. '##############################################################################################################
    
  5. '##############################################################################################################
    
  6. ' (c) 2009-.... By.: /_\ DeltaLab's Germany - Experimental Computing
    
  7. ' Autor: Martin Wiemann
    
  8. ' IRC: IRC://MLN.ath.cx/#mln
    
  9. '##############################################################################################################
    
  10. ' Free for NON-comercial use! For comercial usage send me a mail -> Admin[at]MLN.ath.cx or IRC and u become a free usage
    
  11. '##############################################################################################################
    
  12. 
    
  13. 
    
  14. 
    
  15. 
    
  16. #IFNDEF _TSNE_
    
  17.     #DEFINE _TSNE_
    
  18. '>...
    
  19. 
    
  20. '##############################################################################################################
    
  21. 'BUGFIX for 0.18.5 fbc version inspired by: TJF -> http://www.freebasic.net/forum/viewtopic.php?f=3&t=19889&p=174666&hilit=tsne#p174535
    
  22. #IF __FB_VERSION__ = "0.18.5"
    
  23.     Type timeval
    
  24.         tv_sec As __time_t
    
  25.         tv_usec As __suseconds_t
    
  26.     End Type
    
  27. #ENDIF
    
  28. 
    
  29. 
    
  30. 
    
  31. '##############################################################################################################
    
  32. #DEFINE EMFILE 24
    
  33. #DEFINE ENFILE 23
    
  34. #DEFINE ENOMEM 12
    
  35. #IF Defined(TSNE_ERRNO)
    
  36.     Declare Function errno Cdecl Alias "__errno_location" () As Integer Ptr Ptr
    
  37. #ENDIF
    
  38. 
    
  39. #IF Defined(__FB_LINUX__)
    
  40.     #INCLUDE Once "crt/stdlib.bi"
    
  41.     #INCLUDE Once "crt/unistd.bi"
    
  42.     #INCLUDE Once "crt/netdb.bi"
    
  43.     #INCLUDE Once "crt/sys/types.bi"
    
  44.     #INCLUDE Once "crt/sys/socket.bi"
    
  45.     #INCLUDE Once "crt/sys/select.bi"
    
  46.     #INCLUDE Once "crt/netinet/in.bi"
    
  47.     #INCLUDE Once "crt/arpa/inet.bi"
    
  48.     #DEFINE IOCPARM_MASK &h7f
    
  49.     #DEFINE IOC_IN &h80000000
    
  50.     #DEFINE _IOW(x,y,t) (IOC_IN Or ((t And IOCPARM_MASK) Shl 16) Or ((x) Shl 8) Or (y))
    
  51.     #DEFINE FIONBIO _IOW(Asc("f"), 126, Sizeof(Uinteger))
    
  52.     #DEFINE h_addr h_addr_list[0]
    
  53.     #DEFINE CloseSocket_(_a_) close_(_a_)
    
  54.     #DEFINE INVALID_SOCKET (Cast(Socket, -1))
    
  55.     #DEFINE TSNE_MSG_NOSIGNAL &h4000
    
  56.     #DEFINE EINPROGRESS 36
    
  57. #ELSEIF Defined(__FB_WIN32__)
    
  58.     #DEFINE WIN_INCLUDEALL
    
  59.     #INCLUDE Once "windows.bi"
    
  60.     #INCLUDE Once "win\winsock.bi"
    
  61.     #DEFINE close_(_a_) closesocket(_a_)
    
  62.     #DEFINE memcpy(x__, y__, z__) movememory(x__, y__, z__)
    
  63.     #DEFINE TSNE_MSG_NOSIGNAL &h0
    
  64.     #DEFINE EINPROGRESS WSAEINPROGRESS
    
  65.     Const IP_SUCCESS                = 0
    
  66.     Const IP_DEST_NET_UNREACHABLE   = 1102
    
  67.     Const IP_DEST_HOST_UNREACHABLE  = 1103
    
  68.     Const IP_DEST_PROT_UNREACHABLE  = 1104
    
  69.     Const IP_DEST_PORT_UNREACHABLE  = 1105
    
  70.     Const IP_REQ_TIMED_OUT          = 11010
    
  71.     Const IP_TTL_EXPIRED_TRANSIT    = 11013
    
  72.     Type IP_Option_Information
    
  73.         Ttl             As Ubyte
    
  74.         Tos             As Ubyte
    
  75.         Flags           As Ubyte
    
  76.         OptionsSize     As Ubyte
    
  77.         OptionsData     As Ubyte Ptr
    
  78.     End Type
    
  79.     Type ICMP_Echo_Reply
    
  80.         Adress          As in_addr
    
  81.         Status          As Uinteger
    
  82.         RoundTripTime   As Uinteger
    
  83.         DataSize        As Ushort
    
  84.         Reserved        As Ushort
    
  85.         Data            As Any Ptr
    
  86.         Options         As IP_Option_Information
    
  87.     End Type
    
  88.     #IF Defined(TSNE_PINGICMP)
    
  89.         Declare Function IcmpCreateFile Lib "icmp.dll" () As Integer
    
  90.         Declare Function IcmpCloseHandle Lib "icmp.dll" (Byval IcmpHandle As Integer) As Integer
    
  91.         Declare Function IcmpSendEcho Lib "icmp.dll" (Byval IcmpHandle As Integer, Byval DestinationAddress As in_addr, Byval RequestData As String, Byval RequestSize As Short, Byval RequestOptions As Integer, ReplyBuffer As ICMP_Echo_Reply Ptr, Byval ReplySize As Integer, Byval TimeOut As Integer) As Integer
    
  92.     #ENDIF
    
  93.     Private Sub TSNE_INT_StartWinsock() Constructor 102
    
  94.         Dim xwsa As WSADATA
    
  95.         WSAStartup(MAKEWORD(2, 0), @xwsa)
    
  96.     End Sub
    
  97.     Private Sub TSNE_INT_EndWinsock() Destructor 102
    
  98.         WSAcleanup()
    
  99.     End Sub
    
  100. #ELSE
    
  101.     #ERROR "Unsupported platform"
    
  102. #ENDIF
    
  103. #INCLUDE Once "crt/sys/time.bi"
    
  104. #INCLUDE Once "crt/fcntl.bi"
    
  105. #INCLUDE Once "vbcompat.bi"
    
  106. 
    
  107. 
    
  108. 
    
  109. #IFNDEF icmp
    
  110.     Type icmphdr
    
  111.         Type        As Ubyte
    
  112.         code        As Ubyte
    
  113.         cksum       As Ushort
    
  114.         icd_id      As Ushort
    
  115.         icd_seq     As Ushort
    
  116.         ih_gateway  As Uinteger
    
  117.         unused      As Uinteger
    
  118.         mtu         As Uinteger
    
  119.     End Type
    
  120. 
    
  121.     #DEFINE ICMP_ECHOREPLY      0
    
  122.     #DEFINE ICMP_DEST_UNREACH   3
    
  123.     #DEFINE ICMP_SOURCE_QUENCH  4
    
  124.     #DEFINE ICMP_REDIRECT       5
    
  125.     #DEFINE ICMP_ECHO           8
    
  126.     #DEFINE ICMP_TIME_EXCEEDED  11
    
  127.     #DEFINE ICMP_PARAMETERPROB  12
    
  128.     #DEFINE ICMP_TIMESTAMP      13
    
  129.     #DEFINE ICMP_TIMESTAMPREPLY 14
    
  130.     #DEFINE ICMP_INFO_REQUEST   15
    
  131.     #DEFINE ICMP_INFO_REPLY     16
    
  132.     #DEFINE ICMP_ADDRESS        17
    
  133.     #DEFINE ICMP_ADDRESSREPLY   18
    
  134.     #DEFINE NR_ICMP_TYPES       18
    
  135.     
    
  136.     #DEFINE ICMP_NET_UNREACH    0
    
  137.     #DEFINE ICMP_HOST_UNREACH   1
    
  138.     #DEFINE ICMP_PROT_UNREACH   2
    
  139.     #DEFINE ICMP_PORT_UNREACH   3
    
  140.     #DEFINE ICMP_FRAG_NEEDED    4
    
  141.     #DEFINE ICMP_SR_FAILED      5
    
  142.     #DEFINE ICMP_NET_UNKNOWN    6
    
  143.     #DEFINE ICMP_HOST_UNKNOWN   7
    
  144.     #DEFINE ICMP_HOST_ISOLATED  8
    
  145.     #DEFINE ICMP_NET_ANO        9
    
  146.     #DEFINE ICMP_HOST_ANO       10
    
  147.     #DEFINE ICMP_NET_UNR_TOS    11
    
  148.     #DEFINE ICMP_HOST_UNR_TOS   12
    
  149.     #DEFINE ICMP_PKT_FILTERED   13
    
  150.     #DEFINE ICMP_PREC_VIOLATION 14
    
  151.     #DEFINE ICMP_PREC_CUTOFF    15
    
  152.     #DEFINE NR_ICMP_UNREACH     15
    
  153.     
    
  154.     #DEFINE ICMP_REDIR_NET      0
    
  155.     #DEFINE ICMP_REDIR_HOST     1
    
  156.     #DEFINE ICMP_REDIR_NETTOS   2
    
  157.     #DEFINE ICMP_REDIR_HOSTTOS  3
    
  158.     
    
  159.     #DEFINE ICMP_EXC_TTL        0
    
  160.     #DEFINE ICMP_EXC_FRAGTIME   1
    
  161.     
    
  162.     Type ICMP
    
  163.         icmp_type       As Ubyte
    
  164.         icmp_code       As Ubyte
    
  165.         icmp_cksum      As Ushort
    
  166.         icd_id          As Ushort
    
  167.         icd_seq         As Ushort
    
  168.         ih_pptr         As Ubyte
    
  169.         ih_gwaddr       As in_addr
    
  170.         ih_void         As Uinteger
    
  171.         ipm_void        As Ushort
    
  172.         ipm_nextmtu     As Ushort
    
  173.         irt_num_addrs   As Ubyte
    
  174.         irt_wpa         As Ubyte
    
  175.         irt_lifetime    As Ushort
    
  176.         
    
  177.         #DEFINE icmp_pptr       ih_pptr
    
  178.         #DEFINE icmp_gwadr      ih_gwadr
    
  179.         #DEFINE icmp_id         icd_id
    
  180.         #DEFINE icmp_seq        icd_seq
    
  181.         #DEFINE icmp_void       ih_void
    
  182.         #DEFINE icmp_pmvoid     ipm_void
    
  183.         #DEFINE icmp_nextmtu    ipm_nextmtu
    
  184.         #DEFINE icmp_num_addrs  irt_num_addrs
    
  185.         #DEFINE icmp_wpa        irt_wpa
    
  186.         #DEFINE icmp_lifetiem   irt_lifetime
    
  187.     End Type
    
  188. #ENDIF
    
  189. 
    
  190. 
    
  191. 
    
  192. '##############################################################################################################
    
  193. '|#DEFINE _TSNE_DODEBUG_
    
  194. 
    
  195. 
    
  196. 
    
  197. '##############################################################################################################
    
  198. Dim Shared TSNE_INT_Thread_Master_Ptr           As Any Ptr
    
  199. Dim Shared TSNE_INT_Thread_Master_Close         As Ubyte
    
  200. Dim Shared TSNE_INT_Mutex_Master                As Any Ptr
    
  201. 
    
  202. 
    
  203. 
    
  204. 
    
  205. 
    
  206. '##############################################################################################################
    
  207. Private Const TSNE_INT_BufferSize               As Uinteger = 7936
    
  208. Dim Shared    TSNE_INT_StackSize                As Uinteger = 512000
    
  209. #IF Defined(TSNE_ConstStackSizeOverride)
    
  210.     TSNE_INT_StackSize = TSNE_ConstStackSizeOverride
    
  211. #ENDIF
    
  212. '--------------------------------------------------------------------------------------------------------------
    
  213. Private Const TSNE_Const_UnknowError            As Integer = 0
    
  214. Private Const TSNE_Const_NoError                As Integer = -1
    
  215. Private Const TSNE_Const_UnknowEventID          As Integer = -2
    
  216. Private Const TSNE_Const_NoSocketFound          As Integer = -3
    
  217. Private Const TSNE_Const_CantCreateSocket       As Integer = -4
    
  218. Private Const TSNE_Const_CantBindSocket         As Integer = -5
    
  219. Private Const TSNE_Const_CantSetListening       As Integer = -6
    
  220. Private Const TSNE_Const_SocketAlreadyInit      As Integer = -7
    
  221. Private Const TSNE_Const_MaxSimConReqOutOfRange As Integer = -8
    
  222. Private Const TSNE_Const_PortOutOfRange         As Integer = -9
    
  223. Private Const TSNE_Const_CantResolveIPfromHost  As Integer = -10
    
  224. Private Const TSNE_Const_CantConnectToRemote    As Integer = -11
    
  225. Private Const TSNE_Const_TSNEIDnotFound         As Integer = -12
    
  226. Private Const TSNE_Const_MissingEventPTR        As Integer = -13
    
  227. Private Const TSNE_Const_IPAalreadyInList       As Integer = -14
    
  228. Private Const TSNE_Const_IPAnotInList           As Integer = -15
    
  229. Private Const TSNE_Const_ReturnErrorInCallback  As Integer = -16
    
  230. Private Const TSNE_Const_IPAnotFound            As Integer = -17
    
  231. Private Const TSNE_Const_ErrorSendingData       As Integer = -18
    
  232. Private Const TSNE_Const_UnknowGURUcode         As Integer = -19
    
  233. Private Const TSNE_Const_TSNENoServer           As Integer = -20
    
  234. Private Const TSNE_Const_NoIPV6                 As Integer = -21
    
  235. Private Const TSNE_Const_CantCreateSocketLimit  As Integer = -22
    
  236. Private Const TSNE_Const_UnstableState          As Integer = -23
    
  237. Private Const TSNE_Const_InternalError          As Integer = -99
    
  238. '--------------------------------------------------------------------------------------------------------------
    
  239. Private Enum TSNE_BW_Mode_Enum
    
  240.     TSNE_BW_Mode_None   = 0
    
  241.     TSNE_BW_Mode_Black  = 1
    
  242.     TSNE_BW_Mode_White  = 2
    
  243. End Enum
    
  244. 
    
  245. 
    
  246. 
    
  247. 
    
  248. '##############################################################################################################
    
  249. Private Enum TSNE_Event
    
  250.     TSNE_E_Disconnect = 0
    
  251.     TSNE_E_Connect = 1
    
  252.     TSNE_E_NewConnection = 2
    
  253.     TSNE_E_NewData = 3
    
  254. End Enum
    
  255. '--------------------------------------------------------------------------------------------------------------
    
  256. Private Type TSNE_Event_Type
    
  257.     #IF Defined(TSNE_SUBCALLBACK)
    
  258.         TSNE_Disconnected           As Sub  (Byval V_TSNEID As Uinteger, Byval V_CallBackPtr As Any Ptr)
    
  259.         TSNE_Connected              As Sub  (Byval V_TSNEID As Uinteger, Byval V_CallBackPtr As Any Ptr)
    
  260.         TSNE_NewData                As Sub  (Byval V_TSNEID As Uinteger, Byref V_Data As String, Byval V_CallBackPtr As Any Ptr)
    
  261.     #ELSE
    
  262.         TSNE_Disconnected           As Sub  (Byval V_TSNEID As Uinteger)
    
  263.         TSNE_Connected              As Sub  (Byval V_TSNEID As Uinteger)
    
  264.         TSNE_NewData                As Sub  (Byval V_TSNEID As Uinteger, Byref V_Data As String)
    
  265.     #ENDIF
    
  266.     TSNE_NewConnection              As Sub  (Byval V_TSNEID As Uinteger, Byval V_RequestID As Socket, Byval V_IPA As String)
    
  267.     TSNE_NewConnectionCanceled      As Sub  (Byval V_TSNEID As Uinteger, Byval V_IPA As String)
    
  268.     TSNE_NewDataUDP                 As Sub  (Byval V_TSNEID As Uinteger, Byval V_IPA As String, Byref V_Data As String)
    
  269.     V_AnyPtr                        As Any Ptr
    
  270. End Type
    
  271. 
    
  272. 
    
  273. 
    
  274. 
    
  275. 
    
  276. '##############################################################################################################
    
  277. Private Type TSNE_INT_DNSIPA_Type
    
  278.     V_Next                          As TSNE_INT_DNSIPA_Type Ptr
    
  279.     V_Prev                          As TSNE_INT_DNSIPA_Type Ptr
    
  280.     V_HostIPA                       As String
    
  281.     V_InAddr                        As in_addr
    
  282.     V_TimeOut                       As Double
    
  283. End Type
    
  284. '--------------------------------------------------------------------------------------------------------------
    
  285. Dim Shared TSNE_INT_DNSIPAD         As TSNE_INT_DNSIPA_Type Ptr
    
  286. Dim Shared TSNE_INT_DNSIPAL         As TSNE_INT_DNSIPA_Type Ptr
    
  287. Dim Shared TSNE_INT_DNSIPA_Mutex    As Any Ptr
    
  288. 
    
  289. 
    
  290. 
    
  291. 
    
  292. '##############################################################################################################
    
  293. Private Type TSNE_BWL_Type
    
  294.     V_Next                          As TSNE_BWL_Type Ptr
    
  295.     V_Prev                          As TSNE_BWL_Type Ptr
    
  296.     V_IPA                           As String
    
  297.     V_LockTill                      As Double
    
  298. End Type
    
  299. '--------------------------------------------------------------------------------------------------------------
    
  300. Private Enum TSNE_Protocol
    
  301.     TSNE_P_TCP                       = 0
    
  302.     TSNE_P_UDP                       = 1
    
  303. End Enum
    
  304. 
    
  305. 
    
  306. 
    
  307. 
    
  308. 
    
  309. '##############################################################################################################
    
  310. Private Type TSNE_Socket
    
  311.     V_Next                          As TSNE_Socket Ptr
    
  312.     V_Prev                          As TSNE_Socket Ptr
    
  313.     
    
  314.     V_TSNEID                        As Uinteger
    
  315.     
    
  316.     V_Event                         As TSNE_Event_Type
    
  317.     V_Socket                        As Socket
    
  318.     
    
  319.     V_Prot                          As TSNE_Protocol
    
  320.     V_IsServer                      As Ubyte
    
  321.     V_IPA                           As String
    
  322.     V_Port                          As Ushort
    
  323.     V_USP                           As SOCKADDR_IN
    
  324.     
    
  325.     T_DataIn                        As ULongInt
    
  326.     T_DataOut                       As ULongInt
    
  327.     
    
  328.     T_ThreadOn                      As Integer
    
  329.     T_Thread                        As Any Ptr
    
  330.     
    
  331.     V_BWL_UseType                   As Ubyte
    
  332.     V_BWL_IPAD                      As TSNE_BWL_Type Ptr
    
  333.     V_BWL_IPAL                      As TSNE_BWL_Type Ptr
    
  334. End Type
    
  335. '--------------------------------------------------------------------------------------------------------------
    
  336. Dim Shared TSNE_INT_D               As TSNE_Socket Ptr
    
  337. Dim Shared TSNE_INT_L               As TSNE_Socket Ptr
    
  338. Dim Shared TSNE_INT_C               As Uinteger
    
  339. Dim Shared TSNE_INT_CC              As Uinteger
    
  340. Dim Shared TSNE_INT_Mutex           As Any Ptr
    
  341. 
    
  342. 
    
  343. 
    
  344. 
    
  345. 
    
  346. '##############################################################################################################
    
  347. Declare Sub         TSNE_INT_Thread_Master      (Byval I_Nothing As Any Ptr)
    
  348. Declare Sub         TSNE_INT_Thread_Event       (Byval V_TSNEID As Any Ptr)
    
  349. 
    
  350. 
    
  351. 
    
  352. 
    
  353. 
    
  354. '##############################################################################################################
    
  355. Declare Function    TSNE_GetGURUCode                (Byref V_GURUID As Integer) As String
    
  356. 
    
  357. Declare Function    TSNE_Stats                      (Byref V_TSNEID As Uinteger, Byref R_RX As ULongInt, Byref R_TX As ULongInt) As Integer
    
  358. Declare Function    TSNE_Disconnect                 (Byref V_TSNEID As Uinteger) As Integer
    
  359. Declare Function    TSNE_Create_Server              (Byref R_TSNEID As Uinteger, Byref V_Port As Ushort, Byref V_MaxSimConReq As Ushort = 10, Byval V_Event_NewConPTR As Any Ptr, Byval V_Event_NewConCancelPTR As Any Ptr = 0, Byval V_StackSizeOverride As Uinteger = TSNE_INT_StackSize) As Integer
    
  360. Declare Function    TSNE_Create_ServerWithBindIPA   (Byref R_TSNEID As Uinteger, Byref V_Port As Ushort, Byref V_IPA As String, Byref V_MaxSimConReq As Ushort = 10, Byval V_Event_NewConPTR As Any Ptr, Byval V_Event_NewConCancelPTR As Any Ptr = 0, Byval V_StackSizeOverride As Uinteger  = TSNE_INT_StackSize) As Integer
    
  361. Declare Function    TSNE_Create_Client              (Byref R_TSNEID As Uinteger, Byval V_IPA As String, Byval V_Port As Ushort, Byval V_Event_DisconPTR As Any Ptr = 0, Byval V_Event_ConPTR As Any Ptr = 0, Byval V_Event_NewDataPTR As Any Ptr, Byval V_TimeoutSecs As Uinteger = 60, Byval V_StackSizeOverride As Uinteger = TSNE_INT_StackSize, Byval V_WaitThreadRunning As Ubyte = 1, Byval V_CallbackBackPtr As Any Ptr = 0) As Integer
    
  362. Declare Function    TSNE_Create_Accept              (Byval V_RequestID As Socket, Byref R_TSNEID As Uinteger, Byref R_IPA As String = "", Byval V_Event_DisconPTR As Any Ptr = 0, Byval V_Event_ConPTR As Any Ptr = 0, Byval V_Event_NewDataPTR As Any Ptr, Byref R_RemoteShownServerIPA As String = "", Byval V_StackSizeOverride As Uinteger = TSNE_INT_StackSize, Byval V_WaitThreadRunning As Ubyte = 1, Byval V_CallbackBackPtr As Any Ptr = 0) As Integer
    
  363. Declare Function    TSNE_Create_UDP_RX              (Byref R_TSNEID As Uinteger, Byval V_Port As Ushort, Byval V_Event_NewDataUDPPTR As Any Ptr, Byval V_StackSizeOverride As Uinteger = TSNE_INT_StackSize, Byval V_WaitThreadRunning As Ubyte = 1) As Integer
    
  364. Declare Function    TSNE_Create_UDP_TX              (Byref R_TSNEID As Uinteger, Byval V_DoBroadcast As Ubyte = 0) As Integer
    
  365. Declare Function    TSNE_Data_Send                  (Byref V_TSNEID As Uinteger, Byref V_Data As String, Byref R_BytesSend As Uinteger = 0, Byval V_IPA As String = "", Byval V_Port As Ushort = 0) As Integer
    
  366. 
    
  367. Declare Function    TSNE_Ping                       (Byval V_IPA As String, Byref R_Runtime As Double, Byval V_TimeoutSecs As Ubyte = 10, Byval V_ForceRAWPing As Ubyte = 0, Byval V_FileIOMutex As Any Ptr = 0) As Integer
    
  368. 
    
  369. Declare Sub         TSNE_WaitClose                  (Byref V_TSNEID As Uinteger)
    
  370. Declare Function    TSNE_WaitConnected              (Byref V_TSNEID As Uinteger, V_TimeOut As Uinteger = 60) As Integer
    
  371. Declare Function    TSNE_IsClosed                   (Byref V_TSNEID As Uinteger) As Ubyte
    
  372. 
    
  373. Declare Function    TSNE_BW_SetEnable               (Byval V_Server_TSNEID As Uinteger, V_Type As TSNE_BW_Mode_Enum) As Integer
    
  374. Declare Function    TSNE_BW_GetEnable               (Byval V_Server_TSNEID As Uinteger, R_Type As TSNE_BW_Mode_Enum) As Integer
    
  375. Declare Function    TSNE_BW_Clear                   (Byval V_Server_TSNEID As Uinteger) As Integer
    
  376. Declare Function    TSNE_BW_Add                     (Byval V_Server_TSNEID As Uinteger, V_IPA As String, V_BlockTimeSeconds As Uinteger = 3600) As Integer
    
  377. Declare Function    TSNE_BW_Del                     (Byval V_Server_TSNEID As Uinteger, V_IPA As String) As Integer
    
  378. Declare Function    TSNE_BW_List                    (Byval V_Server_TSNEID As Uinteger, Byref R_IPA_List As TSNE_BWL_Type Ptr) As Integer
    
  379. 
    
  380. 
    
  381. 
    
  382. 
    
  383. '##############################################################################################################
    
  384. Private Function TSNE_INT_BW_GetPtr(Byref V_TSNE As TSNE_Socket Ptr, Byref V_IPA As String) As TSNE_BWL_Type Ptr
    
  385. If V_TSNE = 0 Then Return 0
    
  386. Dim TPtr As TSNE_BWL_Type Ptr = V_TSNE->V_BWL_IPAD
    
  387. Do Until TPtr = 0
    
  388.     If TPtr->V_IPA = V_IPA Then
    
  389. '       If TPtr->V_LockTill
    
  390.         Return TPtr
    
  391.     End If
    
  392.     TPtr = TPtr->V_Next
    
  393. Loop
    
  394. Return 0
    
  395. End Function
    
  396. 
    
  397. '---------------------------------------------------------------------------------------------------------------
    
  398. Private Sub TSNE_INT_BW_Clear(Byref V_TSNE As TSNE_Socket Ptr)
    
  399. If V_TSNE = 0 Then Exit Sub
    
  400. Dim TPtr As TSNE_BWL_Type Ptr = V_TSNE->V_BWL_IPAD
    
  401. Dim TNPtr As TSNE_BWL_Type Ptr
    
  402. Do Until TPtr = 0
    
  403.     TNPtr = TPtr->V_Next
    
  404.     Deallocate(TPtr)
    
  405.     TPtr = TNPtr
    
  406. Loop
    
  407. End Sub
    
  408. 
    
  409. '---------------------------------------------------------------------------------------------------------------
    
  410. Private Function TSNE_INT_BW_Del(Byref V_TSNE As TSNE_Socket Ptr, Byref V_IPA As String) As Ubyte
    
  411. If V_TSNE = 0 Then Return 0
    
  412. Dim TPtr As TSNE_BWL_Type Ptr = TSNE_INT_BW_GetPtr(V_TSNE, V_IPA)
    
  413. If TPtr = 0 Then Return 0
    
  414. If V_TSNE->V_BWL_IPAD = TPtr Then V_TSNE->V_BWL_IPAD = TPtr->V_Next
    
  415. If V_TSNE->V_BWL_IPAL = TPtr Then V_TSNE->V_BWL_IPAL = TPtr->V_Prev
    
  416. If TPtr->V_Prev <> 0 Then TPtr->V_Prev->V_Next = TPtr->V_Next
    
  417. If TPtr->V_Next <> 0 Then TPtr->V_Next->V_Prev = TPtr->V_Prev
    
  418. Deallocate(TPtr)
    
  419. Return 1
    
  420. End Function
    
  421. 
    
  422. '---------------------------------------------------------------------------------------------------------------
    
  423. Private Function TSNE_INT_BW_Add(Byref V_TSNE As TSNE_Socket Ptr, Byref V_IPA As String, Byval V_BlockTimeSeconds As Uinteger = 3600) As Ubyte
    
  424. If V_TSNE = 0 Then Return 0
    
  425. If TSNE_INT_BW_GetPtr(V_TSNE, V_IPA) <> 0 Then Return 0
    
  426. If V_TSNE->V_BWL_IPAL <> 0 Then
    
  427.     V_TSNE->V_BWL_IPAL->V_Next = Callocate(Sizeof(TSNE_BWL_Type))
    
  428.     V_TSNE->V_BWL_IPAL->V_Next->V_PreV = V_TSNE->V_BWL_IPAL
    
  429.     V_TSNE->V_BWL_IPAL = V_TSNE->V_BWL_IPAL->V_Next
    
  430. Else
    
  431.     V_TSNE->V_BWL_IPAL = Callocate(Sizeof(TSNE_BWL_Type))
    
  432.     V_TSNE->V_BWL_IPAD = V_TSNE->V_BWL_IPAL
    
  433. End If
    
  434. V_TSNE->V_BWL_IPAL->V_IPA = V_IPA
    
  435. V_TSNE->V_BWL_IPAL->V_LockTill = Now() + V_BlockTimeSeconds
    
  436. Return 1
    
  437. End Function
    
  438. 
    
  439. 
    
  440. 
    
  441. 
    
  442. 
    
  443. '##############################################################################################################
    
  444. Private Function TSNE_INT_GetPtr(Byref V_TSNEID As Uinteger) As TSNE_Socket Ptr
    
  445. Dim TPtr As TSNE_Socket Ptr = TSNE_INT_D
    
  446. Do Until TPtr = 0
    
  447.     If TPtr->V_TSNEID = V_TSNEID Then Return TPtr
    
  448.     TPtr = TPtr->V_Next
    
  449. Loop
    
  450. Return 0
    
  451. End Function
    
  452. 
    
  453. '---------------------------------------------------------------------------------------------------------------
    
  454. Private Function TSNE_INT_Del(Byref V_TSNE As TSNE_Socket Ptr) As Ubyte
    
  455. Mutexlock(TSNE_INT_Mutex)
    
  456. If V_TSNE = 0 Then Mutexunlock(TSNE_INT_Mutex): Return 0
    
  457. If TSNE_INT_D = V_TSNE Then TSNE_INT_D = V_TSNE->V_Next
    
  458. If TSNE_INT_L = V_TSNE Then TSNE_INT_L = V_TSNE->V_Prev
    
  459. If V_TSNE->V_Prev <> 0 Then V_TSNE->V_Prev->V_Next = V_TSNE->V_Next
    
  460. If V_TSNE->V_Next <> 0 Then V_TSNE->V_Next->V_Prev = V_TSNE->V_Prev
    
  461. TSNE_INT_BW_Clear(V_TSNE)
    
  462. Deallocate(V_TSNE)
    
  463. V_TSNE = 0
    
  464. Mutexunlock(TSNE_INT_Mutex)
    
  465. Return 1
    
  466. End Function
    
  467. 
    
  468. '---------------------------------------------------------------------------------------------------------------
    
  469. Private Function TSNE_INT_Add() As TSNE_Socket Ptr
    
  470. Mutexlock(TSNE_INT_Mutex)
    
  471. TSNE_INT_CC += 1
    
  472. If TSNE_INT_CC = 0 Then TSNE_INT_CC += 1
    
  473. Do Until TSNE_INT_GetPtr(TSNE_INT_CC) = 0
    
  474.     TSNE_INT_CC += 1
    
  475.     If TSNE_INT_CC = 0 Then TSNE_INT_CC += 1
    
  476. Loop
    
  477. If TSNE_INT_L <> 0 Then
    
  478.     TSNE_INT_L->V_Next = Callocate(Sizeof(TSNE_Socket))
    
  479.     TSNE_INT_L->V_Next->V_PreV = TSNE_INT_L
    
  480.     TSNE_INT_L = TSNE_INT_L->V_Next
    
  481. Else
    
  482.     TSNE_INT_L = Callocate(Sizeof(TSNE_Socket))
    
  483.     TSNE_INT_D = TSNE_INT_L
    
  484. End If
    
  485. Dim TPtr As TSNE_Socket Ptr = TSNE_INT_L
    
  486. TPtr->V_TSNEID = TSNE_INT_CC
    
  487. Mutexunlock(TSNE_INT_Mutex)
    
  488. Return TPtr
    
  489. End Function
    
  490. 
    
  491. 
    
  492. 
    
  493. 
    
  494. 
    
  495. '##############################################################################################################
    
  496. Private Sub TSNE_INT_Init() Constructor 101
    
  497. TSNE_INT_Mutex = Mutexcreate
    
  498. TSNE_INT_Mutex_Master = Mutexcreate
    
  499. TSNE_INT_DNSIPA_Mutex = Mutexcreate
    
  500. Mutexlock(TSNE_INT_Mutex_Master)
    
  501. TSNE_INT_Thread_Master_Ptr = Threadcreate(Cast(Any Ptr, @TSNE_INT_Thread_Master), , TSNE_INT_StackSize)
    
  502. Mutexlock(TSNE_INT_Mutex_Master)
    
  503. Mutexunlock(TSNE_INT_Mutex_Master)
    
  504. End Sub
    
  505. 
    
  506. '--------------------------------------------------------------------------------------------------------------
    
  507. Private Sub TSNE_INT_Term() Destructor 101
    
  508. Mutexlock(TSNE_INT_Mutex)
    
  509. Dim TPtr As TSNE_Socket Ptr = TSNE_INT_D
    
  510. Dim TNPtr As TSNE_Socket Ptr
    
  511. Dim XTID As Uinteger
    
  512. Do Until TPtr = 0
    
  513.     TNPtr = TPtr->V_Next
    
  514.     If TPtr->T_Thread <> 0 Then
    
  515.         XTID = TPtr->V_TSNEID
    
  516.         Mutexunlock(TSNE_INT_Mutex)
    
  517.         TSNE_Disconnect(XTID)
    
  518.         Mutexlock(TSNE_INT_Mutex)
    
  519.     End If
    
  520.     TPtr = TNPtr
    
  521. Loop
    
  522. Mutexunlock(TSNE_INT_Mutex)
    
  523. Mutexlock(TSNE_INT_Mutex_Master)
    
  524. TSNE_INT_Thread_Master_Close = 1
    
  525. Mutexunlock(TSNE_INT_Mutex_Master)
    
  526. Threadwait(TSNE_INT_Thread_Master_Ptr)
    
  527. Mutexlock(TSNE_INT_DNSIPA_Mutex)
    
  528. Dim TDNSPtr As TSNE_INT_DNSIPA_Type Ptr = TSNE_INT_DNSIPAD
    
  529. Dim NDNSPtr As TSNE_INT_DNSIPA_Type Ptr
    
  530. Do Until TDNSPtr = 0
    
  531.     NDNSPtr = TDNSPtr->V_Next
    
  532.     Deallocate(TDNSPtr)
    
  533.     TDNSPtr = NDNSPtr
    
  534. Loop
    
  535. Mutexunlock(TSNE_INT_DNSIPA_Mutex)
    
  536. Mutexdestroy(TSNE_INT_DNSIPA_Mutex):    TSNE_INT_DNSIPA_Mutex = 0
    
  537. Mutexdestroy(TSNE_INT_Mutex_Master):    TSNE_INT_Mutex_Master = 0
    
  538. Mutexdestroy(TSNE_INT_Mutex):           TSNE_INT_Mutex = 0
    
  539. End Sub
    
  540. 
    
  541. 
    
  542. 
    
  543. 
    
  544. 
    
  545. '##############################################################################################################
    
  546. Private Function TSNE_INT_GetHostEnd(Byref V_HostIPA As String, Byref R_InAddr As in_addr) As Integer
    
  547. Mutexlock(TSNE_INT_DNSIPA_Mutex)
    
  548. Dim TDNSPtr As TSNE_INT_DNSIPA_Type Ptr = TSNE_INT_DNSIPAD
    
  549. Dim NDNSPtr As TSNE_INT_DNSIPA_Type Ptr
    
  550. Do Until TDNSPtr = 0
    
  551.     If TDNSPtr->V_TimeOut <= Timer() Then
    
  552.         If TDNSPtr->V_Prev <> 0 Then TDNSPtr->V_Prev->V_Next = TDNSPtr->V_Next
    
  553.         If TDNSPtr->V_Next <> 0 Then TDNSPtr->V_Next->V_Prev = TDNSPtr->V_Prev
    
  554.         If TSNE_INT_DNSIPAD = TDNSPtr Then TSNE_INT_DNSIPAD = TDNSPtr->V_Next
    
  555.         If TSNE_INT_DNSIPAL = TDNSPtr Then TSNE_INT_DNSIPAL = TDNSPtr->V_Prev
    
  556.         NDNSPtr = TDNSPtr->V_Next
    
  557.         Deallocate(TDNSPtr)
    
  558.         TDNSPtr = NDNSPtr
    
  559.     Else: TDNSPtr = TDNSPtr->V_Next
    
  560.     End If
    
  561. Loop
    
  562. TDNSPtr = TSNE_INT_DNSIPAD
    
  563. Do Until TDNSPtr = 0
    
  564.     If TDNSPtr->V_HostIPA = V_HostIPA Then
    
  565.         R_InAddr = TDNSPtr->V_InAddr
    
  566.         Mutexunlock(TSNE_INT_DNSIPA_Mutex)
    
  567.         Return TSNE_Const_NoError
    
  568.     End If
    
  569.     TDNSPtr = TDNSPtr->V_Next
    
  570. Loop
    
  571. Dim TADDRIN As in_addr
    
  572. TADDRIN.s_addr = inet_addr(Strptr(V_HostIPA))
    
  573. If (TADDRIN.s_addr = -1) Then
    
  574.     Dim XHost As hostent Ptr = gethostbyname(Strptr(V_HostIPA))
    
  575.     If XHost = 0 Then
    
  576.         Mutexunlock(TSNE_INT_DNSIPA_Mutex)
    
  577.         Return TSNE_Const_CantResolveIPfromHost
    
  578.     End If
    
  579.     TADDRIN = *Cast(in_addr Ptr, XHost->h_addr_list[0])
    
  580.     If TADDRIN.s_addr = INADDR_NONE Then Mutexunlock(TSNE_INT_DNSIPA_Mutex): Return TSNE_Const_CantResolveIPfromHost
    
  581. End If
    
  582. If TSNE_INT_DNSIPAL <> 0 Then
    
  583.     TSNE_INT_DNSIPAL->V_Next = Callocate(Sizeof(TSNE_INT_DNSIPA_Type))
    
  584.     TSNE_INT_DNSIPAL->V_Next->V_Prev = TSNE_INT_DNSIPAL
    
  585.     TSNE_INT_DNSIPAL = TSNE_INT_DNSIPAL->V_Next
    
  586. Else
    
  587.     TSNE_INT_DNSIPAL = Callocate(Sizeof(TSNE_INT_DNSIPA_Type))
    
  588.     TSNE_INT_DNSIPAD = TSNE_INT_DNSIPAL
    
  589. End If
    
  590. TSNE_INT_DNSIPAL->V_HostIPA = V_HostIPA
    
  591. TSNE_INT_DNSIPAL->V_InAddr = TADDRIN
    
  592. TSNE_INT_DNSIPAL->V_TimeOut = Timer() + 60
    
  593. R_InAddr = TADDRIN
    
  594. Mutexunlock(TSNE_INT_DNSIPA_Mutex)
    
  595. Return TSNE_Const_NoError
    
  596. End Function
    
  597. 
    
  598. 
    
  599. 
    
  600. 
    
  601. 
    
  602. '##############################################################################################################
    
  603. Private Sub TSNE_INT_Thread_Master(Byval I_Nothing As Any Ptr)
    
  604. Mutexunlock(TSNE_INT_Mutex_Master)
    
  605. Dim TPtr As TSNE_Socket Ptr
    
  606. Dim TNPtr As TSNE_Socket Ptr
    
  607. Dim TThPtr As Any Ptr
    
  608. Dim TID As Uinteger
    
  609. Dim TEvent As TSNE_Event_Type
    
  610. Do
    
  611.     #IF Defined(_TSNE_DODEBUG_)
    
  612.         Print "=[TSNE]=[TMA]= Lock..."
    
  613.     #ENDIF
    
  614.     Mutexlock(TSNE_INT_Mutex)
    
  615.     #IF Defined(_TSNE_DODEBUG_)
    
  616.         Print "=[TSNE]=[TMA]= Lock-K"
    
  617.     #ENDIF
    
  618.     TPtr = TSNE_INT_D
    
  619.     Do Until TPtr = 0
    
  620.         TNPtr = TPtr->V_Next
    
  621.         If TPtr->T_ThreadOn = 3 Then
    
  622.             TID = TPtr->V_TSNEID
    
  623.             #IF Defined(_TSNE_DODEBUG_)
    
  624.                 Print "=[" & Str(TID) & "]=[TSNE]=[TMA]= ThreadON 3"
    
  625.             #ENDIF
    
  626.             TPtr->T_ThreadOn = 4
    
  627.             #IF Defined(_TSNE_DODEBUG_)
    
  628.                 Print "=[" & Str(TID) & "]=[TSNE]=[TMA]= ThreadON 4"
    
  629.             #ENDIF
    
  630.             TThPtr = TPtr->T_Thread
    
  631.             TEvent = TPtr->V_Event
    
  632.             Mutexunlock(TSNE_INT_Mutex)
    
  633.             #IF Defined(_TSNE_DODEBUG_)
    
  634.                 Print "=[" & Str(TID) & "]=[TSNE]=[TMA]= Unlock"
    
  635.                 Print "=[" & Str(TID) & "]=[TSNE]=[TMA]= Wait..."
    
  636.             #ENDIF
    
  637.             Threadwait(TThPtr)
    
  638.             #IF Defined(_TSNE_DODEBUG_)
    
  639.                 Print "=[" & Str(TID) & "]=[TSNE]=[TMA]= Wait-K"
    
  640.                 Print "=[" & Str(TID) & "]=[TSNE]=[TMA]= Call-Dis..."
    
  641.             #ENDIF
    
  642.             #IF Defined(TSNE_SUBCALLBACK)
    
  643.                 If TEvent.TSNE_Disconnected <> 0 Then TEvent.TSNE_Disconnected(TID, TEvent.V_AnyPtr)
    
  644.             #ELSE
    
  645.                 If TEvent.TSNE_Disconnected <> 0 Then TEvent.TSNE_Disconnected(TID)
    
  646.             #ENDIF
    
  647.             #IF Defined(_TSNE_DODEBUG_)
    
  648.                 Print "=[" & Str(TID) & "]=[TSNE]=[TMA]= Call-Dis-K"
    
  649.             #ENDIF
    
  650.             TSNE_INT_Del(TPtr)
    
  651.             #IF Defined(_TSNE_DODEBUG_)
    
  652.                 Print "=[" & Str(TID) & "]=[TSNE]=[TMA]= Lock..."
    
  653.             #ENDIF
    
  654.             Mutexlock(TSNE_INT_Mutex)
    
  655.             #IF Defined(_TSNE_DODEBUG_)
    
  656.                 Print "=[" & Str(TID) & "]=[TSNE]=[TMA]= Lock-K"
    
  657.             #ENDIF
    
  658.         End If
    
  659.         TPtr = TNPtr
    
  660.     Loop
    
  661.     #IF Defined(_TSNE_DODEBUG_)
    
  662.         Print "=[TSNE]=[TMA]= M-Lock..."
    
  663.     #ENDIF
    
  664.     Mutexlock(TSNE_INT_Mutex_Master)
    
  665.     #IF Defined(_TSNE_DODEBUG_)
    
  666.         Print "=[TSNE]=[TMA]= M-Lock-K"
    
  667.     #ENDIF
    
  668.     If TSNE_INT_Thread_Master_Close = 1 Then If TSNE_INT_D = 0 Then Mutexunlock(TSNE_INT_Mutex): Mutexunlock(TSNE_INT_Mutex_Master): Exit Do
    
  669.     Mutexunlock(TSNE_INT_Mutex_Master)
    
  670.     #IF Defined(_TSNE_DODEBUG_)
    
  671.         Print "=[TSNE]=[TMA]= M-Unlock"
    
  672.     #ENDIF
    
  673.     Mutexunlock(TSNE_INT_Mutex)
    
  674.     #IF Defined(_TSNE_DODEBUG_)
    
  675.         Print "=[TSNE]=[TMA]= Unlock"
    
  676.     #ENDIF
    
  677.     Sleep 1000, 1
    
  678. Loop
    
  679. #IF Defined(_TSNE_DODEBUG_)
    
  680.     Print "=[TSNE]=[TMA]= END SUB"
    
  681. #ENDIF
    
  682. End Sub
    
  683. 
    
  684. 
    
  685. 
    
  686. 
    
  687. 
    
  688. '##############################################################################################################
    
  689. Private Function TSNE_Stats(Byref V_TSNEID As Uinteger, Byref R_RX As ULongInt, Byref R_TX As ULongInt) As Integer
    
  690. Mutexlock(TSNE_INT_Mutex)
    
  691. Dim TPtr As TSNE_Socket Ptr = TSNE_INT_GetPtr(V_TSNEID)
    
  692. If TPtr = 0 Then Mutexunlock(TSNE_INT_Mutex): Return TSNE_Const_TSNEIDnotFound
    
  693. R_RX = TPtr->T_DataIn
    
  694. R_TX = TPtr->T_DataOut
    
  695. Mutexunlock(TSNE_INT_Mutex)
    
  696. Return TSNE_Const_NoError
    
  697. End Function
    
  698. 
    
  699. 
    
  700. 
    
  701. 
    
  702. 
    
  703. '##############################################################################################################
    
  704. Private Function TSNE_Ping(Byval V_IPA As String, Byref R_Runtime As Double, Byval V_TimeoutSecs As Ubyte = 10, Byval V_ForceRAWPing As Ubyte = 0, Byval V_FileIOMutex As Any Ptr = 0) As Integer
    
  705. If V_IPA = "" Then Return TSNE_Const_IPAnotFound
    
  706. If Instr(1, V_IPA, ":") > 0 Then Return TSNE_Const_NoIPV6
    
  707. Dim TADDRIN As in_addr
    
  708. Dim RV As Integer = TSNE_INT_GetHostEnd(V_IPA, TADDRIN)
    
  709. If RV <> TSNE_Const_NoError Then Return RV
    
  710. Dim XFN As Integer
    
  711. #IF Defined(__FB_LINUX__)
    
  712.     If V_ForceRAWPing = 0 Then
    
  713.         If Dir("/bin/ping", -1) <> "" Then
    
  714.             If V_FileIOMutex <> 0 Then Mutexlock(V_FileIOMutex)
    
  715.             XFN = Freefile
    
  716.             If Open Pipe ("/bin/ping -t " & Str(V_TimeoutSecs) & " -c 1 -qU " & V_IPA For Input As XFN) = 0 Then
    
  717.                 If V_FileIOMutex <> 0 Then Mutexunlock(V_FileIOMutex)
    
  718.                 Dim T As String
    
  719.                 Dim TL As String
    
  720.                 Do Until Eof(XFN)
    
  721.                     Line Input #XFN, TL
    
  722.                     If Trim(TL) <> "" Then T = TL
    
  723.                 Loop
    
  724.                 Close #XFN
    
  725.                 XFN = Instr(1, T, "=")
    
  726.                 If XFN = 0 Then Return TSNE_Const_InternalError
    
  727.                 T = Trim(Mid(T, XFN + 1))
    
  728.                 XFN = Instr(1, T, "/")
    
  729.                 If XFN = 0 Then Return TSNE_Const_InternalError
    
  730.                 R_Runtime = Val(Trim(Left(T, XFN - 1))) / 1000
    
  731.                 Return TSNE_Const_NoError
    
  732.             End If
    
  733.             If V_FileIOMutex <> 0 Then Mutexunlock(V_FileIOMutex)
    
  734.         End If
    
  735.     End If
    
  736. #ELSEIF Defined(__FB_WIN32__)
    
  737.     If V_ForceRAWPing = 0 Then
    
  738.         #IF Defined(TSNE_PINGICMP)
    
  739.             Dim TBuff As ICMP_Echo_Reply Ptr = Callocate(Sizeof(ICMP_Echo_Reply) + 4)
    
  740.             XFN = IcmpCreateFile()
    
  741.             If IcmpSendEcho(XFN, TADDRIN, "PING", 4, 0, TBuff, Sizeof(TBuff), (V_TimeoutSecs * 1000)) >= 1 Then
    
  742.                 IcmpCloseHandle(XFN)
    
  743.                 With *TBuff
    
  744.                     Select Case .Status
    
  745.                         Case IP_SUCCESS
    
  746.                             R_Runtime = .RoundTripTime / 1000
    
  747.                             Deallocate(TBuff)
    
  748.                             Return TSNE_Const_NoError
    
  749.                         Case IP_DEST_NET_UNREACHABLE, IP_DEST_HOST_UNREACHABLE, IP_DEST_PROT_UNREACHABLE, IP_DEST_PORT_UNREACHABLE, IP_REQ_TIMED_OUT, IP_TTL_EXPIRED_TRANSIT
    
  750.                             Deallocate(TBuff)
    
  751.                             Return TSNE_Const_CantConnectToRemote
    
  752.                         Case Else: Deallocate(TBuff): Return TSNE_Const_InternalError
    
  753.                     End Select
    
  754.                 End With
    
  755.             Else
    
  756.                 Deallocate(TBuff)
    
  757.         #ENDIF
    
  758.                 If V_FileIOMutex <> 0 Then Mutexlock(V_FileIOMutex)
    
  759.                 XFN = Freefile
    
  760.                 If Open Pipe ("ping -w " & Str(V_TimeoutSecs) & " -n 1 " & V_IPA For Input As XFN) = 0 Then
    
  761.                     If V_FileIOMutex <> 0 Then Mutexunlock(V_FileIOMutex)
    
  762.                     Dim T As String
    
  763.                     Dim TL As String
    
  764.                     Do Until Eof(XFN)
    
  765.                         Line Input #XFN, TL
    
  766.                         If Trim(TL) <> "" Then T = TL
    
  767.                     Loop
    
  768.                     Close #XFN
    
  769.                     XFN = Instr(1, T, "=")
    
  770.                     If XFN = 0 Then Return TSNE_Const_InternalError
    
  771.                     T = Trim(Mid(T, XFN + 1))
    
  772.                     XFN = Instr(1, T, "ms")
    
  773.                     If XFN = 0 Then Return TSNE_Const_InternalError
    
  774.                     R_Runtime = Val(Trim(Left(T, XFN - 1))) / 1000
    
  775.                     Return TSNE_Const_NoError
    
  776.                 End If
    
  777.                 If V_FileIOMutex <> 0 Then Mutexunlock(V_FileIOMutex)
    
  778.         #IF Defined(TSNE_PINGICMP)
    
  779.             End If
    
  780.         #ENDIF
    
  781.     End If
    
  782. #ENDIF  
    
  783. Dim TADDR As SOCKADDR_IN
    
  784. With TADDR
    
  785.     .sin_family = AF_INET
    
  786.     .sin_addr = TADDRIN
    
  787. End With
    
  788. Dim TSock As Socket = opensocket(PF_INET, SOCK_RAW, IPPROTO_ICMP)
    
  789. If TSock = INVALID_SOCKET Then
    
  790.     #IF Defined(TSNE_ERRNO)
    
  791.         Select Case errno
    
  792.             Case EMFILE, ENFILE, ENOMEM: Return TSNE_Const_CantCreateSocketLimit
    
  793.             Case Else: Return TSNE_Const_CantCreateSocket
    
  794.         End Select
    
  795.     #ELSE
    
  796.         Return TSNE_Const_CantCreateSocket
    
  797.     #ENDIF
    
  798. End If
    
  799. Dim XMode As Uinteger
    
  800. #IF Defined(__FB_LINUX__)
    
  801.     Dim XFlag As Integer = fcntl(TSock, F_GETFL, 0)
    
  802.     If XFlag = -1 Then close_(TSock): Return TSNE_Const_ReturnErrorInCallback
    
  803. '|  If fcntl(TSock, F_SETFL, XFlag or O_NONBLOCK) = -1 Then close_(TSock): Return TSNE_Const_ReturnErrorInCallback
    
  804.     If fcntl(TSock, F_SETFL, XFlag) = -1 Then close_(TSock): Return TSNE_Const_ReturnErrorInCallback
    
  805. #ELSEIF Defined(__FB_WIN32__)
    
  806.     XMode = 1
    
  807. '   Dim XFlag as Integer = ioctlsocket(TSock, FIONBIO, @XMode)
    
  808.     Dim XFlag As Integer = ioctlsocket(TSock, FIONBIO, Cast(Any Ptr, 1))
    
  809. #ENDIF  
    
  810. Dim TICMP As ICMP
    
  811. With TICMP
    
  812.     .icmp_type  = ICMP_ECHO
    
  813.     .icmp_code  = 0
    
  814.     .icmp_seq   = 1
    
  815.     .icmp_id    = 0
    
  816. End With
    
  817. Dim TUBP As Ubyte Ptr = Cast(Ubyte Ptr, @TICMP)
    
  818. Dim TSum As Uinteger
    
  819. For X As Uinteger = 0 To Sizeof(ICMP) -1
    
  820.     TSum += *(TUBP + X)
    
  821. Next
    
  822. TSum = (TSum Shr 16) + (TSum And &HFFFF)
    
  823. TSum += (TSum Shr 16)
    
  824. TSum = &HFFFF - TSum
    
  825. TICMP.icmp_cksum = TSum
    
  826. Dim TBuffer As Zstring * TSNE_INT_BufferSize
    
  827. Dim TLenB As Integer
    
  828. Dim TFDSet As fd_Set
    
  829. Dim TTLen As Uinteger = Sizeof(TADDR)
    
  830. Dim TTV As TimeVal
    
  831. With TTV
    
  832.     .tv_sec = Cuint(V_TimeoutSecs)
    
  833.     .tv_usec = 0
    
  834. End With
    
  835. fd_set_(TSock, @TFDSet)
    
  836. Dim TRTT As Double = Timer()
    
  837. RV = sendto(TSock, Cast(Ubyte Ptr, @TICMP), Sizeof(ICMP), 0, Cast(SOCKADDR Ptr, @TADDR), Sizeof(SOCKADDR_IN))
    
  838. If RV <> Sizeof(ICMP) Then close_(TSock): Return TSNE_Const_ErrorSendingData
    
  839. Do
    
  840.     RV = select_(TSock + 1, @TFDSet, 0, 0, @TTV)
    
  841.     If RV <> 1 Then close_(TSock): Return TSNE_Const_CantConnectToRemote
    
  842.     If TSock = INVALID_SOCKET Then close_(TSock): Return TSNE_Const_InternalError
    
  843.     TLenB = recvfrom(TSock, Strptr(TBuffer), TSNE_INT_BufferSize, 0, Cast(SOCKADDR Ptr, @TADDR), @TTLen)
    
  844.     If TLenB <= 0 Then close_(TSock): Return TSNE_Const_InternalError
    
  845.     If TLenB >= 2 Then
    
  846.         If (((TBuffer[0] And &B11110000) Shr 4) = &H4) And (TBuffer[1] = &H00) Then
    
  847.             Dim TIHL As Uinteger = (TBuffer[0] And &B00001111) * 4
    
  848.             If TIHL >= 16 Then
    
  849.                 If TBuffer[TIHL + 0] = ICMP_ECHOREPLY Then
    
  850.                     Dim TSN As Ushort = (TBuffer[TIHL + 6] Shl 8) Or TBuffer[TIHL + 7]
    
  851.                     If TSN = 256 Then
    
  852.                         close_(TSock)
    
  853.                         R_Runtime = Timer() - TRTT
    
  854.                         Return TSNE_Const_NoError
    
  855.                     End If
    
  856.                 End If
    
  857.             End If
    
  858.         End If
    
  859.     End If
    
  860. Loop
    
  861. Return TSNE_Const_CantConnectToRemote
    
  862. End Function
    
  863. 
    
  864. 
    
  865. 
    
  866. 
    
  867. 
    
  868. '##############################################################################################################
    
  869. Private Function TSNE_Disconnect(Byref V_TSNEID As Uinteger) As Integer
    
  870. '|if V_TSNEID = 1 then
    
  871. '|  Dim X as uinteger ptr
    
  872. '|  Print *X
    
  873. '|End If
    
  874. #IF Defined(_TSNE_DODEBUG_)
    
  875.     Print "=[" & Str(V_TSNEID) & "]=[TSNE]=[DIS]= Lock..."
    
  876. #ENDIF
    
  877. Mutexlock(TSNE_INT_Mutex)
    
  878. #IF Defined(_TSNE_DODEBUG_)
    
  879.     Print "=[" & Str(V_TSNEID) & "]=[TSNE]=[DIS]= Lock-K"
    
  880. #ENDIF
    
  881. Dim TPtr As TSNE_Socket Ptr = TSNE_INT_GetPtr(V_TSNEID)
    
  882. If TPtr = 0 Then Mutexunlock(TSNE_INT_Mutex): Return TSNE_Const_TSNEIDnotFound
    
  883. If TPtr->V_Socket = INVALID_SOCKET Then Mutexunlock(TSNE_INT_Mutex): Return TSNE_Const_NoSocketFound
    
  884. If TPtr->T_ThreadOn <> 2 Then Mutexunlock(TSNE_INT_Mutex): Return TSNE_Const_UnstableState
    
  885. Dim TSock As Socket = TPtr->V_Socket
    
  886. TPtr->V_Socket = INVALID_SOCKET
    
  887. TPtr->T_ThreadOn = 3
    
  888. Mutexunlock(TSNE_INT_Mutex)
    
  889. #IF Defined(_TSNE_DODEBUG_)
    
  890.     Print "=[" & Str(V_TSNEID) & "]=[TSNE]=[DIS]= Unlock"
    
  891. #ENDIF
    
  892. close_(TSock)
    
  893. Return TSNE_Const_NoError
    
  894. End Function
    
  895. 
    
  896. '--------------------------------------------------------------------------------------------------------------
    
  897. 'Private Sub TSNE_DisconnectAll()
    
  898. 
    
  899. 'End Sub
    
  900. 
    
  901. 
    
  902. '--------------------------------------------------------------------------------------------------------------
    
  903. Private Function TSNE_Create_Server(Byref R_TSNEID As Uinteger, Byref V_Port As Ushort, Byref V_MaxSimConReq As Ushort = 10, Byval V_Event_NewConPTR As Any Ptr, Byval V_Event_NewConCancelPTR As Any Ptr = 0, Byval V_StackSizeOverride As Uinteger = TSNE_INT_StackSize) As Integer
    
  904. R_TSNEID = 0
    
  905. If (V_MaxSimConReq <= 0) Or (V_MaxSimConReq > 4096) Then Return TSNE_Const_MaxSimConReqOutOfRange
    
  906. If (V_Port < 0) Or (V_Port > 65535) Then Return TSNE_Const_PortOutOfRange
    
  907. If V_Event_NewConPTR = 0 Then Return TSNE_Const_MissingEventPTR
    
  908. Dim TSock As Socket = opensocket(AF_INET, SOCK_STREAM, IPPROTO_IP)
    
  909. If TSock = INVALID_SOCKET Then
    
  910.     #IF Defined(TSNE_ERRNO)
    
  911.         Select Case errno
    
  912.             Case EMFILE, ENFILE, ENOMEM: Return TSNE_Const_CantCreateSocketLimit
    
  913.             Case Else: Return TSNE_Const_CantCreateSocket
    
  914.         End Select
    
  915.     #ELSE
    
  916.         Return TSNE_Const_CantCreateSocket
    
  917.     #ENDIF
    
  918. End If
    
  919. Dim TTADDR As SOCKADDR_IN
    
  920. With TTADDR
    
  921.     .sin_family = AF_INET
    
  922.     .sin_port = htons(V_Port)
    
  923.     .sin_addr.s_addr = INADDR_ANY
    
  924. End With
    
  925. #IF Defined(TSNE_DEF_REUSER)
    
  926.     Dim XV As Integer = 1
    
  927.     #IF Defined(__FB_LINUX__)
    
  928.         If setsockopt(TSock, SOL_SOCKET, SO_REUSEADDR, @XV, Sizeof(Integer)) = -1 Then close_(TSock): Return TSNE_Const_CantBindSocket
    
  929.     #ELSEIF Defined(__FB_WIN32__)
    
  930.         If setsockopt(TSock, SOL_SOCKET, SO_REUSEADDR, Cast(Zstring Ptr, @XV), Sizeof(Integer)) = -1 Then close_(TSock): Return TSNE_Const_CantBindSocket
    
  931.     #ENDIF
    
  932. #ENDIF
    
  933. Dim BV As Integer = bind(TSock, Cptr(SOCKADDR Ptr, @TTADDR), Sizeof(SOCKADDR_IN))
    
  934. If BV = SOCKET_ERROR Then close_(TSock): Return TSNE_Const_CantBindSocket
    
  935. BV = listen(TSock, V_MaxSimConReq)
    
  936. If BV = SOCKET_ERROR Then Return TSNE_Const_CantSetListening
    
  937. Dim TSD As TSNE_Socket Ptr = TSNE_INT_Add()
    
  938. Mutexlock(TSNE_INT_Mutex)
    
  939. TSD->V_Socket = TSock
    
  940. TSD->V_IPA = ""
    
  941. TSD->V_Port = V_Port
    
  942. TSD->V_Prot = TSNE_P_TCP
    
  943. TSD->V_IsServer = 1
    
  944. TSD->T_ThreadOn = 1
    
  945. TSD->V_Event.TSNE_NewConnection = V_Event_NewConPTR
    
  946. TSD->V_Event.TSNE_NewConnectionCanceled = V_Event_NewConCancelPTR
    
  947. R_TSNEID = TSD->V_TSNEID
    
  948. TSD->T_Thread = Threadcreate(Cast(Any Ptr, @TSNE_INT_Thread_Event), Cast(Any Ptr, R_TSNEID), V_StackSizeOverride)
    
  949. Mutexunlock(TSNE_INT_Mutex)
    
  950. Return TSNE_Const_NoError
    
  951. End Function
    
  952. 
    
  953. 
    
  954. '--------------------------------------------------------------------------------------------------------------
    
  955. Private Function TSNE_Create_ServerWithBindIPA(Byref R_TSNEID As Uinteger, Byref V_Port As Ushort, Byref V_IPA As String, Byref V_MaxSimConReq As Ushort = 10, Byval V_Event_NewConPTR As Any Ptr, Byval V_Event_NewConCancelPTR As Any Ptr = 0, Byval V_StackSizeOverride As Uinteger = TSNE_INT_StackSize) As Integer
    
  956. R_TSNEID = 0
    
  957. If (V_MaxSimConReq <= 0) Or (V_MaxSimConReq > 4096) Then Return TSNE_Const_MaxSimConReqOutOfRange
    
  958. If (V_Port < 0) Or (V_Port > 65535) Then Return TSNE_Const_PortOutOfRange
    
  959. If V_IPA = "" Then Return TSNE_Const_IPAnotFound
    
  960. If Instr(1, V_IPA, ":") > 0 Then Return TSNE_Const_NoIPV6
    
  961. If V_Event_NewConPTR = 0 Then Return TSNE_Const_MissingEventPTR
    
  962. Dim TADDRIN As in_addr
    
  963. Dim RV As Integer = TSNE_INT_GetHostEnd(V_IPA, TADDRIN)
    
  964. If RV <> TSNE_Const_NoError Then Return RV
    
  965. Dim TSock As Socket = opensocket(AF_INET, SOCK_STREAM, IPPROTO_IP)
    
  966. If TSock = INVALID_SOCKET Then
    
  967.     #IF Defined(TSNE_ERRNO)
    
  968.         Select Case errno
    
  969.             Case EMFILE, ENFILE, ENOMEM: Return TSNE_Const_CantCreateSocketLimit
    
  970.             Case Else: Return TSNE_Const_CantCreateSocket
    
  971.         End Select
    
  972.     #ELSE
    
  973.         Return TSNE_Const_CantCreateSocket
    
  974.     #ENDIF
    
  975. End If
    
  976. Dim TTADDR As SOCKADDR_IN
    
  977. With TTADDR
    
  978.     .sin_family = AF_INET
    
  979.     .sin_port = htons(V_Port)
    
  980.     .sin_addr = TADDRIN
    
  981. End With
    
  982. #IF Defined(TSNE_DEF_REUSER)
    
  983.     Dim XV As Integer = 1
    
  984.     #IF Defined(__FB_LINUX__)
    
  985.         If setsockopt(TSock, SOL_SOCKET, SO_REUSEADDR, @XV, Sizeof(Integer)) = -1 Then close_(TSock): Return TSNE_Const_CantBindSocket
    
  986.     #ELSEIF Defined(__FB_WIN32__)
    
  987.         If setsockopt(TSock, SOL_SOCKET, SO_REUSEADDR, Cast(Zstring Ptr, @XV), Sizeof(Integer)) = -1 Then close_(TSock): Return TSNE_Const_CantBindSocket
    
  988.     #ENDIF
    
  989. #ENDIF
    
  990. Dim BV As Integer = bind(TSock, Cptr(SOCKADDR Ptr, @TTADDR), Sizeof(SOCKADDR_IN))
    
  991. If BV = SOCKET_ERROR Then close_(TSock): Return TSNE_Const_CantBindSocket
    
  992. BV = listen(TSock, V_MaxSimConReq)
    
  993. If BV = SOCKET_ERROR Then Return TSNE_Const_CantSetListening
    
  994. Dim TSD As TSNE_Socket Ptr = TSNE_INT_Add()
    
  995. Mutexlock(TSNE_INT_Mutex)
    
  996. TSD->V_Socket = TSock
    
  997. TSD->V_IPA = V_IPA
    
  998. TSD->V_Port = V_Port
    
  999. TSD->V_Prot = TSNE_P_TCP
    
  1000. TSD->V_IsServer = 1
    
  1001. TSD->T_ThreadOn = 1
    
  1002. TSD->V_Event.TSNE_NewConnection = V_Event_NewConPTR
    
  1003. TSD->V_Event.TSNE_NewConnectionCanceled = V_Event_NewConCancelPTR
    
  1004. R_TSNEID = TSD->V_TSNEID
    
  1005. TSD->T_Thread = Threadcreate(Cast(Any Ptr, @TSNE_INT_Thread_Event), Cast(Any Ptr, R_TSNEID), V_StackSizeOverride)
    
  1006. Mutexunlock(TSNE_INT_Mutex)
    
  1007. Return TSNE_Const_NoError
    
  1008. End Function
    
  1009. 
    
  1010. 
    
  1011. '--------------------------------------------------------------------------------------------------------------
    
  1012. Private Function TSNE_Create_Client(Byref R_TSNEID As Uinteger, Byval V_IPA As String, Byval V_Port As Ushort, Byval V_Event_DisconPTR As Any Ptr = 0, Byval V_Event_ConPTR As Any Ptr = 0, Byval V_Event_NewDataPTR As Any Ptr, Byval V_TimeoutSecs As Uinteger = 60, Byval V_StackSizeOverride As Uinteger = TSNE_INT_StackSize, Byval V_WaitThreadRunning As Ubyte = 1, Byval V_CallbackBackPtr As Any Ptr = 0) As Integer
    
  1013. R_TSNEID = 0
    
  1014. If (V_Port < 0) Or (V_Port > 65535) Then Return TSNE_Const_PortOutOfRange
    
  1015. If V_IPA = "" Then Return TSNE_Const_IPAnotFound
    
  1016. If Instr(1, V_IPA, ":") > 0 Then Return TSNE_Const_NoIPV6
    
  1017. Dim TADDRIN As in_addr
    
  1018. Dim RV As Integer = TSNE_INT_GetHostEnd(V_IPA, TADDRIN)
    
  1019. If RV <> TSNE_Const_NoError Then Return RV
    
  1020. Dim TADDR As SOCKADDR_IN
    
  1021. With TADDR
    
  1022.     .sin_family = AF_INET
    
  1023.     .sin_port = htons(V_Port)
    
  1024.     .sin_addr = TADDRIN
    
  1025. End With
    
  1026. Dim TSock As Socket = opensocket(PF_INET, SOCK_STREAM, 0)
    
  1027. If TSock = INVALID_SOCKET Then
    
  1028.     #IF Defined(TSNE_ERRNO)
    
  1029.         Select Case errno
    
  1030.             Case EMFILE, ENFILE, ENOMEM: Return TSNE_Const_CantCreateSocketLimit
    
  1031.             Case Else: Return TSNE_Const_CantCreateSocket
    
  1032.         End Select
    
  1033.     #ELSE
    
  1034.         Return TSNE_Const_CantCreateSocket
    
  1035.     #ENDIF
    
  1036. End If
    
  1037. #IF Defined(TSNE_DEF_REUSER)
    
  1038.     Dim XV As Integer = 1
    
  1039.     #IF Defined(__FB_LINUX__)
    
  1040.         If setsockopt(TSock, SOL_SOCKET, SO_REUSEADDR, @XV, Sizeof(Integer)) = -1 Then close_(TSock): Return TSNE_Const_CantBindSocket
    
  1041.     #ELSEIF Defined(__FB_WIN32__)
    
  1042.         If setsockopt(TSock, SOL_SOCKET, SO_REUSEADDR, Cast(Zstring Ptr, @XV), Sizeof(Integer)) = -1 Then close_(TSock): Return TSNE_Const_CantBindSocket
    
  1043.     #ENDIF
    
  1044. #ENDIF
    
  1045. Dim XMode As Uinteger
    
  1046. #IF Defined(__FB_LINUX__)
    
  1047.     Dim XFlag As Integer = fcntl(TSock, F_GETFL, 0)
    
  1048.     If XFlag = -1 Then close_(TSock): Return TSNE_Const_ReturnErrorInCallback
    
  1049.     If fcntl(TSock, F_SETFL, XFlag Or O_NONBLOCK) = -1 Then close_(TSock): Return TSNE_Const_ReturnErrorInCallback
    
  1050. #ELSEIF Defined(__FB_WIN32__)
    
  1051.     XMode = 1
    
  1052. '   Dim XFlag as Integer = ioctlsocket(TSock, FIONBIO, @XMode)
    
  1053.     Dim XFlag As Integer = ioctlsocket(TSock, FIONBIO, Cast(Any Ptr, 1))
    
  1054. #ENDIF  
    
  1055. Dim BV As Integer = connect(TSock, Cptr(SOCKADDR Ptr, @TADDR), Sizeof(SOCKADDR))
    
  1056. If BV <> 0 Then
    
  1057.     Dim TTV As timeval
    
  1058.     Dim TFDSet As fd_Set
    
  1059.     Dim TFDSetW As fd_Set
    
  1060.     With TTV
    
  1061.         .tv_sec = 1
    
  1062.         .tv_usec = 0
    
  1063.     End With
    
  1064.     #IF Defined(__FB_LINUX__)
    
  1065.         Dim XTot As Double = Timer + V_TimeoutSecs
    
  1066.         Do
    
  1067.             If connect(TSock, Cptr(SOCKADDR Ptr, @TADDR), Sizeof(SOCKADDR)) = 0 Then Exit Do
    
  1068.             If XTot < Timer Then close_(TSock): Return TSNE_Const_CantConnectToRemote
    
  1069.             If TSock = INVALID_SOCKET Then Return TSNE_Const_CantConnectToRemote
    
  1070.             With TTV
    
  1071.                 .tv_sec = 0
    
  1072.                 .tv_usec = 1000
    
  1073.             End With
    
  1074.             select_ 0, 0, 0, 0, @TTV
    
  1075.         Loop
    
  1076.     #ELSEIF Defined(__FB_WIN32__)
    
  1077.         FD_SET_(TSock, @TFDSet)
    
  1078.         With TTV
    
  1079.             .tv_sec = V_TimeoutSecs
    
  1080.             .tv_usec = 0
    
  1081.         End With
    
  1082.         FD_ZERO(@TFDSet)
    
  1083.         If select_(TSock + 1, 0, @TFDSet, 0, @TTV) = (INVALID_SOCKET Or 0) Then Return TSNE_Const_CantConnectToRemote
    
  1084.         If Not (FD_ISSET(TSock, @TFDSet)) Then close_(TSock): Return TSNE_Const_CantConnectToRemote
    
  1085.         If TSock = INVALID_SOCKET Then Return TSNE_Const_CantConnectToRemote
    
  1086.     #ENDIF  
    
  1087. End If
    
  1088. #IF Defined(__FB_LINUX__)
    
  1089.     fcntl(TSock, F_SETFL, XFlag)
    
  1090. #ELSEIF Defined(__FB_WIN32__)
    
  1091.     XMode = 0
    
  1092. '   XFlag = ioctlsocket(TSock, FIONBIO, @XMode)
    
  1093.     XFlag = ioctlsocket(TSock, FIONBIO, Cast(Any Ptr, 1))
    
  1094. #ENDIF
    
  1095. Dim TSD As TSNE_Socket Ptr = TSNE_INT_Add()
    
  1096. Mutexlock(TSNE_INT_Mutex)
    
  1097. TSD->V_Socket = TSock
    
  1098. TSD->V_IPA = V_IPA
    
  1099. TSD->V_Port = V_Port
    
  1100. TSD->V_Prot = TSNE_P_TCP
    
  1101. TSD->V_IsServer = 0
    
  1102. TSD->T_ThreadOn = 1
    
  1103. TSD->V_Event.TSNE_Disconnected = V_Event_DisconPTR
    
  1104. TSD->V_Event.TSNE_Connected = V_Event_ConPTR
    
  1105. TSD->V_Event.TSNE_NewData = V_Event_NewDataPTR
    
  1106. TSD->V_Event.V_AnyPtr = V_CallbackBackPtr
    
  1107. R_TSNEID = TSD->V_TSNEID
    
  1108. TSD->T_Thread = Threadcreate(Cast(Any Ptr, @TSNE_INT_Thread_Event), Cast(Any Ptr, R_TSNEID), V_StackSizeOverride)
    
  1109. Mutexunlock(TSNE_INT_Mutex)
    
  1110. #IFNDEF TSNE_SUBCALLBACK
    
  1111.     If V_WaitThreadRunning = 1 Then
    
  1112.         Dim TTot As Double = Timer() + V_TimeoutSecs
    
  1113.         Do
    
  1114.             Mutexlock(TSNE_INT_Mutex)
    
  1115.             TSD = TSNE_INT_GetPtr(R_TSNEID)
    
  1116.             If TSD = 0 Then Mutexunlock(TSNE_INT_Mutex): Return TSNE_Const_InternalError
    
  1117.             If TTot < Timer() Then Mutexunlock(TSNE_INT_Mutex): Return TSNE_Const_InternalError
    
  1118.             If TSD->T_ThreadOn = 2 Then Mutexunlock(TSNE_INT_Mutex): Return TSNE_Const_NoError
    
  1119.             Mutexunlock(TSNE_INT_Mutex)
    
  1120.             Sleep 1, 1
    
  1121.         Loop
    
  1122.     End If
    
  1123. #ENDIF
    
  1124. Return TSNE_Const_NoError
    
  1125. End Function
    
  1126. 
    
  1127. 
    
  1128. '--------------------------------------------------------------------------------------------------------------
    
  1129. Private Function TSNE_Create_Accept(Byval V_RequestID As Socket, Byref R_TSNEID As Uinteger, Byref R_IPA As String = "", Byval V_Event_DisconPTR As Any Ptr = 0, Byval V_Event_ConPTR As Any Ptr = 0, Byval V_Event_NewDataPTR As Any Ptr, Byref R_RemoteShownServerIPA As String = "", Byval V_StackSizeOverride As Uinteger = TSNE_INT_StackSize, Byval V_WaitThreadRunning As Ubyte = 1, Byval V_CallbackBackPtr As Any Ptr = 0) As Integer
    
  1130. Dim TADDR As SOCKADDR_IN
    
  1131. Dim XSize As Integer = 16
    
  1132. Dim OADDR As SOCKADDR_IN 
    
  1133. If getsockname(V_RequestID, Cast(sockaddr Ptr, @OADDR), @XSize) = 0 Then R_RemoteShownServerIPA = *inet_ntoa(OADDR.sin_addr)
    
  1134. If getpeername(V_RequestID, Cast(sockaddr Ptr, @TADDR), @XSize) = 0 Then R_IPA = *inet_ntoa(TADDR.sin_addr)
    
  1135. Dim TSD As TSNE_Socket Ptr = TSNE_INT_Add()
    
  1136. Mutexlock(TSNE_INT_Mutex)
    
  1137. TSD->V_Socket = V_RequestID
    
  1138. TSD->V_Prot = TSNE_P_TCP
    
  1139. TSD->V_IPA = R_IPA
    
  1140. TSD->T_ThreadOn = 1
    
  1141. TSD->V_Event.TSNE_Disconnected = V_Event_DisconPTR
    
  1142. TSD->V_Event.TSNE_Connected = V_Event_ConPTR
    
  1143. TSD->V_Event.TSNE_NewData = V_Event_NewDataPTR
    
  1144. TSD->V_Event.V_AnyPtr = V_CallbackBackPtr
    
  1145. R_TSNEID = TSD->V_TSNEID
    
  1146. TSD->T_Thread = Threadcreate(Cast(Any Ptr, @TSNE_INT_Thread_Event), Cast(Any Ptr, R_TSNEID), V_StackSizeOverride)
    
  1147. Mutexunlock(TSNE_INT_Mutex)
    
  1148. #IFNDEF TSNE_SUBCALLBACK
    
  1149.     If V_WaitThreadRunning = 1 Then
    
  1150.         Dim TTot As Double = Timer() + 60
    
  1151.         Do
    
  1152.             Mutexlock(TSNE_INT_Mutex)
    
  1153.             TSD = TSNE_INT_GetPtr(R_TSNEID)
    
  1154.             If TSD = 0 Then Mutexunlock(TSNE_INT_Mutex): Return TSNE_Const_InternalError
    
  1155.             If TTot < Timer() Then Mutexunlock(TSNE_INT_Mutex): Return TSNE_Const_InternalError
    
  1156.             If TSD->T_ThreadOn = 2 Then Mutexunlock(TSNE_INT_Mutex): Return TSNE_Const_NoError
    
  1157.             Mutexunlock(TSNE_INT_Mutex)
    
  1158.             Sleep 1, 1
    
  1159.         Loop
    
  1160.     End If
    
  1161. #ENDIF
    
  1162. Return TSNE_Const_NoError
    
  1163. End Function
    
  1164. 
    
  1165. 
    
  1166. '--------------------------------------------------------------------------------------------------------------
    
  1167. Private Function TSNE_Create_UDP_RX(Byref R_TSNEID As Uinteger, Byval V_Port As Ushort, Byval V_Event_NewDataUDPPTR As Any Ptr, Byval V_StackSizeOverride As Uinteger = TSNE_INT_StackSize, Byval V_WaitThreadRunning As Ubyte = 1) As Integer
    
  1168. R_TSNEID = 0
    
  1169. If (V_Port < 0) Or (V_Port > 65535) Then Return TSNE_Const_PortOutOfRange
    
  1170. If V_Event_NewDataUDPPTR = 0 Then Return TSNE_Const_MissingEventPTR
    
  1171. Dim TSock As Socket = opensocket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)
    
  1172. If TSock = INVALID_SOCKET Then
    
  1173.     #IF Defined(TSNE_ERRNO)
    
  1174.         Select Case errno
    
  1175.             Case EMFILE, ENFILE, ENOMEM: Return TSNE_Const_CantCreateSocketLimit
    
  1176.             Case Else: Return TSNE_Const_CantCreateSocket
    
  1177.         End Select
    
  1178.     #ELSE
    
  1179.         Return TSNE_Const_CantCreateSocket
    
  1180.     #ENDIF
    
  1181. End If
    
  1182. #IF Defined(TSNE_DEF_REUSER)
    
  1183.     Dim XV As Integer = 1
    
  1184.     #IF Defined(__FB_LINUX__)
    
  1185.         If setsockopt(TSock, SOL_SOCKET, SO_REUSEADDR, @XV, Sizeof(Integer)) = -1 Then close_(TSock): Return TSNE_Const_CantBindSocket
    
  1186.     #ELSEIF Defined(__FB_WIN32__)
    
  1187.         If setsockopt(TSock, SOL_SOCKET, SO_REUSEADDR, Cast(Zstring Ptr, @XV), Sizeof(Integer)) = -1 Then close_(TSock): Return TSNE_Const_CantBindSocket
    
  1188.     #ENDIF
    
  1189. #ENDIF
    
  1190. Dim TTADDR As SOCKADDR_IN
    
  1191. With TTADDR
    
  1192.     .sin_family = AF_INET
    
  1193.     .sin_port = htons(V_Port)
    
  1194.     .sin_addr.s_addr = INADDR_ANY
    
  1195. End With
    
  1196. Dim BV As Integer = bind(TSock, Cptr(SOCKADDR Ptr, @TTADDR), Sizeof(SOCKADDR_IN))
    
  1197. If BV = SOCKET_ERROR Then close_(TSock): Return TSNE_Const_CantBindSocket
    
  1198. Dim TSD As TSNE_Socket Ptr = TSNE_INT_Add()
    
  1199. Mutexlock(TSNE_INT_Mutex)
    
  1200. TSD->V_Socket = TSock
    
  1201. TSD->V_IPA = ""
    
  1202. TSD->V_USP = TTADDR
    
  1203. TSD->V_Port = V_Port
    
  1204. TSD->V_Prot = TSNE_P_UDP
    
  1205. TSD->V_IsServer = 1
    
  1206. TSD->T_ThreadOn = 1
    
  1207. TSD->V_Event.TSNE_NewDataUDP = V_Event_NewDataUDPPTR
    
  1208. R_TSNEID = TSD->V_TSNEID
    
  1209. TSD->T_Thread = Threadcreate(Cast(Any Ptr, @TSNE_INT_Thread_Event), Cast(Any Ptr, R_TSNEID), V_StackSizeOverride)
    
  1210. Mutexunlock(TSNE_INT_Mutex)
    
  1211. If V_WaitThreadRunning = 1 Then
    
  1212.     Dim TTot As Double = Timer() + 60
    
  1213.     Do
    
  1214.         Mutexlock(TSNE_INT_Mutex)
    
  1215.         TSD = TSNE_INT_GetPtr(R_TSNEID)
    
  1216.         If TSD = 0 Then Mutexunlock(TSNE_INT_Mutex): Return TSNE_Const_InternalError
    
  1217.         If TTot < Timer() Then Mutexunlock(TSNE_INT_Mutex): Return TSNE_Const_InternalError
    
  1218.         If TSD->T_ThreadOn = 2 Then Mutexunlock(TSNE_INT_Mutex): Return TSNE_Const_NoError
    
  1219.         Mutexunlock(TSNE_INT_Mutex)
    
  1220.         Sleep 1, 1
    
  1221.     Loop
    
  1222. End If
    
  1223. Return TSNE_Const_NoError
    
  1224. End Function
    
  1225. 
    
  1226. 
    
  1227. '--------------------------------------------------------------------------------------------------------------
    
  1228. Private Function TSNE_Create_UDP_TX(Byref R_TSNEID As Uinteger, Byval V_DoBroadcast As Ubyte = 0) As Integer
    
  1229. R_TSNEID = 0
    
  1230. Dim TSock As Socket = opensocket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)
    
  1231. If TSock = INVALID_SOCKET Then
    
  1232.     #IF Defined(TSNE_ERRNO)
    
  1233.         Select Case errno
    
  1234.             Case EMFILE, ENFILE, ENOMEM: Return TSNE_Const_CantCreateSocketLimit
    
  1235.             Case Else: Return TSNE_Const_CantCreateSocket
    
  1236.         End Select
    
  1237.     #ELSE
    
  1238.         Return TSNE_Const_CantCreateSocket
    
  1239.     #ENDIF
    
  1240. End If
    
  1241. #IF Defined(TSNE_DEF_REUSER)
    
  1242.     Dim XV As Integer = 1
    
  1243.     #IF Defined(__FB_LINUX__)
    
  1244.         If setsockopt(TSock, SOL_SOCKET, SO_REUSEADDR, @XV, Sizeof(Integer)) = -1 Then close_(TSock): Return TSNE_Const_CantBindSocket
    
  1245.     #ELSEIF Defined(__FB_WIN32__)
    
  1246.         If setsockopt(TSock, SOL_SOCKET, SO_REUSEADDR, Cast(Zstring Ptr, @XV), Sizeof(Integer)) = -1 Then close_(TSock): Return TSNE_Const_CantBindSocket
    
  1247.     #ENDIF
    
  1248. #ENDIF
    
  1249. If V_DoBroadcast = 1 Then
    
  1250.     Dim TBD As Integer = 1
    
  1251.     #IF Defined(__FB_LINUX__)
    
  1252.         If setsockopt(TSock, SOL_SOCKET, SO_BROADCAST, @TBD, Sizeof(TBD)) = -1 Then close_(TSock): Return TSNE_Const_CantCreateSocket
    
  1253.     #ELSEIF Defined(__FB_WIN32__)
    
  1254.         If setsockopt(TSock, SOL_SOCKET, SO_BROADCAST, Cast(Zstring Ptr, @TBD), Sizeof(TBD)) = -1 Then close_(TSock): Return TSNE_Const_CantCreateSocket
    
  1255.     #ENDIF
    
  1256. End If
    
  1257. Dim TSD As TSNE_Socket Ptr = TSNE_INT_Add()
    
  1258. Mutexlock(TSNE_INT_Mutex)
    
  1259. TSD->V_Socket = TSock
    
  1260. TSD->V_IPA = ""
    
  1261. TSD->V_Port = 0
    
  1262. TSD->V_Prot = TSNE_P_UDP
    
  1263. TSD->V_IsServer = 1
    
  1264. TSD->T_ThreadOn = 2
    
  1265. R_TSNEID = TSD->V_TSNEID
    
  1266. Mutexunlock(TSNE_INT_Mutex)
    
  1267. Return TSNE_Const_NoError
    
  1268. End Function
    
  1269. 
    
  1270. 
    
  1271. 
    
  1272. 
    
  1273. 
    
  1274. '##############################################################################################################
    
  1275. Private Function TSNE_Data_Send(Byref V_TSNEID As Uinteger, Byref V_Data As String, Byref R_BytesSend As Uinteger = 0, Byval V_IPA As String = "", Byval V_Port As Ushort = 0) As Integer
    
  1276. 'Print "TSNE_SEND >" & V_Data & "<"
    
  1277. R_BytesSend = 0
    
  1278. Mutexlock(TSNE_INT_Mutex)
    
  1279. Dim TSD As TSNE_Socket Ptr = TSNE_INT_GetPtr(V_TSNEID)
    
  1280. If TSD = 0 Then Mutexunlock(TSNE_INT_Mutex): Return TSNE_Const_TSNEIDnotFound
    
  1281. If TSD->V_Socket = INVALID_SOCKET Then Mutexunlock(TSNE_INT_Mutex): Return TSNE_Const_TSNEIDnotFound
    
  1282. Dim TSock As Socket = TSD->V_Socket
    
  1283. Dim TProt As TSNE_Protocol = TSD->V_Prot
    
  1284. Dim TTState As Uinteger = TSD->T_ThreadOn
    
  1285. Mutexunlock(TSNE_INT_Mutex)
    
  1286. Dim XTemp As String = V_Data
    
  1287. Dim XLen As Uinteger = Len(XTemp)
    
  1288. Dim BV As Integer
    
  1289. Select Case TProt
    
  1290.     Case TSNE_P_UDP
    
  1291.         If V_IPA = "" Then Return TSNE_Const_IPAnotFound
    
  1292.         If (V_Port < 0) Or (V_Port > 65535) Then Return TSNE_Const_PortOutOfRange
    
  1293.         Dim TTADDR As SOCKADDR_IN
    
  1294.         With TTADDR
    
  1295.             .sin_family = AF_INET
    
  1296.             .sin_port = htons(V_Port)
    
  1297.             If V_IPA <> "0" Then
    
  1298.                 Dim XHost As hostent Ptr
    
  1299.                 If Instr(1, V_IPA, ":") > 0 Then Return TSNE_Const_NoIPV6
    
  1300.                 Dim TADDRIN As in_addr
    
  1301.                 Dim RV As Integer = TSNE_INT_GetHostEnd(V_IPA, TADDRIN)
    
  1302.                 If RV <> TSNE_Const_NoError Then Return RV
    
  1303.                 .sin_addr = TADDRIN
    
  1304.             Else: .sin_addr.s_addr = INADDR_BROADCAST
    
  1305.             End If
    
  1306.         End With
    
  1307.         Do Until R_BytesSend = XLen
    
  1308.             BV = sendto(TSock, Strptr(XTemp) + R_BytesSend, XLen - R_BytesSend, TSNE_MSG_NOSIGNAL, Cast(SOCKADDR Ptr, @TTADDR), Sizeof(SOCKADDR_IN))
    
  1309.             If BV > 0 Then
    
  1310.                 R_BytesSend += BV
    
  1311.             Elseif BV = 0 Then
    
  1312.             Else: Exit Do
    
  1313.             End If
    
  1314.         Loop
    
  1315.     Case TSNE_P_TCP
    
  1316.         If TTState <> 2 Then Mutexunlock(TSNE_INT_Mutex): Return TSNE_Const_UnstableState
    
  1317.         Do Until R_BytesSend = XLen
    
  1318.             BV = send(TSock, Strptr(XTemp) + R_BytesSend, XLen - R_BytesSend, TSNE_MSG_NOSIGNAL)
    
  1319.             If BV > 0 Then
    
  1320.                 R_BytesSend += BV
    
  1321.             Elseif BV = 0 Then
    
  1322.             Else: Exit Do
    
  1323.             End If
    
  1324.         Loop
    
  1325. End Select
    
  1326. Mutexlock(TSNE_INT_Mutex)
    
  1327. TSD->T_DataOut += R_BytesSend
    
  1328. Mutexunlock(TSNE_INT_Mutex)
    
  1329. If R_BytesSend <> XLen Then Return TSNE_Const_ErrorSendingData
    
  1330. Return TSNE_Const_NoError
    
  1331. End Function
    
  1332. 
    
  1333. 
    
  1334. 
    
  1335. 
    
  1336. 
    
  1337. '##############################################################################################################
    
  1338. Private Sub TSNE_WaitClose(Byref V_TSNEID As Uinteger)
    
  1339. Dim TSD As TSNE_Socket Ptr
    
  1340. Mutexlock(TSNE_INT_Mutex)
    
  1341. Mutexunlock(TSNE_INT_Mutex)
    
  1342. Do
    
  1343.     Mutexlock(TSNE_INT_Mutex)
    
  1344.     TSD = TSNE_INT_GetPtr(V_TSNEID)
    
  1345.     If TSD = 0 Then Exit Do
    
  1346.     If TSD->T_ThreadOn = 0 Then Exit Do
    
  1347.     Mutexunlock(TSNE_INT_Mutex)
    
  1348.     Sleep 10, 1
    
  1349. Loop
    
  1350. Mutexunlock(TSNE_INT_Mutex)
    
  1351. End Sub
    
  1352. 
    
  1353. 
    
  1354. '--------------------------------------------------------------------------------------------------------------
    
  1355. Private Function TSNE_WaitConnected(Byref V_TSNEID As Uinteger, V_TimeOut As Uinteger = 60) As Integer
    
  1356. Dim TSD As TSNE_Socket Ptr
    
  1357. Dim TTot As Double = Timer() + V_TimeOut
    
  1358. Mutexlock(TSNE_INT_Mutex)
    
  1359. Mutexunlock(TSNE_INT_Mutex)
    
  1360. Do
    
  1361.     Mutexlock(TSNE_INT_Mutex)
    
  1362.     TSD = TSNE_INT_GetPtr(V_TSNEID)
    
  1363.     If TSD = 0 Then Exit Do
    
  1364.     If TTot < Timer() Then Exit Do
    
  1365.     If TSD->T_ThreadOn = 2 Then Mutexunlock(TSNE_INT_Mutex): Return TSNE_Const_NoError
    
  1366.     Mutexunlock(TSNE_INT_Mutex)
    
  1367.     Sleep 10, 1
    
  1368. Loop
    
  1369. Mutexunlock(TSNE_INT_Mutex)
    
  1370. Return TSNE_Const_CantConnectToRemote
    
  1371. End Function
    
  1372. 
    
  1373. 
    
  1374. '--------------------------------------------------------------------------------------------------------------
    
  1375. Private Function TSNE_IsClosed(Byref V_TSNEID As Uinteger) As Ubyte
    
  1376. Dim TSD As TSNE_Socket Ptr
    
  1377. Mutexlock(TSNE_INT_Mutex)
    
  1378. TSD = TSNE_INT_GetPtr(V_TSNEID)
    
  1379. If TSD = 0 Then Mutexunlock(TSNE_INT_Mutex): Return 1
    
  1380. If TSD->T_ThreadOn = 0 Then Mutexunlock(TSNE_INT_Mutex): Return 1
    
  1381. Mutexunlock(TSNE_INT_Mutex)
    
  1382. Return 0
    
  1383. End Function
    
  1384. 
    
  1385. 
    
  1386. 
    
  1387. 
    
  1388. 
    
  1389. '##############################################################################################################
    
  1390. Private Sub TSNE_INT_Thread_Event(Byval V_TSNEID As Any Ptr)
    
  1391. #IF Defined(_TSNE_DODEBUG_)
    
  1392.     Print "=[" & Str(Cast(Uinteger, V_TSNEID)) & "]=[TSNE]=[EVT]= Lock..."
    
  1393. #ENDIF
    
  1394. Mutexlock(TSNE_INT_Mutex)
    
  1395. #IF Defined(_TSNE_DODEBUG_)
    
  1396.     Print "=[" & Str(Cast(Uinteger, V_TSNEID)) & "]=[TSNE]=[EVT]= Lock-K"
    
  1397. #ENDIF
    
  1398. Dim TTSNEID As Uinteger = Cast(Uinteger, V_TSNEID)
    
  1399. Dim TSD As TSNE_Socket Ptr = TSNE_INT_GetPtr(TTSNEID)
    
  1400. If TSD = 0 Then Mutexunlock(TSNE_INT_Mutex): Exit Sub
    
  1401. Dim TSock As Socket = TSD->V_Socket
    
  1402. Dim TEvent As TSNE_Event_Type = TSD->V_Event
    
  1403. Dim TTV As TimeVal
    
  1404. With TTV
    
  1405.     .tv_sec = 0
    
  1406.     .tv_usec = 0
    
  1407. End With
    
  1408. Dim TFDSet As fd_Set
    
  1409. Dim TLenB As Integer
    
  1410. Dim TBuffer As Zstring * TSNE_INT_BufferSize
    
  1411. Dim TIPA As String
    
  1412. Dim T As String
    
  1413. Dim TADDR As SOCKADDR_IN
    
  1414. Dim XSize As Integer = Sizeof(sockaddr_in)
    
  1415. If TSD->T_ThreadOn = 1 Then
    
  1416.     TSD->T_ThreadOn = 2
    
  1417.     Dim TProt As TSNE_Protocol = TSD->V_Prot
    
  1418.     Select Case TProt
    
  1419.         Case TSNE_P_UDP
    
  1420.             Dim TTADDRC As SOCKADDR_IN
    
  1421.             Dim TTLen As Uinteger = Sizeof(TADDR)
    
  1422.             Mutexunlock(TSNE_INT_Mutex)
    
  1423.             Do
    
  1424.                 Mutexlock(TSNE_INT_Mutex)
    
  1425.                 TSD = TSNE_INT_GetPtr(TTSNEID): If TSD = 0 Then Mutexunlock(TSNE_INT_Mutex): Exit Do
    
  1426.                 TSock = TSD->V_Socket: If TSock = INVALID_SOCKET Then Mutexunlock(TSNE_INT_Mutex): Exit Do
    
  1427.                 Mutexunlock(TSNE_INT_Mutex)
    
  1428.                 fd_set_(TSock, @TFDSet)
    
  1429.                 If TSock = INVALID_SOCKET Then Exit Do
    
  1430.                 With TTV
    
  1431.                     .tv_sec = 1
    
  1432.                     .tv_usec = 0
    
  1433.                 End With
    
  1434. '|              If select_(TSock + 1, @TFDSet, 0, 0, @TTV) = -1 Then Exit Do
    
  1435.                 select_(TSock + 1, @TFDSet, 0, 0, @TTV)
    
  1436.                 If (FD_ISSET(TSock, @TFDSet)) <> 0 Then
    
  1437.                     If TSock = INVALID_SOCKET Then Exit Do
    
  1438.                     TADDR = TTADDRC
    
  1439.                     TLenB = recvfrom(TSock, Strptr(TBuffer), TSNE_INT_BufferSize, 0, Cast(SOCKADDR Ptr, @TADDR), @TTLen)
    
  1440.                     If TLenB <= 0 Then Exit Do
    
  1441.                     TBuffer[TLenB] = 0
    
  1442.                     T = Space(TLenB + 1)
    
  1443.                     MemCpy(Strptr(T), Strptr(TBuffer), TLenB)
    
  1444.                     Mutexlock(TSNE_INT_Mutex)
    
  1445.                     TSD = TSNE_INT_GetPtr(TTSNEID): If TSD = 0 Then Mutexunlock(TSNE_INT_Mutex): Exit Do
    
  1446.                     TSD->T_DataIn += TLenB
    
  1447.                     Mutexunlock(TSNE_INT_Mutex)
    
  1448.                     T = Mid(T, 1, Len(T) - 1)
    
  1449.                     TIPA = *inet_ntoa(TADDR.sin_addr)
    
  1450.                     If TEvent.TSNE_NewDataUDP <> 0 Then TEvent.TSNE_NewDataUDP(TTSNEID, TIPA, T)
    
  1451.                 End If
    
  1452.             Loop
    
  1453.             
    
  1454.         Case TSNE_P_TCP
    
  1455.             If TSD->V_IsServer <> 1 Then
    
  1456.                 Mutexunlock(TSNE_INT_Mutex)
    
  1457.                 #IF Defined(_TSNE_DODEBUG_)
    
  1458.                     Print "=[" & Str(TTSNEID) & "]=[TSNE]=[EVT]=[TCP-C]= Unlocked"
    
  1459.                 #ENDIF
    
  1460.                 #IF Defined(TSNE_SUBCALLBACK)
    
  1461.                     If TEvent.TSNE_Connected <> 0 Then TEvent.TSNE_Connected(TTSNEID, TEvent.V_AnyPtr)
    
  1462.                 #ELSE
    
  1463.                     If TEvent.TSNE_Connected <> 0 Then TEvent.TSNE_Connected(TTSNEID)
    
  1464.                 #ENDIF
    
  1465.                 Do
    
  1466.                     Mutexlock(TSNE_INT_Mutex)
    
  1467.                     TSD = TSNE_INT_GetPtr(TTSNEID): If TSD = 0 Then Mutexunlock(TSNE_INT_Mutex): Exit Do
    
  1468.                     TSock = TSD->V_Socket: If TSock = INVALID_SOCKET Then Mutexunlock(TSNE_INT_Mutex): Exit Do
    
  1469.                     Mutexunlock(TSNE_INT_Mutex)
    
  1470.                     fd_set_(TSock, @TFDSet)
    
  1471.                     If TSock = INVALID_SOCKET Then Exit Do
    
  1472.                     With TTV
    
  1473.                         .tv_sec = 1
    
  1474.                         .tv_usec = 0
    
  1475.                     End With
    
  1476. '|                  If select_(TSock + 1, @TFDSet, 0, 0, @TTV) = -1 Then Exit Do
    
  1477.                     select_(TSock + 1, @TFDSet, 0, 0, @TTV)
    
  1478.                     If (FD_ISSET(TSock, @TFDSet)) <> 0 Then
    
  1479.                         #IF Defined(_TSNE_DODEBUG_)
    
  1480.                             Print "=[" & Str(TTSNEID) & "]=[TSNE]=[EVT]=[TCP-C]= Event"
    
  1481.                         #ENDIF
    
  1482.                         If TSock = INVALID_SOCKET Then Exit Do
    
  1483.                         #IF Defined(_TSNE_DODEBUG_)
    
  1484.                             Print "=[" & Str(TTSNEID) & "]=[TSNE]=[EVT]=[TCP-C]= Dat"
    
  1485.                         #ENDIF
    
  1486.                         TLenB = recv(TSock, Strptr(TBuffer), TSNE_INT_BufferSize, 0)
    
  1487.                         If TLenB <= 0 Then Exit Do
    
  1488.                         TBuffer[TLenB] = 0
    
  1489.                         T = Space(TLenB + 1)
    
  1490.                         MemCpy(Strptr(T), Strptr(TBuffer), TLenB)
    
  1491.                         #IF Defined(_TSNE_DODEBUG_)
    
  1492.                             Print "=[" & Str(TTSNEID) & "]=[TSNE]=[EVT]=[TCP-C]= Dat Lock..."
    
  1493.                         #ENDIF
    
  1494.                         Mutexlock(TSNE_INT_Mutex)
    
  1495.                         #IF Defined(_TSNE_DODEBUG_)
    
  1496.                             Print "=[" & Str(TTSNEID) & "]=[TSNE]=[EVT]=[TCP-C]= Dat Lock-K"
    
  1497.                         #ENDIF
    
  1498.                         TSD = TSNE_INT_GetPtr(TTSNEID): If TSD = 0 Then Mutexunlock(TSNE_INT_Mutex): Exit Do
    
  1499.                         TSD->T_DataIn += TLenB
    
  1500.                         Mutexunlock(TSNE_INT_Mutex)
    
  1501.                         #IF Defined(_TSNE_DODEBUG_)
    
  1502.                             Print "=[" & Str(TTSNEID) & "]=[TSNE]=[EVT]=[TCP-C]= Dat Unlock"
    
  1503.                         #ENDIF
    
  1504.                         T = Mid(T, 1, Len(T) - 1)
    
  1505.                         #IF Defined(_TSNE_DODEBUG_)
    
  1506.                             Print "=[" & Str(TTSNEID) & "]=[TSNE]=[EVT]=[TCP-C]= Dat Call... (" & Str(Len(T)) & ")"
    
  1507.                         #ENDIF
    
  1508.                         #IF Defined(TSNE_SUBCALLBACK)
    
  1509.                             If TEvent.TSNE_NewData <> 0 Then TEvent.TSNE_NewData(TTSNEID, T, TEvent.V_AnyPtr)
    
  1510.                         #ELSE
    
  1511.                             If TEvent.TSNE_NewData <> 0 Then TEvent.TSNE_NewData(TTSNEID, T)
    
  1512.                         #ENDIF
    
  1513.                         #IF Defined(_TSNE_DODEBUG_)
    
  1514.                             Print "=[" & Str(TTSNEID) & "]=[TSNE]=[EVT]=[TCP-C]= Dat Call-K"
    
  1515.                         #ENDIF
    
  1516.                     End If
    
  1517.                 Loop
    
  1518.             Else
    
  1519.                 Mutexunlock(TSNE_INT_Mutex)
    
  1520.                 #IF Defined(_TSNE_DODEBUG_)
    
  1521.                     Print "=[" & Str(TTSNEID) & "]=[TSNE]=[EVT]=[TCP-S]= Unlocked"
    
  1522.                 #ENDIF
    
  1523.                 Dim TNSock As Socket
    
  1524.                 Dim Y As Uinteger
    
  1525.                 Dim XOK As Integer
    
  1526.                 Dim XFX As Uinteger
    
  1527.                 Dim XV As Integer = 1
    
  1528.                 Do
    
  1529.                     XFX += 1
    
  1530.                     Mutexlock(TSNE_INT_Mutex)
    
  1531.                     TSD = TSNE_INT_GetPtr(TTSNEID): If TSD = 0 Then Mutexunlock(TSNE_INT_Mutex): Exit Do
    
  1532.                     TSock = TSD->V_Socket: If TSock = INVALID_SOCKET Then Mutexunlock(TSNE_INT_Mutex): Exit Do
    
  1533.                     Mutexunlock(TSNE_INT_Mutex)
    
  1534.                     fd_set_(TSock, @TFDSet)
    
  1535.                     With TTV
    
  1536.                         .tv_sec = 1
    
  1537.                         .tv_usec = 0
    
  1538.                     End With
    
  1539. '|                  If selectsocket(TSock + 1, @TFDSet, 0, 0, @TTV) = SOCKET_ERROR Then
    
  1540.                     selectsocket(TSock + 1, @TFDSet, 0, 0, @TTV)
    
  1541.                     If (FD_ISSET(TSock, @TFDSet)) Then
    
  1542.                         #IF Defined(_TSNE_DODEBUG_)
    
  1543.                             Print "=[" & Str(TTSNEID) & "]=[TSNE]=[EVT]=[TCP-S]=  ACP..."
    
  1544.                         #ENDIF
    
  1545.                         TNSock = accept(TSock, 0, 0)
    
  1546.                         #IF Defined(_TSNE_DODEBUG_)
    
  1547.                             Print "=[" & Str(TTSNEID) & "]=[TSNE]=[EVT]=[TCP-S]=  ACP:" & Str(TNSock)
    
  1548.                         #ENDIF
    
  1549.                         If TNSock = INVALID_SOCKET Then Exit Do
    
  1550.                         #IF Defined(TSNE_DEF_REUSER)
    
  1551.                             #IF Defined(__FB_LINUX__)
    
  1552.                                 setsockopt(TNSock, SOL_SOCKET, SO_REUSEADDR, @XV, Sizeof(Integer))
    
  1553.                             #ELSEIF Defined(__FB_WIN32__)
    
  1554.                                 setsockopt(TNSock, SOL_SOCKET, SO_REUSEADDR, Cast(Zstring Ptr, @XV), Sizeof(Integer))
    
  1555.                             #ENDIF
    
  1556.                         #ENDIF
    
  1557.                         TIPA = ""
    
  1558.                         If getpeername(TNSock, Cast(sockaddr Ptr, @TADDR), @XSize) = 0 Then TIPA = *inet_ntoa(TADDR.sin_addr)
    
  1559.                         #IF Defined(_TSNE_DODEBUG_)
    
  1560.                             Print "=[" & Str(TTSNEID) & "]=[TSNE]=[EVT]=[TCP-S]= BWL Lock..."
    
  1561.                         #ENDIF
    
  1562.                         Mutexlock(TSNE_INT_Mutex)
    
  1563.                         #IF Defined(_TSNE_DODEBUG_)
    
  1564.                             Print "=[" & Str(TTSNEID) & "]=[TSNE]=[EVT]=[TCP-S]= BWL Lock-K"
    
  1565.                         #ENDIF
    
  1566.                         Select Case TSD->V_BWL_UseType
    
  1567.                             Case 1
    
  1568.                                 If TSNE_INT_BW_GetPtr(TSD, TIPA) = 0 Then
    
  1569.                                     Mutexunlock(TSNE_INT_Mutex): If TEvent.TSNE_NewConnection <> 0 Then TEvent.TSNE_NewConnection(TTSNEID, TNSock, TIPA)
    
  1570.                                 Else: Mutexunlock(TSNE_INT_Mutex): close_(TNSock): If TEvent.TSNE_NewConnectionCanceled <> 0 Then TEvent.TSNE_NewConnectionCanceled(TTSNEID, TIPA)
    
  1571.                                 End If
    
  1572.                             Case 2
    
  1573.                                 If TSNE_INT_BW_GetPtr(TSD, TIPA) = 0 Then
    
  1574.                                     Mutexunlock(TSNE_INT_Mutex): close_(TNSock): If TEvent.TSNE_NewConnectionCanceled <> 0 Then TEvent.TSNE_NewConnectionCanceled(TTSNEID, TIPA)
    
  1575.                                 Else: Mutexunlock(TSNE_INT_Mutex): If TEvent.TSNE_NewConnection <> 0 Then TEvent.TSNE_NewConnection(TTSNEID, TNSock, TIPA)
    
  1576.                                 End If
    
  1577.                             Case Else: Mutexunlock(TSNE_INT_Mutex): If TEvent.TSNE_NewConnection <> 0 Then TEvent.TSNE_NewConnection(TTSNEID, TNSock, TIPA)
    
  1578.                         End Select
    
  1579.                         #IF Defined(_TSNE_DODEBUG_)
    
  1580.                             Print "=[" & Str(TTSNEID) & "]=[TSNE]=[EVT]=[TCP-S]= BWL Unlock"
    
  1581.                         #ENDIF
    
  1582.                     End If
    
  1583.                 Loop
    
  1584.             End If
    
  1585.             
    
  1586.         Case Else: Mutexunlock(TSNE_INT_Mutex)
    
  1587.     End Select
    
  1588.     #IF Defined(_TSNE_DODEBUG_)
    
  1589.         Print "=[" & Str(TTSNEID) & "]=[TSNE]=[EVT]= Exit Lock"
    
  1590.     #ENDIF
    
  1591.     Mutexlock(TSNE_INT_Mutex)
    
  1592.     #IF Defined(_TSNE_DODEBUG_)
    
  1593.         Print "=[" & Str(TTSNEID) & "]=[TSNE]=[EVT]= Exit Lock-K"
    
  1594.     #ENDIF
    
  1595. End If
    
  1596. TSD = TSNE_INT_GetPtr(TTSNEID)
    
  1597. If TSD <> 0 Then
    
  1598.     TSock = TSD->V_Socket
    
  1599.     If TSock <> INVALID_SOCKET Then
    
  1600.         TSD->V_Socket = INVALID_SOCKET
    
  1601.         close_(TSock)
    
  1602.     End If
    
  1603.     TSD->T_ThreadOn = 3
    
  1604.     #IF Defined(_TSNE_DODEBUG_)
    
  1605.         Print "=[" & Str(TTSNEID) & "]=[TSNE]=[EVT]= Exit ThreadON 3"
    
  1606.     #ENDIF
    
  1607. End If
    
  1608. Mutexunlock(TSNE_INT_Mutex)
    
  1609. #IF Defined(_TSNE_DODEBUG_)
    
  1610.     Print "=[" & Str(TTSNEID) & "]=[TSNE]=[EVT]= Exit Unlocked"
    
  1611. #ENDIF
    
  1612. End Sub
    
  1613. 
    
  1614. 
    
  1615. 
    
  1616. 
    
  1617. 
    
  1618. '##############################################################################################################
    
  1619. Private Function TSNE_BW_SetEnable(Byval V_Server_TSNEID As Uinteger, V_Type As TSNE_BW_Mode_Enum) As Integer
    
  1620. Mutexlock(TSNE_INT_Mutex)
    
  1621. Dim TSD As TSNE_Socket Ptr = TSNE_INT_GetPtr(V_Server_TSNEID)
    
  1622. If TSD = 0 Then Mutexunlock(TSNE_INT_Mutex): Return TSNE_Const_TSNEIDnotFound
    
  1623. If TSD->V_IsServer <> 1 Then Mutexunlock(TSNE_INT_Mutex): Return TSNE_Const_TSNENoServer
    
  1624. TSD->V_BWL_UseType = V_Type
    
  1625. Mutexunlock(TSNE_INT_Mutex)
    
  1626. Return TSNE_Const_NoError
    
  1627. End Function
    
  1628. 
    
  1629. '--------------------------------------------------------------------------------------------------------------
    
  1630. Private Function TSNE_BW_GetEnable(Byval V_Server_TSNEID As Uinteger, R_Type As TSNE_BW_Mode_Enum) As Integer
    
  1631. Mutexlock(TSNE_INT_Mutex)
    
  1632. Dim TSD As TSNE_Socket Ptr = TSNE_INT_GetPtr(V_Server_TSNEID)
    
  1633. If TSD = 0 Then Mutexunlock(TSNE_INT_Mutex): Return TSNE_Const_TSNEIDnotFound
    
  1634. If TSD->V_IsServer <> 1 Then Mutexunlock(TSNE_INT_Mutex): Return TSNE_Const_TSNENoServer
    
  1635. R_Type = TSD->V_BWL_UseType
    
  1636. Mutexunlock(TSNE_INT_Mutex)
    
  1637. Return TSNE_Const_NoError
    
  1638. End Function
    
  1639. 
    
  1640. '--------------------------------------------------------------------------------------------------------------
    
  1641. Private Function TSNE_BW_Clear(Byval V_Server_TSNEID As Uinteger) As Integer
    
  1642. Mutexlock(TSNE_INT_Mutex)
    
  1643. Dim TSD As TSNE_Socket Ptr = TSNE_INT_GetPtr(V_Server_TSNEID)
    
  1644. If TSD = 0 Then Mutexunlock(TSNE_INT_Mutex): Return TSNE_Const_TSNEIDnotFound
    
  1645. If TSD->V_IsServer <> 1 Then Mutexunlock(TSNE_INT_Mutex): Return TSNE_Const_TSNENoServer
    
  1646. TSNE_INT_BW_Clear(TSD)
    
  1647. Mutexunlock(TSNE_INT_Mutex)
    
  1648. Return TSNE_Const_NoError
    
  1649. End Function
    
  1650. 
    
  1651. '--------------------------------------------------------------------------------------------------------------
    
  1652. Private Function TSNE_BW_Add(Byval V_Server_TSNEID As Uinteger, V_IPA As String, V_BlockTimeSeconds As Uinteger = 3600) As Integer
    
  1653. Mutexlock(TSNE_INT_Mutex)
    
  1654. Dim TSD As TSNE_Socket Ptr = TSNE_INT_GetPtr(V_Server_TSNEID)
    
  1655. If TSD = 0 Then Mutexunlock(TSNE_INT_Mutex): Return TSNE_Const_TSNEIDnotFound
    
  1656. If TSD->V_IsServer <> 1 Then Mutexunlock(TSNE_INT_Mutex): Return TSNE_Const_TSNENoServer
    
  1657. If TSNE_INT_BW_Add(TSD, V_IPA, Now() + V_BlockTimeSeconds) = 1 Then
    
  1658.     Mutexunlock(TSNE_INT_Mutex): Return TSNE_Const_NoError
    
  1659. Else: Mutexunlock(TSNE_INT_Mutex): Return TSNE_Const_IPAalreadyInList
    
  1660. End If
    
  1661. Mutexunlock(TSNE_INT_Mutex)
    
  1662. Return TSNE_Const_NoError
    
  1663. End Function
    
  1664. 
    
  1665. '--------------------------------------------------------------------------------------------------------------
    
  1666. Private Function TSNE_BW_Del(Byval V_Server_TSNEID As Uinteger, V_IPA As String) As Integer
    
  1667. Mutexlock(TSNE_INT_Mutex)
    
  1668. Dim TSD As TSNE_Socket Ptr = TSNE_INT_GetPtr(V_Server_TSNEID)
    
  1669. If TSD = 0 Then Mutexunlock(TSNE_INT_Mutex): Return TSNE_Const_TSNEIDnotFound
    
  1670. If TSD->V_IsServer <> 1 Then Mutexunlock(TSNE_INT_Mutex): Return TSNE_Const_TSNENoServer
    
  1671. If TSNE_INT_BW_Del(TSD, V_IPA) = 1 Then
    
  1672.     Mutexunlock(TSNE_INT_Mutex): Return TSNE_Const_NoError
    
  1673. Else: Mutexunlock(TSNE_INT_Mutex): Return TSNE_Const_IPAnotInList
    
  1674. End If
    
  1675. End Function
    
  1676. 
    
  1677. '--------------------------------------------------------------------------------------------------------------
    
  1678. Private Function TSNE_BW_List(Byval V_Server_TSNEID As Uinteger, Byref R_IPA_List As TSNE_BWL_Type Ptr) As Integer
    
  1679. Mutexlock(TSNE_INT_Mutex)
    
  1680. Dim TSD As TSNE_Socket Ptr = TSNE_INT_GetPtr(V_Server_TSNEID)
    
  1681. If TSD = 0 Then Mutexunlock(TSNE_INT_Mutex): Return TSNE_Const_TSNEIDnotFound
    
  1682. If TSD->V_IsServer <> 1 Then Mutexunlock(TSNE_INT_Mutex): Return TSNE_Const_TSNENoServer
    
  1683. Dim TPtr As TSNE_BWL_Type Ptr = TSD->V_BWL_IPAD
    
  1684. Dim TNPtrL As TSNE_BWL_Type Ptr
    
  1685. Do Until TPtr = 0
    
  1686.     If TNPtrL <> 0 Then
    
  1687.         TNPtrL->V_Next = Callocate(Sizeof(TSNE_Socket))
    
  1688.         TNPtrL->V_Next->V_PreV = TNPtrL
    
  1689.         TNPtrL = TNPtrL->V_Next
    
  1690.     Else
    
  1691.         TNPtrL = Callocate(Sizeof(TSNE_Socket))
    
  1692.         R_IPA_List = TNPtrL
    
  1693.     End If
    
  1694.     TNPtrL->V_IPA = TPtr->V_IPA
    
  1695.     TNPtrL->V_LockTill = TPtr->V_LockTill
    
  1696.     TPtr = TPtr->V_Next
    
  1697. Loop
    
  1698. Mutexunlock(TSNE_INT_Mutex)
    
  1699. Return TSNE_Const_NoError
    
  1700. End Function
    
  1701. 
    
  1702. 
    
  1703. 
    
  1704. 
    
  1705. 
    
  1706. '##############################################################################################################
    
  1707. Private Function TSNE_GetGURUCode(Byref V_GURUID As Integer) As String
    
  1708. Select Case V_GURUID
    
  1709.     Case TSNE_Const_UnknowError:                Return "Unknown error."
    
  1710.     Case TSNE_Const_NoError:                    Return "No error."
    
  1711.     Case TSNE_Const_UnknowEventID:              Return "Unknown EventID."
    
  1712.     Case TSNE_Const_NoSocketFound:              Return "No Socket found in 'V_SOCKET'."
    
  1713.     Case TSNE_Const_CantCreateSocket:           Return "Can't create socket."
    
  1714.     Case TSNE_Const_CantBindSocket:             Return "Can't bind port on socket."
    
  1715.     Case TSNE_Const_CantSetListening:           Return "Can't set socket into listening-mode."
    
  1716.     Case TSNE_Const_SocketAlreadyInit:          Return "Socket is already initalized."
    
  1717.     Case TSNE_Const_MaxSimConReqOutOfRange:     Return "'V_MaxSimConReq' is out of range."
    
  1718.     Case TSNE_Const_PortOutOfRange:             Return "Port out of range."
    
  1719.     Case TSNE_Const_CantResolveIPfromHost:      Return "Can't resolve IPA from host."
    
  1720.     Case TSNE_Const_CantConnectToRemote:        Return "Can't connect to remote computer [Timeout?]."
    
  1721.     Case TSNE_Const_TSNEIDnotFound:             Return "TSNE-ID not found."
    
  1722.     Case TSNE_Const_MissingEventPTR:            Return "Missing pointer of 'V_Event...'."
    
  1723.     Case TSNE_Const_IPAalreadyInList:           Return "IPA already in list."
    
  1724.     Case TSNE_Const_IPAnotInList:               Return "IPA is not in list."
    
  1725.     Case TSNE_Const_ReturnErrorInCallback:      Return "Return error in callback."
    
  1726.     Case TSNE_Const_IPAnotFound:                Return "IPA not found."
    
  1727.     Case TSNE_Const_ErrorSendingData:           Return "Error while sending data. Not sure all data transmitted. Maybe connection lost or disconnected."
    
  1728.     Case TSNE_Const_TSNENoServer:               Return "TSNEID is not a server."
    
  1729.     Case TSNE_Const_NoIPV6:                     Return "No IPV6 supported!"
    
  1730.     Case TSNE_Const_CantCreateSocketLimit:      Return "Can't create socket. No more file descriptors available for this process or the system."
    
  1731.     Case TSNE_Const_UnstableState:              Return "Unstable Thread-State!"
    
  1732.     Case TSNE_Const_InternalError:              Return "Internal Error! Please contact support/programmer!"
    
  1733.     Case Else:                                  Return "Unknown GURU-Code [" & Str(V_GURUID) & "]"
    
  1734. End Select
    
  1735. End Function
    
  1736. 
    
  1737. 
    
  1738. 
    
  1739. '##############################################################################################################
    
  1740. '...<
    
  1741. #ENDIF
    
  1742.