Home

Add

Edit

Without Linenumbers

Code in Textfield

Download

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