• Welcome, Guest. Please login.
 
September 17, 2019, 10:01:59 am

News:

Welcome to the SQLitening support forums!


AcceptConnection - Please test

Started by cj, November 23, 2015, 03:58:57 pm

Previous topic - Next topic

cj

November 23, 2015, 03:58:57 pm Last Edit: November 23, 2015, 07:11:13 pm by cj
1. Critical section added to protect globals
2. Values passed to ConnectionMain in a TYPE using an event to eliminate race condition SLEEP 5.

If anyone tests this code, please post you have. 
Thank you
Please see other suggestions at
http://www.sqlitening.com/support/index.php?topic=9422.msg24922;topicseen#msg24922

SQLiteningServer.bas
%UseEvent = 1 'at top of SQLiteningServer.bas to allow undo by simply remarking this line

TYPE Listeners_TYPE
   lTcpFileNumber AS LONG        'The PB filenumber associated with socket
   hSocket AS DWORD              'The operating system file handle.
   hEventReady AS LONG           'Michael Mattias suggestion instead of sleep  4/24/14
END TYPE


SUB AcceptConnection(BYVAL rhSocket AS DWORD)

   LOCAL llIX AS LONG
   LOCAL llTcpFileNumber AS LONG
   LOCAL lqA AS QUAD
   LOCAL lhA AS DWORD

   ' Someone is attempting to connect to the server so
   ' determine which socket this is coming in on.

   EnterCriticalSection guCriticalSection

   IF UBOUND(guaListeners) > -1 THEN ARRAY SCAN guaListeners(), FROM 5 TO 8, = MKDWD$(rhSocket), TO llIX
   IF ISFALSE llIX THEN
      LogIt 5, "Connect can not find socket"
      LeaveCriticalSection guCriticalSection
      EXIT SUB
   END IF

   ' Now we know which socket the connect request is coming
   ' in on and which one we should assign it to,
   ' so accept the connection and specify the notify events
   llTcpFileNumber = FREEFILE
   TCP ACCEPT guaListeners(llIX).lTcpFileNumber AS llTcpFileNumber

   ' Create thread to handle connection and then close
   ' its handle cause we do not need it

   #IF NOT %DEF(%UseEvent)    lqA = MAK(QUAD, llTcpFileNumber, guaListeners(llIX).hSocket)
    LeaveCriticalSection guCriticalSection
    THREAD CREATE ConnectionMain(VARPTR(lqA)) TO lhA
    ' Must sleep a bit so thread will start and get the passed parms.
    'sleep 5  'still leaving this in
    SLEEP 5

   #ELSE
    LOCAL L AS Listeners_type
    LOCAL passthis AS LONG
    L.ltcpfileNumber = llTcpFileNumber
    L.hSocket        = guaListeners(llIX).hSocket
    LeaveCriticalSection guCriticalSection

    L.hEventReady    =  CreateEvent (BYVAL 0, BYVAL %TRUE, BYVAL %FALSE, BYVAL 0)
    passthis = VARPTR(L)
    THREAD CREATE ConnectionMain(passthis) TO lhA  'already is a VARPTR
    WaitForSingleObject L.hEventReady,%INFINITE
    CloseHandle L.hEventReady
   #ENDIF

   THREAD CLOSE lhA TO lhA
END SUB             

D. Wilson

CJ Do I also need to include the changes to the sqlitengclient.dll when testing this or does this eliminate the need to update the sqliteningclient.dll.

cj

November 23, 2015, 05:39:56 pm #2 Last Edit: November 24, 2015, 05:57:05 am by cj
This update is optional, the other is required.
Test program

#DIM ALL
$LocalIP = "192.168.1.2"
%PortNumber = 51234
%Threads = 5
%ConnectionsPerThread =3
#INCLUDE "win32api.inc"
#INCLUDE "sqlitening.inc"
'
GLOBAL gs AS STRING
'
FUNCTION PBMAIN AS LONG 'test3.bas
  DIM hThread(1 TO %Threads) AS LONG, x AS LONG
  FOR x = 1 TO %Threads:THREAD CREATE MyThread(x) TO hThread(x):SLEEP 50:NEXT
  WaitforMultipleObjects UBOUND(hThread),hThread(1),%TRUE,%INFINITE
  FOR x = 1 TO %Threads:THREAD CLOSE hThread(x) TO hThread(x):NEXT
  ? gs,,"DisconnectWhenDone"
END FUNCTION
'
THREAD FUNCTION MyThread(BYVAL dummy AS LONG) AS LONG
  LOCAL x AS LONG, s() AS STRING
  LOCAL s AS STRING
  FOR x = 1 TO %ConnectionsPerThread
    slconnect $LocalIP,%PortNumber
    s+=USING$("connected thread # connection #",dummy,x) + $CR
    slDisconnect
NEXT
UpdateMessage "Values in thread" + STR$(dummy) + $CR +  s
END FUNCTION

SUB UpdateMessage(s AS STRING) THREADSAFE
  gs+=s
END SUB