Little tweaks and fixes too numerous to count, but mostly involved with

adding entry_refs and BMessages.  Lots of stuff for specifiers and also for
sending replies (with thanks to Mike Nordell).


git-svn-id: file:///srv/svn/repos/haiku/trunk/current@3295 a95241bf-73f2-0310-859d-f6bbb57e9c96
This commit is contained in:
ejakowatz 2003-05-23 23:13:02 +00:00
parent 9abc4d2dcd
commit 80e966963f
5 changed files with 235 additions and 25 deletions

View File

@ -260,6 +260,7 @@ virtual ~BMessage();
const void *data, ssize_t data_size); const void *data, ssize_t data_size);
void *operator new(size_t size); void *operator new(size_t size);
void *operator new(size_t, void* p);
void operator delete(void *ptr, size_t size); void operator delete(void *ptr, size_t size);
// Private, reserved, or obsolete ---------------------------------------------- // Private, reserved, or obsolete ----------------------------------------------

View File

@ -141,6 +141,13 @@ struct BMessageFieldFlattenPolicy
} }
}; };
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
template<class T>
struct BMessageFieldGetDataPolicy
{
inline static const void* GetData(const T* data)
{ return (const void*)data; }
};
//------------------------------------------------------------------------------
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
template template
@ -149,7 +156,8 @@ template
class StoragePolicy = BMessageFieldStoragePolicy<T1>, class StoragePolicy = BMessageFieldStoragePolicy<T1>,
class SizePolicy = BMessageFieldSizePolicy<T1>, class SizePolicy = BMessageFieldSizePolicy<T1>,
class PrintPolicy = BMessageFieldPrintPolicy<T1>, class PrintPolicy = BMessageFieldPrintPolicy<T1>,
class FlattenPolicy = BMessageFieldFlattenPolicy<T1> class FlattenPolicy = BMessageFieldFlattenPolicy<T1>,
class GetDataPolicy = BMessageFieldGetDataPolicy<T1>
> >
class BMessageFieldImpl : public BMessageField class BMessageFieldImpl : public BMessageField
{ {
@ -192,10 +200,11 @@ template
class StoragePolicy, class StoragePolicy,
class SizePolicy, class SizePolicy,
class PrintPolicy, class PrintPolicy,
class FlattenPolicy class FlattenPolicy,
class GetDataPolicy
> >
ssize_t ssize_t
BMessageFieldImpl<T1, StoragePolicy, SizePolicy, PrintPolicy, FlattenPolicy>:: BMessageFieldImpl<T1, StoragePolicy, SizePolicy, PrintPolicy, FlattenPolicy, GetDataPolicy>::
FlattenedSize() const FlattenedSize() const
{ {
ssize_t size = 0; ssize_t size = 0;
@ -220,10 +229,11 @@ template
class StoragePolicy, class StoragePolicy,
class SizePolicy, class SizePolicy,
class PrintPolicy, class PrintPolicy,
class FlattenPolicy class FlattenPolicy,
class GetDataPolicy
> >
status_t status_t
BMessageFieldImpl<T1, StoragePolicy, SizePolicy, PrintPolicy, FlattenPolicy>:: BMessageFieldImpl<T1, StoragePolicy, SizePolicy, PrintPolicy, FlattenPolicy, GetDataPolicy>::
Flatten(BDataIO& stream) const Flatten(BDataIO& stream) const
{ {
status_t err = B_OK; status_t err = B_OK;
@ -271,10 +281,11 @@ template
class StoragePolicy, class StoragePolicy,
class SizePolicy, class SizePolicy,
class PrintPolicy, class PrintPolicy,
class FlattenPolicy class FlattenPolicy,
class GetDataPolicy
> >
void void
BMessageFieldImpl<T1, StoragePolicy, SizePolicy, PrintPolicy, FlattenPolicy>:: BMessageFieldImpl<T1, StoragePolicy, SizePolicy, PrintPolicy, FlattenPolicy, GetDataPolicy>::
AddItem(const T1& data) AddItem(const T1& data)
{ {
fData.Add(data); fData.Add(data);
@ -311,10 +322,11 @@ template
class StoragePolicy, class StoragePolicy,
class SizePolicy, class SizePolicy,
class PrintPolicy, class PrintPolicy,
class FlattenPolicy class FlattenPolicy,
class GetDataPolicy
> >
const void* const void*
BMessageFieldImpl<T1, StoragePolicy, SizePolicy, PrintPolicy, FlattenPolicy>:: BMessageFieldImpl<T1, StoragePolicy, SizePolicy, PrintPolicy, FlattenPolicy, GetDataPolicy>::
DataAt(int32 index, ssize_t* size) const DataAt(int32 index, ssize_t* size) const
{ {
if (index > CountItems()) if (index > CountItems())
@ -323,8 +335,8 @@ DataAt(int32 index, ssize_t* size) const
*size = SizePolicy::Size(fData[index]); *size = SizePolicy::Size(fData[index]);
const T1& ref = fData[index]; const T1& ref = fData[index];
const T1* data = &ref; const T1* data = &ref;
return (const void*)data; return GetDataPolicy::GetData(data);//(const void*)data;
} }
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
template template
@ -333,10 +345,11 @@ template
class StoragePolicy, class StoragePolicy,
class SizePolicy, class SizePolicy,
class PrintPolicy, class PrintPolicy,
class FlattenPolicy class FlattenPolicy,
class GetDataPolicy
> >
void void
BMessageFieldImpl<T1, StoragePolicy, SizePolicy, PrintPolicy, FlattenPolicy>:: BMessageFieldImpl<T1, StoragePolicy, SizePolicy, PrintPolicy, FlattenPolicy, GetDataPolicy>::
PrintDataItem(int32 index) const PrintDataItem(int32 index) const
{ {
if (index && FixedSize()) if (index && FixedSize())
@ -569,6 +582,13 @@ struct BMessageFieldFlattenPolicy<BMessage*>
return err; return err;
} }
}; };
// GetData policy specializations ----------------------------------------------
template<>
struct BMessageFieldGetDataPolicy<BDataBuffer>
{
inline static const void* GetData(const BDataBuffer* data)
{ return data->Buffer(); }
};
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
} // namespace BPrivate } // namespace BPrivate

View File

@ -26,6 +26,14 @@
uint32 _checksum_(const uchar *buf, int32 size); uint32 _checksum_(const uchar *buf, int32 size);
namespace BPrivate { // Only putting these here because Be did
status_t entry_ref_flatten(char* buffer, size_t* size, const entry_ref* ref);
status_t entry_ref_unflatten(entry_ref* ref, const char* buffer, size_t size);
status_t entry_ref_swap(char* buffer, size_t size);
} // namespace BPrivate
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
// _set_message_target_ // _set_message_target_
/*! \brief Sets the target of a message. /*! \brief Sets the target of a message.

View File

@ -77,7 +77,7 @@
// Globals --------------------------------------------------------------------- // Globals ---------------------------------------------------------------------
#ifdef USING_TEMPLATE_MADNESS #ifdef USING_TEMPLATE_MADNESS
using BPrivate::BDataBuffer; using namespace BPrivate;
#endif // USING_TEMPLATE_MADNESS #endif // USING_TEMPLATE_MADNESS
const char* B_SPECIFIER_ENTRY = "specifiers"; const char* B_SPECIFIER_ENTRY = "specifiers";
const char* B_PROPERTY_ENTRY = "property"; const char* B_PROPERTY_ENTRY = "property";
@ -740,17 +740,43 @@ status_t BMessage::AddMessenger(const char* name, BMessenger messenger)
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
status_t BMessage::AddRef(const char* name, const entry_ref* ref) status_t BMessage::AddRef(const char* name, const entry_ref* ref)
{ {
#if 0
return fBody->AddData<entry_ref>(name, *ref, B_REF_TYPE); return fBody->AddData<entry_ref>(name, *ref, B_REF_TYPE);
#endif
char* buffer = new(nothrow) char[sizeof (entry_ref) + B_PATH_NAME_LENGTH];
size_t size;
status_t err = entry_ref_flatten(buffer, &size, ref);
if (!err)
{
BDataBuffer DB((void*)buffer, size);
err = fBody->AddData<BDataBuffer>(name, DB, B_REF_TYPE);
}
return err;
} }
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
status_t BMessage::AddMessage(const char* name, const BMessage* msg) status_t BMessage::AddMessage(const char* name, const BMessage* msg)
{ {
BMessage* data = new BMessage(*msg); #if 0
status_t err = fBody->AddData<const BMessage*>(name, msg, B_MESSAGE_TYPE); return fBody->AddData<BMessage>(name, *msg, B_MESSAGE_TYPE);
if (err) #endif
status_t err = B_OK;
ssize_t size = msg->FlattenedSize();
char* buffer = new(nothrow) char[size];
if (buffer)
{ {
delete data; err = msg->Flatten(buffer, size);
if (!err)
{
BDataBuffer DB((void*)buffer, size);
err = fBody->AddData<BDataBuffer>(name, DB, B_MESSAGE_TYPE);
}
} }
else
{
err = B_NO_MEMORY;
}
return err; return err;
} }
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
@ -765,6 +791,10 @@ status_t BMessage::AddFlat(const char* name, BFlattenable* obj, int32 count)
obj->IsFixedSize(), count); obj->IsFixedSize(), count);
delete[] buffer; delete[] buffer;
} }
else
{
err = B_NO_MEMORY;
}
return err; return err;
} }
@ -825,14 +855,19 @@ status_t BMessage::AddData(const char* name, type_code type, const void* data,
break; break;
case B_REF_TYPE: case B_REF_TYPE:
{ {
err = AddRef(name, (entry_ref*)data); // err = AddRef(name, (entry_ref*)data);
BDataBuffer DB((void*)data, numBytes, true);
err = fBody->AddData<BDataBuffer>(name, DB, type);
break; break;
} }
case B_MESSAGE_TYPE: case B_MESSAGE_TYPE:
{ {
BMessage msg; // BMessage msg;
msg.Unflatten((const char*)data); // msg.Unflatten((const char*)data);
err = AddMessage(name, &msg); // err = AddMessage(name, &msg);
// err = AddMessage(name, (BMessage*)data);
BDataBuffer DB((void*)data, numBytes, true);
err = fBody->AddData<BDataBuffer>(name, DB, type);
break; break;
} }
case B_MESSENGER_TYPE: case B_MESSENGER_TYPE:
@ -847,7 +882,7 @@ status_t BMessage::AddData(const char* name, type_code type, const void* data,
default: default:
// TODO: test // TODO: test
// Using the mythical BDataBuffer // Using the mythical BDataBuffer
BDataBuffer DB((void*)data, numBytes); BDataBuffer DB((void*)data, numBytes, true);
err = fBody->AddData<BDataBuffer>(name, DB, type); err = fBody->AddData<BDataBuffer>(name, DB, type);
break; break;
} }
@ -927,7 +962,18 @@ status_t BMessage::FindRef(const char* name, entry_ref* ref) const
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
status_t BMessage::FindRef(const char* name, int32 index, entry_ref* ref) const status_t BMessage::FindRef(const char* name, int32 index, entry_ref* ref) const
{ {
#if 0
return fBody->FindData<entry_ref>(name, index, ref, B_REF_TYPE); return fBody->FindData<entry_ref>(name, index, ref, B_REF_TYPE);
#endif
void* data = NULL;
ssize_t size = 0;
status_t err = FindData(name, B_REF_TYPE, index, (const void**)&data, &size);
if (!err)
{
err = entry_ref_unflatten(ref, (char*)data, size);
}
return err;
} }
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
status_t BMessage::FindMessage(const char* name, BMessage* msg) const status_t BMessage::FindMessage(const char* name, BMessage* msg) const
@ -937,7 +983,19 @@ status_t BMessage::FindMessage(const char* name, BMessage* msg) const
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
status_t BMessage::FindMessage(const char* name, int32 index, BMessage* msg) const status_t BMessage::FindMessage(const char* name, int32 index, BMessage* msg) const
{ {
#if 0
return fBody->FindData<BMessage>(name, index, msg, B_MESSAGE_TYPE); return fBody->FindData<BMessage>(name, index, msg, B_MESSAGE_TYPE);
#endif
void* data = NULL;
ssize_t size = 0;
status_t err = FindData(name, B_MESSAGE_TYPE, index,
(const void**)&data, &size);
if (!err)
{
err = msg->Unflatten((const char*)data);
}
return err;
} }
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
status_t BMessage::FindFlat(const char* name, BFlattenable* obj) const status_t BMessage::FindFlat(const char* name, BFlattenable* obj) const
@ -1028,7 +1086,19 @@ status_t BMessage::ReplaceRef(const char* name, int32 index, const entry_ref* re
{ {
// TODO: test // TODO: test
// Use voidref's theoretical BDataBuffer // Use voidref's theoretical BDataBuffer
#if 0
return fBody->ReplaceData<entry_ref>(name, index, *ref, B_REF_TYPE); return fBody->ReplaceData<entry_ref>(name, index, *ref, B_REF_TYPE);
#endif
char* buffer = new(nothrow) char[sizeof (entry_ref) + B_PATH_NAME_LENGTH];
size_t size;
status_t err = entry_ref_flatten(buffer, &size, ref);
if (!err)
{
BDataBuffer DB((void*)buffer, size);
err = fBody->ReplaceData<BDataBuffer>(name, index, DB, B_REF_TYPE);
}
return err;
} }
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
status_t BMessage::ReplaceMessage(const char* name, const BMessage* msg) status_t BMessage::ReplaceMessage(const char* name, const BMessage* msg)
@ -1039,7 +1109,28 @@ status_t BMessage::ReplaceMessage(const char* name, const BMessage* msg)
status_t BMessage::ReplaceMessage(const char* name, int32 index, status_t BMessage::ReplaceMessage(const char* name, int32 index,
const BMessage* msg) const BMessage* msg)
{ {
#if 0
return fBody->ReplaceData<BMessage>(name, index, *msg, B_MESSAGE_TYPE); return fBody->ReplaceData<BMessage>(name, index, *msg, B_MESSAGE_TYPE);
#endif
status_t err = B_OK;
ssize_t size = msg->FlattenedSize();
char* buffer = new(nothrow) char[size];
if (buffer)
{
err = msg->Flatten(buffer, size);
if (!err)
{
BDataBuffer DB((void*)buffer, size);
err = fBody->ReplaceData<BDataBuffer>(name, index, DB,
B_MESSAGE_TYPE);
}
}
else
{
err = B_NO_MEMORY;
}
return err;
} }
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
status_t BMessage::ReplaceFlat(const char* name, BFlattenable* obj) status_t BMessage::ReplaceFlat(const char* name, BFlattenable* obj)
@ -1121,7 +1212,7 @@ status_t BMessage::ReplaceData(const char* name, type_code type, int32 index,
default: default:
// TODO: test // TODO: test
// Using the mythical BDataBuffer // Using the mythical BDataBuffer
BDataBuffer DB((void*)data, data_size); BDataBuffer DB((void*)data, data_size, true);
err = fBody->ReplaceData<BDataBuffer>(name, index, DB, type); err = fBody->ReplaceData<BDataBuffer>(name, index, DB, type);
break; break;
} }
@ -1134,6 +1225,11 @@ void* BMessage::operator new(size_t size)
return ::new char[size]; return ::new char[size];
} }
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
void* BMessage::operator new(size_t, void* p)
{
return p;
}
//------------------------------------------------------------------------------
void BMessage::operator delete(void* ptr, size_t size) void BMessage::operator delete(void* ptr, size_t size)
{ {
::delete(ptr); ::delete(ptr);
@ -1282,10 +1378,19 @@ status_t BMessage::unflatten_hdr(BDataIO* stream, bool& swap)
{ {
swap = true; swap = true;
} }
else else if (data == 'FOB1')
{ {
swap = false; swap = false;
} }
else
{
// This is *not* a message
return B_NOT_A_MESSAGE;
}
// Make way for the new data
MakeEmpty();
read_helper.SetSwap(swap); read_helper.SetSwap(swap);
// get the checksum // get the checksum

View File

@ -6,6 +6,7 @@
// Standard Includes ----------------------------------------------------------- // Standard Includes -----------------------------------------------------------
// System Includes ------------------------------------------------------------- // System Includes -------------------------------------------------------------
#include <ByteOrder.h>
// Project Includes ------------------------------------------------------------ // Project Includes ------------------------------------------------------------
#include <MessageUtils.h> #include <MessageUtils.h>
@ -42,6 +43,81 @@ uint32 _checksum_(const uchar* buf, int32 size)
return sum; return sum;
} }
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
namespace BPrivate { // Only putting these here because Be did
//------------------------------------------------------------------------------
status_t entry_ref_flatten(char* buffer, size_t* size, const entry_ref* ref)
{
memcpy((void*)buffer, (const void*)&ref->device, sizeof (ref->device));
buffer += sizeof (ref->device);
memcpy((void*)buffer, (const void*)&ref->directory, sizeof (ref->directory));
buffer += sizeof (ref->directory);
size_t len = 0;
if (ref->name)
{
len = strlen(ref->name) + 1; // extra for NULL terminator
memcpy((void*)buffer, (const void*)ref->name, len);
}
*size = sizeof (ref->device) + sizeof (ref->directory) + len;
return B_OK;
}
//------------------------------------------------------------------------------
status_t entry_ref_unflatten(entry_ref* ref, const char* buffer, size_t size)
{
if (size < (sizeof (ref->device) + sizeof (ref->directory)))
{
*ref = entry_ref();
return B_BAD_VALUE;
}
memcpy((void*)&ref->device, (const void*)buffer, sizeof (ref->device));
buffer += sizeof (ref->device);
memcpy((void*)&ref->directory, (const void*)buffer,
sizeof (ref->directory));
buffer += sizeof (ref->directory);
if (ref->device != -1 &&
size > (sizeof (ref->device) + sizeof (ref->directory)))
{
ref->set_name(buffer);
if (ref->name == NULL)
{
*ref = entry_ref();
return B_NO_MEMORY;
}
}
else
{
ref->set_name(NULL);
}
return B_OK;
}
//------------------------------------------------------------------------------
status_t entry_ref_swap(char* buffer, size_t size)
{
if (size < (sizeof (dev_t) + sizeof (ino_t)))
{
return B_BAD_DATA;
}
dev_t* dev = (dev_t*)buffer;
*dev = B_SWAP_INT32(*dev);
buffer += sizeof (dev_t);
ino_t* ino = (ino_t*)buffer;
*ino = B_SWAP_INT64(*ino);
return B_OK;
}
//------------------------------------------------------------------------------
} // namespace BPrivate
//------------------------------------------------------------------------------
int32 TChecksumHelper::CheckSum() int32 TChecksumHelper::CheckSum()
{ {
return _checksum_(fBuffer, fBufPtr - fBuffer); return _checksum_(fBuffer, fBufPtr - fBuffer);