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.
- Alphabetic
- By Inheritance
- ScalableSelector
- Runnable
- AnyRef
- Any
- Hide All
- Show All
- Public
- All
Instance Constructors
-
new
ScalableSelector(onStart: Runnable, onStop: Runnable, onError: (Exception) ⇒ Boolean)
Creates a new ScalableSelector with Runnable handlers (for Java compatibility).
-
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).
-
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
-
final
def
!=(arg0: Any): Boolean
- Definition Classes
- AnyRef → Any
-
final
def
##(): Int
- Definition Classes
- AnyRef → Any
-
final
def
==(arg0: Any): Boolean
- Definition Classes
- AnyRef → Any
-
final
def
asInstanceOf[T0]: T0
- Definition Classes
- Any
-
def
clone(): AnyRef
- Attributes
- protected[lang]
- Definition Classes
- AnyRef
- Annotations
- @throws( ... ) @native()
-
final
def
eq(arg0: AnyRef): Boolean
- Definition Classes
- AnyRef
-
def
equals(arg0: Any): Boolean
- Definition Classes
- AnyRef → Any
-
def
finalize(): Unit
- Attributes
- protected[lang]
- Definition Classes
- AnyRef
- Annotations
- @throws( classOf[java.lang.Throwable] )
-
final
def
getClass(): Class[_]
- Definition Classes
- AnyRef → Any
- Annotations
- @native()
-
def
hashCode(): Int
- Definition Classes
- AnyRef → Any
- Annotations
- @native()
-
final
def
isInstanceOf[T0]: Boolean
- Definition Classes
- Any
-
def
isRunning: Boolean
Checks if the selector is running.
Checks if the selector is running.
- returns
true if this selector is running
-
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
falsewithout 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.
-
final
def
ne(arg0: AnyRef): Boolean
- Definition Classes
- AnyRef
-
final
def
notify(): Unit
- Definition Classes
- AnyRef
- Annotations
- @native()
-
final
def
notifyAll(): Unit
- Definition Classes
- AnyRef
- Annotations
- @native()
-
def
run(): Unit
Executes the selector loop.
Executes the selector loop.
- Definition Classes
- ScalableSelector → Runnable
-
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
-
def
stop(): Unit
Stops the Selector's thread and all registered ServerSocketChannels.
-
final
def
synchronized[T0](arg0: ⇒ T0): T0
- Definition Classes
- AnyRef
-
def
toString(): String
- Definition Classes
- AnyRef → Any
-
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
-
final
def
wait(): Unit
- Definition Classes
- AnyRef
- Annotations
- @throws( ... )
-
final
def
wait(arg0: Long, arg1: Int): Unit
- Definition Classes
- AnyRef
- Annotations
- @throws( ... )
-
final
def
wait(arg0: Long): Unit
- Definition Classes
- AnyRef
- Annotations
- @throws( ... ) @native()