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 class BGradient;
21
22 /*
23 * Error checking rules: (for if you don't want to check every return code)
24 * - Calling EndMessage() is optional, implied by Flush() or StartMessage().
25 * - If you are sending just one message you only need to test Flush() == B_OK
26 * - If you are buffering multiple messages without calling Flush() you must
27 * check EndMessage() == B_OK, or the last Attach() for each message.
28 * Check Flush() at the end.
29 * - If you are reading, check the last Read() or ReadString() you perform.
30 */
31
32 namespace BPrivate {
33
34 class ServerLink {
35 public:
36 ServerLink();
37 virtual ~ServerLink();
38
39 void SetTo(port_id sender, port_id receiver);
40
41 // send methods
42
43 void SetSenderPort(port_id port);
44 port_id SenderPort();
45
46 void SetTargetTeam(team_id team);
47 team_id TargetTeam();
48
49 status_t StartMessage(int32 code, size_t minSize = 0);
50 void CancelMessage();
51 status_t EndMessage();
52
53 status_t Flush(bigtime_t timeout = B_INFINITE_TIMEOUT,
54 bool needsReply = false);
55 status_t Attach(const void* data, ssize_t size);
56 status_t AttachString(const char* string,
57 int32 length = -1);
58 status_t AttachRegion(const BRegion& region);
59 status_t AttachShape(BShape& shape);
60 status_t AttachGradient(const BGradient& gradient);
61
62 template <class Type>
63 status_t Attach(const Type& data);
64
65 // receive methods
66
67 void SetReceiverPort(port_id port);
68 port_id ReceiverPort();
69
70 status_t GetNextMessage(int32& code,
71 bigtime_t timeout = B_INFINITE_TIMEOUT);
72 bool NeedsReply() const;
73 status_t Read(void* data, ssize_t size);
74 status_t ReadString(char* buffer, size_t bufferSize);
75 status_t ReadString(BString& string,
76 size_t* _length = NULL);
77 status_t ReadString(char** _string,
78 size_t* _length = NULL);
79 status_t ReadRegion(BRegion* region);
80 status_t ReadShape(BShape* shape);
81 status_t ReadGradient(BGradient** _gradient);
82
83 template <class Type>
84 status_t Read(Type* data);
85
86 // convenience methods
87
88 status_t FlushWithReply(int32& code);
Sender()89 LinkSender& Sender() { return *fSender; }
Receiver()90 LinkReceiver& Receiver() { return *fReceiver; }
91
92 protected:
93 LinkSender* fSender;
94 LinkReceiver* fReceiver;
95 };
96
97
98 // #pragma mark - sender inline functions
99
100
101 inline void
SetSenderPort(port_id port)102 ServerLink::SetSenderPort(port_id port)
103 {
104 fSender->SetPort(port);
105 }
106
107
108 inline port_id
SenderPort()109 ServerLink::SenderPort()
110 {
111 return fSender->Port();
112 }
113
114
115 inline void
SetTargetTeam(team_id team)116 ServerLink::SetTargetTeam(team_id team)
117 {
118 fSender->SetTargetTeam(team);
119 }
120
121
122 inline team_id
TargetTeam()123 ServerLink::TargetTeam()
124 {
125 return fSender->TargetTeam();
126 }
127
128
129 inline status_t
StartMessage(int32 code,size_t minSize)130 ServerLink::StartMessage(int32 code, size_t minSize)
131 {
132 return fSender->StartMessage(code, minSize);
133 }
134
135
136 inline status_t
EndMessage()137 ServerLink::EndMessage()
138 {
139 return fSender->EndMessage();
140 }
141
142
143 inline void
CancelMessage()144 ServerLink::CancelMessage()
145 {
146 fSender->CancelMessage();
147 }
148
149
150 inline status_t
Flush(bigtime_t timeout,bool needsReply)151 ServerLink::Flush(bigtime_t timeout, bool needsReply)
152 {
153 return fSender->Flush(timeout, needsReply);
154 }
155
156
157 inline status_t
Attach(const void * data,ssize_t size)158 ServerLink::Attach(const void* data, ssize_t size)
159 {
160 return fSender->Attach(data, size);
161 }
162
163
164 inline status_t
AttachString(const char * string,int32 length)165 ServerLink::AttachString(const char* string, int32 length)
166 {
167 return fSender->AttachString(string, length);
168 }
169
170
171 template<class Type> status_t
Attach(const Type & data)172 ServerLink::Attach(const Type& data)
173 {
174 return Attach(&data, sizeof(Type));
175 }
176
177
178 // #pragma mark - receiver inline functions
179
180
181 inline void
SetReceiverPort(port_id port)182 ServerLink::SetReceiverPort(port_id port)
183 {
184 fReceiver->SetPort(port);
185 }
186
187
188 inline port_id
ReceiverPort()189 ServerLink::ReceiverPort()
190 {
191 return fReceiver->Port();
192 }
193
194
195 inline status_t
GetNextMessage(int32 & code,bigtime_t timeout)196 ServerLink::GetNextMessage(int32& code, bigtime_t timeout)
197 {
198 return fReceiver->GetNextMessage(code, timeout);
199 }
200
201
202 inline bool
NeedsReply()203 ServerLink::NeedsReply() const
204 {
205 return fReceiver->NeedsReply();
206 }
207
208
209 inline status_t
Read(void * data,ssize_t size)210 ServerLink::Read(void* data, ssize_t size)
211 {
212 return fReceiver->Read(data, size);
213 }
214
215
216 inline status_t
ReadString(char * buffer,size_t bufferSize)217 ServerLink::ReadString(char* buffer, size_t bufferSize)
218 {
219 return fReceiver->ReadString(buffer, bufferSize);
220 }
221
222
223 inline status_t
ReadString(BString & string,size_t * _length)224 ServerLink::ReadString(BString& string, size_t* _length)
225 {
226 return fReceiver->ReadString(string, _length);
227 }
228
229
230 inline status_t
ReadString(char ** _string,size_t * _length)231 ServerLink::ReadString(char** _string, size_t* _length)
232 {
233 return fReceiver->ReadString(_string, _length);
234 }
235
236
237 template <class Type> status_t
Read(Type * data)238 ServerLink::Read(Type* data)
239 {
240 return Read(data, sizeof(Type));
241 }
242
243
244 } // namespace BPrivate
245
246 #endif /* _SERVER_LINK_H */
247