Almost rewrote LinkMsgSender; it's now much cleaner and works better:
- StartMessage() can now get a size to make sure there is enough free space - if StartMessage() is called with the current message behind a certain watermark, the buffer is flushed in order to prevent moving around messages in the buffer. The actual value should be tested in real life, though. - enlarged maximum buffer size to 64k - fixed bug: could use memcpy() to move overlapping memory around - added a flag to Flush() that marks messages as needing a reply - the other way would be to mark the message "code" to contain this information Some cleanup in LinkMsgReader. BPortLink now has most methods as inlines. The buffer sizes are now declared in a shared header, so that receiver and sender are always equipped equally. git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@12997 a95241bf-73f2-0310-859d-f6bbb57e9c96
This commit is contained in:
@@ -1,71 +1,56 @@
|
||||
//------------------------------------------------------------------------------
|
||||
// Copyright (c) 2001-2002, Haiku
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person obtaining a
|
||||
// copy of this software and associated documentation files (the "Software"),
|
||||
// to deal in the Software without restriction, including without limitation
|
||||
// the rights to use, copy, modify, merge, publish, distribute, sublicense,
|
||||
// and/or sell copies of the Software, and to permit persons to whom the
|
||||
// Software is furnished to do so, subject to the following conditions:
|
||||
//
|
||||
// The above copyright notice and this permission notice shall be included in
|
||||
// all copies or substantial portions of the Software.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
||||
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
||||
// DEALINGS IN THE SOFTWARE.
|
||||
//
|
||||
// File Name: LinkMsgReader.h
|
||||
// Author: DarkWyrm <[email protected]>
|
||||
// Pahtz <[email protected]>
|
||||
// Description: Class for receiving low-overhead port-based messages
|
||||
//
|
||||
//------------------------------------------------------------------------------
|
||||
/*
|
||||
* Copyright 2001-2005, Haiku.
|
||||
* Distributed under the terms of the MIT License.
|
||||
*
|
||||
* Authors:
|
||||
* DarkWyrm <[email protected]>
|
||||
* Pahtz <[email protected]>
|
||||
*/
|
||||
#ifndef _LINKMSGREADER_H
|
||||
#define _LINKMSGREADER_H
|
||||
|
||||
#include <OS.h>
|
||||
|
||||
class LinkMsgReader
|
||||
{
|
||||
public:
|
||||
LinkMsgReader(port_id port);
|
||||
virtual ~LinkMsgReader(void);
|
||||
|
||||
virtual void SetPort(port_id port);
|
||||
virtual port_id GetPort(void);
|
||||
|
||||
virtual status_t GetNextMessage(int32 *code, bigtime_t timeout = B_INFINITE_TIMEOUT);
|
||||
virtual status_t Read(void *data, ssize_t size);
|
||||
virtual status_t ReadString(char **string);
|
||||
template <class Type> status_t Read(Type *data)
|
||||
{
|
||||
return Read(data, sizeof(Type));
|
||||
}
|
||||
|
||||
protected:
|
||||
virtual status_t ReadFromPort(bigtime_t timeout);
|
||||
virtual status_t AdjustReplyBuffer(bigtime_t timeout);
|
||||
void ResetBuffer();
|
||||
|
||||
port_id fReceivePort;
|
||||
|
||||
char *fRecvBuffer;
|
||||
//namespace BPrivate {
|
||||
|
||||
int32 fRecvPosition; //current read position
|
||||
class LinkMsgReader {
|
||||
public:
|
||||
LinkMsgReader(port_id port);
|
||||
virtual ~LinkMsgReader(void);
|
||||
|
||||
int32 fRecvStart; //start of current message
|
||||
|
||||
int32 fRecvBufferSize;
|
||||
void SetPort(port_id port);
|
||||
port_id Port(void) { return fReceivePort; }
|
||||
|
||||
int32 fDataSize; //size of data in recv buffer
|
||||
int32 fReplySize; //size of current reply message
|
||||
|
||||
status_t fReadError; //Read failed for current message
|
||||
status_t GetNextMessage(int32 *code, bigtime_t timeout = B_INFINITE_TIMEOUT);
|
||||
status_t Read(void *data, ssize_t size);
|
||||
status_t ReadString(char **string);
|
||||
template <class Type> status_t Read(Type *data)
|
||||
{
|
||||
return Read(data, sizeof(Type));
|
||||
}
|
||||
|
||||
protected:
|
||||
virtual status_t ReadFromPort(bigtime_t timeout);
|
||||
virtual status_t AdjustReplyBuffer(bigtime_t timeout);
|
||||
void ResetBuffer();
|
||||
|
||||
port_id fReceivePort;
|
||||
|
||||
char *fRecvBuffer;
|
||||
|
||||
int32 fRecvPosition; //current read position
|
||||
|
||||
int32 fRecvStart; //start of current message
|
||||
|
||||
int32 fRecvBufferSize;
|
||||
|
||||
int32 fDataSize; //size of data in recv buffer
|
||||
int32 fReplySize; //size of current reply message
|
||||
|
||||
status_t fReadError; //Read failed for current message
|
||||
};
|
||||
|
||||
//} // namespace BPrivate
|
||||
|
||||
#endif
|
||||
|
||||
@@ -1,82 +1,63 @@
|
||||
//------------------------------------------------------------------------------
|
||||
// Copyright (c) 2001-2002, OpenBeOS
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person obtaining a
|
||||
// copy of this software and associated documentation files (the "Software"),
|
||||
// to deal in the Software without restriction, including without limitation
|
||||
// the rights to use, copy, modify, merge, publish, distribute, sublicense,
|
||||
// and/or sell copies of the Software, and to permit persons to whom the
|
||||
// Software is furnished to do so, subject to the following conditions:
|
||||
//
|
||||
// The above copyright notice and this permission notice shall be included in
|
||||
// all copies or substantial portions of the Software.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
||||
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
||||
// DEALINGS IN THE SOFTWARE.
|
||||
//
|
||||
// File Name: LinkMsgSender.h
|
||||
// Author: DarkWyrm <[email protected]>
|
||||
// Pahtz <[email protected]>
|
||||
// Description: Class for sending low-overhead port-based messaging
|
||||
//
|
||||
//------------------------------------------------------------------------------
|
||||
/*
|
||||
* Copyright 2001-2005, Haiku.
|
||||
* Distributed under the terms of the MIT License.
|
||||
*
|
||||
* Authors:
|
||||
* DarkWyrm <[email protected]>
|
||||
* Pahtz <[email protected]>
|
||||
* Axel Dörfler, [email protected]e
|
||||
*/
|
||||
#ifndef LINKMSGSENDER_H
|
||||
#define LINKMSGSENDER_H
|
||||
|
||||
|
||||
#include <OS.h>
|
||||
|
||||
class LinkMsgSender
|
||||
{
|
||||
public:
|
||||
LinkMsgSender(port_id sendport);
|
||||
virtual ~LinkMsgSender(void);
|
||||
|
||||
status_t StartMessage(int32 code);
|
||||
void CancelMessage(void);
|
||||
status_t EndMessage(void);
|
||||
|
||||
status_t Flush(bigtime_t timeout = B_INFINITE_TIMEOUT);
|
||||
//namespace BPrivate {
|
||||
|
||||
// see BPrivate::BAppServerLink which inherits from BPortLink
|
||||
//status_t FlushWithReply(int32 *code);
|
||||
class LinkMsgSender {
|
||||
public:
|
||||
LinkMsgSender(port_id sendport);
|
||||
virtual ~LinkMsgSender(void);
|
||||
|
||||
void SetPort(port_id port);
|
||||
port_id GetPort();
|
||||
void SetPort(port_id port);
|
||||
port_id Port() { return fPort; }
|
||||
|
||||
status_t Attach(const void *data, ssize_t size);
|
||||
status_t AttachString(const char *string);
|
||||
template <class Type> status_t Attach(const Type& data)
|
||||
{
|
||||
return Attach(&data, sizeof(Type));
|
||||
}
|
||||
status_t StartMessage(int32 code, size_t minSize = 0);
|
||||
void CancelMessage(void);
|
||||
status_t EndMessage(bool needsReply = false);
|
||||
|
||||
protected:
|
||||
status_t FlushCompleted(ssize_t newbuffersize);
|
||||
status_t AdjustReplyBuffer(bigtime_t timeout);
|
||||
void ResetReplyBuffer();
|
||||
|
||||
port_id fSendPort;
|
||||
status_t Flush(bigtime_t timeout = B_INFINITE_TIMEOUT, bool needsReply = false);
|
||||
|
||||
char *fSendBuffer;
|
||||
// see BPrivate::BAppServerLink which inherits from BPortLink
|
||||
//status_t FlushWithReply(int32 *code);
|
||||
|
||||
int32 fSendPosition; //current append position
|
||||
status_t Attach(const void *data, size_t size);
|
||||
status_t AttachString(const char *string);
|
||||
template <class Type> status_t Attach(const Type& data)
|
||||
{
|
||||
return Attach(&data, sizeof(Type));
|
||||
}
|
||||
|
||||
int32 fSendStart; //start of current message
|
||||
|
||||
int32 fSendBufferSize;
|
||||
protected:
|
||||
size_t SpaceLeft() const { return fBufferSize - fCurrentEnd; }
|
||||
size_t CurrentMessageSize() const { return fCurrentEnd - fCurrentStart; }
|
||||
|
||||
int32 fSendCount; //number of completed messages in buffer
|
||||
status_t AdjustBuffer(size_t newBufferSize, char **_oldBuffer = NULL);
|
||||
status_t FlushCompleted(size_t newBufferSize);
|
||||
|
||||
int32 fDataSize; //size of data in recv buffer
|
||||
int32 fReplySize; //size of current reply message
|
||||
|
||||
status_t fWriteError; //Attach failed for current message
|
||||
port_id fPort;
|
||||
|
||||
char *fBuffer;
|
||||
size_t fBufferSize;
|
||||
|
||||
uint32 fCurrentEnd; // current append position
|
||||
uint32 fCurrentStart; // start of current message
|
||||
|
||||
status_t fCurrentStatus;
|
||||
};
|
||||
|
||||
//} // namespace BPrivate
|
||||
|
||||
#endif
|
||||
#endif /* LINKMSGSENDER_H */
|
||||
|
||||
+146
-66
@@ -1,34 +1,18 @@
|
||||
//------------------------------------------------------------------------------
|
||||
// Copyright (c) 2001-2002, OpenBeOS
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person obtaining a
|
||||
// copy of this software and associated documentation files (the "Software"),
|
||||
// to deal in the Software without restriction, including without limitation
|
||||
// the rights to use, copy, modify, merge, publish, distribute, sublicense,
|
||||
// and/or sell copies of the Software, and to permit persons to whom the
|
||||
// Software is furnished to do so, subject to the following conditions:
|
||||
//
|
||||
// The above copyright notice and this permission notice shall be included in
|
||||
// all copies or substantial portions of the Software.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
||||
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
||||
// DEALINGS IN THE SOFTWARE.
|
||||
//
|
||||
// File Name: PortLink.h
|
||||
// Author: DarkWyrm <[email protected]>
|
||||
// Pahtz <[email protected]>
|
||||
// Description: Class for low-overhead port-based messaging
|
||||
//
|
||||
//------------------------------------------------------------------------------
|
||||
/*
|
||||
* Copyright 2001-2005, Haiku.
|
||||
* Distributed under the terms of the MIT License.
|
||||
*
|
||||
* Authors:
|
||||
* DarkWyrm <[email protected]>
|
||||
* Pahtz <[email protected]>
|
||||
* Axel Dörfler, [email protected]e
|
||||
*/
|
||||
#ifndef _PORTLINK_H
|
||||
#define _PORTLINK_H
|
||||
|
||||
#include <OS.h>
|
||||
#include <LinkMsgReader.h>
|
||||
#include <LinkMsgSender.h>
|
||||
|
||||
/*
|
||||
Error checking rules: (for if you don't want to check every return code)
|
||||
@@ -45,48 +29,144 @@
|
||||
|
||||
*/
|
||||
|
||||
class LinkMsgReader;
|
||||
class LinkMsgSender;
|
||||
class BPortLink
|
||||
{
|
||||
public:
|
||||
BPortLink(port_id send = -1, port_id reply = -1);
|
||||
virtual ~BPortLink();
|
||||
// ToDo: put this into the private namespace
|
||||
//namespace BPrivate {
|
||||
|
||||
status_t StartMessage(int32 code);
|
||||
void CancelMessage();
|
||||
status_t EndMessage();
|
||||
//class LinkMsgReader;
|
||||
//class LinkMsgSender;
|
||||
|
||||
status_t Flush(bigtime_t timeout = B_INFINITE_TIMEOUT);
|
||||
|
||||
void SetSendPort(port_id port);
|
||||
port_id GetSendPort();
|
||||
|
||||
void SetReplyPort(port_id port);
|
||||
port_id GetReplyPort();
|
||||
class BPortLink {
|
||||
public:
|
||||
BPortLink(port_id send = -1, port_id reply = -1);
|
||||
virtual ~BPortLink();
|
||||
|
||||
status_t Attach(const void *data, ssize_t size);
|
||||
status_t AttachString(const char *string);
|
||||
status_t AttachRegion(const BRegion ®ion);
|
||||
status_t AttachShape(BShape &shape);
|
||||
template <class Type> status_t Attach(const Type& data)
|
||||
{
|
||||
return Attach(&data, sizeof(Type));
|
||||
}
|
||||
// send methods
|
||||
|
||||
status_t GetNextReply(int32 *code, bigtime_t timeout = B_INFINITE_TIMEOUT);
|
||||
status_t Read(void *data, ssize_t size);
|
||||
status_t ReadString(char **string);
|
||||
status_t ReadRegion(BRegion *region);
|
||||
status_t ReadShape(BShape *shape);
|
||||
template <class T> status_t Read(T *data)
|
||||
{
|
||||
return Read(data,sizeof(T));
|
||||
}
|
||||
|
||||
protected:
|
||||
LinkMsgReader *fReader;
|
||||
LinkMsgSender *fSender;
|
||||
void SetSendPort(port_id port);
|
||||
port_id SendPort();
|
||||
|
||||
status_t StartMessage(int32 code, size_t minSize = 0);
|
||||
void CancelMessage();
|
||||
status_t EndMessage();
|
||||
|
||||
status_t Flush(bigtime_t timeout = B_INFINITE_TIMEOUT, bool needsReply = false);
|
||||
status_t Attach(const void *data, ssize_t size);
|
||||
status_t AttachString(const char *string);
|
||||
status_t AttachRegion(const BRegion ®ion);
|
||||
status_t AttachShape(BShape &shape);
|
||||
template <class Type> status_t Attach(const Type& data);
|
||||
|
||||
// receive methods
|
||||
|
||||
void SetReplyPort(port_id port);
|
||||
port_id ReplyPort();
|
||||
|
||||
status_t GetNextReply(int32 *code, bigtime_t timeout = B_INFINITE_TIMEOUT);
|
||||
status_t Read(void *data, ssize_t size);
|
||||
status_t ReadString(char **string);
|
||||
status_t ReadRegion(BRegion *region);
|
||||
status_t ReadShape(BShape *shape);
|
||||
template <class Type> status_t Read(Type *data);
|
||||
|
||||
protected:
|
||||
LinkMsgReader *fReader;
|
||||
LinkMsgSender *fSender;
|
||||
};
|
||||
|
||||
#endif
|
||||
// sender inline functions
|
||||
|
||||
inline void
|
||||
BPortLink::SetSendPort(port_id port)
|
||||
{
|
||||
fSender->SetPort(port);
|
||||
}
|
||||
|
||||
inline port_id
|
||||
BPortLink::SendPort()
|
||||
{
|
||||
return fSender->Port();
|
||||
}
|
||||
|
||||
inline status_t
|
||||
BPortLink::StartMessage(int32 code, size_t minSize)
|
||||
{
|
||||
return fSender->StartMessage(code, minSize);
|
||||
}
|
||||
|
||||
inline status_t
|
||||
BPortLink::EndMessage()
|
||||
{
|
||||
return fSender->EndMessage();
|
||||
}
|
||||
|
||||
inline void
|
||||
BPortLink::CancelMessage()
|
||||
{
|
||||
fSender->CancelMessage();
|
||||
}
|
||||
|
||||
inline status_t
|
||||
BPortLink::Flush(bigtime_t timeout, bool needsReply)
|
||||
{
|
||||
return fSender->Flush(timeout, needsReply);
|
||||
}
|
||||
|
||||
inline status_t
|
||||
BPortLink::Attach(const void *data, ssize_t size)
|
||||
{
|
||||
return fSender->Attach(data, size);
|
||||
}
|
||||
|
||||
inline status_t
|
||||
BPortLink::AttachString(const char *string)
|
||||
{
|
||||
return fSender->AttachString(string);
|
||||
}
|
||||
|
||||
template<class Type> status_t
|
||||
BPortLink::Attach(const Type &data)
|
||||
{
|
||||
return Attach(&data, sizeof(Type));
|
||||
}
|
||||
|
||||
// #pragma mark - receiver inline functions
|
||||
|
||||
inline void
|
||||
BPortLink::SetReplyPort(port_id port)
|
||||
{
|
||||
fReader->SetPort(port);
|
||||
}
|
||||
|
||||
inline port_id
|
||||
BPortLink::ReplyPort()
|
||||
{
|
||||
return fReader->Port();
|
||||
}
|
||||
|
||||
inline status_t
|
||||
BPortLink::GetNextReply(int32 *code, bigtime_t timeout)
|
||||
{
|
||||
return fReader->GetNextMessage(code, timeout);
|
||||
}
|
||||
|
||||
inline status_t
|
||||
BPortLink::Read(void *data, ssize_t size)
|
||||
{
|
||||
return fReader->Read(data, size);
|
||||
}
|
||||
|
||||
inline status_t
|
||||
BPortLink::ReadString(char **string)
|
||||
{
|
||||
return fReader->ReadString(string);
|
||||
}
|
||||
|
||||
template <class Type> status_t
|
||||
BPortLink::Read(Type *data)
|
||||
{
|
||||
return Read(data, sizeof(Type));
|
||||
}
|
||||
|
||||
//} // namespace BPrivate
|
||||
|
||||
#endif /* _PORTLINK_H */
|
||||
|
||||
Reference in New Issue
Block a user