From 6b76dd01f5ba345fdc1f8e09b18efa6e1286e5cf Mon Sep 17 00:00:00 2001 From: DarkWyrm Date: Mon, 23 Jun 2003 13:18:39 +0000 Subject: [PATCH] Can get replies as PortMessages now git-svn-id: file:///srv/svn/repos/haiku/trunk/current@3627 a95241bf-73f2-0310-859d-f6bbb57e9c96 --- headers/private/app/PortLink.h | 3 ++ src/kits/app/PortLink.cpp | 91 +++++++++++++++++++++++++++++++++- 2 files changed, 93 insertions(+), 1 deletion(-) diff --git a/headers/private/app/PortLink.h b/headers/private/app/PortLink.h index 9211fa7c51..a0b4e9cc0d 100644 --- a/headers/private/app/PortLink.h +++ b/headers/private/app/PortLink.h @@ -8,6 +8,7 @@ #include class PortLinkData; +class PortMessage; class PortLink { @@ -31,6 +32,8 @@ public: int8* FlushWithReply(int32 *code, status_t *status, ssize_t *buffersize, bigtime_t timeout=B_INFINITE_TIMEOUT); status_t FlushWithReply(PortLink::ReplyData *data,bigtime_t timeout=B_INFINITE_TIMEOUT); + status_t FlushWithReply(PortMessage *msg,bigtime_t timeout=B_INFINITE_TIMEOUT); + status_t Attach(const void *data, size_t size); status_t Attach(int32 data); status_t Attach(int16 data); diff --git a/src/kits/app/PortLink.cpp b/src/kits/app/PortLink.cpp index e7dc0cff26..998b330039 100644 --- a/src/kits/app/PortLink.cpp +++ b/src/kits/app/PortLink.cpp @@ -24,7 +24,8 @@ // Description: A helper class for port-based messaging // //------------------------------------------------------------------------------ -#include "PortLink.h" +#include +#include #include #include #include @@ -395,6 +396,94 @@ printf("\tFlushWithReply(): unable to assign reply port to data\n"); return B_OK; } +status_t PortLink::FlushWithReply(PortMessage *msg,bigtime_t timeout=B_INFINITE_TIMEOUT) +{ +#ifdef PLDEBUG +printf("PortLink::FlushWithReply(PortMessage*,bigtime_t)\n"); +#endif + // Fires a message to the target and then waits for a reply. The target will + // receive a message with the first item being the port_id to reply to. + + // Effectively, an Attach() call inlined for changes + if(!port_ok || !msg) + { +#ifdef PLDEBUG +printf("\tFlushWithReply(): invalid port\n"); +#endif + return B_BAD_VALUE; + } + + // create a new storage object and stash the data + PortLinkData *pld=new PortLinkData; + if(pld->Set(&replyport,sizeof(port_id))==B_OK) + { + bufferlength+=sizeof(port_id); + } + else + { +#ifdef PLDEBUG +printf("\tFlushWithReply(): unable to assign reply port to data\n"); +#endif + delete pld; + return B_ERROR; + } + + // Flatten() inlined to make some necessary changes + int8 *buffer=new int8[bufferlength]; + int8 *bufferindex=buffer; + size_t size=0; + + // attach our port_id first + memcpy(bufferindex, pld->buffer, pld->buffersize); + bufferindex += pld->buffersize; + size+=pld->buffersize; + + // attach everything else + for(int i=0;iCountItems();i++) + { + pld=(PortLinkData*)attachlist->ItemAt(i); + memcpy(bufferindex, pld->buffer, pld->buffersize); + bufferindex += pld->buffersize; + size+=pld->buffersize; + } + + // Flush the thing....FOOSH! :P + write_port(target,opcode,buffer,size); + MakeEmpty(); + delete buffer; + + // Now we wait for the reply + ssize_t rbuffersize; + int8 *rbuffer=NULL; + int32 rcode; + + if(timeout==B_INFINITE_TIMEOUT) + { + rbuffersize=port_buffer_size(replyport); + if(rbuffersize>0) + rbuffer=(int8*)new int8[rbuffersize]; + + read_port(replyport,&rcode,rbuffer,rbuffersize); + } + else + { + rbuffersize=port_buffer_size_etc(replyport,0,timeout); + if(rbuffersize==B_TIMED_OUT) + return B_TIMED_OUT; + + if(rbuffersize>0) + rbuffer=(int8*)new int8[rbuffersize]; + + read_port(replyport,&rcode,rbuffer,rbuffersize); + } + + // We got this far, so we apparently have some data + msg->SetCode(rcode); + msg->SetBuffer(rbuffer,rbuffersize,false); + + return B_OK; +} + status_t PortLink::Attach(const void *data, size_t size) { #ifdef PLDEBUG