xref: /haiku/headers/private/app/PortLink.h (revision db10640de90f7f9519ba2da9577b7c1af3c64f6b)
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 
33 /*
34 	Error checking rules: (for if you don't want to check every return code)
35 
36 	Calling EndMessage() is optional, implied by Flush() or StartMessage().
37 
38 	If you are sending just one message you only need to test Flush() == B_OK
39 
40 	If you are buffering multiple messages without calling Flush() you must
41 		check EndMessage() == B_OK, or the last Attach() for each message. Check
42 		Flush() at the end.
43 
44 	If you are reading, check the last Read() or ReadString() you perform.
45 
46 */
47 
48 class BPortLink
49 {
50 public:
51 	BPortLink(port_id send = -1, port_id reply = -1);
52 	virtual ~BPortLink();
53 
54 	status_t StartMessage(int32 code);
55 	void CancelMessage();
56 	status_t EndMessage();
57 
58 	status_t Flush(bigtime_t timeout = B_INFINITE_TIMEOUT);
59 
60 	// see BPrivate::BAppServerLink which inherits from BPortLink
61 	//status_t FlushWithReply(int32 *code);
62 
63 	void SetSendPort(port_id port);
64 	port_id	GetSendPort();
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 Type> status_t Read(Type *data)
79 	{
80 		return Read(data, sizeof(Type));
81 	}
82 
83 protected:
84 	status_t FlushCompleted(ssize_t newbuffersize);
85 	status_t ReadFromPort(bigtime_t timeout);
86 	status_t AdjustReplyBuffer(bigtime_t timeout);
87 	void ResetReplyBuffer();
88 
89 	port_id	fSendPort;
90 	port_id fReceivePort;
91 
92 	char	*fSendBuffer;
93 	char	*fRecvBuffer;
94 
95 	int32	fSendPosition;	//current append position
96 	int32	fRecvPosition;	//current read position
97 
98 	int32	fSendStart;	//start of current message
99 	int32	fRecvStart;	//start of current message
100 
101 	int32	fSendBufferSize;
102 	int32	fRecvBufferSize;
103 
104 	int32	fSendCount;	//number of completed messages in buffer
105 
106 	int32	fDataSize;	//size of data in recv buffer
107 	int32	fReplySize;	//size of current reply message
108 
109 	status_t fWriteError;	//Attach failed for current message
110 	status_t fReadError;	//Read failed for current message
111 };
112 
113 #endif
114