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