Buchempfehlung
Windows System Programming
Windows System Programming
Das Kompendium liefert viele interessante Informationen zur Windows-Programmierung auf Englisch. [Mehr Infos...]
FreeBASIC-Chat
Es sind Benutzer im FreeBASIC-Chat online.
(Stand:  )
FreeBASIC bei Twitter
Twitter FreeBASIC-Nachrichten jetzt auch über Twitter erhalten. Follow us!

fb:porticula NoPaste

Info
Info / Hilfe
Liste
Übersicht / Liste
Neu
Datei hochladen
Suche
Quellcode suchen
Download
Dateidownload

TSNE_V2_experimental.bi

Uploader:MitgliedThePuppetMaster
Datum/Zeit:05.06.2008 20:09:15

'#####################################################################################################
'#####################################################################################################
' TSNE_V2 - TCP Socket Networking [Eventing] Version: 2.0 (0.18.5)
'#####################################################################################################
'#####################################################################################################
' 2007 By.: /_\ DeltaLab's - Deutschland
' Autor: Martin Wiemann
'#####################################################################################################



'##############################################################################################################
#include once "crt/errno.bi"
#IF DEFINED(__FB_LINUX__)
    #INCLUDE once "crt/stdlib.bi"
    #INCLUDE once "crt/unistd.bi"
    #INCLUDE once "crt/sys/types.bi"
    #INCLUDE once "crt/sys/socket.bi"
    #INCLUDE once "crt/netinet/in.bi"
    #INCLUDE once "crt/arpa/inet.bi"
    #INCLUDE once "crt/netdb.bi"
    #INCLUDE once "crt/linux/fcntl.bi"
    #include once "crt/sys/select.bi"
    #DEFINE IOCPARM_MASK &h7f
    #DEFINE IOC_IN &h80000000
    #DEFINE _IOW(x,y,t) (IOC_IN or ((t and IOCPARM_MASK) shl 16) or ((x) shl 8) or (y))
    #DEFINE FIONBIO _IOW(asc("f"), 126, sizeof(ulong))
    #DEFINE h_addr h_addr_list[0]
    #DEFINE CloseSocket_(_a_) close_(_a_)
    #DEFINE INVALID_SOCKET (Cast(Socket, -1))
    #DEFINE TSNE_MSG_NOSIGNAL &h4000
    #DEFINE EINPROGRESS 36
#ELSEIF DEFINED(__FB_WIN32__)
    #DEFINE WIN_INCLUDEALL
    #INCLUDE once "windows.bi"
    #INCLUDE once "win\winsock.bi"
    #DEFINE close_(_a_) closesocket(_a_)
    #DEFINE memcpy(x__, y__, z__) movememory(x__, y__, z__)
    #DEFINE TSNE_MSG_NOSIGNAL &h0
    #DEFINE EINPROGRESS WSAEINPROGRESS
    SUB StartWinsock() CONSTRUCTOR
        DIM xwsa AS WSADATA
        WSAStartup(MAKEWORD(2,0),@xwsa)
    END SUB
    SUB EndWinsock DESTRUCTOR
        WSAcleanup()
    END SUB
#ELSE
    #error "Unsupported platform"
#ENDIF
#include once "crt/sys/time.bi"


'declare function _errno cdecl alias "_errno" () as integer ptr
'declare function errno cdecl lib "c" alias "_errno" () as integer
'extern errno alias "errno" as integer

Dim Shared G_Thread_Master          as Any PTR
Dim Shared G_Thread_Master_Close    as UByte
Dim Shared G_MUX_Master             as Any PTR
Declare SUB TSNE_Thread_Master()


'##############################################################################################################
Const TSNE_BufferSize   as Long = 7936


'##############################################################################################################
Enum TSNE_Event
    TSNEE_Disconnect = 0
    TSNEE_Connect = 1
    TSNEE_NewConnection = 2
    TSNEE_NewData = 3
End Enum
'--------------------------------------------------------------------------------------------------------------
Type TSNE_Event_Type
    TSNE_Disconnected               as Sub  (ByVal V_TSNEID as UInteger)
    TSNE_Connected                  as Sub  (ByVal V_TSNEID as UInteger)
    TSNE_NewConnection              as Sub  (ByVal V_TSNEID as UInteger, ByVal V_RequestID as Socket, ByVal V_IPA as String)
    TSNE_NewConnectionCanceled      as Sub  (ByVal V_TSNEID as UInteger, ByVal V_IPA as String)
    TSNE_NewData                    as Sub  (ByVal V_TSNEID as UInteger, ByRef V_Data as String)
End Type
Type TSNE_Socket
    V_Event         as TSNE_Event_Type
    V_Socket        as Socket
    V_IsServer      as Long
    V_IPA           as String
    V_Port          as USHORT
    T_DataIn        as ULongInt
    T_DataOut       as ULongInt
    T_ThreadOn      as Long
    T_Thread        as Any PTR
End Type
Dim Shared TSNED()      as TSNE_Socket
Dim Shared TSNEC        as UInteger
Dim Shared TSNEMux      as any PTR

Dim Shared TSNE_Debug   as UByte = 0

Type TSNE_BlackList_Type
    V_InUse             as UByte
    V_UseTable          as UByte
    V_ServerTSNEID      as UInteger
    V_BlockIPAD(1024)   as String
End Type
Dim Shared TSNE_BlackListD()    as TSNE_BlackList_Type
Dim Shared TSNE_BlackListC      as UInteger
Dim Shared TSNE_BlackListUse    as UByte




'##############################################################################################################
Declare Sub         Thread_Event                (ByVal V_TSNEID as UInteger)

Declare Function    TSNE_Stats                  (ByVal V_TSNEID as UInteger, ByRef B_RX as ULongInt, ByRef B_TX as ULongInt) as Long
Declare Function    TSNE_Disconnect             (ByVal V_TSNEID as UInteger) as Long
Declare Function    TSNE_Create_Server          (ByRef B_TSNEID as UInteger, ByRef V_Port as USHORT, ByRef MaxSimultanConnectionRequests as Long = 10, ByVal V_EventNewConPTR as any PTR, ByVal V_EventNewConCancelPTR as any PTR) as Long
Declare Function    TSNE_Create_Client          (ByRef B_TSNEID as UInteger, ByVal V_IPA as String, ByVal V_Port as USHORT, ByVal V_EventDisconPTR as any PTR, ByVal V_EventConPTR as any PTR, ByVal V_EventNewDataPTR as any PTR, ByVal V_TimeoutSecs as UInteger = 60) as Long
Declare Function    TSNE_Create_Accept          (ByVal V_RequestID as Socket, ByRef B_TSNEID as UInteger, ByRef B_IPA as String, ByVal V_EventDisconPTR as any PTR, ByVal V_EventConPTR as any PTR, ByVal V_EventNewDataPTR as any PTR) as Long
Declare Function    TSNE_Data_Send              (ByVal V_TSNEID as UInteger, ByRef V_Data as String) as Long
Declare Sub         TSNE_WaitClose              (ByRef V_TSNEID as UInteger)
Declare Function    TSNE_IsClosed               (ByRef V_TSNEID as UInteger) as Long

Declare Function    TSNE_BlackList_Create       (ByVal V_TSNEID as UInteger) as UInteger
Declare Sub         TSNE_BlackList_Use_Global   (V_Use as UByte)
Declare Function    TSNE_BlackList_Use          (ByVal V_TSNEID as UInteger, V_Use as UByte) as Long
Declare Function    TSNE_BlackList_Add          (ByVal V_TSNEID as UInteger, ByVal V_IPA as String) as Long
Declare Function    TSNE_BlackList_Del          (ByVal V_TSNEID as UInteger, ByVal V_IPA as String) as Long
Declare Function    TSNE_BlackList_Clear        (ByVal V_TSNEID as UInteger) as Long
Declare Function    TSNE_BlackList_Blocked      (ByVal V_TSNEID as UInteger, V_IPA as String) as Long



'##############################################################################################################
SUB TSNE_Init() CONSTRUCTOR
TSNEMux = MutexCreate
G_MUX_Master = MutexCreate
G_Thread_Master = ThreadCreate(cast(any PTR, @TSNE_Thread_Master))
END SUB


'--------------------------------------------------------------------------------------------------------------
SUB TSNE_Thread_Master()
Dim X as UInteger
Dim XTH as Any PTR
Dim XTX as ULong
Do
    XTX = 0
    MutexLock(TSNEMux)
    For X = 1 to TSNEC
        If TSNED(X).T_ThreadOn = 2 Then
            XTH = TSNED(X).T_Thread
            MutexUnLock(TSNEMux)
            ThreadWait(XTH)
            MutexLock(TSNEMux)
            TSNED(X).T_Thread = 0
            TSNED(X).T_ThreadOn = 0
            MutexUnLock(TSNEMux)
            TSNE_Disconnect(X)
            MutexLock(TSNEMux)
        Else: If TSNED(X).T_Thread = 0 Then XTX += 1
        End if
    Next
    MutexLock(G_MUX_Master)
    If G_Thread_Master_Close = 1 Then If XTX = TSNEC Then MutexUnLock(G_MUX_Master): MutexUnLock(TSNEMux): Exit Sub
    MutexUnLock(G_MUX_Master)
    MutexUnLock(TSNEMux)
    sleep 1, 1
Loop
End Sub


'--------------------------------------------------------------------------------------------------------------
SUB TSNE_Term() DESTRUCTOR
MutexLock(TSNEMux)
Dim XTID as UInteger
For X as ULong = 1 to TSNEC
    If TSNED(X).T_Thread <> 0 Then
        XTID = X
        MutexUnLock(TSNEMux)
        TSNE_Disconnect(XTID)
        MutexLock(TSNEMux)
    End if
Next
MutexUnLock(TSNEMux)
MutexLock(G_MUX_Master)
G_Thread_Master_Close = 1
MutexUnLock(G_MUX_Master)
ThreadWait(G_Thread_Master)
MutexDestroy(G_MUX_Master)
MutexDestroy(TSNEMux)
END SUB



'##############################################################################################################
Function TSNE_Stats(ByVal V_TSNEID as UInteger, ByRef B_RX as ULongInt, ByRef B_TX as ULongInt) as Long
TSNE_Stats = -1
If V_TSNEID <= 0 or V_TSNEID > TSNEC Then Return -12
MutexLock(TSNEMux)
B_RX = TSNED(V_TSNEID).T_DataIn
B_TX = TSNED(V_TSNEID).T_DataOut
MutexUnLock(TSNEMux)
Return 0
End Function



'##############################################################################################################
Function TSNE_Disconnect(ByVal V_TSNEID as UInteger) as Long
TSNE_Disconnect = -1
If V_TSNEID <= 0 or V_TSNEID > TSNEC Then Return -12
Dim TT as TSNE_Socket
MutexLock(TSNEMux)
TT = TSNED(V_TSNEID)
MutexUnLock(TSNEMux)
TSNE_BlackList_Clear(V_TSNEID)
If TT.V_Socket <> INVALID_SOCKET Then
    close_(TT.V_Socket)
    MutexLock(TSNEMux)
    TSNED(V_TSNEID).V_Socket = INVALID_SOCKET
    MutexUnLock(TSNEMux)
'   MutexLock(G_MUX_Master)
'   If G_Thread_Master_Close <> 1 Then
'       MutexUnLock(G_MUX_Master)
        If TT.V_Event.TSNE_Disconnected <> 0 Then TT.V_Event.TSNE_Disconnected(V_TSNEID)
'   Else: MutexUnLock(G_MUX_Master)
'   End If
    Return 0
Else
    MutexLock(TSNEMux)
    TSNED(V_TSNEID).V_Socket = INVALID_SOCKET
    MutexUnLock(TSNEMux)
    Return -3
End If
Return -1
End Function


'--------------------------------------------------------------------------------------------------------------
Function TSNE_Create_Server(ByRef B_TSNEID as UInteger, ByRef V_Port as USHORT, ByRef MaxSimultanConnectionRequests as Long = 10, ByVal V_EventNewConPTR as any PTR, ByVal V_EventNewConCancelPTR as any PTR) as Long
TSNE_Create_Server = -1
B_TSNEID = 0
If MaxSimultanConnectionRequests <= 0 or MaxSimultanConnectionRequests > 4096 Then Return -8
If V_Port < 0 or V_Port > 65535 Then Return -9
If V_EventNewConPTR = 0 Then  Return -13
DIM BV AS Long
Dim TSID as UInteger
Dim X as UInteger
MutexLock(TSNEMux)
For X = 1 to TSNEC
    If TSNED(X).V_Socket = INVALID_SOCKET Then TSID = X: Exit For
Next
MutexUnLock(TSNEMux)
DIM TSock AS Socket
TSock = opensocket(AF_INET, SOCK_STREAM, IPPROTO_IP)
IF TSock = INVALID_SOCKET THEN Return -4
DIM TADDR AS SOCKADDR_IN
TADDR.sin_family = AF_INET
TADDR.sin_port = htons(V_Port)
TADDR.sin_addr.s_addr = INADDR_ANY
BV = bind(TSock, CPTR(SOCKADDR PTR, @TADDR), SIZEOF(SOCKADDR_IN))
IF BV = SOCKET_ERROR THEN Return - 5
BV = listen(TSock, MaxSimultanConnectionRequests)
IF BV = SOCKET_ERROR THEN Return -6
Dim TSD as TSNE_Socket
With TSD
    .V_Socket = TSock
    .V_IPA = ""
    .V_Port = V_Port
    .V_IsServer = 1
    .T_ThreadOn = 1
    .V_Event.TSNE_NewConnection = V_EventNewConPTR
    .V_Event.TSNE_NewConnectionCanceled = V_EventNewConCancelPTR
End With
MutexLock(TSNEMux)
If TSID = 0 Then
    TSNEC += 1
    TSID = TSNEC
    Redim Preserve TSNED(TSNEC) as TSNE_Socket
End if
TSNED(TSID) = TSD
B_TSNEID = TSID
TSNED(TSID).T_Thread = ThreadCreate(cast(Any Ptr, @Thread_Event), cast(Any Ptr, B_TSNEID))
MutexUnLock(TSNEMux)
TSNE_BlackList_Create(TSID)
Return 0
End Function


'--------------------------------------------------------------------------------------------------------------
Function TSNE_Create_Client(ByRef B_TSNEID as UInteger, ByVal V_IPA as String, ByVal V_Port as USHORT, ByVal V_EventDisconPTR as any PTR, ByVal V_EventConPTR as any PTR, ByVal V_EventNewDataPTR as any PTR, ByVal V_TimeoutSecs as UInteger = 60) as Long
TSNE_Create_Client = -1
B_TSNEID = 0
If V_Port < 0 or V_Port > 65535 Then Return -9
DIM TSock AS Socket
DIM TADDR AS SOCKADDR_IN
DIM TADDRIN AS in_addr
DIM XHost AS hostent PTR
DIM BV AS Integer
Dim TSID as UInteger
Dim X as UInteger
Dim TTV AS timeval
DIM TFDSet AS fd_Set
DIM TFDSetW AS fd_Set
MutexLock(TSNEMux)
For X = 1 to TSNEC
    If TSNED(X).V_Socket = INVALID_SOCKET Then TSID = X: Exit For
Next
MutexUnLock(TSNEMux)
TADDRIN.s_addr = inet_addr(STRPTR(V_IPA))
IF (TADDRIN.s_addr = -1) THEN
    XHost = gethostbyname(STRPTR(V_IPA))
    IF XHost = 0 THEN RETURN -10
    TADDRIN = *CAST(in_addr PTR, XHost->h_addr_list[0])
    IF TADDRIN.s_addr = INADDR_NONE THEN RETURN -10
END IF
TADDR.sin_family = AF_INET
TADDR.sin_port = htons(V_Port)
TADDR.sin_addr = TADDRIN
TSock = opensocket(PF_INET, SOCK_STREAM, 0)
IF TSock = INVALID_SOCKET THEN RETURN -4
Dim XFlag as Integer = fcntl(TSock, F_GETFL, 0)
If XFlag = -1 Then Return -18
If fcntl(TSock, F_SETFL, XFlag or O_NONBLOCK) = -1 Then Return -18
BV = connect(TSock, CPTR(SOCKADDR PTR, @TADDR), SIZEOF(SOCKADDR))
If BV <> 0 Then
    #IF DEFINED(__FB_LINUX__)
        Dim XTot as Double = Timer + V_TimeoutSecs
        Do
            If connect(TSock, CPTR(SOCKADDR PTR, @TADDR), SIZEOF(SOCKADDR)) = 0 Then Exit Do
            If XTot < Timer Then Return -11
            If TSock = INVALID_SOCKET Then Return -11
            With TTV
                .tv_sec = 0
                .tv_usec = 100
            End With
            select_ 0, 0, 0, 0, @TTV
        Loop
    #ELSEIF DEFINED(__FB_WIN32__)
        With TTV
            .tv_sec = V_TimeoutSecs
            .tv_usec = 0
        End With
        If select_(TSock + 1, 0, 0, 0, @TTV) = INVALID_SOCKET Then Return -11
        If TSock = INVALID_SOCKET Then Return -11
    #ENDIF
End If
fcntl(TSock, F_SETFL, XFlag)
Dim TSD as TSNE_Socket
With TSD
    .V_Socket = TSock
    .V_IPA = V_IPA
    .V_Port = V_Port
    .V_IsServer = 0
    .T_ThreadOn = 1
    .V_Event.TSNE_Disconnected = V_EventDisconPTR
    .V_Event.TSNE_Connected = V_EventConPTR
    .V_Event.TSNE_NewData = V_EventNewDataPTR
End With
MutexLock(TSNEMux)
If TSID = 0 Then
    TSNEC += 1
    TSID = TSNEC
    Redim Preserve TSNED(TSNEC) as TSNE_Socket
End if
TSNED(TSID) = TSD
B_TSNEID = TSID
TSNED(TSID).T_Thread = ThreadCreate(cast(Any Ptr, @Thread_Event), cast(Any Ptr, B_TSNEID))
MutexUnLock(TSNEMux)
RETURN 0
End Function


'--------------------------------------------------------------------------------------------------------------
Function TSNE_Create_Accept(ByVal V_RequestID as Socket, ByRef B_TSNEID as UInteger, ByRef B_IPA as String, ByVal V_EventDisconPTR as any PTR, ByVal V_EventConPTR as any PTR, ByVal V_EventNewDataPTR as any PTR) as Long
TSNE_Create_Accept = -1
DIM TADDR AS SOCKADDR_IN
DIM XSize AS LONG = 16
Dim TSD as TSNE_Socket
Dim TSID as UInteger
Dim X as UInteger
MutexLock(TSNEMux)
For X = 1 to TSNEC
    If TSNED(X).V_Socket = INVALID_SOCKET Then TSID = X: Exit For
Next
MutexUnLock(TSNEMux)
With TSD
    .V_Socket = V_RequestID
    .V_Event.TSNE_Disconnected = V_EventDisconPTR
    .V_Event.TSNE_Connected = V_EventConPTR
    .V_Event.TSNE_NewData = V_EventNewDataPTR
    IF getpeername (V_RequestID, CAST(sockaddr PTR, @TADDR), @XSize) = 0 THEN .V_IPA = *inet_ntoa(TADDR.sin_addr)
    B_IPA = .V_IPA
    .T_ThreadOn = 1
End With
MutexLock(TSNEMux)
If TSID = 0 Then
    TSNEC += 1
    TSID = TSNEC
    Redim Preserve TSNED(TSNEC) as TSNE_Socket
End if
TSNED(TSID) = TSD
B_TSNEID = TSID
TSNED(TSID).T_Thread = ThreadCreate(cast(Any Ptr, @Thread_Event), cast(Any Ptr, B_TSNEID))
MutexUnLock(TSNEMux)
RETURN 0
End Function


'--------------------------------------------------------------------------------------------------------------
Function TSNE_Data_Send(ByVal V_TSNEID as UInteger, ByRef V_Data as String) as Long
TSNE_Data_Send = -1
If V_TSNEID <= 0 or V_TSNEID > TSNEC Then Return -12
MutexLock(TSNEMux)
If TSNED(V_TSNEID).V_Socket = INVALID_SOCKET Then
    MutexUnLock(TSNEMux)
    Return -12
End If
MutexUnLock(TSNEMux)
DIM XTemp AS STRING
DIM XSC AS LONG
DIM XLen AS LONG
DIM BV AS LONG
Dim TSock as Socket
MutexLock(TSNEMux)
TSock = TSNED(V_TSNEID).V_Socket
MutexUnLock(TSNEMux)
XTemp = V_Data
XLen = Len(XTemp)
DO
    BV = send(TSock, STRPTR(XTemp) + XSC, XLen - XSC, TSNE_MSG_NOSIGNAL)
    IF BV > 0 THEN
        XSC += BV
    ELSEIF BV = 0 THEN
    ELSE: exit do
    END IF
LOOP UNTIL XSC = XLen
MutexLock(TSNEMux)
TSNED(V_TSNEID).T_DataOut += XSC
MutexUnLock(TSNEMux)
RETURN XSC
End Function


'--------------------------------------------------------------------------------------------------------------
Sub TSNE_WaitClose(ByRef V_TSNEID as UInteger)
If V_TSNEID <= 0 or V_TSNEID > TSNEC Then Exit Sub
MutexLock(TSNEMux)
If TSNED(V_TSNEID).V_Socket = INVALID_SOCKET Then
    MutexUnLock(TSNEMux)
    Exit Sub
End If
MutexUnLock(TSNEMux)
Do
    MutexLock(TSNEMux)
    If TSNED(V_TSNEID).T_ThreadOn = 0 Then Exit Do
    MutexUnLock(TSNEMux)
    Sleep 1, 1
Loop
MutexUnLock(TSNEMux)
End Sub


'--------------------------------------------------------------------------------------------------------------
Function TSNE_IsClosed(ByRef V_TSNEID as UInteger) as Long
If V_TSNEID <= 0 or V_TSNEID > TSNEC Then Return 0
MutexLock(TSNEMux)
If TSNED(V_TSNEID).V_Socket = INVALID_SOCKET Then MutexUnLock(TSNEMux): Return 1
If TSNED(V_TSNEID).T_ThreadOn = 0 Then MutexUnLock(TSNEMux): Return 1
MutexUnLock(TSNEMux)
Return 0
End Function



'##############################################################################################################
Sub Thread_Event(ByVal V_TSNEID as UInteger)
Dim GSID as UInteger = V_TSNEID
Dim TSID as UInteger
DIM TTV AS TimeVal
DIM TFDSet AS fd_Set
Dim TSock as Socket
Dim TNSock as Socket
DIM TLenB AS INTEGER
DIM TBuffer AS ZSTRING * TSNE_BufferSize
DIM T AS STRING
Dim TT as TSNE_Socket
Dim TIPA as String
DIM TADDR AS SOCKADDR_IN
DIM XSize AS LONG = 16
Dim Y as ULong
Dim XOK as Long
TTV.tv_sec = 0
TTV.tv_usec = 0
TSID = GSID
MutexLock(TSNEMux)
TT = TSNED(V_TSNEID)
TSock = TSNED(TSID).V_Socket
MutexUnLock(TSNEMux)
Sleep 10, 1
If TT.V_IsServer <> 1 Then
    If TT.V_Event.TSNE_Connected <> 0 Then TT.V_Event.TSNE_Connected(TSID)
    Do
        TSID = GSID
        MutexLock(TSNEMux)
        If TSNED(TSID).V_Socket = INVALID_SOCKET Then MutexUnLock(TSNEMux): Exit Do
        MutexUnLock(TSNEMux)
        fd_set_(TSock, @TFDSet)
        If TSock = INVALID_SOCKET Then Exit Do
        With TTV
            .tv_sec = 1
            .tv_usec = 0
        End With
        If select_(TSock + 1, @TFDSet, 0, 0, @TTV) <> -1 Then
            IF (FD_ISSET(TSock, @TFDSet)) <> 0 THEN
                If TSock = INVALID_SOCKET Then Exit Do
                TLenB = recv(TSock, STRPTR(TBuffer), TSNE_BufferSize, 0)
                IF TLenB > 0 THEN
                    TBuffer[TLenB] = 0
                    T = STRING(TLenB + 1, Chr(0))
                    MemCpy(STRPTR(T), STRPTR(TBuffer), TLenB)
                    MutexLock(TSNEMux)
                    TSNED(TSID).T_DataIn += TLenB
                    MutexUnLock(TSNEMux)
                    T = Mid(T, 1, Len(T) - 1)
                    If TT.V_Event.TSNE_NewData <> 0 Then TT.V_Event.TSNE_NewData(TSID, T)
                Else: Exit Do
                END IF
            END IF
        ELSE: Exit Do
        END IF
    Loop
Else
    Dim XFX as ULong
    Do
        XFX += 1
        TSID = GSID
        MutexLock(TSNEMux)
        If TSNED(TSID).V_Socket = INVALID_SOCKET Then Exit Do
        MutexUnLock(TSNEMux)
        fd_set_(TSock, @TFDSet)
        With TTV
            .tv_sec = 1
            .tv_usec = 0
        End With
        IF selectsocket(TSock + 1, @TFDSet, 0, 0, @TTV) = SOCKET_ERROR THEN Exit Do
        IF (FD_ISSET(TSock, @TFDSet)) THEN
            TNSock = accept(TSock, 0, 0)
            IF TNSock = INVALID_SOCKET THEN Exit Do
            TIPA = ""
            IF getpeername (TNSock, CAST(sockaddr PTR, @TADDR), @XSize) = 0 THEN TIPA = *inet_ntoa(TADDR.sin_addr)
            MutexLock(TSNEMux)
            If TSNE_BlackListUse > 0 Then
                MutexUnLock(TSNEMux)
                XOK = TSNE_BlackList_Blocked(TSID, TIPA)
                If XOK <> 0 Then
                    If TT.V_Event.TSNE_NewConnection <> 0 Then TT.V_Event.TSNE_NewConnection(TSID, TNSock, TIPA)
                Else: close_(TNSock): If TT.V_Event.TSNE_NewConnectionCanceled <> 0 Then TT.V_Event.TSNE_NewConnectionCanceled(TSID, TIPA)
                End If
            Else
                MutexUnLock(TSNEMux)
                If TT.V_Event.TSNE_NewConnection <> 0 Then TT.V_Event.TSNE_NewConnection(TSID, TNSock, TIPA)
            End If
        End If
    Loop
End If
MutexUnLock(TSNEMux)
MutexLock(TSNEMux)
TSNED(TSID).T_ThreadOn = 2
MutexUnLock(TSNEMux)
End Sub



'##############################################################################################################
Function TSNE_BlackList_Find(ByVal V_TSNEID as UInteger) as UInteger
Dim Y as ULong
MutexLock(TSNEMux)
For Y = 1 to TSNE_BlackListC
    With TSNE_BlackListD(Y)
        If .V_UseTable = 1 Then
            If .V_ServerTSNEID = V_TSNEID Then MutexUnLock(TSNEMux): Return Y
        End If
    End With
Next
MutexUnLock(TSNEMux)
Return 0
End Function



'--------------------------------------------------------------------------------------------------------------
Function TSNE_BlackList_Create(ByVal V_TSNEID as UInteger) as UInteger
Dim Y as ULong
Dim XID as UInteger
MutexLock(TSNEMux)
For Y = 1 to TSNE_BlackListC
    With TSNE_BlackListD(Y)
        If .V_UseTable = 0 Then XID = Y: Exit For
    End With
Next
If XID = 0 Then
    TSNE_BlackListC += 1
    XID = TSNE_BlackListC
    Redim preserve TSNE_BlackListD(TSNE_BlackListC) as TSNE_BlackList_Type
End If
With TSNE_BlackListD(XID)
    .V_UseTable = 1
    .V_ServerTSNEID = V_TSNEID
    MutexUnLock(TSNEMux)
End With
MutexUnLock(TSNEMux)
Return 0
End Function



'--------------------------------------------------------------------------------------------------------------
Function TSNE_BlackList_Blocked(ByVal V_TSNEID as UInteger, V_IPA as String) as Long
TSNE_BlackList_Blocked = -1
Dim XBLID as UInteger = TSNE_BlackList_Find(V_TSNEID)
If XBLID <= 0 Then Return -17
Dim X as UInteger
MutexLock(TSNEMux)
With TSNE_BlackListD(XBLID)
    For X = 1 to 1024
        If .V_BlockIPAD(X) = V_IPA Then MutexUnLock(TSNEMux): Return 0
    Next
End With
MutexUnLock(TSNEMux)
Return - 15
End Function



'--------------------------------------------------------------------------------------------------------------
Sub TSNE_BlackList_Use_Global(V_Use as UByte)
TSNE_BlackListUse = V_Use
End Sub



'--------------------------------------------------------------------------------------------------------------
Function TSNE_BlackList_Clear(ByVal V_TSNEID as UInteger) as Long
TSNE_BlackList_Clear = -1
Dim XBLID as UInteger = TSNE_BlackList_Find(V_TSNEID)
If XBLID <= 0 Then Return -17
Dim X as UInteger
MutexLock(TSNEMux)
With TSNE_BlackListD(XBLID)
    .V_UseTable = 0
    For X = 1 to 1024
        .V_BlockIPAD(X) = ""
    Next
End With
MutexUnLock(TSNEMux)
Return 0
End Function



'--------------------------------------------------------------------------------------------------------------
Function TSNE_BlackList_Use(ByVal V_TSNEID as UInteger, V_Use as UByte) as Long
TSNE_BlackList_Use = -1
Dim XBLID as UInteger = TSNE_BlackList_Find(V_TSNEID)
If XBLID <= 0 Then Return -17
MutexLock(TSNEMux)
TSNE_BlackListD(XBLID).V_UseTable = V_Use
MutexUnLock(TSNEMux)
Return 0
End Function



'--------------------------------------------------------------------------------------------------------------
Function TSNE_BlackList_Add(ByVal V_TSNEID as UInteger, ByVal V_IPA as String) as Long
TSNE_BlackList_Add = -1
Dim XBLID as UInteger = TSNE_BlackList_Find(V_TSNEID)
If XBLID <= 0 Then Return -17
Dim X as UInteger
Dim XID as UInteger
MutexLock(TSNEMux)
With TSNE_BlackListD(XBLID)
    For X = 1 to 1024
        If .V_BlockIPAD(X) = V_IPA Then
            MutexUnLock(TSNEMux): Return -14
        Else: If XID = 0 Then XID = X
        End If
    Next
    If XID = 0 Then
        For X = 1 to 1024
            If .V_BlockIPAD(X) = "" Then XID = X: Exit For
        Next
    End If
    If XID > 0 Then .V_BlockIPAD(XID) = V_IPA: MutexUnLock(TSNEMux): Return 0
End With
MutexUnLock(TSNEMux)
Return -16
End Function



'--------------------------------------------------------------------------------------------------------------
Function TSNE_BlackList_Del(ByVal V_TSNEID as UInteger, ByVal V_IPA as String) as Long
TSNE_BlackList_Del = -1
Dim XBLID as UInteger = TSNE_BlackList_Find(V_TSNEID)
If XBLID <= 0 Then Return -17
Dim X as UInteger
MutexLock(TSNEMux)
With TSNE_BlackListD(XBLID)
    For X = 1 to 1024
        If .V_BlockIPAD(X) = V_IPA Then .V_BlockIPAD(X) = "": MutexUnLock(TSNEMux): Return 0
    Next
End With
MutexUnLock(TSNEMux)
Return -15
End Function



'##############################################################################################################
Function TSNE_GetGURUCode(ByRef V_GURUID as Long) as String
Select Case V_GURUID
    case 0: Return "No error"

    case -1: Return "Unknow error in function"

    case -2: Return "Unknow EVENT-ID"
    case -3: Return "No Socket found in V_SOCKET"
    case -4: Return "Can't create socket"
    case -5: Return "Can't bind port on socket"
    case -6: Return "Can't set socket into listening-mode"
    case -7: Return "Socket is already initalized"
    case -8: Return "MaxSimultanConnectionRequests out of valid range"
    case -9: Return "Port out of valid range"
    case -10: Return "Can't resolve IP from Host"
    case -11: Return "Can't connect to remote computer [TimeOut]"
    case -12: Return "TSNE-ID not found"
    case -13: Return "Missing pointer of V_EventNewConPTR callback"
    case -14: Return "IPA alright in BlackList"
    case -15: Return "IPA not in BlackList"
    case -16: Return "BlackList is full"
    case -17: Return "TSNE-ID not found in BlackList"
    case -18: Return "Error in return of internal calling!"

    case else: Return "Unknow GURU-Code"
End Select
End Function