xref: /haiku/headers/private/app/PortLink.h (revision 39241fe22890fb958b6ba32d6ab9526da98be187)
1 //------------------------------------------------------------------------------
2 //	Copyright (c) 2001-2002, OpenBeOS
3 //
4 //	Permission is hereby granted, free of charge, to any person obtaining a
5 //	copy of this software and associated documentation files (the "Software"),
6 //	to deal in the Software without restriction, including without limitation
7 //	the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 //	and/or sell copies of the Software, and to permit persons to whom the
9 //	Software is furnished to do so, subject to the following conditions:
10 //
11 //	The above copyright notice and this permission notice shall be included in
12 //	all copies or substantial portions of the Software.
13 //
14 //	THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 //	IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 //	FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 //	AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 //	LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
19 //	FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
20 //	DEALINGS IN THE SOFTWARE.
21 //
22 //	File Name:		PortLink.h
23 //	Author:			DarkWyrm <bpmagic@columbus.rr.com>
24 //					Pahtz <pahtz@yahoo.com.au>
25 //	Description:	Class for low-overhead port-based messaging
26 //
27 //------------------------------------------------------------------------------
28 #ifndef _PORTLINK_H
29 #define _PORTLINK_H
30 
31 #include <OS.h>
32 #include <LinkMsgReader.h>
33 #include <LinkMsgSender.h>
34 
35 /*
36 	Error checking rules: (for if you don't want to check every return code)
37 
38 	Calling EndMessage() is optional, implied by Flush() or StartMessage().
39 
40 	If you are sending just one message you only need to test Flush() == B_OK
41 
42 	If you are buffering multiple messages without calling Flush() you must
43 		check EndMessage() == B_OK, or the last Attach() for each message. Check
44 		Flush() at the end.
45 
46 	If you are reading, check the last Read() or ReadString() you perform.
47 
48 */
49 
50 class BPortLink
51 {
52 public:
53 	BPortLink(port_id send = -1, port_id reply = -1);
54 	virtual ~BPortLink();
55 
56 	status_t StartMessage(int32 code);
57 	void CancelMessage();
58 	status_t EndMessage();
59 
60 	status_t Flush(bigtime_t timeout = B_INFINITE_TIMEOUT);
61 
62 	void SetSendPort(port_id port);
63 	port_id	GetSendPort();
64 
65 	void SetReplyPort(port_id port);
66 	port_id	GetReplyPort();
67 
68 	status_t Attach(const void *data, ssize_t size);
69 	status_t AttachString(const char *string);
70 	template <class Type> status_t Attach(const Type& data)
71 	{
72 		return Attach(&data, sizeof(Type));
73 	}
74 
75 	status_t GetNextReply(int32 *code, bigtime_t timeout = B_INFINITE_TIMEOUT);
76 	status_t Read(void *data, ssize_t size);
77 	status_t ReadString(char **string);
78 	template <class T> status_t Read(T *data)
79 	{
80 		return fReader->Read(data,sizeof(T));
81 	}
82 
83 protected:
84 	LinkMsgReader *fReader;
85 	LinkMsgSender *fSender;
86 };
87 
88 #endif
89