Am I to understand the PB Documentation in 8.04 that THREAD STATUS will return if the thread is still running regardless that the thread was closed?
I'm thinking it's NOT in my best interest to close the thread until I've done a thread status and ensured it's complete. We had that conversation in the internet forum.....
Found my solution to handling the connection in a separate thread though, and it's far faster...
Reason I believe this is that my lil mini webserver may get bogged down and take more time in the event say, 200 people connected....There's also preventing a DOS attack on it...
I open a thread, receive a header, send a header and a page and close....
But I want to put a bandwidth limitation in of say 250 connections....so if Thread Status returns that a thread still has &H103 status (STill running) then I want to subtract the available threads, going to make that a user definable option starting at say 100....
Unless anyone has input on limiting connections?
I'm thinking it's NOT in my best interest to close the thread until I've done a thread status and ensured it's complete. We had that conversation in the internet forum.....
Found my solution to handling the connection in a separate thread though, and it's far faster...
Reason I believe this is that my lil mini webserver may get bogged down and take more time in the event say, 200 people connected....There's also preventing a DOS attack on it...
I open a thread, receive a header, send a header and a page and close....
But I want to put a bandwidth limitation in of say 250 connections....so if Thread Status returns that a thread still has &H103 status (STill running) then I want to subtract the available threads, going to make that a user definable option starting at say 100....
Unless anyone has input on limiting connections?
Code:
%MAXTHREADS = 100 'Concurrent connections %THREADALIVE = &H103 Case %TCP_ACCEPT Select Case Lo(Word, CbLParam) Case %FD_ACCEPT 'Create the new thread here Incr tcpCount Control Set Text CbHndl,%IDCONNLABELTL, Format$(tcpCount,"") Control Set Focus CbHndl, %IDLABEL1 hTcp = FreeFile Tcp Accept nServer As hTcp Tcp Notify hTcp, Recv Close To CbHndl As %TCP_HTML Function = %TRUE End Select Case %TCP_HTML Select Case Lo(Word, CbLParam) Case %FD_READ If hTcp <> %INVALID_SOCKET Then 'Grab client IP and insert here lHost = RemoteIp(ByVal hTcp) End If hThread(tcpCount) = SendDataThread(ByVal hTcp,lHost) hTcp = %INVALID_SOCKET 'This may be too soon on a slow connection...maybe poll it at 60 seconds? Case %FD_CLOSE Tcp Close hTcp hTcp = %INVALID_SOCKET End Select Case %WM_TIMER Select Case CbWParam Case %IDT_TIMER1 g_BBSDown = LoadHtmlPage() g_BBSDownImage = LoadImageFile() sTmp = BBSDown.szMine & " " & BBSDown.szVer & " - " & BBSDown.szCopyrite SendMessage BBSDown.hStatus, %WM_SETTEXT, 0, StrPtr(sTmp) For lLoop = 1 To %MAXTHREADS Thread Status hThread(lLoop) To lResult If lResult = %THREADALIVE Then Decr ThreadsAvailable End If Next -------------------------------------------------------------------------------------------- '========================================================================================== 'THREAD FOR Sending Data Function SendDataThread(ByVal hTcp As Long,lHost As String) As Long Local hThread As Long Local lResult As Long Thread Create SendDataThreadProc(ByVal hTcp) To hThread Thread Close hThread To lResult hThread = %FALSE End Function '========================================================================================== Function SendDataThreadProc(ByVal hTcp As Long) As Long Local lResult As Long Local lLoop As Long Local lCount As Long Local ErrType As Long Local Header As String Local sBuffer As String Local sPacket As String Local sTmp As String Local lHost As String lHost = RemoteIp(ByVal hTcp) sTmp = TimeStamp() & " Connection established from: " & lHost lResult = SendMessage(BBSDown.txtHandle, %LB_ADDSTRING, 0, StrPtr(sTmp)) lResult = SendMessage(BBSDown.txtHandle, %LVM_GETITEMCOUNT, 0, 0) lResult = SendMessage(BBSDown.txtHandle, %LVM_SETITEM, 0, lResult) ' Perform a receive-loop until there is no data left (ie, the end of stream) sBuffer = "" sPacket = "" Do Tcp Recv hTcp, 1024, sBuffer sPacket = sPacket & sBuffer If Len(sPacket) > 4096 Then Exit Do 'Buffer overflow prevention ' ErrType = GetLastError() Loop Until sBuffer = "" Or IsTrue Eof(hTcp) Or IsTrue Err sPacket = Left$(sPacket,Len(sPacket)-2) lResult = ParseAndUpdateLogfile(ByVal sPacket,ByVal hTcp) 'Updates window ' <-------------Send data and disconnect-------------> Header = "HTTP/1.1 200" & $CrLf Header = Header & "Server: " & BBSDown.szMine & $CrLf Header = Header & "Date: " & GetPCTimeandDate() & $CrLf Header = Header & "Cache-Control: no-cache" & $CrLf Header = Header & "Connection: close" & $CrLf If IsFalse lResult Then 'html Header = Header & "Content-Type: text/html" & $CrLf Header = Header & "Content-Length: " & Format$(Len(g_BBSDown)) & $CrLf Tcp Send hTcp, Header & $CrLf Tcp Send hTcp, g_BBSDown Else 'jpg Header = Header & "Content-Type: image/jpeg" & $CrLf Header = Header & "Content-Length: " & Format$(Len(g_BBSDownImage)) & $CrLf Header = Header & "Accept-range: bytes" & $CrLf 'or something like that. Tcp Send hTcp, Header & $CrLf Tcp Send hTcp, g_BBSDownImage End If Tcp Close hTcp ' <-------------Send data and disconnect-------------> 'End connection here ErrType = GetLastError() If ErrType <> %ERROR_SUCCESS Then ' Control Get Text CbHndl,%IDTEXT1 To sTmp If Not IsFalse Len(sTmp) Then sTmp = sTmp & $CrLf & TimeStamp() & " Error: " & Format$(ErrType) & " - " & GetLastErrorDescription(ErrType)' & $CrLf & "Remote IP: " & lHost Else sTmp = TimeStamp() & " Error: " & Format$(ErrType) & " - " & GetLastErrorDescription(ErrType)' & $CrLf & "Remote IP: " & lHost End If 'Write sTmp SendMessage(BBSDown.txtHandle, %LB_ADDSTRING, 0, StrPtr(sTmp)) End If End Function '==========================================================================================
Comment