Windows-only asynchronous TCP acceptor. windows/TcpAcceptor starts Winsock AcceptEx operations and reports their completion through windows/dispatcher; it is distinct from the fiber-based sync/TcpAcceptor.

Known issue: constructing a TcpAcceptor fails to compile because the module references the unavailable sockaddr_in binding; the compiler reports Name was not found.


Object and construction


Methods

startListening

(address port -- result) Creates one IPv4 listening socket. address is a host-order Nat32, port is a host-order Nat16, and result is a String, empty on successful setup.

Preconditions

  • The object state is zero.

Postconditions

  • Success sets TCP_NODELAY, binds and listens, associates the listener with the dispatcher completion port, and sets LISTENING.

stopListening

(--) Closes the owned listening socket and stores the zero state.

Preconditions

  • The state is exactly LISTENING, with no acceptance in progress.

accept

(onAccept -- result) Starts one asynchronous AcceptEx operation and returns a String initiation result. onAccept is a Function with source signature ({result: String Ref; connection: TcpConnection Ref;} {} {}) Function; its callback receives the result reference and the accepted windows/TcpConnection reference through that declared input schema. The implementation stores self as the dispatcher context and consumes no separate context stack object.

Preconditions

  • The state is LISTENING and the callback is not nil.

Postconditions

  • Success creates the pending connection socket, stores the callback, changes state to LISTENING | ACCEPTING, and returns an empty string. Completion invokes the callback.

cancel

(-- isCanceled) Attempts to cancel the pending accept with CancelIoEx and returns isCanceled: Cond.

Results

  • TRUE means the cancellation call succeeded; FALSE means no accept was pending or cancellation failed.

Callback and dispatcher model

This callback/dispatcher path differs from sync/TcpAcceptor, whose accept waits in a fiber and returns a connection, address, and result directly.


Ownership and threading


Results and errors


Examples

Windows dispatcher accept

This example does not compile with the checked mpl-sl revision (see the note above), so it carries no Expected Output.

"Function"              use
"String"                use
"control"               use
"windows/TcpAcceptor"   use
"windows/TcpConnection" use
"windows/dispatcher"    use

{} Int32 {} [
  acceptor: TcpAcceptor;
  onAccept: ({result: String Ref; connection: TcpConnection Ref;} {} {}) Function;
  callback: [connection: result:;;];
  @callback @onAccept.assign
  result: 0x7F000001n32 6614n16 @acceptor.startListening;
  [result.size 0 =] "startListening failed" ensure
  acceptResult: @onAccept @acceptor.accept;
  [acceptResult.size 0 =] "accept failed" ensure
  dispatcher.dispatch
  @acceptor.stopListening
  0
] "main" exportFunction

See also