TaskSocketChannel

abstract class TaskSocketChannel

A Task based asynchronous channel for reading, writing, and manipulating a TCP socket.

On the JVM this is a wrapper around java.nio.channels.AsynchronousSocketChannel (class available since Java 7 for doing async I/O on sockets).

Example
 val taskSocketChannel = TaskSocketChannel()
 val writeT =
   for {
     _ <- taskSocketChannel.connect(new InetSocketAddress("google.com", 80))
     written <- taskSocketChannel.write(ByteBuffer.wrap("Hello world!".getBytes("UTF-8")), None)
   } yield {
     written
   }
 writeT.runAsync(new Callback[Int] {
   override def onSuccess(value: Int): Unit = println(f"Bytes written: $value%d")
   override def onError(ex: Throwable): Unit = println(s"ERR: $ex")
 })
Companion
object
class Object
trait Matchable
class Any

Value members

Concrete methods

def close(): Task[Unit]

Closes this channel

Closes this channel

def connect(remote: InetSocketAddress): Task[Unit]

Connects this channel.

Connects this channel.

Value Params
remote

the remote address to which this channel is to be connected

def localAddress(): Task[Option[InetSocketAddress]]

Asks the socket address that this channel's socket is bound to

Asks the socket address that this channel's socket is bound to

def read(dst: ByteBuffer, timeout: Option[Duration]): Task[Int]

Reads a sequence of bytes from this channel into the given buffer

Reads a sequence of bytes from this channel into the given buffer

Value Params
dst

is the buffer holding the bytes read on completion

timeout

an optional maximum time for the I/O operation to complete

Returns

the number of bytes read or -1 if no bytes could be read because the channel has reached end-of-stream

def remoteAddress(): Task[Option[InetSocketAddress]]

Asks the remote address to which this channel's socket is connected

Asks the remote address to which this channel's socket is connected

def stopReading(): Task[Unit]

Indicates that this channel will not read more data - end-of-stream indication

Indicates that this channel will not read more data - end-of-stream indication

def stopWriting(): Task[Unit]

Indicates that this channel will not write more data - end-of-stream indication

Indicates that this channel will not write more data - end-of-stream indication

def write(src: ByteBuffer, timeout: Option[Duration]): Task[Int]

Writes a sequence of bytes to this channel from the given buffer

Writes a sequence of bytes to this channel from the given buffer

Value Params
src

is the buffer holding the sequence of bytes to write

timeout

an optional maximum time for the I/O operation to complete

Returns

the number of bytes that were written