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 status_t StartMessage(int32 code, size_t minSize = 0); 47 void CancelMessage(); 48 status_t EndMessage(); 49 50 status_t Flush(bigtime_t timeout = B_INFINITE_TIMEOUT, 51 bool needsReply = false); 52 status_t Attach(const void* data, ssize_t size); 53 status_t AttachString(const char* string, 54 int32 length = -1); 55 status_t AttachRegion(const BRegion& region); 56 status_t AttachShape(BShape& shape); 57 status_t AttachGradient(const BGradient& gradient); 58 59 template <class Type> 60 status_t Attach(const Type& data); 61 62 // receive methods 63 64 void SetReceiverPort(port_id port); 65 port_id ReceiverPort(); 66 67 status_t GetNextMessage(int32& code, 68 bigtime_t timeout = B_INFINITE_TIMEOUT); 69 bool NeedsReply() const; 70 status_t Read(void* data, ssize_t size); 71 status_t ReadString(char* buffer, size_t bufferSize); 72 status_t ReadString(BString& string, 73 size_t* _length = NULL); 74 status_t ReadString(char** _string, 75 size_t* _length = NULL); 76 status_t ReadRegion(BRegion* region); 77 status_t ReadShape(BShape* shape); 78 status_t ReadGradient(BGradient** _gradient); 79 80 template <class Type> 81 status_t Read(Type* data); 82 83 // convenience methods 84 85 status_t FlushWithReply(int32& code); 86 LinkSender& Sender() { return *fSender; } 87 LinkReceiver& Receiver() { return *fReceiver; } 88 89 protected: 90 LinkSender* fSender; 91 LinkReceiver* fReceiver; 92 }; 93 94 95 // #pragma mark - sender inline functions 96 97 98 inline void 99 ServerLink::SetSenderPort(port_id port) 100 { 101 fSender->SetPort(port); 102 } 103 104 105 inline port_id 106 ServerLink::SenderPort() 107 { 108 return fSender->Port(); 109 } 110 111 112 inline status_t 113 ServerLink::StartMessage(int32 code, size_t minSize) 114 { 115 return fSender->StartMessage(code, minSize); 116 } 117 118 119 inline status_t 120 ServerLink::EndMessage() 121 { 122 return fSender->EndMessage(); 123 } 124 125 126 inline void 127 ServerLink::CancelMessage() 128 { 129 fSender->CancelMessage(); 130 } 131 132 133 inline status_t 134 ServerLink::Flush(bigtime_t timeout, bool needsReply) 135 { 136 return fSender->Flush(timeout, needsReply); 137 } 138 139 140 inline status_t 141 ServerLink::Attach(const void* data, ssize_t size) 142 { 143 return fSender->Attach(data, size); 144 } 145 146 147 inline status_t 148 ServerLink::AttachString(const char* string, int32 length) 149 { 150 return fSender->AttachString(string, length); 151 } 152 153 154 template<class Type> status_t 155 ServerLink::Attach(const Type& data) 156 { 157 return Attach(&data, sizeof(Type)); 158 } 159 160 161 // #pragma mark - receiver inline functions 162 163 164 inline void 165 ServerLink::SetReceiverPort(port_id port) 166 { 167 fReceiver->SetPort(port); 168 } 169 170 171 inline port_id 172 ServerLink::ReceiverPort() 173 { 174 return fReceiver->Port(); 175 } 176 177 178 inline status_t 179 ServerLink::GetNextMessage(int32& code, bigtime_t timeout) 180 { 181 return fReceiver->GetNextMessage(code, timeout); 182 } 183 184 185 inline bool 186 ServerLink::NeedsReply() const 187 { 188 return fReceiver->NeedsReply(); 189 } 190 191 192 inline status_t 193 ServerLink::Read(void* data, ssize_t size) 194 { 195 return fReceiver->Read(data, size); 196 } 197 198 199 inline status_t 200 ServerLink::ReadString(char* buffer, size_t bufferSize) 201 { 202 return fReceiver->ReadString(buffer, bufferSize); 203 } 204 205 206 inline status_t 207 ServerLink::ReadString(BString& string, size_t* _length) 208 { 209 return fReceiver->ReadString(string, _length); 210 } 211 212 213 inline status_t 214 ServerLink::ReadString(char** _string, size_t* _length) 215 { 216 return fReceiver->ReadString(_string, _length); 217 } 218 219 220 template <class Type> status_t 221 ServerLink::Read(Type* data) 222 { 223 return Read(data, sizeof(Type)); 224 } 225 226 227 } // namespace BPrivate 228 229 #endif /* _SERVER_LINK_H */ 230