2 * Copyright 2009, Haiku, Inc. All Rights Reserved.
3 * Distributed under the terms of the MIT License.
6 * Scott T. Mansfield, thephantom@mac.com
7 * Bruno Albuquerque, bga@bug-br.org.br
10 #include <ByteOrder.h>
12 #include <TypeConstants.h>
14 #include "DynamicBuffer.h"
15 #include "NetBuffer.h"
20 BNetBuffer::BNetBuffer(size_t size
) :
24 fImpl
= new (std::nothrow
) DynamicBuffer(size
);
26 fInit
= fImpl
->InitCheck();
30 BNetBuffer::~BNetBuffer()
36 BNetBuffer::BNetBuffer(const BNetBuffer
& buffer
) :
40 fImpl
= new (std::nothrow
) DynamicBuffer(*buffer
.GetImpl());
42 fInit
= fImpl
->InitCheck();
46 BNetBuffer::BNetBuffer(BMessage
* archive
) :
50 const unsigned char* bufferPtr
;
53 if (archive
->FindData("buffer", B_RAW_TYPE
, (const void**)&bufferPtr
,
54 &bufferSize
) == B_OK
) {
55 fImpl
= new (std::nothrow
) DynamicBuffer(bufferSize
);
57 ssize_t result
= fImpl
->Write(bufferPtr
, bufferSize
);
59 fInit
= fImpl
->InitCheck();
67 BNetBuffer::operator=(const BNetBuffer
& buffer
)
71 fImpl
= new (std::nothrow
) DynamicBuffer(*buffer
.GetImpl());
73 fInit
= fImpl
->InitCheck();
80 BNetBuffer::Archive(BMessage
* into
, bool deep
) const
85 status_t result
= into
->AddData("buffer", B_RAW_TYPE
, fImpl
->Data(),
86 fImpl
->BytesRemaining());
93 BNetBuffer::Instantiate(BMessage
* archive
)
95 if (!validate_instantiation(archive
, "BNetBuffer"))
98 BNetBuffer
* buffer
= new (std::nothrow
) BNetBuffer(archive
);
102 if (buffer
->InitCheck() != B_OK
) {
112 BNetBuffer::InitCheck()
119 BNetBuffer::AppendInt8(int8 data
)
121 return AppendData((const void*)&data
, sizeof(int8
));
126 BNetBuffer::AppendUint8(uint8 data
)
128 return AppendData((const void*)&data
, sizeof(int8
));
133 BNetBuffer::AppendInt16(int16 data
)
135 int16 be_data
= B_HOST_TO_BENDIAN_INT16(data
);
136 return AppendData((const void*)&be_data
, sizeof(int16
));
141 BNetBuffer::AppendUint16(uint16 data
)
143 uint16 be_data
= B_HOST_TO_BENDIAN_INT16(data
);
144 return AppendData((const void*)&be_data
, sizeof(uint16
));
149 BNetBuffer::AppendInt32(int32 data
)
151 int32 be_data
= B_HOST_TO_BENDIAN_INT32(data
);
152 return AppendData((const void*)&be_data
, sizeof(int32
));
157 BNetBuffer::AppendUint32(uint32 data
)
159 uint32 be_data
= B_HOST_TO_BENDIAN_INT32(data
);
160 return AppendData((const void*)&be_data
, sizeof(uint32
));
165 BNetBuffer::AppendFloat(float data
)
167 return AppendData((const void*)&data
, sizeof(float));
172 BNetBuffer::AppendDouble(double data
)
174 return AppendData((const void*)&data
, sizeof(double));
179 BNetBuffer::AppendString(const char* data
)
181 return AppendData((const void*)data
, strlen(data
) + 1);
186 BNetBuffer::AppendData(const void* data
, size_t size
)
191 ssize_t bytesWritten
= fImpl
->Write(data
, size
);
192 if (bytesWritten
< 0)
193 return (status_t
)bytesWritten
;
194 return (size_t)bytesWritten
== size
? B_OK
: B_ERROR
;
198 #define STACK_BUFFER_SIZE 2048
201 BNetBuffer::AppendMessage(const BMessage
& data
)
203 char stackFlattenedData
[STACK_BUFFER_SIZE
];
205 ssize_t dataSize
= data
.FlattenedSize();
213 status_t result
= B_OK
;
215 if (dataSize
> STACK_BUFFER_SIZE
) {
216 char* flattenedData
= new (std::nothrow
) char[dataSize
];
217 if (flattenedData
== NULL
)
220 if (data
.Flatten(flattenedData
, dataSize
) == B_OK
)
221 result
= AppendData((const void*)&flattenedData
, dataSize
);
223 delete[] flattenedData
;
225 if (data
.Flatten(stackFlattenedData
, dataSize
) == B_OK
)
226 result
= AppendData((const void*)&stackFlattenedData
, dataSize
);
234 BNetBuffer::AppendInt64(int64 data
)
236 int64 be_data
= B_HOST_TO_BENDIAN_INT64(data
);
237 return AppendData((const void*)&be_data
, sizeof(int64
));
242 BNetBuffer::AppendUint64(uint64 data
)
244 uint64 be_data
= B_HOST_TO_BENDIAN_INT64(data
);
245 return AppendData((const void*)&be_data
, sizeof(uint64
));
250 BNetBuffer::RemoveInt8(int8
& data
)
252 return RemoveData((void*)&data
, sizeof(int8
));
257 BNetBuffer::RemoveUint8(uint8
& data
)
259 return RemoveData((void*)&data
, sizeof(uint8
));
264 BNetBuffer::RemoveInt16(int16
& data
)
267 status_t result
= RemoveData((void*)&be_data
, sizeof(int16
));
271 data
= B_BENDIAN_TO_HOST_INT16(be_data
);
278 BNetBuffer::RemoveUint16(uint16
& data
)
281 status_t result
= RemoveData((void*)&be_data
, sizeof(uint16
));
285 data
= B_BENDIAN_TO_HOST_INT16(be_data
);
292 BNetBuffer::RemoveInt32(int32
& data
)
295 status_t result
= RemoveData((void*)&be_data
, sizeof(int32
));
299 data
= B_BENDIAN_TO_HOST_INT32(be_data
);
306 BNetBuffer::RemoveUint32(uint32
& data
)
309 status_t result
= RemoveData((void*)&be_data
, sizeof(uint32
));
313 data
= B_BENDIAN_TO_HOST_INT32(be_data
);
320 BNetBuffer::RemoveFloat(float& data
)
322 return RemoveData((void*)&data
, sizeof(float));
327 BNetBuffer::RemoveDouble(double& data
)
329 return RemoveData((void*)&data
, sizeof(double));
334 BNetBuffer::RemoveString(char* data
, size_t size
)
336 // TODO(bga): Should we do anything specific to handle the terminating
338 return RemoveData((void*)data
, size
);
343 BNetBuffer::RemoveData(void* data
, size_t size
)
348 ssize_t bytesRead
= fImpl
->Read(data
, size
);
350 return (status_t
)bytesRead
;
351 return (size_t)bytesRead
== size
? B_OK
: B_BUFFER_OVERFLOW
;
356 BNetBuffer::RemoveMessage(BMessage
& data
)
361 unsigned char* bufferPtr
= fImpl
->Data();
363 if (*(int32
*)bufferPtr
!= B_MESSAGE_TYPE
)
366 bufferPtr
+= sizeof(int32
);
367 int32 dataSize
= *(int32
*)bufferPtr
;
369 char* flattenedData
= new (std::nothrow
) char[dataSize
];
370 if (flattenedData
== NULL
)
373 status_t result
= RemoveData(flattenedData
, dataSize
);
375 result
= data
.Unflatten(flattenedData
);
377 delete[] flattenedData
;
384 BNetBuffer::RemoveInt64(int64
& data
)
387 status_t result
= RemoveData((void*)&be_data
, sizeof(int64
));
391 data
= B_BENDIAN_TO_HOST_INT64(be_data
);
398 BNetBuffer::RemoveUint64(uint64
& data
)
401 status_t result
= RemoveData((void*)&be_data
, sizeof(uint64
));
405 data
= B_BENDIAN_TO_HOST_INT64(be_data
);
412 BNetBuffer::Data() const
417 return fImpl
->Data();
422 BNetBuffer::Size() const
427 return fImpl
->Size();
432 BNetBuffer::BytesRemaining() const
437 return fImpl
->BytesRemaining();
442 BNetBuffer::_ReservedBNetBufferFBCCruft1()
448 BNetBuffer::_ReservedBNetBufferFBCCruft2()
454 BNetBuffer::_ReservedBNetBufferFBCCruft3()
460 BNetBuffer::_ReservedBNetBufferFBCCruft4()
466 BNetBuffer::_ReservedBNetBufferFBCCruft5()
472 BNetBuffer::_ReservedBNetBufferFBCCruft6()