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:
Axel Dörfler
2005-06-07 22:36:03 +00:00
parent 7475dcdf3a
commit 750b92faf3
8 changed files with 526 additions and 539 deletions
+44 -59
View File
@@ -1,71 +1,56 @@
//------------------------------------------------------------------------------ /*
// Copyright (c) 2001-2002, Haiku * Copyright 2001-2005, Haiku.
// * Distributed under the terms of the MIT License.
// Permission is hereby granted, free of charge, to any person obtaining a *
// copy of this software and associated documentation files (the "Software"), * Authors:
// to deal in the Software without restriction, including without limitation * DarkWyrm <[email protected]>
// the rights to use, copy, modify, merge, publish, distribute, sublicense, * Pahtz <[email protected]>
// 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
//
//------------------------------------------------------------------------------
#ifndef _LINKMSGREADER_H #ifndef _LINKMSGREADER_H
#define _LINKMSGREADER_H #define _LINKMSGREADER_H
#include <OS.h> #include <OS.h>
class LinkMsgReader
{
public:
LinkMsgReader(port_id port);
virtual ~LinkMsgReader(void);
virtual void SetPort(port_id port); //namespace BPrivate {
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;
int32 fRecvPosition; //current read position class LinkMsgReader {
public:
LinkMsgReader(port_id port);
virtual ~LinkMsgReader(void);
int32 fRecvStart; //start of current message void SetPort(port_id port);
port_id Port(void) { return fReceivePort; }
int32 fRecvBufferSize;
int32 fDataSize; //size of data in recv buffer status_t GetNextMessage(int32 *code, bigtime_t timeout = B_INFINITE_TIMEOUT);
int32 fReplySize; //size of current reply message status_t Read(void *data, ssize_t size);
status_t ReadString(char **string);
status_t fReadError; //Read failed for current message 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 #endif
+45 -64
View File
@@ -1,82 +1,63 @@
//------------------------------------------------------------------------------ /*
// Copyright (c) 2001-2002, OpenBeOS * Copyright 2001-2005, Haiku.
// * Distributed under the terms of the MIT License.
// Permission is hereby granted, free of charge, to any person obtaining a *
// copy of this software and associated documentation files (the "Software"), * Authors:
// to deal in the Software without restriction, including without limitation * DarkWyrm <[email protected]>
// the rights to use, copy, modify, merge, publish, distribute, sublicense, * Pahtz <[email protected]>
// and/or sell copies of the Software, and to permit persons to whom the * Axel Dörfler, [email protected]e
// 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
//
//------------------------------------------------------------------------------
#ifndef LINKMSGSENDER_H #ifndef LINKMSGSENDER_H
#define LINKMSGSENDER_H #define LINKMSGSENDER_H
#include <OS.h> #include <OS.h>
class LinkMsgSender
{
public:
LinkMsgSender(port_id sendport);
virtual ~LinkMsgSender(void);
status_t StartMessage(int32 code); //namespace BPrivate {
void CancelMessage(void);
status_t EndMessage(void);
status_t Flush(bigtime_t timeout = B_INFINITE_TIMEOUT);
// see BPrivate::BAppServerLink which inherits from BPortLink class LinkMsgSender {
//status_t FlushWithReply(int32 *code); public:
LinkMsgSender(port_id sendport);
virtual ~LinkMsgSender(void);
void SetPort(port_id port); void SetPort(port_id port);
port_id GetPort(); port_id Port() { return fPort; }
status_t Attach(const void *data, ssize_t size); status_t StartMessage(int32 code, size_t minSize = 0);
status_t AttachString(const char *string); void CancelMessage(void);
template <class Type> status_t Attach(const Type& data) status_t EndMessage(bool needsReply = false);
{
return Attach(&data, sizeof(Type));
}
protected: status_t Flush(bigtime_t timeout = B_INFINITE_TIMEOUT, bool needsReply = false);
status_t FlushCompleted(ssize_t newbuffersize);
status_t AdjustReplyBuffer(bigtime_t timeout);
void ResetReplyBuffer();
port_id fSendPort;
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 protected:
size_t SpaceLeft() const { return fBufferSize - fCurrentEnd; }
int32 fSendBufferSize; 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 port_id fPort;
int32 fReplySize; //size of current reply message
char *fBuffer;
status_t fWriteError; //Attach failed for current message 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
View File
@@ -1,34 +1,18 @@
//------------------------------------------------------------------------------ /*
// Copyright (c) 2001-2002, OpenBeOS * Copyright 2001-2005, Haiku.
// * Distributed under the terms of the MIT License.
// Permission is hereby granted, free of charge, to any person obtaining a *
// copy of this software and associated documentation files (the "Software"), * Authors:
// to deal in the Software without restriction, including without limitation * DarkWyrm <[email protected]>
// the rights to use, copy, modify, merge, publish, distribute, sublicense, * Pahtz <[email protected]>
// and/or sell copies of the Software, and to permit persons to whom the * Axel Dörfler, [email protected]e
// 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
//
//------------------------------------------------------------------------------
#ifndef _PORTLINK_H #ifndef _PORTLINK_H
#define _PORTLINK_H #define _PORTLINK_H
#include <OS.h> #include <OS.h>
#include <LinkMsgReader.h>
#include <LinkMsgSender.h>
/* /*
Error checking rules: (for if you don't want to check every return code) Error checking rules: (for if you don't want to check every return code)
@@ -45,48 +29,144 @@
*/ */
class LinkMsgReader; // ToDo: put this into the private namespace
class LinkMsgSender; //namespace BPrivate {
class BPortLink
{
public:
BPortLink(port_id send = -1, port_id reply = -1);
virtual ~BPortLink();
status_t StartMessage(int32 code); //class LinkMsgReader;
void CancelMessage(); //class LinkMsgSender;
status_t EndMessage();
status_t Flush(bigtime_t timeout = B_INFINITE_TIMEOUT); class BPortLink {
public:
void SetSendPort(port_id port); BPortLink(port_id send = -1, port_id reply = -1);
port_id GetSendPort(); virtual ~BPortLink();
void SetReplyPort(port_id port);
port_id GetReplyPort();
status_t Attach(const void *data, ssize_t size); // send methods
status_t AttachString(const char *string);
status_t AttachRegion(const BRegion &region);
status_t AttachShape(BShape &shape);
template <class Type> status_t Attach(const Type& data)
{
return Attach(&data, sizeof(Type));
}
status_t GetNextReply(int32 *code, bigtime_t timeout = B_INFINITE_TIMEOUT); void SetSendPort(port_id port);
status_t Read(void *data, ssize_t size); port_id SendPort();
status_t ReadString(char **string);
status_t ReadRegion(BRegion *region); status_t StartMessage(int32 code, size_t minSize = 0);
status_t ReadShape(BShape *shape); void CancelMessage();
template <class T> status_t Read(T *data) status_t EndMessage();
{
return Read(data,sizeof(T)); 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);
protected: status_t AttachRegion(const BRegion &region);
LinkMsgReader *fReader; status_t AttachShape(BShape &shape);
LinkMsgSender *fSender; 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 */
+71 -72
View File
@@ -30,9 +30,6 @@ extern const char *bstrcode(int32 code);
# define STRACE(x) ; # define STRACE(x) ;
#endif #endif
static const int32 kInitialReceiveBufferSize = 2048;
static const int32 kMaxReceiveBufferSize = 2048;
LinkMsgReader::LinkMsgReader(port_id port) LinkMsgReader::LinkMsgReader(port_id port)
: :
@@ -56,13 +53,6 @@ LinkMsgReader::SetPort(port_id port)
} }
port_id
LinkMsgReader::GetPort()
{
return fReceivePort;
}
status_t status_t
LinkMsgReader::GetNextMessage(int32 *code, bigtime_t timeout) LinkMsgReader::GetNextMessage(int32 *code, bigtime_t timeout)
{ {
@@ -82,7 +72,7 @@ LinkMsgReader::GetNextMessage(int32 *code, bigtime_t timeout)
remaining = fDataSize; remaining = fDataSize;
header = (message_header *)fRecvBuffer; header = (message_header *)fRecvBuffer;
} else { } else {
fRecvStart += fReplySize; //start of the next message fRecvStart += fReplySize; // start of the next message
fRecvPosition = fRecvStart; fRecvPosition = fRecvStart;
header = (message_header *)(fRecvBuffer + fRecvStart); header = (message_header *)(fRecvBuffer + fRecvStart);
} }
@@ -126,45 +116,46 @@ status_t
LinkMsgReader::AdjustReplyBuffer(bigtime_t timeout) LinkMsgReader::AdjustReplyBuffer(bigtime_t timeout)
{ {
// Here we take advantage of the compiler's dead-code elimination // Here we take advantage of the compiler's dead-code elimination
if (kInitialReceiveBufferSize == kMaxReceiveBufferSize) { if (kInitialBufferSize == kMaxBufferSize) {
// fixed buffer size // fixed buffer size
if (fRecvBuffer != NULL) if (fRecvBuffer != NULL)
return B_OK; return B_OK;
fRecvBuffer = (char *)malloc(kInitialReceiveBufferSize); fRecvBuffer = (char *)malloc(kInitialBufferSize);
if (fRecvBuffer == NULL) if (fRecvBuffer == NULL)
return B_NO_MEMORY; return B_NO_MEMORY;
fRecvBufferSize = kInitialReceiveBufferSize;
fRecvBufferSize = kInitialBufferSize;
} else { } else {
STRACE(("info: LinkMsgReader getting port_buffer_size().\n")); STRACE(("info: LinkMsgReader getting port_buffer_size().\n"));
ssize_t buffersize; ssize_t bufferSize;
if (timeout == B_INFINITE_TIMEOUT) if (timeout == B_INFINITE_TIMEOUT)
buffersize = port_buffer_size(fReceivePort); bufferSize = port_buffer_size(fReceivePort);
else else
buffersize = port_buffer_size_etc(fReceivePort, B_TIMEOUT, timeout); bufferSize = port_buffer_size_etc(fReceivePort, B_TIMEOUT, timeout);
STRACE(("info: LinkMsgReader got port_buffer_size() = %ld.\n", buffersize)); STRACE(("info: LinkMsgReader got port_buffer_size() = %ld.\n", bufferSize));
if (buffersize < 0) if (bufferSize < 0)
return (status_t)buffersize; return (status_t)bufferSize;
// make sure our receive buffer is large enough // make sure our receive buffer is large enough
if (buffersize > fRecvBufferSize) { if (bufferSize > fRecvBufferSize) {
if (buffersize <= kInitialReceiveBufferSize) if (bufferSize <= (ssize_t)kInitialBufferSize)
buffersize = kInitialReceiveBufferSize; bufferSize = (ssize_t)kInitialBufferSize;
else else
buffersize = (buffersize + B_PAGE_SIZE) - (buffersize % B_PAGE_SIZE); bufferSize = (bufferSize + B_PAGE_SIZE) - (bufferSize % B_PAGE_SIZE);
if (buffersize > kMaxReceiveBufferSize) if (bufferSize > (ssize_t)kMaxBufferSize)
return B_ERROR; //we can't continue return B_ERROR; // we can't continue
STRACE(("info: LinkMsgReader setting receive buffersize to %ld.\n", buffersize)); STRACE(("info: LinkMsgReader setting receive buffersize to %ld.\n", bufferSize));
char *buffer = (char *)malloc(buffersize); char *buffer = (char *)malloc(bufferSize);
if (buffer == NULL) if (buffer == NULL)
return B_NO_MEMORY; return B_NO_MEMORY;
free(fRecvBuffer); free(fRecvBuffer);
fRecvBuffer = buffer; fRecvBuffer = buffer;
fRecvBufferSize = buffersize; fRecvBufferSize = bufferSize;
} }
} }
@@ -182,34 +173,39 @@ LinkMsgReader::ReadFromPort(bigtime_t timeout)
if (err < B_OK) if (err < B_OK)
return err; return err;
int32 protocol; int32 code;
ssize_t bytesread; ssize_t bytesRead;
STRACE(("info: LinkMsgReader reading port %ld.\n", fReceivePort)); STRACE(("info: LinkMsgReader reading port %ld.\n", fReceivePort));
if (timeout != B_INFINITE_TIMEOUT) { while (true) {
do { if (timeout != B_INFINITE_TIMEOUT) {
bytesread = read_port_etc(fReceivePort, &protocol, fRecvBuffer, do {
fRecvBufferSize, B_TIMEOUT, timeout); bytesRead = read_port_etc(fReceivePort, &code, fRecvBuffer,
} while(bytesread == B_INTERRUPTED); fRecvBufferSize, B_TIMEOUT, timeout);
} else { } while (bytesRead == B_INTERRUPTED);
do { } else {
bytesread = read_port(fReceivePort, &protocol, fRecvBuffer, do {
fRecvBufferSize); bytesRead = read_port(fReceivePort, &code, fRecvBuffer,
} while(bytesread == B_INTERRUPTED); fRecvBufferSize);
} while (bytesRead == B_INTERRUPTED);
}
STRACE(("info: LinkMsgReader read %ld bytes.\n", bytesRead));
if (bytesRead < B_OK)
return bytesRead;
// we just ignore incorrect messages, and don't bother our caller
if (code != kLinkCode) {
STRACE(("wrong port message %lx received.\n", code));
continue;
}
// port read seems to be valid
break;
} }
STRACE(("info: LinkMsgReader read %ld bytes.\n", bytesread)); fDataSize = bytesRead;
if (bytesread < B_OK)
return bytesread;
// TODO: we only need AS_SERVER_PORTLINK when all OBOS uses Link messages
if (protocol != AS_SERVER_PORTLINK && protocol != AS_SERVER_SESSION)
return B_ERROR;
if (protocol == AS_SERVER_PORTLINK && bytesread != *((int32 *)fRecvBuffer))
// should only be one message for PORTLINK so the size declared in the header
// (the first int32 in the header) should be the same as bytesread
return B_ERROR;
fDataSize = bytesread;
return B_OK; return B_OK;
} }
@@ -242,33 +238,36 @@ LinkMsgReader::Read(void *data, ssize_t size)
status_t status_t
LinkMsgReader::ReadString(char **string) LinkMsgReader::ReadString(char **_string)
{ {
status_t err; int32 length = 0;
int32 len = 0; status_t status;
err = Read<int32>(&len); status = Read<int32>(&length);
if (err < B_OK) if (status < B_OK)
return err; return status;
if (len) { if (length > 0) {
*string = (char *)malloc(len); char *string = (char *)malloc(length);
if (*string == NULL) { if (string == NULL) {
fRecvPosition -= sizeof(int32); //rewind the transaction fRecvPosition -= sizeof(int32); // rewind the transaction
return B_NO_MEMORY; return B_NO_MEMORY;
} }
err = Read(*string, len); status = Read(string, length);
if (err < B_OK) { if (status < B_OK) {
free(*string); free(string);
*string = NULL; fRecvPosition -= sizeof(int32); // rewind the transaction
fRecvPosition -= sizeof(int32); //rewind the transaction return status;
return err;
} }
(*string)[len-1] = '\0';
// make sure the string is null terminated (although it already should be)
string[length - 1] = '\0';
*_string = string;
return B_OK; return B_OK;
} else { } else {
fRecvPosition -= sizeof(int32); //rewind the transaction fRecvPosition -= sizeof(int32); // rewind the transaction
return B_ERROR; return B_ERROR;
} }
} }
+176 -180
View File
@@ -4,7 +4,7 @@
* *
* Authors: * Authors:
* Pahtz <[email protected]> * Pahtz <[email protected]>
* Axel Dörfler * Axel Dörfler, [email protected]
*/ */
/** Class for low-overhead port-based messaging */ /** Class for low-overhead port-based messaging */
@@ -30,80 +30,92 @@
# define STRACE(x) ; # define STRACE(x) ;
#endif #endif
//set Initial==Max for a fixed buffer size static const size_t kWatermark = kInitialBufferSize - 24;
static const int32 kInitialSendBufferSize = 2048; // if a message is started after this mark, the buffer is flushed automatically
static const int32 kMaxSendBufferSize = 2048;
LinkMsgSender::LinkMsgSender(port_id send) LinkMsgSender::LinkMsgSender(port_id port)
: :
fSendPort(send), fSendBuffer(NULL), fSendPosition(0), fSendStart(0), fPort(port),
fSendBufferSize(0), fSendCount(0), fDataSize(0), fBuffer(NULL),
fReplySize(0), fWriteError(B_OK) fBufferSize(0),
fCurrentEnd(0),
fCurrentStart(0),
fCurrentStatus(B_OK)
{ {
} }
LinkMsgSender::~LinkMsgSender() LinkMsgSender::~LinkMsgSender()
{ {
free(fSendBuffer); free(fBuffer);
}
void
LinkMsgSender::SetPort(port_id port)
{
fPort = port;
} }
status_t status_t
LinkMsgSender::StartMessage(int32 code) LinkMsgSender::StartMessage(int32 code, size_t minSize)
{ {
// end previous message // end previous message
if (EndMessage() < B_OK) if (EndMessage() < B_OK)
CancelMessage(); CancelMessage();
if (fSendBufferSize == 0) { if (minSize > kMaxBufferSize - sizeof(message_header))
fSendBuffer = (char *)malloc(kInitialSendBufferSize); return fCurrentStatus = B_BUFFER_OVERFLOW;
if (fSendBuffer == NULL) {
fWriteError = B_NO_MEMORY; minSize += sizeof(message_header);
return B_NO_MEMORY;
} // Eventually flush buffer to make space for the new message.
fSendBufferSize = kInitialSendBufferSize; // Note, we do not take the actual buffer size into account to not
// delay the time between buffer flushes too much.
if (fBufferSize > 0 && (minSize > SpaceLeft() || fCurrentStart >= kWatermark)) {
status_t status = Flush();
if (status < B_OK)
return status;
} }
status_t err; if (minSize > fBufferSize) {
// must have space for at least size + code + flags if (AdjustBuffer(minSize) != B_OK)
if (fSendBufferSize - fSendPosition < (int32)sizeof(message_header)) { return fCurrentStatus = B_NO_MEMORY;
err = Flush(); // will set fSendPosition and fSendStart to 0
if (err < B_OK)
return err;
} }
message_header *header = (message_header *)(fSendBuffer + fSendStart); message_header *header = (message_header *)(fBuffer + fCurrentStart);
header->size = 0; header->size = 0;
// will be set later // will be set later
header->code = code; header->code = code;
header->flags = 0; header->flags = 0;
STRACE(("info: LinkMsgSender buffered header %s [%lu %lu %lu].\n", STRACE(("info: LinkMsgSender buffered header %s (%lx) [%lu %lu %lu].\n",
strcode(code), header->size, header->code, header->flags)); strcode(code), code, header->size, header->code, header->flags));
fSendPosition += sizeof(message_header); fCurrentEnd += sizeof(message_header);
return B_OK; return B_OK;
} }
status_t status_t
LinkMsgSender::EndMessage() LinkMsgSender::EndMessage(bool needsReply)
{ {
if (fSendPosition == fSendStart || fWriteError < B_OK) if (fCurrentEnd == fCurrentStart || fCurrentStatus < B_OK)
return fWriteError; return fCurrentStatus;
// record the size of the message // record the size of the message
message_header *header = (message_header *)(fSendBuffer + fSendStart); message_header *header = (message_header *)(fBuffer + fCurrentStart);
header->size = fSendPosition - fSendStart; header->size = CurrentMessageSize();
if (needsReply)
header->flags |= needsReply;
STRACE(("info: LinkMsgSender EndMessage() of size %ld.\n", header->size)); STRACE(("info: LinkMsgSender EndMessage() of size %ld.\n", header->size));
fSendCount++; // increase the number of completed messages // bump to start of next message
fCurrentStart = fCurrentEnd;
fSendStart = fSendPosition; // start of next new message
return B_OK; return B_OK;
} }
@@ -111,177 +123,161 @@ LinkMsgSender::EndMessage()
void void
LinkMsgSender::CancelMessage() LinkMsgSender::CancelMessage()
{ {
fSendPosition = fSendStart; fCurrentEnd = fCurrentStart;
fWriteError = B_OK; fCurrentStatus = B_OK;
} }
status_t status_t
LinkMsgSender::Attach(const void *data, ssize_t size) LinkMsgSender::Attach(const void *data, size_t size)
{ {
if (fWriteError < B_OK) if (fCurrentStatus < B_OK)
return fWriteError; return fCurrentStatus;
if (size <= 0) { if (size == 0)
fWriteError = B_BAD_VALUE; return fCurrentStatus = B_BAD_VALUE;
return B_BAD_VALUE;
}
if (fSendPosition == fSendStart) if (fCurrentEnd == fCurrentStart)
return B_NO_INIT; // need to call StartMessage() first return B_NO_INIT; // need to call StartMessage() first
int32 remaining = fSendBufferSize - fSendPosition; if (SpaceLeft() < size) {
if (remaining < size) {
// we have to make space for the data // we have to make space for the data
int32 total = size + (fSendPosition - fSendStart); status_t status = FlushCompleted(size + CurrentMessageSize());
// resulting size of current message if (status < B_OK)
return fCurrentStatus = status;
int32 newbuffersize;
if (total <= fSendBufferSize)
newbuffersize = fSendBufferSize; // no change
else if (total > kMaxSendBufferSize) {
fWriteError = B_BAD_VALUE;
return B_BAD_VALUE;
} else if (total <= kInitialSendBufferSize)
newbuffersize = kInitialSendBufferSize;
else
newbuffersize = (total + B_PAGE_SIZE) - (total % B_PAGE_SIZE);
// FlushCompleted() to make space
status_t err;
err = FlushCompleted(newbuffersize);
if (err < B_OK) {
fWriteError = err;
return err;
}
} }
memcpy(fSendBuffer + fSendPosition, data, size); memcpy(fBuffer + fCurrentEnd, data, size);
fSendPosition += size; fCurrentEnd += size;
return fWriteError;
}
status_t
LinkMsgSender::FlushCompleted(ssize_t newbuffersize)
{
char *buffer = NULL;
if (newbuffersize == fSendBufferSize)
buffer = fSendBuffer; // keep existing buffer
else {
// create new larger buffer
buffer = (char *)malloc(newbuffersize);
if (buffer == NULL)
return B_NO_MEMORY;
}
int32 position = fSendPosition;
int32 start = fSendStart;
fSendPosition = fSendStart; // trick to hide the incomplete message
status_t err;
err = Flush();
if (err < B_OK) {
fSendPosition = position;
if (buffer != fSendBuffer)
free(buffer);
return err;
}
// move the incomplete message to the start of the buffer
fSendPosition = min_c(position - start, newbuffersize);
memcpy(buffer, fSendBuffer + start, fSendPosition);
if (fSendBuffer != buffer) {
free(fSendBuffer);
fSendBuffer = buffer;
fSendBufferSize = newbuffersize;
}
return B_OK; return B_OK;
} }
void
LinkMsgSender::SetPort(port_id port)
{
fSendPort = port;
}
port_id
LinkMsgSender::GetPort()
{
return fSendPort;
}
status_t
LinkMsgSender::Flush(bigtime_t timeout)
{
if (fWriteError < B_OK)
return fWriteError;
EndMessage();
if (fSendCount == 0)
return B_OK;
STRACE(("info: LinkMsgSender Flush() waiting to send %ld messages of %ld bytes on port %ld.\n", fSendCount, fSendPosition, fSendPort));
// TODO: we only need AS_SERVER_PORTLINK when all OBOS uses LinkMsgSender
int32 protocol = (fSendCount > 1 ? AS_SERVER_SESSION : AS_SERVER_PORTLINK);
status_t err;
if (timeout != B_INFINITE_TIMEOUT) {
do {
err = write_port_etc(fSendPort, protocol, fSendBuffer,
fSendPosition, B_RELATIVE_TIMEOUT, timeout);
} while(err == B_INTERRUPTED);
} else {
do {
err = write_port(fSendPort, protocol, fSendBuffer, fSendPosition);
} while(err == B_INTERRUPTED);
}
if (err == B_OK) {
STRACE(("info: LinkMsgSender Flush() %ld messages total of %ld bytes on port %ld.\n", fSendCount, fSendPosition, fSendPort));
fSendPosition = 0;
fSendStart = 0;
fSendCount = 0;
return B_OK;
}
STRACE(("error info: LinkMsgSender Flush() failed for %ld bytes (%s) on port %ld.\n", fSendPosition, strerror(err), fSendPort));
return err;
}
status_t status_t
LinkMsgSender::AttachString(const char *string) LinkMsgSender::AttachString(const char *string)
{ {
status_t err; if (string == NULL)
if (string == NULL) {
// TODO: This whole comm thing is so broken.... if we're for some
// reason attaching a NULL string, and don't do it, the receiving
// party will still try to read a string!! The whole communication
// will be messed up if the stream does not contain what the client
// things it contains. This is a quick fix (but just for this
// particular problem). -Stephan
// return B_BAD_VALUE;
string = ""; string = "";
int32 length = strlen(string) + 1;
status_t status = Attach<int32>(length);
if (status < B_OK)
return status;
status = Attach(string, length);
if (status < B_OK)
fCurrentEnd -= sizeof(int32); // rewind the transaction
return status;
}
status_t
LinkMsgSender::AdjustBuffer(size_t newSize, char **_oldBuffer)
{
// make sure the new size is within bounds
if (newSize <= kInitialBufferSize)
newSize = kInitialBufferSize;
else if (newSize > kMaxBufferSize)
return B_BUFFER_OVERFLOW;
else if (newSize > kInitialBufferSize)
newSize = (newSize + B_PAGE_SIZE - 1) & ~(B_PAGE_SIZE - 1);
char *buffer = NULL;
if (newSize == fBufferSize) {
// keep existing buffer
if (_oldBuffer)
*_oldBuffer = fBuffer;
return B_OK;
} }
int32 len = strlen(string) + 1; // create new larger buffer
err = Attach<int32>(len); buffer = (char *)malloc(newSize);
if (err < B_OK) if (buffer == NULL)
return B_NO_MEMORY;
if (_oldBuffer)
*_oldBuffer = fBuffer;
else
free(fBuffer);
fBuffer = buffer;
fBufferSize = newSize;
return B_OK;
}
status_t
LinkMsgSender::FlushCompleted(size_t newBufferSize)
{
// we need to hide the incomplete message so that it's not flushed
int32 end = fCurrentEnd;
int32 start = fCurrentStart;
fCurrentEnd = fCurrentStart;
status_t status = Flush();
if (status < B_OK) {
fCurrentEnd = end;
return status;
}
char *oldBuffer = NULL;
status = AdjustBuffer(newBufferSize, &oldBuffer);
if (status != B_OK)
return status;
// move the incomplete message to the start of the buffer
fCurrentEnd = end - start;
if (oldBuffer != fBuffer) {
memcpy(fBuffer, oldBuffer + start, fCurrentEnd);
free(oldBuffer);
} else
memmove(fBuffer, fBuffer + start, fCurrentEnd);
return B_OK;
}
status_t
LinkMsgSender::Flush(bigtime_t timeout, bool needsReply)
{
if (fCurrentStatus < B_OK)
return fCurrentStatus;
EndMessage(needsReply);
if (fCurrentStart == 0)
return B_OK;
STRACE(("info: LinkMsgSender Flush() waiting to send messages of %ld bytes on port %ld.\n",
fCurrentEnd, fPort));
status_t err;
if (timeout != B_INFINITE_TIMEOUT) {
do {
err = write_port_etc(fPort, kLinkCode, fBuffer,
fCurrentEnd, B_RELATIVE_TIMEOUT, timeout);
} while (err == B_INTERRUPTED);
} else {
do {
err = write_port(fPort, kLinkCode, fBuffer, fCurrentEnd);
} while (err == B_INTERRUPTED);
}
if (err < B_OK) {
STRACE(("error info: LinkMsgSender Flush() failed for %ld bytes (%s) on port %ld.\n",
fCurrentEnd, strerror(err), fPort));
return err; return err;
}
err = Attach(string, len); STRACE(("info: LinkMsgSender Flush() messages total of %ld bytes on port %ld.\n",
if (err < B_OK) fCurrentEnd, fPort));
fSendPosition -= sizeof(int32); // rewind the transaction
return err; fCurrentEnd = 0;
fCurrentStart = 0;
return B_OK;
} }
+28 -97
View File
@@ -1,118 +1,43 @@
//------------------------------------------------------------------------------ /*
// Copyright (c) 2001-2002, Haiku, Inc. * Copyright 2001-2005, Haiku.
// * Distributed under the terms of the MIT License.
// Permission is hereby granted, free of charge, to any person obtaining a *
// copy of this software and associated documentation files (the "Software"), * Authors:
// to deal in the Software without restriction, including without limitation * Pahtz <[email protected]>
// the rights to use, copy, modify, merge, publish, distribute, sublicense, * Axel Dörfler, [email protected]
// 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:
// /** Class for low-overhead port-based messaging */
// 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.cpp
// Author: Pahtz <[email protected]>
// Description: Class for low-overhead port-based messaging
//
//------------------------------------------------------------------------------
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <new> #include <new>
#include <Region.h> #include <Region.h>
#include <Shape.h> #include <Shape.h>
#include <LinkMsgReader.h> #include <LinkMsgReader.h>
#include <LinkMsgSender.h> #include <LinkMsgSender.h>
#include <PortLink.h> #include <PortLink.h>
#include <ServerProtocol.h> #include <ServerProtocol.h>
BPortLink::BPortLink(port_id send, port_id receive) :
fReader(new LinkMsgReader(receive)), fSender(new LinkMsgSender(send)) BPortLink::BPortLink(port_id send, port_id receive)
:
fReader(new LinkMsgReader(receive)),
fSender(new LinkMsgSender(send))
{ {
} }
BPortLink::~BPortLink() BPortLink::~BPortLink()
{ {
delete fReader; delete fReader;
delete fSender; delete fSender;
} }
status_t BPortLink::StartMessage(int32 code)
{
return fSender->StartMessage(code);
}
status_t BPortLink::EndMessage() status_t
{ BPortLink::ReadRegion(BRegion *region)
return fSender->EndMessage();
}
void BPortLink::CancelMessage()
{
fSender->CancelMessage();
}
status_t BPortLink::Attach(const void *data, ssize_t size)
{
return fSender->Attach(data,size);
}
void BPortLink::SetSendPort( port_id port )
{
fSender->SetPort(port);
}
port_id BPortLink::GetSendPort()
{
return fSender->GetPort();
}
void BPortLink::SetReplyPort( port_id port )
{
fReader->SetPort(port);
}
port_id BPortLink::GetReplyPort()
{
return fReader->GetPort();
}
status_t BPortLink::Flush(bigtime_t timeout)
{
return fSender->Flush(timeout);
}
status_t BPortLink::GetNextReply(int32 *code, bigtime_t timeout)
{
return fReader->GetNextMessage(code,timeout);
}
status_t BPortLink::Read(void *data, ssize_t size)
{
return fReader->Read(data,size);
}
status_t BPortLink::ReadString(char **string)
{
return fReader->ReadString(string);
}
status_t BPortLink::AttachString(const char *string)
{
return fSender->AttachString(string);
}
status_t BPortLink::ReadRegion(BRegion *region)
{ {
fReader->Read(&region->count, sizeof(long)); fReader->Read(&region->count, sizeof(long));
fReader->Read(&region->bound, sizeof(clipping_rect)); fReader->Read(&region->bound, sizeof(clipping_rect));
@@ -120,14 +45,18 @@ status_t BPortLink::ReadRegion(BRegion *region)
return fReader->Read(region->data, region->count * sizeof(clipping_rect)); return fReader->Read(region->data, region->count * sizeof(clipping_rect));
} }
status_t BPortLink::AttachRegion(const BRegion &region)
status_t
BPortLink::AttachRegion(const BRegion &region)
{ {
fSender->Attach(&region.count, sizeof(long)); fSender->Attach(&region.count, sizeof(long));
fSender->Attach(&region.bound, sizeof(clipping_rect)); fSender->Attach(&region.bound, sizeof(clipping_rect));
return fSender->Attach(region.data, region.count * sizeof(clipping_rect)); return fSender->Attach(region.data, region.count * sizeof(clipping_rect));
} }
status_t BPortLink::ReadShape(BShape *shape)
status_t
BPortLink::ReadShape(BShape *shape)
{ {
int32 opCount, ptCount; int32 opCount, ptCount;
fReader->Read(&opCount, sizeof(int32)); fReader->Read(&opCount, sizeof(int32));
@@ -143,7 +72,9 @@ status_t BPortLink::ReadShape(BShape *shape)
return B_OK; return B_OK;
} }
status_t BPortLink::AttachShape(BShape &shape)
status_t
BPortLink::AttachShape(BShape &shape)
{ {
int32 opCount, ptCount; int32 opCount, ptCount;
uint32 *opList; uint32 *opList;
+1 -1
View File
@@ -49,7 +49,7 @@ ServerMemIO::ServerMemIO(size_t size)
BPrivate::BAppServerLink link; BPrivate::BAppServerLink link;
link.StartMessage(AS_ACQUIRE_SERVERMEM); link.StartMessage(AS_ACQUIRE_SERVERMEM);
link.Attach<size_t>(size); link.Attach<size_t>(size);
link.Attach<port_id>(link.GetReplyPort()); link.Attach<port_id>(link.ReplyPort());
link.Flush(); link.Flush();
int32 code; int32 code;
+15
View File
@@ -1,3 +1,10 @@
/*
* Copyright 2005, Haiku.
* Distributed under the terms of the MIT License.
*
* Authors:
* Axel Dörfler
*/
#ifndef _LINK_MESSAGE_H_ #ifndef _LINK_MESSAGE_H_
#define _LINK_MESSAGE_H_ #define _LINK_MESSAGE_H_
@@ -5,10 +12,18 @@
#include <SupportDefs.h> #include <SupportDefs.h>
static const int32 kLinkCode = '_PTL';
static const size_t kInitialBufferSize = 2048;
static const size_t kMaxBufferSize = 65536;
// anything beyond that should be sent with a different mechanism
struct message_header { struct message_header {
int32 size; int32 size;
uint32 code; uint32 code;
uint32 flags; uint32 flags;
}; };
static const uint32 kNeedsReply = 0x01;
#endif /* _LINK_MESSAGE_H_ */ #endif /* _LINK_MESSAGE_H_ */