1/*
2 * Copyright 2001-2011 Haiku, Inc. All rights reserved.
3 * Distributed under the terms of the MIT License.
4 *
5 * Authors:
6 *		Ingo Weinhold (bonefish@users.sf.net)
7 */
8#ifndef _MESSENGER_H
9#define _MESSENGER_H
10
11
12#include <OS.h>
13#include <ByteOrder.h>
14#include <Message.h>
15
16
17class BHandler;
18class BLooper;
19
20class BMessenger {
21public:
22									BMessenger();
23									BMessenger(const char* signature,
24										team_id team = -1,
25										status_t* result = NULL);
26									BMessenger(const BHandler* handler,
27										const BLooper* looper = NULL,
28										status_t* result = NULL);
29									BMessenger(const BMessenger& other);
30									~BMessenger();
31
32	// Target
33
34			bool					IsTargetLocal() const;
35			BHandler*				Target(BLooper **looper) const;
36			bool					LockTarget() const;
37			status_t				LockTargetWithTimeout(
38										bigtime_t timeout) const;
39
40	// Message sending
41
42			status_t				SendMessage(uint32 command,
43										BHandler* replyTo = NULL) const;
44			status_t				SendMessage(BMessage* message,
45										BHandler* replyTo = NULL,
46										bigtime_t timeout
47											= B_INFINITE_TIMEOUT) const;
48			status_t				SendMessage(BMessage* message,
49										BMessenger replyTo,
50										bigtime_t timeout
51											= B_INFINITE_TIMEOUT) const;
52			status_t				SendMessage(uint32 command,
53										BMessage* reply) const;
54			status_t				SendMessage(BMessage* message,
55										BMessage* reply,
56										bigtime_t deliveryTimeout
57											= B_INFINITE_TIMEOUT,
58										bigtime_t replyTimeout
59											= B_INFINITE_TIMEOUT) const;
60
61	// Operators and misc
62
63			status_t				SetTo(const char* signature,
64										team_id team = -1);
65			status_t				SetTo(const BHandler* handler,
66										const BLooper* looper = NULL);
67
68			BMessenger&				operator=(const BMessenger& other);
69			bool					operator==(const BMessenger& other) const;
70
71			bool					IsValid() const;
72			team_id					Team() const;
73
74			uint32					HashValue() const;
75
76	//----- Private or reserved -----------------------------------------
77
78	class Private;
79
80private:
81	friend class Private;
82
83			void					_SetTo(team_id team, port_id port,
84										int32 token);
85			void					_InitData(const char* signature,
86										team_id team, status_t* result);
87			void					_InitData(const BHandler* handler,
88										const BLooper *looper,
89										status_t* result);
90
91private:
92			port_id					fPort;
93			int32					fHandlerToken;
94			team_id					fTeam;
95
96			int32					_reserved[3];
97};
98
99bool operator<(const BMessenger& a, const BMessenger& b);
100bool operator!=(const BMessenger& a, const BMessenger& b);
101
102
103#endif	// _MESSENGER_H
104