Files
haiku-beta6/src/kits/app/PortLink.cpp
T

152 lines
3.7 KiB
C++
Raw Normal View History

//------------------------------------------------------------------------------
// Copyright (c) 2001-2002, OpenBeOS
//
// 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: PortLink.cpp
// Author: DarkWyrm <[email protected]>
// Description: Class for low-overhead port-based messaging
//
//------------------------------------------------------------------------------
2003-10-03 22:23:17 +00:00
#include <ServerProtocol.h>
#include "PortLink.h"
#include "PortMessage.h"
2003-10-03 22:23:17 +00:00
PortLink::PortLink(port_id port)
{
2003-10-03 22:23:17 +00:00
port_info pi;
port_ok = (get_port_info(port, &pi)==B_OK)? true: false;
2003-10-03 22:23:17 +00:00
fSendPort=port;
fReceivePort=create_port(30,"PortLink reply port");
2003-10-03 22:23:17 +00:00
fSendCode=0;
2003-10-03 23:28:46 +00:00
fSendBuffer=new char[4096];
fSendPosition=8;
fDataSize=(int32*)(fSendBuffer+sizeof(int32));
*fDataSize=0;
}
2003-10-03 22:23:17 +00:00
PortLink::PortLink( const PortLink &link )
2002-07-09 12:24:59 +00:00
{
2003-10-03 22:23:17 +00:00
port_ok=link.port_ok;
2003-10-03 22:23:17 +00:00
fSendPort=link.fSendPort;
fReceivePort=create_port(30,"PortLink reply port");
2002-07-09 12:24:59 +00:00
fSendCode = 0;
2003-10-03 23:28:46 +00:00
fSendBuffer=new char[4096];
fSendPosition = 8;
fDataSize=(int32*)(fSendBuffer+sizeof(int32));
*fDataSize=0;
2002-07-09 12:24:59 +00:00
}
2003-10-03 22:23:17 +00:00
2003-10-03 23:28:46 +00:00
PortLink::~PortLink(void)
{
delete [] fSendBuffer;
}
2003-10-03 22:23:17 +00:00
void PortLink::SetOpCode( int32 code )
{
fSendCode=code;
int32 *cast=(int32*)fSendBuffer;
*cast=code;
2002-07-09 12:24:59 +00:00
}
2003-10-03 22:23:17 +00:00
void PortLink::SetPort( port_id port )
{
port_info pi;
2003-10-03 22:23:17 +00:00
fSendPort=port;
port_ok=(get_port_info(port, &pi) == B_OK)? true: false;
2002-07-09 12:24:59 +00:00
}
2003-10-03 22:23:17 +00:00
port_id PortLink::GetPort()
{
return fSendPort;
2002-07-09 12:24:59 +00:00
}
2003-10-03 22:23:17 +00:00
status_t PortLink::Flush(bigtime_t timeout)
2003-10-03 22:23:17 +00:00
{
status_t write_stat;
2002-09-30 22:34:20 +00:00
2003-10-03 22:23:17 +00:00
if(!port_ok)
2002-09-30 22:34:20 +00:00
return B_BAD_VALUE;
2003-10-03 22:23:17 +00:00
if(timeout!=B_INFINITE_TIMEOUT)
write_stat=write_port_etc(fSendPort, AS_SERVER_PORTLINK, fSendBuffer,
2003-10-03 22:23:17 +00:00
fSendPosition, B_TIMEOUT, timeout);
2002-07-09 12:24:59 +00:00
else
write_stat=write_port(fSendPort, AS_SERVER_PORTLINK, fSendBuffer, fSendPosition);
fSendPosition=8;
*fDataSize=0;
2002-09-30 22:34:20 +00:00
return write_stat;
2002-07-09 12:24:59 +00:00
}
status_t PortLink::FlushWithReply( PortMessage *msg,bigtime_t timeout )
2003-06-23 13:18:39 +00:00
{
if(!port_ok || !msg)
return B_BAD_VALUE;
2003-10-03 22:23:17 +00:00
// attach our reply port_id at the end
Attach<int32>(fReceivePort);
2003-06-23 13:18:39 +00:00
// Flush the thing....FOOSH! :P
write_port(fSendPort, AS_SERVER_PORTLINK, fSendBuffer, fSendPosition);
fSendPosition = 8;
*fDataSize=0;
2003-06-23 13:18:39 +00:00
// Now we wait for the reply
msg->ReadFromPort(fReceivePort,timeout);
2003-10-03 23:28:46 +00:00
2002-09-30 22:34:20 +00:00
return B_OK;
2002-07-09 12:24:59 +00:00
}
2003-10-03 22:23:17 +00:00
status_t PortLink::Attach(const void *data, size_t size)
{
if (size <= 0)
return B_ERROR;
if (4096 - fSendPosition > (int32)size)
{
memcpy(fSendBuffer + fSendPosition, data, size);
fSendPosition += size;
*fDataSize+=size;
2002-09-30 22:34:20 +00:00
return B_OK;
2002-07-09 12:24:59 +00:00
}
return B_NO_MEMORY;
}
2003-10-03 22:23:17 +00:00
status_t PortLink::AttachString(const char *string)
{
int16 len = (int16)strlen(string)+1;
Attach<int16>(len);
return Attach(string, len);
}
2003-10-03 22:23:17 +00:00
void PortLink::MakeEmpty()
{
fSendPosition=8;
*fDataSize=0;
2002-07-09 12:24:59 +00:00
}
2003-10-03 22:23:17 +00:00