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

142 lines
2.7 KiB
C++
Raw Normal View History

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];
2003-10-03 22:23:17 +00:00
fSendPosition=4;
}
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];
2003-10-03 22:23:17 +00:00
fSendPosition = 4;
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=B_INFINITE_TIMEOUT)
{
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_SESSION, fSendBuffer,
fSendPosition, B_TIMEOUT, timeout);
2002-07-09 12:24:59 +00:00
else
2003-10-03 22:23:17 +00:00
write_stat=write_port(fSendPort, AS_SERVER_SESSION, fSendBuffer, fSendPosition);
2003-10-03 22:23:17 +00:00
fSendPosition=4;
2002-09-30 22:34:20 +00:00
return write_stat;
2002-07-09 12:24:59 +00:00
}
2003-10-03 22:23:17 +00:00
status_t PortLink::FlushWithReply( PortMessage *msg,bigtime_t timeout=B_INFINITE_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_SESSION, fSendBuffer, fSendPosition);
fSendPosition = 4;
2003-06-23 13:18:39 +00:00
// Now we wait for the reply
2003-10-03 22:23:17 +00:00
ssize_t rbuffersize;
int8 *rbuffer = NULL;
int32 rcode;
2003-06-23 13:18:39 +00:00
if( timeout == B_INFINITE_TIMEOUT )
2003-06-23 13:18:39 +00:00
{
rbuffersize = port_buffer_size(fReceivePort);
if( rbuffersize > 0 )
rbuffer = new int8[rbuffersize];
read_port( fReceivePort, &rcode, rbuffer, rbuffersize);
2003-06-23 13:18:39 +00:00
}
else
{
rbuffersize = port_buffer_size_etc( fReceivePort, 0, timeout);
if( rbuffersize == B_TIMED_OUT )
2003-06-23 13:18:39 +00:00
return B_TIMED_OUT;
if( rbuffersize > 0 )
2003-10-03 22:23:17 +00:00
rbuffer = new int8[rbuffersize];
read_port( fReceivePort, &rcode, rbuffer, rbuffersize);
2003-06-23 13:18:39 +00:00
}
// We got this far, so we apparently have some data
msg->SetCode(rcode);
msg->SetBuffer(rbuffer,rbuffersize,false);
2003-10-03 23:28:46 +00:00
msg->BSessionWorkaround();
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;
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()
{
2003-10-03 23:28:46 +00:00
fSendPosition=4;
2002-07-09 12:24:59 +00:00
}
2003-10-03 22:23:17 +00:00