xref: /haiku/headers/private/net/SocketMessenger.h (revision 385ee03ba83b7a40d315e17b03031b3ca37820c0)
1 /*
2  * Copyright 2011-2016, Haiku, Inc. All Rights Reserved.
3  * Distributed under the terms of the MIT License.
4  */
5 #ifndef SOCKET_MESSENGER_H
6 #define SOCKET_MESSENGER_H
7 
8 #include <Socket.h>
9 
10 class BMessage;
11 class BMessenger;
12 
13 
14 class BSocketMessenger {
15 public:
16 								BSocketMessenger();
17 								BSocketMessenger(
18 									const BNetworkAddress& address,
19 									bigtime_t timeout = B_INFINITE_TIMEOUT);
20 								// adopt an existing already connected socket.
21 								BSocketMessenger(const BSocket& socket);
22 	virtual						~BSocketMessenger();
23 
24 			void				Unset();
25 			status_t			SetTo(const BNetworkAddress& address,
26 									bigtime_t timeout = B_INFINITE_TIMEOUT);
27 			status_t			SetTo(const BSocketMessenger& target,
28 									bigtime_t timeout = B_INFINITE_TIMEOUT);
29 
30 			status_t			InitCheck() const { return fInitStatus; }
31 
32 			const BNetworkAddress& Address() const { return fSocket.Peer(); }
33 
34 	virtual	status_t			SendMessage(const BMessage& message);
35 	virtual	status_t			SendMessage(const BMessage& message,
36 									BMessage& _reply,
37 									bigtime_t timeout = B_INFINITE_TIMEOUT);
38 	virtual	status_t			SendMessage(const BMessage& message,
39 									BMessenger& replyTarget,
40 									bigtime_t timeout = B_INFINITE_TIMEOUT);
41 	virtual	status_t			SendReply(const BMessage& message,
42 									const BMessage& reply);
43 
44 								// wait for unsolicited message on socket
45 	virtual	status_t			ReceiveMessage(BMessage& _message,
46 									bigtime_t timeout = B_INFINITE_TIMEOUT);
47 
48 private:
49 			struct Private;
50 private:
51 								BSocketMessenger(const BSocketMessenger&);
52 			BSocketMessenger&	operator=(const BSocketMessenger&);
53 
54 			void				_Init();
55 			status_t			_WaitForMessage(bigtime_t timeout);
56 			status_t			_SendMessage(const BMessage& message);
57 			status_t			_ReadMessage(BMessage& _message);
58 			status_t			_ReadReply(int64 replyID,
59 									BMessage& _reply, bigtime_t timeout);
60 
61 	static	status_t			_MessageReader(void* arg);
62 
63 private:
64 			Private*			fPrivateData;
65 			BSocket				fSocket;
66 			status_t			fInitStatus;
67 };
68 
69 #endif	// SOCKET_MESSENGER_H
70