Sybase Technical Library - Product Manuals Home
[Search Forms] [Previous Section with Hits] [Next Section with Hits] [Clear Search] Expand Search

Declare callbacks with CS_PUBLIC [Table of Contents] Completion callbacks

Open Client Client-Library/C Reference Manual

[-] Chapter 2 Topics
[-] Callbacks
[-] Client message callbacks

Client message callbacks

An application handles Client-Library error and informational messages inline or through a client message callback routine.

When a connection is allocated, it picks up a default client message callback from its parent context. If the parent context has no client message callback installed, then the connection is created without a default client message callback.

After allocating a connection, an application:

If a client message callback is not installed for a connection or its parent context and inline message handling is not enabled, Client-Library discards message information.

If callbacks are not implemented for a particular programming language or platform version of Client-Library, an application must handle Client-Library messages inline, using ct_diag.

If a connection is handling Client-Library messages through a client message callback, then the callback is called whenever Client-Library generates an error or informational message.

The exception to this rule is that Client-Library does not call the client message callback when a message is generated from within most types of callback routines. Client-Library does call the client message callback when a message is generated within a completion callback. That is, if a Client-Library routine fails within a callback routine other than the completion callback, the routine returns CS_FAIL but does not trigger the client message callback.

Defining a client message callback

A client message callback is defined as follows:

CS_RETCODE CS_PUBLIC 
 clientmsg_cb(context, connection, message)
CS_CONTEXT           *context;
 CS_CONNECTION   *connection;
 CS_CLIENTMSG       *message;

where:

A client message callback must return either CS_SUCCEED or CS_FAIL:

Table 2-3 lists the Client-Library routines that a client message callback can call:

Routines that a client-message callback can call

Callable routine

Permitted use

ct_config

To retrieve information only

ct_con_props

To retrieve information or to set the CS_USERDATA property only

ct_cmd_props

To retrieve information or to set the CS_USERDATA property only

ct_cancel (CS_CANCEL_ATTN)

Any circumstances

Most applications use a client message callback that simply displays the error details or logs them to a file. However, some applications may require a callback that recognizes certain errors and takes specific action. See "Handling specific Client-Library messages" for more information on how this is done.

Client message callback example

This is an example of a client message callback:

        /*
         ** ex_clientmsg_cb()
         **
         ** Type of function:
         **      Example program client message handler
         **
         ** Purpose:
         **      Installed as a callback into Open Client.
         **
         ** Returns:
         **      CS_SUCCEED
         **
         ** Side Effects:
         **      None
         */

        CS_RETCODE CS_PUBLIC
         ex_clientmsg_cb(context, connection, errmsg)
         CS_CONTEXT        *context
         CS_CONNECTION     *connection;
         CS_CLIENTMSG      *errmsg;
         {
             fprintf(EX_ERROR_OUT, "\nOpen Client Message:\n");
             fprintf(EX_ERROR_OUT, "Message number: 
                 LAYER = (%ld) ORIGIN = (%ld) ",
                 CS_LAYER(errmsg->msgnumber),
                 CS_ORIGIN(errmsg->msgnumber));
             fprintf(EX_ERROR_OUT, "SEVERITY = (%ld) 
                 NUMBER = (%ld)\n",
                 CS_SEVERITY(errmsg->msgnumber), 
                 CS_NUMBER(errmsg->msgnumber));
             fprintf(EX_ERROR_OUT, "Message String: %s\n", 
                 errmsg->msgstring);
             if (errmsg->osstringlen > 0)
             {
                 fprintf(EX_ERROR_OUT, "Operating System \ 
                     Error: %s\n", errmsg->osstring);
             }

            return CS_SUCCEED;
         }


Declare callbacks with CS_PUBLIC [Table of Contents] Completion callbacks