Private Sub ws_public_Close(index As Integer)
Dim mRemoteIP As String
mRemoteIP = ws_public(index).RemoteHostIP
ws_public(index).Tag = Now
ws_public(index).Close
Call writelog("connection " & index & ", " & mRemoteIP & " closed by remote user," & ws_public(index).state)
writelog ""
End Sub
Private Sub ws_public_ConnectionRequest(index As Integer, ByVal requestID As Long)
Dim x As Integer, free_socket As Integer
On Error GoTo jmp99
free_socket = -1
If index = 0 Then
If ws_public.UBound = 0 Then
GoTo jmp20
End If
' // find a free control
For x = 1 To ws_public.UBound
If ws_public(x).state <> sckClosed Then
writelog "socket " & x & " " & sckstatus(ws_public(x).state) & ", IP " & ws_public(x).RemoteHostIP
Else
writelog "socket " & x & " " & sckstatus(ws_public(x).state)
End If
' // kill the socket, it has been open for too long with no data
If ws_public(x).state = sckConnected Then
If Abs(DateDiff("n", Now, CDate(ws_public(x).Tag))) >= 2 Then
writelog "killing socket " & x & " " & ws_public(x).Tag & ", " & Now & ", open for too long without receiving data."
ws_public(x).Close
ws_public(x).Tag = ""
End If
End If
If ws_public(x).state = sckClosed Then
GoTo jmp00
Else
GoTo jmp10
End If
jmp00: ' // got a free connection
free_socket = x
Exit For
jmp10:
Next x
jmp20:
If free_socket > 0 Then
' // process the new connection
'ws_public(free_socket).Close
ws_public(free_socket).LocalPort = 0
ws_public(free_socket).Accept requestID
ws_public(free_socket).Tag = Now
Call writelog("session " & requestID & " granted access on " & free_socket & " - " & ws_public(free_socket).RemoteHostIP)
Else
free_socket = ws_public.UBound + 1
' // new socket
Load ws_public(free_socket)
GoTo jmp20
End If
End If
If free_socket > UBound(appSettings.UCONN) Then
ReDim Preserve appSettings.UCONN(free_socket)
End If
' // init buffer
appSettings.UCONN(free_socket).buffer = ""
On Error GoTo 0
Exit Sub
jmp99:
If err.Number <> 0 Then
writelog "ws_public_ConnectionRequest() error - " & err.Description & ", " & err.Number
End If
End Sub
Private Sub ws_public_DataArrival(index As Integer, ByVal bytesTotal As Long)
Dim bdata As String, x As Integer
jmp00:
'ws_public(Index).GetData bdata, , bytesTotal
ws_public(index).PeekData bdata, , bytesTotal
ws_public(index).Tag = Now
' // look for termintor, a full packet
x = InStr(1, bdata, PKT_TERM)
If x > 0 Then
ws_public(index).GetData bdata, , x + (Len(PKT_TERM) - 1)
writelog "DATA=>" & bdata & "<= " & ws_public(index).RemoteHostIP & ":" & ws_public(index).RemotePort
appSettings.public_buffer = appSettings.public_buffer & bdata
ws_public(index).SendData PKT_ACK
GoTo jmp00
End If
End Sub
Private Sub ws_public_Error(index As Integer, ByVal Number As Integer, Description As String, ByVal Scode As Long, ByVal Source As String, ByVal HelpFile As String, ByVal HelpContext As Long, CancelDisplay As Boolean)
ws_public(index).Close
writelog "ws_public_Error() " & Number & "," & Description
End Sub
Private Sub ws_public_SendComplete(index As Integer)
writelog (index & ", send complete")
End Sub