fb:porticula NoPaste
TSNE.bi
Uploader: | ThePuppetMaster |
Datum/Zeit: | 06.12.2007 12:34:06 |
'#####################################################################################################
'#####################################################################################################
' TSNE - TCP Socket Networking [Eventing]
'#####################################################################################################
'#####################################################################################################
' 2007 By.: /_\ DeltaLab's - Deutschland
' Autor: Martin Wiemann
'#####################################################################################################
'##############################################################################################################
#IFDEF __FB_LINUX__
#INCLUDE "crt/stdlib.bi"
#INCLUDE "crt/unistd.bi"
#INCLUDE "crt/sys/types.bi"
#INCLUDE "crt/sys/socket.bi"
#INCLUDE "crt/netinet/in.bi"
#INCLUDE "crt/arpa/inet.bi"
#INCLUDE "crt/netdb.bi"
#DEFINE h_addr h_addr_list[0]
#DEFINE CloseSocket_(_a_) close_(_a_)
#DEFINE INVALID_SOCKET (Cast(Socket, -1))
#ELSE
#DEFINE WIN_INCLUDEALL
#INCLUDE "windows.bi"
#INCLUDE "win\winsock.bi"
#DEFINE close_(_a_) closesocket(_a_)
#DEFINE memcpy(x__, y__, z__) movememory(x__, y__, z__)
SUB StartWinsock() CONSTRUCTOR
DIM wsa AS WSADATA
WSAStartup(MAKEWORD(2,0),@wsa)
END SUB
SUB EndWinsock DESTRUCTOR
WSAcleanup()
END SUB
#Endif
Const TSNE_BufferSize as Long = 4096
'##############################################################################################################
Type TSNE_Socket
V_Socket as Socket
V_IsServer as Long
V_AutoAccept as Long
V_IPA as String
V_Port as UInteger
T_DataIn as ULongInt
T_DataOut as ULongInt
T_ThreadOn as Long
T_InUse as Long
End Type
'##############################################################################################################
Enum TSNE_Event
TSNEE_Disconnect = 0
TSNEE_Connect = 1
TSNEE_NewConnection = 2
TSNEE_NewData = 3
TSNEE_SendSuccess = 4
End Enum
'--------------------------------------------------------------------------------------------------------------
Type TSNE_Event_Type
TSNE_Disconnected as Sub (ByRef V_Socket as TSNE_Socket)
TSNE_Connected as Sub (ByRef V_Socket as TSNE_Socket)
TSNE_NewConnection as Sub (ByRef V_Socket as TSNE_Socket, ByRef V_RequestID as Socket)
TSNE_NewData as Sub (ByRef V_Socket as TSNE_Socket, ByRef V_Data as String)
TSNE_SendSuccess as Sub (ByRef V_Socket as TSNE_Socket, ByRef V_Bytes as Long)
End Type
Dim Shared TSNEEvent as TSNE_Event_Type
Declare Sub Thread_Event(V_Socket as TSNE_Socket PTR)
Declare Sub TSNE_Event_Set (ByVal V_EventDisconPTR as any PTR, ByVal V_EventConPTR as any PTR, ByVal V_EventNewConPTR as any PTR, ByVal V_EventNewDataPTR as any PTR, ByVal V_EventSendSucPTR as any PTR)
Declare Function TSNE_Event_SetSingle (ByRef V_Event as TSNE_Event, ByVal V_EventPTR as any PTR) as Long
Declare Function TSNE_Disconnect (ByRef V_Socket as TSNE_Socket) as Long
Declare Function TSNE_Create_Server (ByRef B_Socket as TSNE_Socket, ByRef V_Port as UInteger, ByRef MaxSimultanConnectionRequests as Long = 10, ByRef V_AutoAccept as Long = 0) as Long
Declare Function TSNE_Create_Client (ByRef B_Socket as TSNE_Socket, V_IPA as String, V_Port as UInteger) as Long
Declare Function TSNE_Create_Accept (ByRef V_RequestID as Socket, ByRef B_Socket as TSNE_Socket) as Long
Declare Function TSNE_Data_Send (ByRef V_Socket as TSNE_Socket, ByRef V_Data as String) as Long
Declare Sub TSNE_WaitClose (ByRef V_Socket as TSNE_Socket)
Declare Function TSNE_GetGURUCode (ByRef V_GURUID as Long) as String
'##############################################################################################################
Sub TSNE_Event_Set(ByVal V_EventDisconPTR as any PTR, ByVal V_EventConPTR as any PTR, ByVal V_EventNewConPTR as any PTR, ByVal V_EventNewDataPTR as any PTR, ByVal V_EventSendSucPTR as any PTR)
With TSNEEvent
.TSNE_Disconnected = V_EventDisconPTR
.TSNE_Connected = V_EventConPTR
.TSNE_NewConnection = V_EventNewConPTR
.TSNE_NewData = V_EventNewDataPTR
.TSNE_SendSuccess = V_EventSendSucPTR
End With
End Sub
'--------------------------------------------------------------------------------------------------------------
Function TSNE_Event_SetSingle(ByRef V_Event as TSNE_Event, ByVal V_EventPTR as any PTR) as Long
TSNE_Event_SetSingle = -1
Select Case V_Event
case TSNEE_Disconnect: TSNEEvent.TSNE_Disconnected = @V_EventPTR
case TSNEE_Connect: TSNEEvent.TSNE_Connected = @V_EventPTR
case TSNEE_NewConnection: TSNEEvent.TSNE_NewConnection = @V_EventPTR
case TSNEE_NewData: TSNEEvent.TSNE_NewData = @V_EventPTR
case TSNEE_SendSuccess: TSNEEvent.TSNE_SendSuccess = @V_EventPTR
case else: Return -2
End Select
Return 0
End Function
'##############################################################################################################
Function TSNE_Disconnect(ByRef V_Socket as TSNE_Socket) as Long
TSNE_Disconnect = -1
Dim TSock as TSNE_Socket
TSock.V_Socket = INVALID_SOCKET
If V_Socket.V_Socket <> INVALID_SOCKET Then
close_(V_Socket.V_Socket)
V_Socket.V_Socket = INVALID_SOCKET
If TSNEEvent.TSNE_Disconnected <> 0 Then
Dim TSocket as TSNE_Socket
TSocket = V_Socket
TSNEEvent.TSNE_Disconnected(TSocket)
End If
V_Socket = TSock
Return 0
Else
V_Socket = TSock
Return -3
End If
Return -1
End Function
'--------------------------------------------------------------------------------------------------------------
Function TSNE_Create_Server(ByRef B_Socket as TSNE_Socket, ByRef V_Port as UInteger, ByRef MaxSimultanConnectionRequests as Long = 10, ByRef V_AutoAccept as Long = 0) as Long
TSNE_Create_Server = -1
If B_Socket.V_Socket <> 0 and B_Socket.V_Socket <> INVALID_SOCKET then Return -7
Dim TSocket as TSNE_Socket
TSocket.V_Socket = INVALID_SOCKET
B_Socket = TSocket
If MaxSimultanConnectionRequests <= 0 or MaxSimultanConnectionRequests > 4096 Then Return -8
If V_Port < 0 or V_Port > 65535 Then Return -9
DIM BV AS LONG
DIM TADDR AS SOCKADDR_IN
DIM TSock AS Socket
TSock = opensocket(AF_INET, SOCK_STREAM, IPPROTO_IP)
IF TSock = INVALID_SOCKET THEN Return -4
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
With B_Socket
.V_Socket = TSock
.V_IPA = ""
.V_Port = V_Port
.V_IsServer = 1
.V_AutoAccept = V_AutoAccept
End With
B_Socket.T_ThreadOn = 1
ThreadCreate(cast(Any Ptr, @Thread_Event), @B_Socket)
Return 0
End Function
'--------------------------------------------------------------------------------------------------------------
Function TSNE_Create_Client(ByRef B_Socket as TSNE_Socket, V_IPA as String, V_Port as UInteger) as Long
TSNE_Create_Client = -1
If B_Socket.V_Socket <> 0 and B_Socket.V_Socket <> INVALID_SOCKET then Return -7
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 UINTEGER
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
BV = connect(TSock, CPTR(SOCKADDR PTR,@TADDR), SIZEOF(SOCKADDR))
IF BV = SOCKET_ERROR THEN RETURN -11
With B_Socket
.V_Socket = TSock
.V_IPA = V_IPA
.V_Port = V_Port
.V_IsServer = 0
.V_AutoAccept = 0
End With
B_Socket.T_ThreadOn = 1
ThreadCreate(cast(Any Ptr, @Thread_Event), @B_Socket)
RETURN 0
End Function
'--------------------------------------------------------------------------------------------------------------
Function TSNE_Create_Accept(ByRef V_RequestID as Socket, ByRef B_Socket as TSNE_Socket) as Long
TSNE_Create_Accept = -1
Dim TSNEC as TSNE_Socket
B_Socket = TSNEC
B_Socket.V_Socket = V_RequestID
'B_Socket.V_Port = TSNE.V_Port
DIM TADDR AS SOCKADDR_IN
DIM XSize AS LONG = 16
IF getpeername (V_RequestID, CAST(sockaddr PTR, @TADDR), @XSize) = 0 THEN
B_Socket.V_IPA = *inet_ntoa(TADDR.sin_addr)
END IF
ThreadCreate(cast(Any Ptr, @Thread_Event), @B_Socket)
RETURN 0
End Function
'--------------------------------------------------------------------------------------------------------------
Function TSNE_Data_Send(ByRef V_Socket as TSNE_Socket, ByRef V_Data as String) as Long
TSNE_Data_Send = -1
DIM XTemp AS STRING
DIM XSC AS LONG
DIM XLen AS LONG
DIM BV AS LONG
Dim TSock as Socket
TSock = V_Socket.V_Socket
XTemp = V_Data
XLen = LEN(XTemp)
DO
BV = send(TSock, STRPTR(XTemp) + XSC, XLen - XSC, MSG_NOSIGNAL)
IF BV > 0 THEN
XSC += BV
ELSEIF BV = 0 THEN
ELSE
END IF
LOOP UNTIL XSC = XLen
RETURN XSC
End Function
'--------------------------------------------------------------------------------------------------------------
Sub TSNE_WaitClose(ByRef V_Socket as TSNE_Socket)
Do
If V_Socket.T_ThreadOn = 0 Then Exit Do
Loop
Sleep 1
End Sub
'##############################################################################################################
Sub Thread_Event(V_Socket as TSNE_Socket PTR)
Dim TSNE as TSNE_Socket = *V_Socket
DIM TTV AS TimeVal
DIM TFDSet AS fd_Set
Dim TSock as Socket
Dim TNSock as Socket
DIM TLenB AS UINTEGER
DIM TBuffer AS ZSTRING * TSNE_BufferSize
DIM T AS STRING
TTV.tv_sec = 0
TTV.tv_usec = 0
TSock = TSNE.V_Socket
If TSNE.V_IsServer <> 1 Then TSNEEvent.TSNE_Connected(TSNE)
If TSNE.V_IsServer <> 1 Then
Do
If V_Socket->V_Socket = INVALID_SOCKET Then Exit Do
fd_set_(TSock, @TFDSet)
If select_(TSock + 1, @TFDSet, 0, 0, @TTV) <> -1 Then
IF (FD_ISSET(TSock, @TFDSet)) <> 0 THEN
TLenB = recv(TSock, STRPTR(TBuffer), TSNE_BufferSize, 0)
IF TLenB <= 0 THEN Exit Do
IF TLenB > 0 THEN
TBuffer[TLenB] = 0
T = STRING(TLenB + 1, " ")
MemCpy(STRPTR(T), STRPTR(TBuffer), TLenB)
V_Socket->T_DataIn += TLenB
TSNEEvent.TSNE_NewData(TSNE, T)
END IF
END IF
ELSE: Exit Do
END IF
Loop
Else
Do
If V_Socket->V_Socket = INVALID_SOCKET Then Exit Do
fd_set_(TSock, @TFDSet)
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
TSNEEvent.TSNE_NewConnection(TSNE, TNSock)
End If
Loop
End If
TSNE_Disconnect(TSNE)
V_Socket->T_ThreadOn = 0
TSNE.V_Socket = INVALID_SOCKET
End Sub
'##############################################################################################################
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"
case else: Return "Unknow GURU-Code"
End Select
End Function