xref: /haiku/headers/private/app/ServerLink.h (revision e6b30aee0fd7a23d6a6baab9f3718945a0cd838a)
1 /*
2  * Copyright 2001-2007, Haiku.
3  * Distributed under the terms of the MIT License.
4  *
5  * Authors:
6  *		DarkWyrm <bpmagic@columbus.rr.com>
7  *		Pahtz <pahtz@yahoo.com.au>
8  *		Axel Dörfler, axeld@pinc-software.de
9  */
10 #ifndef _SERVER_LINK_H
11 #define _SERVER_LINK_H
12 
13 
14 #include <OS.h>
15 #include <LinkReceiver.h>
16 #include <LinkSender.h>
17 
18 class BShape;
19 class BString;
20 
21 /*
22  * Error checking rules: (for if you don't want to check every return code)
23  * - Calling EndMessage() is optional, implied by Flush() or StartMessage().
24  * - If you are sending just one message you only need to test Flush() == B_OK
25  * - If you are buffering multiple messages without calling Flush() you must
26  *   check EndMessage() == B_OK, or the last Attach() for each message.
27  *   Check Flush() at the end.
28  * - If you are reading, check the last Read() or ReadString() you perform.
29  */
30 
31 namespace BPrivate {
32 
33 class ServerLink {
34 	public:
35 		ServerLink();
36 		virtual ~ServerLink();
37 
38 		// send methods
39 
40 		void SetSenderPort(port_id port);
41 		port_id	SenderPort();
42 
43 		status_t StartMessage(int32 code, size_t minSize = 0);
44 		void CancelMessage();
45 		status_t EndMessage();
46 
47 		status_t Flush(bigtime_t timeout = B_INFINITE_TIMEOUT, bool needsReply = false);
48 		status_t Attach(const void *data, ssize_t size);
49 		status_t AttachString(const char *string, int32 length = -1);
50 		status_t AttachRegion(const BRegion &region);
51 		status_t AttachShape(BShape &shape);
52 		template <class Type> status_t Attach(const Type& data);
53 
54 		// receive methods
55 
56 		void SetReceiverPort(port_id port);
57 		port_id	ReceiverPort();
58 
59 		status_t GetNextMessage(int32 &code, bigtime_t timeout = B_INFINITE_TIMEOUT);
60 		bool NeedsReply() const;
61 		status_t Read(void *data, ssize_t size);
62 		status_t ReadString(char *buffer, size_t bufferSize);
63 		status_t ReadString(BString& string, size_t* _length = NULL);
64 		status_t ReadString(char** _string, size_t* _length = NULL);
65 		status_t ReadRegion(BRegion *region);
66 		status_t ReadShape(BShape *shape);
67 		template <class Type> status_t Read(Type *data);
68 
69 		// convenience methods
70 
71 		status_t FlushWithReply(int32 &code);
72 		LinkSender &Sender() { return *fSender; }
73 		LinkReceiver &Receiver() { return *fReceiver; }
74 
75 	protected:
76 		LinkSender *fSender;
77 		LinkReceiver *fReceiver;
78 };
79 
80 
81 // sender inline functions
82 
83 inline void
84 ServerLink::SetSenderPort(port_id port)
85 {
86 	fSender->SetPort(port);
87 }
88 
89 inline port_id
90 ServerLink::SenderPort()
91 {
92 	return fSender->Port();
93 }
94 
95 inline status_t
96 ServerLink::StartMessage(int32 code, size_t minSize)
97 {
98 	return fSender->StartMessage(code, minSize);
99 }
100 
101 inline status_t
102 ServerLink::EndMessage()
103 {
104 	return fSender->EndMessage();
105 }
106 
107 inline void
108 ServerLink::CancelMessage()
109 {
110 	fSender->CancelMessage();
111 }
112 
113 inline status_t
114 ServerLink::Flush(bigtime_t timeout, bool needsReply)
115 {
116 	return fSender->Flush(timeout, needsReply);
117 }
118 
119 inline status_t
120 ServerLink::Attach(const void *data, ssize_t size)
121 {
122 	return fSender->Attach(data, size);
123 }
124 
125 inline status_t
126 ServerLink::AttachString(const char *string, int32 length)
127 {
128 	return fSender->AttachString(string, length);
129 }
130 
131 template<class Type> status_t
132 ServerLink::Attach(const Type &data)
133 {
134 	return Attach(&data, sizeof(Type));
135 }
136 
137 // #pragma mark - receiver inline functions
138 
139 inline void
140 ServerLink::SetReceiverPort(port_id port)
141 {
142 	fReceiver->SetPort(port);
143 }
144 
145 inline port_id
146 ServerLink::ReceiverPort()
147 {
148 	return fReceiver->Port();
149 }
150 
151 inline status_t
152 ServerLink::GetNextMessage(int32 &code, bigtime_t timeout)
153 {
154 	return fReceiver->GetNextMessage(code, timeout);
155 }
156 
157 inline bool
158 ServerLink::NeedsReply() const
159 {
160 	return fReceiver->NeedsReply();
161 }
162 
163 inline status_t
164 ServerLink::Read(void *data, ssize_t size)
165 {
166 	return fReceiver->Read(data, size);
167 }
168 
169 inline status_t
170 ServerLink::ReadString(char *buffer, size_t bufferSize)
171 {
172 	return fReceiver->ReadString(buffer, bufferSize);
173 }
174 
175 inline status_t
176 ServerLink::ReadString(BString& string, size_t* _length)
177 {
178 	return fReceiver->ReadString(string, _length);
179 }
180 
181 inline status_t
182 ServerLink::ReadString(char** _string, size_t* _length)
183 {
184 	return fReceiver->ReadString(_string, _length);
185 }
186 
187 template <class Type> status_t
188 ServerLink::Read(Type *data)
189 {
190 	return Read(data, sizeof(Type));
191 }
192 
193 }	// namespace BPrivate
194 
195 #endif	/* _SERVER_LINK_H */
196