Main Page   Namespace List   Compound List   File List   Namespace Members   Compound Members  

RemoteMessage.cpp

00001 // RemoteMessage.cpp
00002 // 
00003 // Copyright (c) 2004, Ingo Weinhold (bonefish@cs.tu-berlin.de)
00004 // 
00005 // Permission is hereby granted, free of charge, to any person obtaining a
00006 // copy of this software and associated documentation files (the "Software"),
00007 // to deal in the Software without restriction, including without limitation 
00008 // the rights to use, copy, modify, merge, publish, distribute, sublicense,
00009 // and/or sell copies of the Software, and to permit persons to whom the
00010 // Software is furnished to do so, subject to the following conditions:
00011 // 
00012 // The above copyright notice and this permission notice shall be included in
00013 // all copies or substantial portions of the Software.
00014 // 
00015 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
00016 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
00017 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
00018 // THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
00019 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
00020 // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
00021 // DEALINGS IN THE SOFTWARE.
00022 // 
00023 // Except as contained in this notice, the name of a copyright holder shall
00024 // not be used in advertising or otherwise to promote the sale, use or other
00025 // dealings in this Software without prior written authorization of the
00026 // copyright holder.
00027 
00028 #include <Message.h>
00029 
00030 #include <RemoteMessage.h>
00031 
00032 #include "Compatibility.h"
00033 
00062 // SetData
00092 status_t
00093 RemoteMessage::SetData(BMessage* envelope, const void* data, size_t size,
00094     BMessage* parameters)
00095 {
00096     // check parameters
00097     if (!envelope || !data && size > 0)
00098         return B_BAD_VALUE;
00099     status_t error = B_OK;
00100     envelope->what = RMESSAGE_DATA;
00101     envelope->MakeEmpty();
00102     // add data
00103     if (size > 0) {
00104         // get area
00105         area_id area = area_for(const_cast<void*>(data));
00106         if (area < 0)
00107             return area;
00108         // get area info
00109         area_info info;
00110         error = get_area_info(area, &info);
00111         if (error != B_OK)
00112             return error;
00113         // add area ID and in-area offset
00114         error = envelope->AddInt32("area", area);
00115         if (error != B_OK)
00116             return error;
00117         error = envelope->AddInt32("offset",
00118             (const uint8*)data - (const uint8*)info.address);
00119         if (error != B_OK)
00120             return error;
00121     }
00122     // add data size
00123     error = envelope->AddInt32("size", (int32)size);
00124     if (error != B_OK)
00125         return error;
00126     // add parameter message
00127     if (parameters)
00128         error = envelope->AddMessage("parameters", parameters);
00129     return error;
00130 }
00131 
00132 // GetData
00156 status_t
00157 RemoteMessage::GetData(const BMessage* envelope, const void** _data,
00158     size_t* _size, BMessage* parameters, bool* _hasParameters)
00159 {
00160     // check parameters
00161     if (!envelope || !_data || !_size)
00162         return B_BAD_VALUE;
00163     // get parameter message
00164     if (parameters) {
00165         bool hasParameters
00166             = (envelope->FindMessage("parameters", parameters) == B_OK);
00167         if (_hasParameters)
00168             *_hasParameters = hasParameters;
00169     }
00170     // get data size
00171     int32 size;
00172     status_t error = envelope->FindInt32("size", &size);
00173     if (error != B_OK)
00174         return error;
00175     if (size < 0)
00176         return B_BAD_DATA;
00177     // get area and in-area offset
00178     uint8* data = NULL;
00179     if (size > 0) {
00180         area_id area;
00181         error = envelope->FindInt32("area", &area);
00182         if (error != B_OK)
00183             return error;
00184         int32 offset;
00185         error = envelope->FindInt32("offset", &offset);
00186         if (error != B_OK)
00187             return error;
00188         // get area info
00189         area_info info;
00190         error = get_area_info(area, &info);
00191         if (error != B_OK)
00192             return error;
00193         // check the offset
00194         if (offset < 0 || (uint32)offset + size > info.size)
00195             return B_BAD_DATA;
00196         // clone the area and set the result
00197         void* address;
00198         area_id clonedArea = clone_area("cloned rmessage data", &address,
00199             B_ANY_ADDRESS, B_READ_AREA, area);
00200         if (clonedArea < 0)
00201             return clonedArea;
00202         data = (uint8*)address + offset;
00203     }
00204     *_data = data;
00205     *_size = size;
00206     return error;
00207 }
00208 
00209 // ReleaseData
00224 status_t
00225 RemoteMessage::ReleaseData(const void* data, size_t size)
00226 {
00227     if (data) {
00228         area_id area = area_for(const_cast<void*>(data));
00229         if (area >= 0)
00230             delete_area(area);
00231     } else if (size > 0)
00232         return B_BAD_VALUE;
00233     return B_OK;
00234 }
00235 
00236 

Generated at Mon Mar 22 02:35:22 2004 for RMessage by doxygen1.2.1 written by Dimitri van Heesch, © 1997-2000