範例程式碼:初始化和路由程序

本主題包含從動態存取模組接收訊息的原始程式碼大綱, (DMOD) 。

注意

TRACEn () 是用來指定要追蹤資料的宏。 此資料可以包含變數參數。 n 值會識別追蹤的嚴重性層級。 不相符的括弧是刻意的。 它們是由宏的擴充所解決。

/********************************************************************/  
/* Sample code for initialization and routing procedure.            */  
/********************************************************************/  
  
HSEM  dummysem = NULL;      /* This semaphore is never used         */  
  
/********************************************************************/  
/* Initialization procedure                                         */  
/********************************************************************/  
USHORT init_proc()  
{  
  COM_ENTRY("initp");  
  rc = sbpuinit(&dmodsem, CLIENT, CES3270, username);  
  TRACE4()"DMOD initialized, rc=%d",rc));  
  if (rc == NO_ERROR)  
  {  
  
    /********************************************************************/  
/* The procedure routproc will be called whenever a message is      */  
/* received by the DMOD. This is used to post back the application, */  
/* but take care to protect any queues against concurrent access by */  
/* multiple threads.                                                */  
/********************************************************************/  
    rc = sepdrout(routproc);  
    TRACE4()"Rout proc set up, rc=%d",rc));  
    if (rc == NO_ERROR)  
    {  
      /* Other initialization here                                  */  
    }  
  }  
  return (rc);  
}  
/********************************************************************/  
/* The routine routproc is called whenever the DMOD receives a      */  
/* message or a status indication.                                   */  
/********************************************************************/  
USHORT FAR _loadds routproc(buf, srcl, status)  
BUFHDR FAR *buf;        /* Buffer that has been received            */  
USHORT      srcl;       /* Locality from which buffer was received  */  
USHORT      status;     /* Reason for call                          */  
                        /* CEDINMSG = message received              */  
                        /* CEDINLLN = path error occurred (on srcl) */  
{  
  COM_ENTRY("routp");   /* initialize rc=FALSE                      */  
  
                        /* Call the DL BASE to handle re-resource   */  
                        /* location                                 */  
  if (!sbpurcvx(&buf, srcl, status))  
  {  
    switch (status) {  
      case CEDINMSG:  
        if (buf->destp == S3PROD)         /* Is the message for us? */  
        {  
          /********************************************************************/  
     /* Process the received message.                               */  
     /*                                                             */  
     /* If the message is DATAFMI on the PLU-SLU session, and the   */  
     /* application has requested to use flow control on the        */  
     /* session, then this processing should include:               */  
     /*                                                             */  
     /* -  increment number of messages received by the client      */  
     /* -  check whether the number received exceeds the threshold  */  
     /*    for normally returning credit to the node. If so, check  */  
     /*    whether it is OK to return credit (for example, not short of      */  
     /*    buffers), and if OK send a status-resource message to    */  
     /*    the node to give it credit to send more messages to the  */  
     /*    client.                                                  */  
/********************************************************************/  
          rc = TRUE;  
          TRACE2()"Routing proc got message at %p",buf));  
        }  
        else  
        {  
          TRACE2()"Routing proc did not take message at %p",buf));  
        }  
        break;  
  
      case CEDINLLN:  
        TRACE2()"Path error on %d",srcl));  
            /********************************************************************/  
        /* Process the path error status.                           */  
/********************************************************************/  
        break;  
    }  
  
    /********************************************************************/  
    /* If the message/status cannot be completely processed here,   */  
    /* the application can queue the message and clear a semaphore for the  */  
    /* main thread to continue the processing.                      */  
/********************************************************************/  
  } else {  
    rc = TRUE;       /* DLBase handled the message on our behalf    */<<<<<<<<<<This should be FALSE or Zero  
  }  
  /* Returning a value of TRUE indicates that we processed the      */  
  /* event return(rc);  
}  
  
Solution:  
A possible solution is to remove the else statment altogether.  
  
  Copy Code   
/********************************************************************/  
/* Sample code for initialization and routing procedure.            */  
/********************************************************************/  
  
HSEM  dummysem = NULL;      /* This semaphore is never used         */  
  
/********************************************************************/  
/* Initialization procedure                                         */  
/********************************************************************/  
USHORT init_proc()  
{  
  COM_ENTRY("initp");  
  rc = sbpuinit(&dmodsem, CLIENT, CES3270, username);  
  TRACE4()"DMOD initialized, rc=%d",rc));  
  if (rc == NO_ERROR)  
  {  
  
    /********************************************************************/  
/* The procedure routproc will be called whenever a message is      */  
/* received by the DMOD. This is used to post back the application, */  
/* but take care to protect any queues against concurrent access by */  
/* multiple threads.                                                */  
/********************************************************************/  
    rc = sepdrout(routproc);  
    TRACE4()"Rout proc set up, rc=%d",rc));  
    if (rc == NO_ERROR)  
    {  
      /* Other initialization here                                  */  
    }  
  }  
  return (rc);  
}  
/********************************************************************/  
/* The routine routproc is called whenever the DMOD receives a      */  
/* message or a status indication.                                   */  
/********************************************************************/  
USHORT FAR _loadds routproc(buf, srcl, status)  
BUFHDR FAR *buf;        /* Buffer that has been received            */  
USHORT      srcl;       /* Locality from which buffer was received  */  
USHORT      status;     /* Reason for call                          */  
                        /* CEDINMSG = message received              */  
                        /* CEDINLLN = path error occurred (on srcl) */  
{  
  COM_ENTRY("routp");   /* initialize rc=FALSE                      */  
  
                        /* Call the DL BASE to handle re-resource   */  
                        /* location                                 */  
  if (!sbpurcvx(&buf, srcl, status))  
  {  
    switch (status) {  
      case CEDINMSG:  
        if (buf->destp == S3PROD)         /* Is the message for us? */  
        {  
          /********************************************************************/  
     /* Process the received message.                               */  
     /*                                                             */  
     /* If the message is DATAFMI on the PLU-SLU session, and the   */  
     /* application has requested to use flow control on the        */  
     /* session, then this processing should include:               */  
     /*                                                             */  
     /* -  increment number of messages received by the client      */  
     /* -  check whether the number received exceeds the threshold  */  
     /*    for normally returning credit to the node. If so, check  */  
     /*    whether it is OK to return credit (for example, not short of      */  
     /*    buffers), and if OK send a status-resource message to    */  
     /*    the node to give it credit to send more messages to the  */  
     /*    client.                                                  */  
/********************************************************************/  
          rc = TRUE;  
          TRACE2()"Routing proc got message at %p",buf));  
        }  
        else  
        {  
          TRACE2()"Routing proc did not take message at %p",buf));  
        }  
        break;  
  
      case CEDINLLN:  
        TRACE2()"Path error on %d",srcl));  
            /********************************************************************/  
        /* Process the path error status.                           */  
/********************************************************************/  
        break;  
    }  
  
    /********************************************************************/  
    /* If the message/status cannot be completely processed here,   */  
    /* the application can queue the message and clear a semaphore for the  */  
    /* main thread to continue the processing.                      */  
/********************************************************************/  
  }   
  
  /* Returning a value of TRUE indicates that we processed the      */  
  /* event otherwise FALSE means the message was processed by */   
  /* DL - Base */  
  
return(rc);  
  
}