Advertisement
deseven

ntest3 main loop

Apr 12th, 2015
813
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ; main loop
  2. Repeat
  3.   ev = NetworkServerEvent(bindPort)
  4.   If ev
  5.     clientID = EventClient()
  6.     Select ev
  7.       Case #PB_NetworkEvent_Connect
  8.         AddElement(clients()) ; adding new client to our list
  9.         clients()\clientID = clientID
  10.         clients()\time = Date()
  11.         clients()\clientIP = GetClientIP(clientID)
  12.         clients()\clientPort = GetClientPort(clientID)
  13.         clientSessID.s = Str(clientID + clientIP + clientPort + ElapsedMilliseconds())
  14.         clients()\clientSessID = MD5Fingerprint(@clientSessID,StringByteLength(clientSessID))
  15.         toLog("[" + clients()\clientSessID + "] new connection from " + IPString(clients()\clientIP))
  16.       Case #PB_NetworkEvent_Data
  17.         ForEach clients()
  18.           If clientID = clients()\clientID
  19.             clients()\time = Date()
  20.             *buffer = AllocateMemory(#rec_buffer)
  21.             ReceiveNetworkData(clientID,*buffer,#rec_buffer)
  22.             request.s = PeekS(*buffer)
  23.             toLog("got new request " + request)
  24.             FreeMemory(*buffer)
  25.             If FindString(request,#nt_hi) = 1 ; the client should send a correct greeting and a version info
  26.               clientVer.s = StringField(request,2,#nt_delim)
  27.               If Len(clientVer) > 0
  28.                 clients()\clientVer = clientVer
  29.                 workerPort.i = takePort()
  30.                 If workerPort
  31.                   AddElement(workers())
  32.                   workers()\port = workerPort
  33.                   workers()\clientIP = clients()\clientIP
  34.                   workers()\sessionID = clients()\clientSessID
  35.                   workers()\time = Date()
  36.                   *worker.worker = AllocateMemory(SizeOf(worker))
  37.                   *worker\address = bindAddress
  38.                   *worker\clientIP = workers()\clientIP
  39.                   *worker\port = workers()\port
  40.                   *worker\sessionID = workers()\sessionID
  41.                   If bindWorker(*worker\port)
  42.                     workers()\tid = CreateThread(@workerThread(),*worker)
  43.                     Delay(#worker_startup_sleep) ; give some time to the new worker
  44.                     toLog("[" + workers()\sessionID + "] [" + clients()\clientVer + "] spawned new worker on port " + Str(workers()\port) + ", tid " + Str(workers()\tid))
  45.                     SendNetworkString(clientID,#nt_conn + #nt_delim + Str(workers()\address) + #nt_delim + Str(workers()\port) + #nt_delim + workers()\sessionID)
  46.                     CloseNetworkConnection(clientID)
  47.                     DeleteElement(clients())
  48.                   Else
  49.                     toLog("can't bind new worker to port " + Str(workers()\port),#log_error)
  50.                     SendNetworkString(clientID,#nt_wait)
  51.                   EndIf
  52.                 Else
  53.                   SendNetworkString(clientID,#nt_wait)
  54.                 EndIf
  55.               Else
  56.                 CloseNetworkConnection(clientID)
  57.                 toLog("[" + clients()\clientSessID + "] diconnected: unknown request",#log_warn)
  58.                 DeleteElement(clients())
  59.               EndIf
  60.             Else
  61.               CloseNetworkConnection(clientID)
  62.               toLog("[" + clients()\clientSessID + "] diconnected: bad client",#log_warn)
  63.             EndIf
  64.             Break
  65.           EndIf
  66.         Next
  67.       Case #PB_NetworkEvent_Disconnect
  68.         ForEach clients()
  69.           If clientID = clients()\clientID
  70.             toLog("[" + clients()\clientSessID + "] diconnected",#log_warn)
  71.             DeleteElement(clients())
  72.             Break
  73.           EndIf
  74.         Next
  75.     EndSelect
  76.   Else
  77.     Delay(#default_sleep_time) ; there is no network activity, time to sleep
  78.   EndIf
  79.   ForEach workers()
  80.     If IsThread(workers()\tid)
  81.       If Date() - workers()\time > #worker_forced_timeout
  82.         KillThread(workers()\tid)
  83.         toLog("[" + workers()\sessionID + "] killed worker with tid " + Str(workers()\tid) + " due to timeout",#log_warn)
  84.         unbindWorker(workers()\port)
  85.         freePort(workers()\port)
  86.         LockMutex(workerResLock)
  87.         ForEach workersRes()
  88.           If workers()\sessionID = workersRes()\sessionID
  89.             DeleteElement(workersRes())
  90.             Break
  91.           EndIf
  92.         Next
  93.         UnlockMutex(workerResLock)
  94.         DeleteElement(workers())
  95.       EndIf
  96.     Else
  97.       foundRes.b = #False
  98.       LockMutex(workerResLock)
  99.       ForEach workersRes()
  100.         If workers()\sessionID = workersRes()\sessionID
  101.           foundRes = #True
  102.           Select workersRes()\code
  103.             Case #code_ok
  104.               toLog("[" + workers()\sessionID + "] session ended successfully")
  105.               export()
  106.             Case #code_binderror
  107.               toLog("[" + workers()\sessionID + "] worker can't bind to port " + Str(workers()\port),#log_error)
  108.             Case #code_timeout
  109.               toLog("[" + workers()\sessionID + "] worker exited due to timeout",#log_warn)
  110.             Default
  111.               toLog("[" + workers()\sessionID + "] worker died for an unknown reason (code " + Str(workersRes()\code) + "), report to the developer",#log_error)
  112.           EndSelect
  113.           Break
  114.         EndIf
  115.         DeleteElement(workersRes())
  116.       Next
  117.       UnlockMutex(workerResLock)
  118.       If Not foundRes
  119.         toLog("[" + workers()\sessionID + "] worker died silently, report to the developer",#log_error)
  120.       EndIf
  121.       unbindWorker(workers()\port)
  122.       freePort(workers()\port)
  123.       DeleteElement(workers())
  124.     EndIf
  125.   Next
  126.   ForEach clients()
  127.     If clients()\time < Date() - #client_timeout
  128.       CloseNetworkConnection(clients()\clientID)
  129.       toLog("[" + clients()\clientSessID + "] disconnected client due to timeout")
  130.       DeleteElement(clients())
  131.     EndIf
  132.   Next
  133.  
  134.   CompilerIf #PB_Compiler_Debugger
  135.     i = 0
  136.     ForEach ports()
  137.       If ports()\isFree
  138.         i + 1
  139.       EndIf
  140.     Next
  141.     Debug "workers: " + Str(ListSize(workers())) + "/" + Str(maxWorkers) + "; ports: " + Str(i) + "/" + Str(ListSize(ports())) + "; clients: " + Str(ListSize(clients())) + "; workersRes: " + Str(ListSize(workersRes()))
  142.   CompilerEndIf
  143.  
  144. ForEver
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement