1 /* 2 * Copyright 2001-2005, 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 _PORTLINK_H 11 #define _PORTLINK_H 12 13 #include <OS.h> 14 #include <LinkMsgReader.h> 15 #include <LinkMsgSender.h> 16 17 /* 18 Error checking rules: (for if you don't want to check every return code) 19 20 Calling EndMessage() is optional, implied by Flush() or StartMessage(). 21 22 If you are sending just one message you only need to test Flush() == B_OK 23 24 If you are buffering multiple messages without calling Flush() you must 25 check EndMessage() == B_OK, or the last Attach() for each message. Check 26 Flush() at the end. 27 28 If you are reading, check the last Read() or ReadString() you perform. 29 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 template <class Type> status_t Attach(const Type& data); 54 55 // receive methods 56 57 void SetReceiverPort(port_id port); 58 port_id ReceiverPort(); 59 60 status_t GetNextMessage(int32 &code, bigtime_t timeout = B_INFINITE_TIMEOUT); 61 bool NeedsReply() const; 62 status_t Read(void *data, ssize_t size); 63 status_t ReadString(char **string); 64 status_t ReadRegion(BRegion *region); 65 status_t ReadShape(BShape *shape); 66 template <class Type> status_t Read(Type *data); 67 68 // convenience methods 69 70 status_t FlushWithReply(int32 &code); 71 LinkSender &Sender() { return *fSender; } 72 LinkReceiver &Receiver() { return *fReceiver; } 73 74 protected: 75 LinkSender *fSender; 76 LinkReceiver *fReceiver; 77 }; 78 79 class PortLink : public ServerLink { 80 public: 81 PortLink(port_id sender = -1, port_id receiver = -1); 82 virtual ~PortLink(); 83 84 void SetTo(port_id sender, port_id receiver); 85 }; 86 87 88 // sender inline functions 89 90 inline void 91 ServerLink::SetSenderPort(port_id port) 92 { 93 fSender->SetPort(port); 94 } 95 96 inline port_id 97 ServerLink::SenderPort() 98 { 99 return fSender->Port(); 100 } 101 102 inline status_t 103 ServerLink::StartMessage(int32 code, size_t minSize) 104 { 105 return fSender->StartMessage(code, minSize); 106 } 107 108 inline status_t 109 ServerLink::EndMessage() 110 { 111 return fSender->EndMessage(); 112 } 113 114 inline void 115 ServerLink::CancelMessage() 116 { 117 fSender->CancelMessage(); 118 } 119 120 inline status_t 121 ServerLink::Flush(bigtime_t timeout, bool needsReply) 122 { 123 return fSender->Flush(timeout, needsReply); 124 } 125 126 inline status_t 127 ServerLink::Attach(const void *data, ssize_t size) 128 { 129 return fSender->Attach(data, size); 130 } 131 132 inline status_t 133 ServerLink::AttachString(const char *string, int32 length) 134 { 135 return fSender->AttachString(string, length); 136 } 137 138 template<class Type> status_t 139 ServerLink::Attach(const Type &data) 140 { 141 return Attach(&data, sizeof(Type)); 142 } 143 144 // #pragma mark - receiver inline functions 145 146 inline void 147 ServerLink::SetReceiverPort(port_id port) 148 { 149 fReceiver->SetPort(port); 150 } 151 152 inline port_id 153 ServerLink::ReceiverPort() 154 { 155 return fReceiver->Port(); 156 } 157 158 inline status_t 159 ServerLink::GetNextMessage(int32 &code, bigtime_t timeout) 160 { 161 return fReceiver->GetNextMessage(code, timeout); 162 } 163 164 inline bool 165 ServerLink::NeedsReply() const 166 { 167 return fReceiver->NeedsReply(); 168 } 169 170 inline status_t 171 ServerLink::Read(void *data, ssize_t size) 172 { 173 return fReceiver->Read(data, size); 174 } 175 176 inline status_t 177 ServerLink::ReadString(char **string) 178 { 179 return fReceiver->ReadString(string); 180 } 181 182 template <class Type> status_t 183 ServerLink::Read(Type *data) 184 { 185 return Read(data, sizeof(Type)); 186 } 187 188 } // namespace BPrivate 189 190 #endif /* _PORTLINK_H */ 191