Derived From:
Mix-in Classes:BArchivable
Declared In:app/Messenger.h
Allocation:Stack or constructor
Class Overview

Constructor and Destructor


BMessenger(const BHandler* handler,
           const BLooper* looper = NULL,
           status_t* error = NULL);
BMessenger(const char* signature,
           team_id team = -1,
           status_t* error = NULL);
BMessenger(const BMessenger& messenger); BMessenger();

Creates a new BMessenger and sets its target to a local looper/handler, to the (running) application identified by signature or team, or to the target of some other messenger.



To target a looper, supply a looper and pass a NULL handler. When the messenger sends a message, the message will be handled by looper's preferred handler. If you want the message to be sent to a specific handler within a looper, supply a handler and pass a NULL looper. The handler must already be attached to a looper, and can't switch loopers after this BMessenger is constructed.

signature or team.

If you supply a signature but leave team as -1, the messenger targets an app with that signature. (The app must already be running; in the case of multiple instances of a running app, the exact instance is indeterminate) If you supply a team but no signature, you target exactly that team, regardless of signature. By supplying both a team and a signature, you can specify a specific instance of an app. In this case, team must be an app that has the proper signature.

Messages sent to a remote target are received and handled by the remote application's BApplication object.

The BMessenger doesn't own its target.

The constructor places an error code in error (if provided).

Return CodeDescription


The target was properly set.


The application identified by signature couldn't be found, or both handler and looper are invalid.


Invalid team.


team isn't a signature app, or handler is associated with a BLooper other than looper.


handler isn't associated with a BLooper



Frees the BMessenger; the target isn't affected.

Member Functions


bool IsValid() const;

Returns true if the target looper, whether local or remote, still exists.


This function doesn't tell you whether the looper is actually ready to receive messages, or whether the handler (if it was specified in the constructor) exists. In other words, a valid BMessenger is no guarantee that a message will actually get to the target.

LockTarget(), LockTargetWithTimeout()

bool LockTarget() const;status_t LockTargetWithTimeout(bigtime_t timeout) const;

These functions apply to local targets only.

These functions attempt to lock the target looper in the manner of the similarly named BLooper functions (see BLooper::Lock() ). In addition to the error codes reported there, these functions return false and B_BAD_VALUE (respectively) if the target isn't local, or if the looper is otherwise invalid.


status_t SendMessage(BMessagemessage,
                     bigtime_t deliveryTimeout = B_INFINITE_TIMEOUT,
                     bigtime_t replyTimeout = B_INFINITE_TIMEOUT) const;
status_t SendMessage(BMessagemessage,
                     BHandlerreplyHandler = NULL,
                     bigtime_t deliveryTimeout = B_INFINITE_TIMEOUT) const;
status_t SendMessage(BMessagemessage,
                     bigtime_t deliveryTimeout = B_INFINITE_TIMEOUT) const;
status_t SendMessage(uint32 command,
                     BMessagereply) const;
status_t SendMessage(uint32 command,
                     BHandlerreplyHandler = NULL) const;

Sends a copy of message (or a BMessage based on a command constant) to the object's target. The caller retains ownership of message. The function doesn't return until the message has been delivered; if you're sending a message (as opposed to a command constant) you can set a microsecond delivery timeout through deliveryTimeout.

The target can respond to the message:

  • If you supply a reply BMessage, the response is synchronous, with an optional timeout (replyTimeout) that starts ticking after the original message has been delivered. If the response times out, or the target deletes the original message without responding, the reply->what is set to B_NO_REPLY. The caller is responsible for allocating and freeing reply. message and reply can be the same object.


    Use caution when requesting a synchronous reply: If you call SendMessage() from the target looper's thread, you'll deadlock (or, at best, time out).

  • If you supply a reply target (replyMessenger or replyHandler), the response is asynchronous, and is sent to the reply target.

  • If you supply neither a reply message nor a reply target, the target's response is sent to be_app_messenger.

Return CodeDescription


The message was delivered (and the synchronous reply was received, if applicable).


deliveryTimeout expired; the message never made it to the target.


You requested a 0 deliveryTimeout, and the target's message queue is full.


The messenger's target is invalid, or the reply port was deleted while waiting for a reply (synchronous response requests only).


You asked for a synchronous reply, but there are no more reply ports.


If you specified a handler when you constructed your BMessenger, and if that handler has since changed loopers, SendMessage() won't deliver its message, but it doesn't complain (it returns B_OK).

Target(), IsTargetLocal(), Team()

BHandler Target(BLooper** looper) const;bool IsTargetLocal() const;inline team_id Team() const;

Target() returns the BMessenger's handler (directly) and looper (by reference in looper). This function only works for local targets. If Target() returns NULL, it can mean one of four things:

  1. The target is remote; looper is set to NULL.

  2. The BMessenger hasn't been initialized; looper is set to NULL.

  3. The handler is the looper's preferred handler; looper will be valid.

  4. The handler has been deleted; looper will be valid given that it hasn't been deleted as well.

IsTargetLocal() returns true if the target is local. Team() returns a target's team.


= (assignment)

BMessenger& operator=(const BMessenger& from);

Sets the left-side BMessenger's target to that of the right-side object.

== (equality)

bool operator==(const BMessenger& other) const;

Two BMessengers are equal if they have the same target.

Creative Commons License
Legal Notice
This work is licensed under a Creative Commons Attribution-Non commercial-No Derivative Works 3.0 License.