Packages

c

com.electronwill.niol.network.tcp

ScalableSelector

final class ScalableSelector extends Runnable

A ScalableSelector uses one NIO Selector to handle many TCP connections on several ports with only one thread.

Port listening

To start listening for connections on a port, call the ScalableSelector.listen method.

One (and only one) TcpListener is assigned for each port. When a new client connects to the port, the listener's TcpListener.onAccept method is called. It creates an instance of ClientAttach for the new client. The ClientAttach will handle the data received from the client and sent to the client.

Buffer providers and sizes

If there is no data transformation in the ClientAttach, the incoming data will be read in a (generally) low-level off-heap buffer provided by the readBufferProvider. The minimum size of the buffer will be packetBufferBaseSize.

If there is a data transformation in the ClientAttach, the incoming data will still be read in a buffer provided by the readBufferProvider, but with a fixed size equal to preTransformReadSize. The read data is then transformed by the transformation function. Once the transformation is done, the packets are reconstructed in an other buffer of minimum size packetBufferBaseSize and provided by postTransformBufferProvider.

The packet buffer's minimum size

The incoming packets arrive in several parts. One part can contain several packets, and one packet can be split into different parts. Therefore they need to be reconstructed in a packet buffer. To avoid the allocation of a new buffer each time some data is read, a minimum "base" buffer is kept during the while connection. When the incoming packet is larger than the base buffer, an additional buffer is allocated, providing the missing capacity. Once the big packet is handled, the additional buffer is discarded.

Linear Supertypes
Runnable, AnyRef, Any
Ordering
  1. Alphabetic
  2. By Inheritance
Inherited
  1. ScalableSelector
  2. Runnable
  3. AnyRef
  4. Any
  1. Hide All
  2. Show All
Visibility
  1. Public
  2. All

Instance Constructors

  1. new ScalableSelector(onStart: Runnable, onStop: Runnable, onError: (Exception) ⇒ Boolean)

    Creates a new ScalableSelector with Runnable handlers (for Java compatibility).

  2. new ScalableSelector()

    Creates a new ScalableSelector with default event handlers.

    Creates a new ScalableSelector with default event handlers.

    • The default startHandler and stopHandler do nothing.
    • The default errorHandler prints the stack trace and returns false (stops the selector).
  3. new ScalableSelector(startHandler: () ⇒ Unit, stopHandler: () ⇒ Unit, errorHandler: (Exception) ⇒ Boolean)

    startHandler

    the function to call when the Selector's thread starts

    stopHandler

    the function to call when the Selector's thread stops

    errorHandler

    handles errors, returns false to stop the Selector's execution

Value Members

  1. final def !=(arg0: Any): Boolean
    Definition Classes
    AnyRef → Any
  2. final def ##(): Int
    Definition Classes
    AnyRef → Any
  3. final def ==(arg0: Any): Boolean
    Definition Classes
    AnyRef → Any
  4. final def asInstanceOf[T0]: T0
    Definition Classes
    Any
  5. def clone(): AnyRef
    Attributes
    protected[lang]
    Definition Classes
    AnyRef
    Annotations
    @throws( ... ) @native()
  6. final def eq(arg0: AnyRef): Boolean
    Definition Classes
    AnyRef
  7. def equals(arg0: Any): Boolean
    Definition Classes
    AnyRef → Any
  8. def finalize(): Unit
    Attributes
    protected[lang]
    Definition Classes
    AnyRef
    Annotations
    @throws( classOf[java.lang.Throwable] )
  9. final def getClass(): Class[_]
    Definition Classes
    AnyRef → Any
    Annotations
    @native()
  10. def hashCode(): Int
    Definition Classes
    AnyRef → Any
    Annotations
    @native()
  11. final def isInstanceOf[T0]: Boolean
    Definition Classes
    Any
  12. def isRunning: Boolean

    Checks if the selector is running.

    Checks if the selector is running.

    returns

    true if this selector is running

  13. def listen[A <: ClientAttach[A]](port: Int, settings: BufferSettings, listener: TcpListener[A]): Boolean

    Starts a TCP ServerSocketChannel and registers it to the selector.

    Starts a TCP ServerSocketChannel and registers it to the selector. If there already is a ServerSocketChannel registered with the specified port, this methods returns false without starting a new server.

    port

    the server's port

    settings

    the settings to apply to the channel's clients

    listener

    the listener that will be called when some events (defined in the listener) related to the ServerSocketChannel occur.

    returns

    true if the server has been started, false if there already is a ServerSocketChannel bound to the specified port and registered to this selector.

  14. final def ne(arg0: AnyRef): Boolean
    Definition Classes
    AnyRef
  15. final def notify(): Unit
    Definition Classes
    AnyRef
    Annotations
    @native()
  16. final def notifyAll(): Unit
    Definition Classes
    AnyRef
    Annotations
    @native()
  17. def run(): Unit

    Executes the selector loop.

    Executes the selector loop.

    Definition Classes
    ScalableSelector → Runnable
  18. def start(threadName: String): Thread

    Starts the ScalableSelector in a new thread.

    Starts the ScalableSelector in a new thread. This method may be called at most once, any further invocation will throw IllegalStateException.

    The state of the selector can be checked with isRunning.

    threadName

    the thread's name

    returns

    the newly created Thread

  19. def stop(): Unit

    Stops the Selector's thread and all registered ServerSocketChannels.

  20. final def synchronized[T0](arg0: ⇒ T0): T0
    Definition Classes
    AnyRef
  21. def toString(): String
    Definition Classes
    AnyRef → Any
  22. def unlisten(port: Int): Unit

    Stops a ServerSocketChannel that has been registered with listen.

    Stops a ServerSocketChannel that has been registered with listen.

    port

    the server's port

  23. final def wait(): Unit
    Definition Classes
    AnyRef
    Annotations
    @throws( ... )
  24. final def wait(arg0: Long, arg1: Int): Unit
    Definition Classes
    AnyRef
    Annotations
    @throws( ... )
  25. final def wait(arg0: Long): Unit
    Definition Classes
    AnyRef
    Annotations
    @throws( ... ) @native()

Inherited from Runnable

Inherited from AnyRef

Inherited from Any

Ungrouped