sok / Sok.Socket.TCP / TCPClientSocket
expect class TCPClientSocket
(source)
Class representing a client socket. You can use it to perform any I/O operation. Keep in mind that this class keep an internal queue for write operations thus storing data until written so you should have some kind of backpressure mechanism to prevent the accumulation of too many data.
<init> | TCPClientSocket(channel: SocketChannel , selectorPool: SelectorPool ) Helper contrutor that simply get the less busy selector from the pool then use the “standard” constructor TCPClientSocket(channel: SocketChannel , selector: Selector ) Wrap a NIO channel with a Sok client socket class TCPClientSocket(socket: dynamic) Wrap a Node.js socket with Sok Client socket class |
exceptionHandler | var exceptionHandler: (exception: Throwable ) -> Unit Lambda that will be called when an exception resulting in the closing of the socket is thrown. This happen when calling the close or forceClose method, if the peer close the socket or if something wrong happen internally. Exception that does not affect the socket state will not be received by this handler (exception in the bulkRead operation lambda for example) |
isClosed | var isClosed: Boolean Keep track of the socket status |
bulkRead | suspend fun bulkRead(buffer: MultiplatformBuffer , operation: (buffer: MultiplatformBuffer , read: Int ) -> Boolean ): Long suspend actual fun bulkRead(buffer: <ERROR CLASS>, operation: (<ERROR CLASS>, read: Int ) -> Boolean ): Long Used to do efficient read-intensive loops, it will basically execute the operation each time there is data to be read and avoid registrations/allocation between each iteration. The passed lambda must return true to continue the loop or false to exit. The call will suspend as long as the loop is running. |
close | suspend fun close(): Unit gracefully stops the socket. The method suspends as it waits for all the writing requests in the channel to be executed before effectively closing the channel. Once the socket is closed a NormalCloseException will be passed to the exception handler and to any ongoing read method call |
forceClose | fun forceClose(): Unit forcefully closes the channel without checking the writing request queue. Once the socket is closed a ForceCloseException will be passed to the exception handler and to any ongoing read method call |
getOption | fun <T> getOption(name: Options ): SocketOption < T > get a socket option and try to convert it to the given type, throw an exception if the option is not of the correct type exemple: actual fun <T> getOption(name: <ERROR CLASS>): <ERROR CLASS>< T > get a socket option and try to convert it to the given type |
read | suspend fun read(buffer: MultiplatformBuffer ): Int suspend actual fun read(buffer: <ERROR CLASS>): Int Perform a suspending read, the method will read n bytes ( 0 < n <= buffer.remaining() ) and update the cursor. If the peer closes the socket while reading, a PeerClosedException will be thrown. If the socket is manually closed while reading, either NormalCloseException or ForceCloseException will be thrownsuspend fun read(buffer: MultiplatformBuffer , minToRead: Int ): Int suspend actual fun read(buffer: <ERROR CLASS>, minToRead: Int ): Int Perform a suspending read, the method will read n bytes ( minToRead < n <= buffer.remaining() ) and update the cursor If the peer closes the socket while reading, a PeerClosedException will be thrown. If the socket is manually closed while reading, either NormalCloseException or ForceCloseException will be thrown |
setOption | fun <T> setOption(option: SocketOption < T >): Boolean set a socket option exemple: actual fun <T> setOption(option: <ERROR CLASS>< T >): Boolean set a socket option |
write | suspend fun write(buffer: MultiplatformBuffer ): Boolean suspend actual fun write(buffer: <ERROR CLASS>): Boolean Perform a suspending write, the method will not return until all the data between buffer.cursor and buffer.limit are written. The socket use an internal write queue, allowing multiple threads to concurrently write. Backpressure mechanisms should be implemented by the developer to avoid having too much data in the queue. If the peer closes the socket while reading, a PeerClosedException will be thrown. If the socket is manually closed while reading, either NormalCloseException or ForceCloseException will be thrown |