home *** CD-ROM | disk | FTP | other *** search
-
- #include <classes/Exec/Ports.h>
-
- #include <pragma/exec_lib.h>
-
- MessageC::MessageC(class PortC &replyport)
- {
- mn_ReplyPort = replyport.port();
- }
-
- MessageC::MessageC(MsgPort *replyport)
- {
- mn_ReplyPort = replyport;
- }
-
- VOID MessageC::reply()
- {
- ReplyMsg(this);
- }
-
- // *****************************************************
-
- PortC::PortC()
- : SignalHandlerC(), ShareC()
- {
- msgport = CreateMsgPort();
- if (!msgport)
- throw PortX();
- mask = 1L << msgport->mp_SigBit;
- pub = FALSE;
- own = TRUE;
- }
-
- PortC::PortC(STRPTR name, BYTE pri)
- : SignalHandlerC(), ShareC()
- {
- msgport = CreateMsgPort();
- if (!msgport)
- throw PortX();
- mask = 1L << msgport->mp_SigBit;
- msgport->mp_Node.ln_Name = name;
- msgport->mp_Node.ln_Pri = pri;
- Forbid();
- if (FindPort(name))
- {
- Permit();
- DeleteMsgPort(msgport);
- throw PortX(TRUE);
- };
- AddPort(msgport);
- Permit();
- pub = TRUE;
- own = TRUE;
- }
-
- PortC::PortC(MsgPort *port)
- : SignalHandlerC(), ShareC()
- {
- if (!port)
- throw PortX();
- msgport = port;
- mask = 1L << msgport->mp_SigBit;
- pub = FALSE;
- own = FALSE;
- }
-
- PortC::PortC(const PortC &s)
- : ShareC(s)
- {
- msgport = s.msgport;
- pub = s.pub;
- own = s.own;
- }
-
- PortC::~PortC()
- {
- if (only())
- {
- if (pub)
- RemPort(msgport);
- flush();
- if (own)
- DeleteMsgPort(msgport);
- };
- }
-
- PortC &PortC::operator = (const PortC &s)
- {
- if (this != &s)
- {
- if (only())
- {
- if (pub)
- RemPort(msgport);
- flush();
- if (own)
- DeleteMsgPort(msgport);
- };
- ShareC::operator=(s);
- SignalHandlerC::operator=(s);
- msgport = s.msgport;
- pub = s.pub;
- own = s.own;
- };
- return *this;
- }
-
- BOOL PortC::forMe(ULONG)
- {
- return !isEmpty();
- }
-
- BOOL PortC::handle(ULONG)
- {
- MessageC *msg;
- while (msg = (MessageC *) GetMsg(msgport)) {
- if (handleMsg(*msg))
- return TRUE;
- };
- return FALSE;
- }
-
- VOID PortC::flush()
- {
- MessageC *msg;
- while (msg = (MessageC *) GetMsg(msgport)) {
- if (msg->mn_Node.ln_Type == NT_MESSAGE)
- msg->reply();
- };
- }
-
- BOOL PortC::handleMsg(MessageC &msg)
- {
- if (msg.mn_Node.ln_Type == NT_MESSAGE)
- msg.reply();
- return FALSE;
- }
-
- // ******************************************************
-
- VOID HandlerChainC::add(MessageHandlerC &h)
- {
- addTail(h);
- }
-
- BOOL HandlerChainC::handle(MessageC &msg)
- {
- ListCursorC lc(*this);
- _exit = FALSE;
- BOOL r = FALSE;
- while (!lc.isDone()) {
- MessageHandlerC *h = (MessageHandlerC *) lc.item();
- if (h->handle(msg))
- {
- _exit |= h->exit();
- r = TRUE;
- };
- lc.next();
- };
- return r;
- }
-
- // ******************************************************
-
- VOID HandlerPortC::add(MessageHandlerC &h)
- {
- addTail(h)
- }
-
- BOOL HandlerPortC::handleMsg(MessageC &msg)
- {
- ListCursorC lc(*this);
- BOOL r = FALSE;
- while (!lc.isDone()) {
- MessageHandlerC *h = (MessageHandlerC *) lc.item();
- if (h->forMe(msg))
- {
- if (h->handle(msg))
- {
- BOOL b = h->exit();
- r = r | b;
- };
- };
- lc.next();
- };
- if (msg.mn_Node.ln_Type == NT_MESSAGE)
- msg.reply();
- return r;
- }
-
- // *******************************************************
-
- SendPortC::SendPortC(PortC &port)
- {
- msgport = port.port();
- portname = NULL;
- }
-
- SendPortC::SendPortC(MsgPort *port)
- {
- if (!port)
- throw PortX();
- msgport = port;
- portname = NULL;
- }
-
- SendPortC::SendPortC(STRPTR name)
- {
- if (!name)
- throw PortX();
- portname = name;
- msgport = FindPort(portname);
- }
-
- SendPortC::~SendPortC()
- {
- }
-
- BOOL SendPortC::send(MessageC &msg)
- {
- if (!portname)
- {
- PutMsg(msgport,&msg);
- return TRUE;
- }
- else {
- Forbid();
- msgport = FindPort(portname);
- if (!msgport)
- {
- Permit();
- return FALSE;
- };
- PutMsg(msgport,&msg);
- Permit();
- return TRUE;
- };
- }
-