# PortLink Use Cases:

This document describes the PortLink interface and some basics of how it is implemented. The document has the following sections:

1. PortLink Interface
2. PortLink Use Cases

## PortLink Interface:

The PortLink class is a lightweight class designed to ease the pain of sending a message to a port. Normal use boils down to creating a PortLink object, setting the message code, attaching any extra data via Attach(), and calling Flush() to send it.

While this class is designed to facilitate port-based messaging, it does not devise any protocols for such. The recipient will need to know if data is included in a message, for example. Likewise, all extra data must be freed by the recipient.

## PortLink Use Cases:

The following use cases cover the PortLink functionality:

1. Construction: A PortLink is created by passing it a port ID. Error-checking is not performed on the port itself, so be sure it is a valid port.

2. Destruction: When a PortLink is destroyed, any data which is currently attached to a pending message is freed.

3. Creating a message: Creating a message can be as simple as setting the message code (similar to BMessage's what member). Extra data is not required.

4. Attaching Data: Adding extra data is as simple as calling the member function Attach(), which makes a copy of the parameter passed to it.

5. Sending a message: Call Flush(). Whatever opcode has been set will be sent to the target. Optionally, a timeout (in microseconds) of type bigtime_t can be specified. This can be useful in preventing deadlocks if the target has crashed and its port fills up.

6. Synchronous Messaging: This one requires a little more care in order to prevent deadlocks. Attachments may be used as with Flush(), but FlushWithReply() will wait until the target replies unless a timeout value is specified in microseconds of type bigtime_t. A return code of B_ERROR indicates an internal data error and your message is intact. If a reply times out, it will return B_TIMED_OUT. Otherwise, it returns B_OK.

   Reply Protocol: The target will receive the message with all attached data with one slight modification to the otherwise chosen message protocol - the first item will be a port_id which is the port to which the sender is to reply. All other attached data (if any) immediately follows this port id.

###### HTML Documentation Format by Jeremy Rand