NamedPipeServerConnection class

One server-side per-client connection. A NamedPipeServer creates one of these for every client that connects, and surfaces it as the Connection parameter of every server event. Use it to send messages to that specific client, to manually issue reads when NamedPipeServer.ContinuouslyReadFromPipe is False, and to close the connection from the server side.

The class is tagged [COMCreatable(False)] and its constructor takes a package-private interface — reach instances only through NamedPipeServer events. Connection-lifecycle and message events come through the parent NamedPipeServer; this class carries the per-connection data and methods only.

Private Sub server_ClientConnected(Connection As NamedPipeServerConnection)
    ' attach per-client state through the CustomData slot
    Connection.CustomData = New ClientSession
End Sub

Private Sub server_ClientMessageReceived( _
        Connection As NamedPipeServerConnection, _
        ByRef Cookie As Variant, _
        ByRef Data() As Byte)

    Dim session As ClientSession = Connection.CustomData
    session.HandleMessage Data
End Sub

See the package overview for the IOCP / event-marshalling architecture, the cookie correlation pattern, and the transient lifetime of Data() As Byte inside events.

Properties

CustomData

A per-connection opaque slot the consumer can attach state to — typically a session object scoped to that one client. Variant, default Empty. The package never reads or writes this field; it is provided for convenience so that consumers do not have to maintain a parallel Dictionary keyed by Handle.

Handle

The underlying Win32 named-pipe handle. LongPtr. Exposed for low-level / debugging use — most consumers can ignore it. Do not call CloseHandle on this value yourself; use AsyncClose so the IOCP loop and the parent server’s bookkeeping stay consistent.

IsConnected

True between the client connecting and the connection dropping. Boolean. Set internally; consumer code typically reads this rather than writes it. Becomes False as soon as the underlying pipe drops, even before the ClientDisconnected event fires (the event waits until every outstanding I/O has returned).

IsOpening

True during the brief window between the package creating the connection object and ConnectNamedPipe completing. Boolean. Used internally by NamedPipeServer.Stop to avoid a race condition during shutdown; consumer code does not normally need to read it.

Methods

AsyncClose

Cancels every outstanding I/O against this connection and closes the underlying pipe handle. Eventually triggers a ClientDisconnected event on the parent server once the cancellation completes. Automatically invoked from Class_Terminate when the last reference to the connection drops.

Syntax: connection.AsyncClose

AsyncRead

Manually issues an asynchronous read against this connection.

Syntax: connection.AsyncRead [ Cookie [, OverlappedStruct ] ]

Cookie
optional A Variant correlation value, surfaced as the Cookie parameter of the matching ClientMessageReceived event. Default Empty.
OverlappedStruct
optional A LongPtr to a pre-allocated OVERLAPPED_CUSTOM structure. Internal use only — the IOCP machinery passes this when re-issuing a read after ERROR_MORE_DATA. Consumer code should always omit this parameter.

Only needed when the parent server’s ContinuouslyReadFromPipe is False; otherwise the IOCP loop keeps a read pending automatically and explicit calls are redundant.

AsyncWrite

Sends a message back to this specific client.

Syntax: connection.AsyncWrite Data() [, Cookie ]

Data
required A Byte() array carrying the bytes to send. An uninitialised or zero-length array is a no-op.
Cookie
optional A Variant correlation value, surfaced as the Cookie parameter of the matching ClientMessageSent event. Default Empty.

Returns immediately; the actual transmission runs through the IOCP loop. The completion fires ClientMessageSent on the parent server.

To send the same message to every connected client at once, use NamedPipeServer.AsyncBroadcast.

See Also