Introducing Message4. The changes to the related sources are ifdefed with USING_MESSAGE4 which is defined in Message4.h. To use Message4 the Message4.cpp, Message4.h, MessageUtils4.cpp, MessageUtils4.h and MessagePrivate4.h have to be linked to their counterparts without 4 suffix. Then MessageBody.cpp and MessageField.cpp have to be commented out in the app kit Jamfile and r5_message.cpp has to be added. There remain some bugs to be found. Feel free to change that.

git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@14882 a95241bf-73f2-0310-859d-f6bbb57e9c96
This commit is contained in:
Michael Lotz
2005-11-13 11:31:07 +00:00
parent 4cfc480f5d
commit cf10934e5f
18 changed files with 3742 additions and 0 deletions
+330
View File
@@ -0,0 +1,330 @@
/*
* Copyright 2005, Haiku Inc. All Rights Reserved.
* Distributed under the terms of the MIT License.
*
* Authors:
* Michael Lotz <[email protected]>
*/
#ifndef _MESSAGE_H
#define _MESSAGE_H
#define USING_MESSAGE4 1
#include <BeBuild.h>
#include <DataIO.h>
#include <Flattenable.h>
#include <OS.h>
#include <Rect.h>
#include <AppDefs.h> /* For convenience */
#include <TypeConstants.h> /* For convenience */
class BBlockCache;
class BMessenger;
class BHandler;
class BString;
struct message_header_s;
struct field_header_s;
// Private or reserved ---------------------------------------------------------
extern "C" void _msg_cache_cleanup_();
extern "C" int _init_message_();
extern "C" int _delete_message_();
//------------------------------------------------------------------------------
// Name lengths and Scripting specifiers ---------------------------------------
#define B_FIELD_NAME_LENGTH 255
#define B_PROPERTY_NAME_LENGTH 255
enum
{
B_NO_SPECIFIER = 0,
B_DIRECT_SPECIFIER = 1,
B_INDEX_SPECIFIER,
B_REVERSE_INDEX_SPECIFIER,
B_RANGE_SPECIFIER,
B_REVERSE_RANGE_SPECIFIER,
B_NAME_SPECIFIER,
B_ID_SPECIFIER,
B_SPECIFIERS_END = 128
// app-defined specifiers start at B_SPECIFIERS_END + 1
};
//------------------------------------------------------------------------------
class BMessage {
public:
uint32 what;
BMessage();
BMessage(uint32 what);
BMessage(const BMessage &other);
virtual ~BMessage();
BMessage &operator=(const BMessage &other);
// Statistics and misc info
status_t GetInfo(type_code typeRequested, int32 index,
char **nameFound, type_code *typeFound,
int32 *countFound = NULL) const;
status_t GetInfo(const char *name, type_code *typeFound,
int32 *countFound = NULL) const;
status_t GetInfo(const char *name, type_code *typeFound,
bool *fixedSize) const;
int32 CountNames(type_code type) const;
bool IsEmpty() const;
bool IsSystem() const;
bool IsReply() const;
void PrintToStream() const;
status_t Rename(const char *oldEntry, const char *newEntry);
// Delivery info
bool WasDelivered() const;
bool IsSourceWaiting() const;
bool IsSourceRemote() const;
BMessenger ReturnAddress() const;
const BMessage *Previous() const;
bool WasDropped() const;
BPoint DropPoint(BPoint *offset = NULL) const;
// Replying
status_t SendReply(uint32 command, BHandler *replyTo = NULL);
status_t SendReply(BMessage *reply, BHandler *replyTo = NULL,
bigtime_t timeout = B_INFINITE_TIMEOUT);
status_t SendReply(BMessage *reply, BMessenger replyTo,
bigtime_t timeout = B_INFINITE_TIMEOUT);
status_t SendReply(uint32 command, BMessage *replyToReply);
status_t SendReply(BMessage *the_reply, BMessage *replyToReply,
bigtime_t sendTimeout = B_INFINITE_TIMEOUT,
bigtime_t replyTimeout = B_INFINITE_TIMEOUT);
// Flattening data
ssize_t FlattenedSize() const;
status_t Flatten(char *buffer, ssize_t size) const;
status_t Flatten(BDataIO *stream, ssize_t *size = NULL) const;
status_t Unflatten(const char *flatBuffer);
status_t Unflatten(BDataIO *stream);
// Specifiers (scripting)
status_t AddSpecifier(const char *property);
status_t AddSpecifier(const char *property, int32 index);
status_t AddSpecifier(const char *property, int32 index, int32 range);
status_t AddSpecifier(const char *property, const char *name);
status_t AddSpecifier(const BMessage *specifier);
status_t SetCurrentSpecifier(int32 index);
status_t GetCurrentSpecifier(int32 *index,
BMessage *specifier = NULL, int32 *what = NULL,
const char **property = NULL) const;
bool HasSpecifiers() const;
status_t PopSpecifier();
// Adding data
status_t AddRect(const char *name, BRect aRect);
status_t AddPoint(const char *name, BPoint aPoint);
status_t AddString(const char *name, const char *aString);
status_t AddString(const char *name, const BString &aString);
status_t AddInt8(const char *name, int8 value);
status_t AddInt16(const char *name, int16 value);
status_t AddInt32(const char *name, int32 value);
status_t AddInt64(const char *name, int64 value);
status_t AddBool(const char *name, bool aBoolean);
status_t AddFloat(const char *name, float aFloat);
status_t AddDouble(const char *name, double aDouble);
status_t AddPointer(const char *name, const void *aPointer);
status_t AddMessenger(const char *name, BMessenger messenger);
status_t AddRef(const char *name, const entry_ref *ref);
status_t AddMessage(const char *name, const BMessage *message);
status_t AddFlat(const char *name, BFlattenable *object,
int32 count = 1);
status_t AddData(const char *name, type_code type,
const void *data, ssize_t numBytes,
bool isFixedSize = true, int32 count = 1);
// Removing data
status_t RemoveData(const char *name, int32 index = 0);
status_t RemoveName(const char *name);
status_t MakeEmpty();
// Finding data
status_t FindRect(const char *name, BRect *rect) const;
status_t FindRect(const char *name, int32 index, BRect *rect) const;
status_t FindPoint(const char *name, BPoint *point) const;
status_t FindPoint(const char *name, int32 index, BPoint *point) const;
status_t FindString(const char *name, const char **string) const;
status_t FindString(const char *name, int32 index, const char **string) const;
status_t FindString(const char *name, BString *string) const;
status_t FindString(const char *name, int32 index, BString *string) const;
status_t FindInt8(const char *name, int8 *value) const;
status_t FindInt8(const char *name, int32 index, int8 *value) const;
status_t FindInt16(const char *name, int16 *value) const;
status_t FindInt16(const char *name, int32 index, int16 *value) const;
status_t FindInt32(const char *name, int32 *value) const;
status_t FindInt32(const char *name, int32 index, int32 *value) const;
status_t FindInt64(const char *name, int64 *value) const;
status_t FindInt64(const char *name, int32 index, int64 *value) const;
status_t FindBool(const char *name, bool *value) const;
status_t FindBool(const char *name, int32 index, bool *value) const;
status_t FindFloat(const char *name, float *value) const;
status_t FindFloat(const char *name, int32 index, float *value) const;
status_t FindDouble(const char *name, double *value) const;
status_t FindDouble(const char *name, int32 index, double *value) const;
status_t FindPointer(const char *name, void **pointer) const;
status_t FindPointer(const char *name, int32 index, void **pointer) const;
status_t FindMessenger(const char *name, BMessenger *messenger) const;
status_t FindMessenger(const char *name, int32 index, BMessenger *messenger) const;
status_t FindRef(const char *name, entry_ref *ref) const;
status_t FindRef(const char *name, int32 index, entry_ref *ref) const;
status_t FindMessage(const char *name, BMessage *message) const;
status_t FindMessage(const char *name, int32 index, BMessage *message) const;
status_t FindFlat(const char *name, BFlattenable *object) const;
status_t FindFlat(const char *name, int32 index, BFlattenable *object) const;
status_t FindData(const char *name, type_code type,
const void **data, ssize_t *numBytes) const;
status_t FindData(const char *name, type_code type, int32 index,
const void **data, ssize_t *numBytes) const;
// Replacing data
status_t ReplaceRect(const char *name, BRect aRect);
status_t ReplaceRect(const char *name, int32 index, BRect aRect);
status_t ReplacePoint(const char *name, BPoint aPoint);
status_t ReplacePoint(const char *name, int32 index, BPoint aPoint);
status_t ReplaceString(const char *name, const char *aString);
status_t ReplaceString(const char *name, int32 index, const char *aString);
status_t ReplaceString(const char *name, const BString &aString);
status_t ReplaceString(const char *name, int32 index, const BString &aString);
status_t ReplaceInt8(const char *name, int8 value);
status_t ReplaceInt8(const char *name, int32 index, int8 value);
status_t ReplaceInt16(const char *name, int16 value);
status_t ReplaceInt16(const char *name, int32 index, int16 value);
status_t ReplaceInt32(const char *name, int32 value);
status_t ReplaceInt32(const char *name, int32 index, int32 value);
status_t ReplaceInt64(const char *name, int64 value);
status_t ReplaceInt64(const char *name, int32 index, int64 value);
status_t ReplaceBool(const char *name, bool aBoolean);
status_t ReplaceBool(const char *name, int32 index, bool aBoolean);
status_t ReplaceFloat(const char *name, float aFloat);
status_t ReplaceFloat(const char *name, int32 index, float aFloat);
status_t ReplaceDouble(const char *name, double aDouble);
status_t ReplaceDouble(const char *name, int32 index, double aDouble);
status_t ReplacePointer(const char *name, const void *pointer);
status_t ReplacePointer(const char *name,int32 index,const void *pointer);
status_t ReplaceMessenger(const char *name, BMessenger messenger);
status_t ReplaceMessenger(const char *name, int32 index, BMessenger messenger);
status_t ReplaceRef( const char *name,const entry_ref *ref);
status_t ReplaceRef( const char *name, int32 index, const entry_ref *ref);
status_t ReplaceMessage(const char *name, const BMessage *message);
status_t ReplaceMessage(const char *name, int32 index, const BMessage *message);
status_t ReplaceFlat(const char *name, BFlattenable *object);
status_t ReplaceFlat(const char *name, int32 index, BFlattenable *object);
status_t ReplaceData(const char *name, type_code type,
const void *data, ssize_t numBytes);
status_t ReplaceData(const char *name, type_code type, int32 index,
const void *data, ssize_t numBytes);
void *operator new(size_t size);
void *operator new(size_t, void *pointer);
void operator delete(void *pointer, size_t size);
// Private, reserved, or obsolete ----------------------------------------------
bool HasRect(const char *, int32 n = 0) const;
bool HasPoint(const char *, int32 n = 0) const;
bool HasString(const char *, int32 n = 0) const;
bool HasInt8(const char *, int32 n = 0) const;
bool HasInt16(const char *, int32 n = 0) const;
bool HasInt32(const char *, int32 n = 0) const;
bool HasInt64(const char *, int32 n = 0) const;
bool HasBool(const char *, int32 n = 0) const;
bool HasFloat(const char *, int32 n = 0) const;
bool HasDouble(const char *, int32 n = 0) const;
bool HasPointer(const char *, int32 n = 0) const;
bool HasMessenger(const char *, int32 n = 0) const;
bool HasRef(const char *, int32 n = 0) const;
bool HasMessage(const char *, int32 n = 0) const;
bool HasFlat(const char *, const BFlattenable *) const;
bool HasFlat(const char *, int32 n, const BFlattenable *) const;
bool HasData(const char *, type_code , int32 n = 0) const;
BRect FindRect(const char *, int32 n = 0) const;
BPoint FindPoint(const char *, int32 n = 0) const;
const char *FindString(const char *, int32 n = 0) const;
int8 FindInt8(const char *, int32 n = 0) const;
int16 FindInt16(const char *, int32 n = 0) const;
int32 FindInt32(const char *, int32 n = 0) const;
int64 FindInt64(const char *, int32 n = 0) const;
bool FindBool(const char *, int32 n = 0) const;
float FindFloat(const char *, int32 n = 0) const;
double FindDouble(const char *, int32 n = 0) const;
class Private;
private:
friend class Private;
friend class BMessageQueue;
status_t _InitCommon();
status_t _InitHeader();
status_t _Clear();
status_t _ResizeData(int32 offset, int32 change);
uint32 _HashName(const char *name) const;
status_t _FindField(const char *name, type_code type,
field_header_s **result) const;
status_t _AddField(const char *name, type_code type,
bool isFixedSize, field_header_s **result);
status_t _RemoveField(field_header_s *field);
ssize_t _NativeFlattenedSize() const;
status_t _NativeFlatten(char *buffer, ssize_t size) const;
status_t _NativeFlatten(BDataIO *stream, ssize_t *size = NULL) const;
message_header_s*fHeader;
field_header_s *fFields;
uint8 *fData;
mutable BMessage *fOriginal;
// fQueueLink is used by BMessageQueue to build a linked list
BMessage *fQueueLink;
uint32 fReserved[11];
// deprecated
BMessage(BMessage *a_message);
virtual void _ReservedMessage1();
virtual void _ReservedMessage2();
virtual void _ReservedMessage3();
status_t _SendMessage(port_id port, int32 token, bool preferred,
bigtime_t timeout, bool replyRequired,
BMessenger &replyTo) const;
status_t _SendMessage(port_id port, team_id portOwner,
int32 token, bool preferred, BMessage *reply,
bigtime_t sendTimeout, bigtime_t replyTimeout) const;
static status_t _SendFlattenedMessage(void *data, int32 size,
port_id port, int32 token, bool preferred,
bigtime_t timeout);
static void _StaticInit();
static void _StaticCleanup();
static void _StaticCacheCleanup();
static int32 _StaticGetCachedReplyPort();
enum { sNumReplyPorts = 3 };
static port_id sReplyPorts[sNumReplyPorts];
static long sReplyPortInUse[sNumReplyPorts];
static int32 sGetCachedReplyPort();
static BBlockCache *sMsgCache;
};
#endif // _MESSAGE_H
+201
View File
@@ -0,0 +1,201 @@
#ifndef _MESSAGE_PRIVATE_H_
#define _MESSAGE_PRIVATE_H_
#include <Message.h>
#include <Messenger.h>
#include <MessengerPrivate.h>
#include <TokenSpace.h>
#define MESSAGE_BODY_HASH_TABLE_SIZE 10
#define MAX_DATA_PREALLOCATION B_PAGE_SIZE * 10
#define MAX_FIELD_PREALLOCATION 50
#define MAX_ITEM_PREALLOCATION B_PAGE_SIZE
enum {
MESSAGE_FLAG_VALID = 0x0001,
MESSAGE_FLAG_REPLY_REQUIRED = 0x0002,
MESSAGE_FLAG_REPLY_DONE = 0x0004,
MESSAGE_FLAG_IS_REPLY = 0x0008,
MESSAGE_FLAG_WAS_DELIVERED = 0x0010,
MESSAGE_FLAG_HAS_SPECIFIERS = 0x0020,
MESSAGE_FLAG_READ_ONLY = 0x0040
};
enum {
FIELD_FLAG_VALID = 0x0001,
FIELD_FLAG_FIXED_SIZE = 0x0002,
};
typedef struct field_header_s {
uint32 flags;
type_code type;
int32 nameLength;
int32 count;
ssize_t dataSize;
ssize_t allocated;
int32 offset;
int32 nextField;
} FieldHeader;
typedef struct message_header_s {
uint32 format;
uint32 what;
uint32 flags;
ssize_t fieldsSize;
ssize_t dataSize;
ssize_t fieldsAvailable;
ssize_t dataAvailable;
int32 target;
int32 currentSpecifier;
// reply info
port_id replyPort;
int32 replyTarget;
team_id replyTeam;
// body info
int32 fieldCount;
int32 hashTableSize;
int32 hashTable[MESSAGE_BODY_HASH_TABLE_SIZE];
/* The hash table does contain indexes into the field list and
not direct offsets to the fields. This has the advantage
of not needing to update offsets in two locations.
The hash table must be reevaluated when we remove a field
though.
*/
} MessageHeader;
class BMessage::Private {
public:
Private(BMessage *msg)
: fMessage(msg)
{
}
Private(BMessage &msg)
: fMessage(&msg)
{
}
inline void SetTarget(int32 token, bool preferred)
{
fMessage->fHeader->target = (preferred
? B_PREFERRED_TOKEN : token);
}
inline void SetReply(BMessenger messenger)
{
BMessenger::Private messengerPrivate(messenger);
fMessage->fHeader->replyPort = messengerPrivate.Port();
fMessage->fHeader->replyTarget = messengerPrivate.Token();
fMessage->fHeader->replyTeam = messengerPrivate.Team();
}
inline int32 GetTarget()
{
return fMessage->fHeader->target;
}
inline bool UsePreferredTarget()
{
return fMessage->fHeader->target == B_PREFERRED_TOKEN;
}
inline status_t Clear()
{
return fMessage->_Clear();
}
inline status_t InitHeader()
{
return fMessage->_InitHeader();
}
inline MessageHeader *GetMessageHeader()
{
return fMessage->fHeader;
}
inline FieldHeader *GetMessageFields()
{
return fMessage->fFields;
}
inline uint8 *GetMessageData()
{
return fMessage->fData;
}
inline ssize_t NativeFlattenedSize() const
{
return fMessage->_NativeFlattenedSize();
}
inline status_t NativeFlatten(char *buffer, ssize_t size) const
{
return fMessage->_NativeFlatten(buffer, size);
}
inline status_t NativeFlatten(BDataIO *stream, ssize_t *size) const
{
return fMessage->_NativeFlatten(stream, size);
}
inline status_t SendMessage(port_id port, int32 token, bool preferred,
bigtime_t timeout, bool replyRequired,
BMessenger &replyTo) const
{
return fMessage->_SendMessage(port, token,
preferred, timeout, replyRequired, replyTo);
}
inline status_t SendMessage(port_id port, team_id portOwner,
int32 token, bool preferred, BMessage *reply,
bigtime_t sendTimeout, bigtime_t replyTimeout) const
{
return fMessage->_SendMessage(port, portOwner, token,
preferred, reply, sendTimeout, replyTimeout);
}
static
inline status_t SendFlattenedMessage(void *data, int32 size,
port_id port, int32 token, bool preferred,
bigtime_t timeout)
{
return BMessage::_SendFlattenedMessage(data, size,
port, token, preferred, timeout);
}
static
inline void StaticInit()
{
BMessage::_StaticInit();
}
static
inline void StaticCleanup()
{
BMessage::_StaticCleanup();
}
static
inline void StaticCacheCleanup()
{
BMessage::_StaticCacheCleanup();
}
private:
BMessage *fMessage;
};
#endif // _MESSAGE_PRIVATE_H_
+173
View File
@@ -0,0 +1,173 @@
#ifndef _MESSAGE_UTILS_H_
#define _MESSAGE_UTILS_H_
#include <ByteOrder.h>
#include <DataIO.h>
#include <Entry.h>
#include <Message.h>
#include <SupportDefs.h>
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);
uint32 CalculateChecksum(const uint8 *buffer, int32 size);
} // namespace BPrivate
template<class T>
inline void
byte_swap(T &/*data*/)
{
// Specialize for data types which actually swap
}
inline void
write_helper(BDataIO *stream, const void *data, size_t size)
{
status_t error = stream->Write(data, size);
if (error < B_OK)
throw error;
}
class TReadHelper {
public:
TReadHelper(BDataIO *stream)
: fStream(stream),
fError(B_OK),
fSwap(false)
{
}
TReadHelper(BDataIO *stream, bool swap)
: fStream(stream),
fError(B_OK),
fSwap(swap)
{
}
template<class T>
inline void operator()(T &data)
{
fError = fStream->Read((void *)&data, sizeof(T));
if (fError > B_OK) {
if (IsSwapping())
byte_swap(data);
return;
}
if (fError == 0)
throw B_ERROR;
throw fError;
}
template<class T>
inline void operator()(T data, size_t len)
{
fError = fStream->Read((void *)data, len);
if (fError > B_OK)
return;
if (fError == 0)
throw B_ERROR;
throw fError;
}
status_t Status() { return fError; };
void SetSwap(bool yesNo) { fSwap = yesNo; };
bool IsSwapping() { return fSwap; };
private:
BDataIO *fStream;
status_t fError;
bool fSwap;
};
class TChecksumHelper {
public:
TChecksumHelper(uchar* buffer)
: fBuffer(buffer),
fBufPtr(buffer)
{
}
template<class T>
inline void Cache(const T &data)
{
*((T*)fBufPtr) = data;
fBufPtr += sizeof (T);
}
int32 CheckSum();
private:
uchar *fBuffer;
uchar *fBufPtr;
};
template<class T>
inline status_t
read_helper(BDataIO *stream, T &data)
{
return normalize_err(stream->Read((void *)&data, sizeof(T)));
}
template<>
inline void
byte_swap(double &data)
{
data = __swap_double(data);
}
template<>
inline void
byte_swap(float &data)
{
data = __swap_float(data);
}
template<>
inline void
byte_swap(int64 &data)
{
data = __swap_int64(data);
}
template<>
inline void
byte_swap(int32 &data)
{
data = __swap_int32(data);
}
template<>
inline void
byte_swap(int16 &data)
{
data = __swap_int16(data);
}
template<>
inline void
byte_swap(entry_ref &data)
{
byte_swap(data.device);
byte_swap(data.directory);
}
#endif // _MESSAGE_UTILS_H_