Amiga.org

Operating System Specific Discussions => Amiga OS => Amiga OS -- Development => Topic started by: jahc on March 15, 2003, 11:49:04 PM

Title: ReplyMsg() not working
Post by: jahc on March 15, 2003, 11:49:04 PM
I have two tasks running at the same time and I want them to talk to each other. A message is sent from task a to task b..if that is sucessful, then task a will ReplyMsg() it. Task b is doing a WaitPort() waiting for the ReplyMsg(), but for some reason it never seems to get it, because at that point it just waits forever. I've tried to just paste the relevant parts of my program. There may be some missing close brackets from my pasting, but dont take any notice of those. Would really appreciate it if someone could help me out..

ULONG signals;
ULONG portsig;
ULONG portsig2;

struct MsgPort *SlapshotNBX_port;
struct MsgPort *SlapshotNBX_replyport;

struct SM_Message {
    struct Message xym_Msg;
    char           SM_info[50];
} *ss_msg, *reply_msg;

int main()
{

 if (SlapshotNBX_port = CreateMsgPort() )
   {
      SlapshotNBX_port->mp_Node.ln_Name = "SlapshotNBX_port";
      SlapshotNBX_port->mp_Node.ln_Pri = 0;

      AddPort(SlapshotNBX_port);
      portsig = 1 << SlapshotNBX_port->mp_SigBit;

   } else cout<<"Unable to create an Exec MsgPort!\n"<
   if (SlapshotNBX_replyport = CreateMsgPort() )
   {
      SlapshotNBX_replyport->mp_Node.ln_Name = "SlapshotNBX_replyport";
      SlapshotNBX_replyport->mp_Node.ln_Pri = 0;

      AddPort(SlapshotNBX_replyport);
      portsig2 = 1 << SlapshotNBX_replyport->mp_SigBit;

   } else cout<<"Unable to create an Exec MsgPort!\n"<
   while (running)
   {

      switch(DoMethod(app->App,MUIM_Application_Input,&signals))
      {

         case MUIV_Application_ReturnID_Quit:
            running = FALSE;
            break;

// ETC ETC ETC

      }

      if (running && signals) signals = Wait(signals | portsig | portsig2); // | SIGBREAKF_CTRL_C);

        if(ng_config_gui_active) newsgroup_parta_task();


   }

int newsgroup_parta_task()
{

if(have_we_connected_sucessfully)
      {
         if(signals & portsig)
           {
              if(ss_msg = (struct SM_Message *)GetMsg(SlapshotNBX_port));
            {
                 if( strcmp(ss_msg->SM_info, "a")==0 )
               {
                  cout<<"Message recieved, did not connect!\n"<                  set2(app->TX_ng_config_status_window,MUIA_Text_Contents,"Unable to connect");
                  have_we_connected_sucessfully=0;

                  ReplyMsg((struct Message *)ss_msg);

               }

               if( strcmp(ss_msg->SM_info, "b")==0)
               {
                   cout<<"Message recieved, connected okay!\n"<                   set2(app->TX_ng_config_status_window,MUIA_Text_Contents,"Connected to server");
                   have_we_connected_sucessfully=0;
                   ReplyMsg((struct Message *)ss_msg);
               }
            }

         }



NOW.. heres task b which runs at the same time..

int ng_config_main()
{

struct SM_Message
{    struct Message xym_Msg;
   char           SM_info[50];
   } *ss3_msg, *ss2_msg;

    struct MsgPort *SlapshotNBX_replyport = FindPort("SlapshotNBX_replyport");
    struct MsgPort *SlapshotNBX_port=FindPort("SlapshotNBX_port");

if (connect(a, (struct sockaddr*)&slapshot_in, sizeof(struct sockaddr)) == -1)
    {
//          ss3_msg->xym_Msg.mn_Node.ln_Type = NT_MESSAGE;
//         ss3_msg->xym_Msg.mn_Node
           ss3_msg->xym_Msg.mn_Length = sizeof(struct SM_Message);
           ss3_msg->xym_Msg.mn_ReplyPort = SlapshotNBX_replyport;
           strcpy(ss3_msg->SM_info,"a");
            
           perror("connect");
           if (SafePutToPort((struct Message *)ss3_msg, "SlapshotNBX_port"))
         {  
              cout<<"Unable to connect, message sent from ng_config_task\n"<              WaitPort(SlapshotNBX_replyport);
           }
    }
    else
    {
           ss3_msg->xym_Msg.mn_Node.ln_Type = NT_MSGPORT;
           ss3_msg->xym_Msg.mn_Length = sizeof(struct SM_Message);
           ss3_msg->xym_Msg.mn_ReplyPort = SlapshotNBX_replyport;
           strcpy(ss3_msg->SM_info,"b");
           
           if (SafePutToPort((struct Message *)ss3_msg, "SlapshotNBX_port"))
         {         
            WaitPort(SlapshotNBX_replyport);
            //ss3_msg = (struct SM_Message *)GetMsg(SlapshotNBX_replyport);
            
              cout<<"Connected, message sent from ng_config_task\n"<           }
    }
Title: Re: ReplyMsg() not working
Post by: jahc on March 15, 2003, 11:51:20 PM
A bit more information. I'm using WinUAE with OS 3.9 and StormC 3.0.
Title: Re: ReplyMsg() not working
Post by: FluffyMcDeath on March 16, 2003, 08:41:48 AM
Well, I can't answer your question but ...

Quote

jahc wrote:

NOW.. heres task b which runs at the same time..

int ng_config_main()
{

struct SM_Message
{    struct Message xym_Msg;
   char           SM_info[50];
   } *ss3_msg, *ss2_msg;

    struct MsgPort *SlapshotNBX_replyport = FindPort("SlapshotNBX_replyport");
    struct MsgPort *SlapshotNBX_port=FindPort("SlapshotNBX_port");



Task b should create and own the reply port rather than having task a create and own it. If task a goes away, your reply port goes away too. Holding a pointer to the port doen't guarantee that it will exist. Generally, pointers to message ports can only be considered valid after Forbid, then FindPort, upto Permit, so you shouldn't hold onto a pointer to another tasks message port.

Hmmmm. Perhaps task b never gets a signal because the port was created in the context of task a. Perhaps a, being the owner, gets the signal.
Title: Re: ReplyMsg() not working
Post by: ncafferkey on March 16, 2003, 10:48:53 PM
Quote
Hmmmm. Perhaps task b never gets a signal because the port was created in the context of task a. Perhaps a, being the owner, gets the signal.


I definitely think that's the problem. You'll either have to create the reply port in task B, or allocate it manually in task A and fill in the task address and signal number for task B.
Title: Re: ReplyMsg() not working
Post by: Piru on March 16, 2003, 10:57:36 PM
Here is an example of Interprocess Communications (http://www.iki.fi/sintonen/src/ipc/) I wrote while ago. Maybe it gives you an idea how to manage this.
Title: Re: ReplyMsg() not working
Post by: jahc on March 17, 2003, 12:12:57 AM
Thanks for your help guys, someone has basically told me the same thing on the Amiga C mailing list as well, so I shall attempt to rewrite that part of my program.

Title: Re: ReplyMsg() not working
Post by: jahc on March 17, 2003, 12:19:31 AM
It works!!

Yes the problem was the reply port had to be created in task b, we couldnt just use FindPort() there...

Thanks again for your help.

I'm sure there was nothing about this in the RKM's though..