Home

Add

Edit

Without Linenumbers

Code in Textfield

Download

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