TCPListener

Listens for new network connections.

The following program creates an echo server that listens for connections on port 8989 and echoes back any data it receives.

use "net"

class MyTCPConnectionNotify is TCPConnectionNotify
  fun ref received(
    conn: TCPConnection ref,
    data: Array[U8] iso,
    times: USize)
    : Bool
  =>
    conn.write(String.from_array(consume data))
    true

  fun ref connect_failed(conn: TCPConnection ref) =>
    None

class MyTCPListenNotify is TCPListenNotify
  fun ref connected(listen: TCPListener ref): TCPConnectionNotify iso^ =>
    MyTCPConnectionNotify

  fun ref not_listening(listen: TCPListener ref) =>
    None

actor Main
  new create(env: Env) =>
    try
      TCPListener(env.root as AmbientAuth,
        recover MyTCPListenNotify end, "", "8989")
    end
actor tag TCPListener

Constructors

create

new tag create(
  auth: (AmbientAuth val | NetAuth val | TCPAuth val | 
    TCPListenAuth val),
  notify: TCPListenNotify iso,
  host: String val = seq,
  service: String val = seq,
  limit: USize val = seq,
  init_size: USize val = seq,
  max_size: USize val = seq)
: TCPListener tag^

Parameters

Returns


ip4

new tag ip4(
  auth: (AmbientAuth val | NetAuth val | TCPAuth val | 
    TCPListenAuth val),
  notify: TCPListenNotify iso,
  host: String val = seq,
  service: String val = seq,
  limit: USize val = seq,
  init_size: USize val = seq,
  max_size: USize val = seq)
: TCPListener tag^

Parameters

Returns


ip6

new tag ip6(
  auth: (AmbientAuth val | NetAuth val | TCPAuth val | 
    TCPListenAuth val),
  notify: TCPListenNotify iso,
  host: String val = seq,
  service: String val = seq,
  limit: USize val = seq,
  init_size: USize val = seq,
  max_size: USize val = seq)
: TCPListener tag^

Parameters

Returns


Public Behaviours

set_notify

Change the notifier.

be set_notify(
  notify: TCPListenNotify iso)

Parameters


dispose

Stop listening.

be dispose()

Public Functions

local_address

Return the bound IP address.

fun box local_address()
: NetAddress val

Returns


close

Dispose of resources.

fun ref close()
: None val

Returns


Private Behaviours

_event_notify

When we are readable, we accept new connections until none remain.

be _event_notify(
  event: Pointer[AsioEvent val] tag,
  flags: U32 val,
  arg: U32 val)

Parameters


_conn_closed

An accepted connection has closed. If we have dropped below the limit, try to accept new connections.

be _conn_closed()

Private Functions

_accept

Accept connections as long as we have spawned fewer than our limit.

fun ref _accept(
  ns: U32 val = seq)
: None val

Parameters

  • ns: U32 val = seq

Returns


_spawn

Spawn a new connection.

fun ref _spawn(
  ns: U32 val)
: None val

Parameters

Returns


_notify_listening

Inform the notifier that we're listening.

fun ref _notify_listening()
: None val

Returns