Guest User

Untitled

a guest
May 23rd, 2018
182
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. listenLoop
  2.     "Private! This loop is run in a separate process. It will establish up to maxQueueLength connections on the given port."
  3.     "Details: When out of sockets or queue is full, retry more frequently, since a socket may become available, space may open in the queue, or a previously queued connection may be aborted by the client, making it available for a fresh connection."
  4.     "Note: If the machine is disconnected from the network while the server is running, the currently waiting socket will go from 'isWaitingForConnection' to 'unconnected', and attempts to create new sockets will fail. When this happens, delete the broken socket and keep trying to create a socket in case the network connection is re-established. Connecting and disconnecting was tested under PPP on Mac system 8.1. It is not if this will work on other platforms.
  5.    
  6.     Fixed to not accept the connection if the queue is full (gvc)."
  7.  
  8.  
  9.     | newConnection |
  10.  
  11.     socket := Socket newTCP.
  12.     "We'll accept four simultanous connections at the same time"
  13.     socket listenOn: portNumber backlogSize: 4.
  14.     "If the listener is not valid then the we cannot use the
  15.     BSD style accept() mechanism."
  16.     socket isValid ifFalse: [^self oldStyleListenLoop].
  17.     [true] whileTrue: [
  18.         socket isValid ifFalse: [
  19.             "socket has stopped listening for some reason"
  20.             socket destroy.
  21.             (Delay forMilliseconds: 10) wait.
  22.             ^self listenLoop ].
  23.         [newConnection := socket waitForConnectionFor: 10]
  24.             on: ConnectionTimedOut
  25.             do: [:ex | newConnection := nil].
  26.         (newConnection notNil and: [newConnection isConnected])
  27.             ifTrue: [(accessSema critical: [connections size < maxQueueLength])
  28.                         ifTrue: [newConnection := socket accept]
  29.                         ifFalse: [socket closeAndDestroy: 0]]
  30.             ifFalse: [newConnection := nil].
  31.         (newConnection notNil and: [newConnection isConnected]) ifTrue: [
  32.             accessSema critical: [connections addLast: newConnection].
  33.             newConnection := nil.
  34.             self changed].
  35.         self pruneStaleConnections].
Add Comment
Please, Sign In to add comment