Files
haiku-beta6/src/kits/app/ServerMemIO.cpp
T
Axel Dörfler dd10337fd0 Renamed BAppServerLink to AppServerLink, BPortLink to PortLink, LinkMsgReader
to LinkReceiver, LinkMsgSender to LinkSender, and put everything into the
BPrivate namespace.
Made AppServerLink a cheap object - it will use the applications receiver/sender
and not create its own buffers.
Fixed broken communication stuff here and there (mostly Font.cpp).
Put the newly introduced set|get_system_colors() into the BPrivate namespace -
please don't introduce private functions into the public namespace!!!
Also fixed their broken communication use, as Darkwyrm obviously forgot about
it again: the sequence Flush(); GetNextMessage() without error checking is
purely wrong and can make the app hang and/or crash! :-)
Other minor cleanup.
The input_server used some test mode with the haiku build target which is
probably wrong.
Hopefully I did not forget anything this time.


git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@13128 a95241bf-73f2-0310-859d-f6bbb57e9c96
2005-06-14 21:28:56 +00:00

189 lines
4.0 KiB
C++

//------------------------------------------------------------------------------
// Copyright (c) 2001-2005, Haiku
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the "Software"),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense,
// 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: ServerMemIO.cpp
// Author(s): Stefano Ceccherini ([email protected])
// The Storage Team
// DarkWyrm <[email protected]>
//
// Description: Adaptation of BMemoryIO code to allow for client-side usage
// of shared memory which is allocated by the server. Model is
// similar to BBitmap in this respect.
//
//------------------------------------------------------------------------------
#include <algorithm>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <AppServerLink.h>
#include <ServerProtocol.h>
#include "ServerMemIO.h"
ServerMemIO::ServerMemIO(size_t size)
:
fLen(0),
fPhys(0),
fPos(0)
{
if (size == 0)
return;
BPrivate::AppServerLink link;
link.StartMessage(AS_ACQUIRE_SERVERMEM);
link.Attach<size_t>(size);
int32 code;
if (link.FlushWithReply(code) == B_OK
&& code == SERVER_TRUE) {
area_info info;
link.Read<area_id>(&fSourceArea);
link.Read<size_t>(&fOffset);
if (fSourceArea >= B_OK && get_area_info(fSourceArea, &info) == B_OK) {
fPhys = size;
fArea = clone_area("ServerMemIO area", (void **)&fBuf, B_CLONE_ADDRESS,
B_READ_AREA|B_WRITE_AREA, fSourceArea);
fBuf += fOffset;
} else {
debugger("PANIC: bad data or something in ServerMemIO constructor");
}
}
}
// Destruction
ServerMemIO::~ServerMemIO()
{
BPrivate::AppServerLink link;
link.StartMessage(AS_RELEASE_SERVERMEM);
link.Attach<area_id>(fSourceArea);
link.Attach<int32>(fOffset);
link.Flush();
}
// ReadAt
ssize_t
ServerMemIO::ReadAt(off_t pos, void *buffer, size_t size)
{
if (buffer == NULL || pos < 0)
return B_BAD_VALUE;
ssize_t sizeRead = 0;
if (pos < fLen) {
sizeRead = min_c(static_cast<off_t>(size), fLen - pos);
memcpy(buffer, fBuf + pos, sizeRead);
}
return sizeRead;
}
// WriteAt
ssize_t
ServerMemIO::WriteAt(off_t pos, const void *buffer, size_t size)
{
if (buffer == NULL || pos < 0)
return B_BAD_VALUE;
ssize_t sizeWritten = 0;
if (pos < fPhys) {
sizeWritten = min_c(static_cast<off_t>(size), fPhys - pos);
memcpy(fBuf + pos, buffer, sizeWritten);
}
if (pos + sizeWritten > fLen)
fLen = pos + sizeWritten;
return sizeWritten;
}
// Seek
off_t
ServerMemIO::Seek(off_t position, uint32 seek_mode)
{
switch (seek_mode) {
case SEEK_SET:
fPos = position;
break;
case SEEK_CUR:
fPos += position;
break;
case SEEK_END:
fPos = fLen + position;
break;
default:
break;
}
return fPos;
}
// Position
off_t
ServerMemIO::Position() const
{
return fPos;
}
// SetSize
status_t
ServerMemIO::SetSize(off_t size)
{
return B_NOT_ALLOWED;
}
size_t
ServerMemIO::GetSize(void) const
{
return fLen;
}
void *
ServerMemIO::GetPointer(void) const
{
return (void*)fBuf;
}
// Private or Reserved
ServerMemIO::ServerMemIO(const ServerMemIO &)
{
//Copying not allowed
}
ServerMemIO &
ServerMemIO::operator=(const ServerMemIO &)
{
//Copying not allowed
return *this;
}