Windows-only asynchronous TCP connection. windows/TcpConnection starts Winsock overlapped connect, receive, and send operations and reports completion through windows/dispatcher; it is distinct from the fiber-based sync/TcpConnection.

Known issue: setConnection calls the unavailable copy helper, so accepting a connection through windows/TcpAcceptor or calling setConnection directly fails with Name was not found; construction and the other methods compile.


Object and construction


Methods

isConnected

(-- connected) Reports whether the atomic state contains CONNECTED; result connected is Cond.

setConnection

(connection --) Installs one connected socket handle, initializes both dispatcher contexts, and stores CONNECTED. connection is a raw Natx handle supplied by the acceptor handoff.

Use

  • This is the cross-module handoff called by windows/TcpAcceptor after successful AcceptEx completion.

connect

(address port onConnect -- result) Starts one asynchronous ConnectEx. address is host-order Nat32, port is host-order Nat16, onConnect is a Function with source signature ({result: String Ref;} {} {}) Function, and result is the immediate String initiation result.

Postconditions

  • Success binds a local wildcard address, associates the socket with the dispatcher completion port, stores the callback, sets CONNECTING, and returns an empty string. Completion invokes the callback and changes the state to connected or zero.

cancelConnect

(-- isCanceled) Attempts to cancel a pending ConnectEx with CancelIoEx; returns isCanceled: Cond.
  • The source may post a completion-port entry again if the connect callback raced with cancellation.

disconnect

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

Preconditions

  • The state is CONNECTED; pending read or write operations must not remain active.

read

(data onRead -- result) Starts one overlapped receive into mutable data: Nat8 Span. onRead is a Function with source signature ({result: String Ref; numberOfTransferredBytes: Int32;} {} {}) Function; result is the immediate initiation result.

Preconditions

  • The state is CONNECTED; data converts to a mutable Nat8 Span; no read is already active.

cancelRead

(-- isCanceled) Attempts to cancel the active receive with CancelIoEx; returns isCanceled: Cond.

write

(data onWrite -- result) Starts one overlapped send from data: Nat8 Cref Span. onWrite is a Function with source signature ({result: String Ref;} {} {}) Function; result is the immediate initiation result.

Preconditions

  • The state is CONNECTED; data converts to a read-only Nat8 Cref Span; no write is already active.

cancelWrite

(-- isCanceled) Attempts to cancel the active send with CancelIoEx; returns isCanceled: Cond.

Completion and callback model


Ownership and threading


Results and errors


Examples

Windows dispatcher connection

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

{} Int32 {} [
  connection: TcpConnection;
  handled: FALSE dynamic;
  onConnect: ({result: String Ref;} {} {}) Function;
  callback: [result:;];
  @callback @onConnect.assign
  result: 0x7F000001n32 6615n16 @onConnect @connection.connect;
  [result.size 0 =] "connect failed" ensure
  [handled ~] [dispatcher.tryDispatch @handled set] while
  [@connection.isConnected] "connect did not complete" ensure
  @connection.disconnect
  0
] "main" exportFunction

See also