📄 ex_amain.c
字号:
/*
** Async language Query Example Program.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctpublic.h>
#include "example.h"
#include "exutils.h"
#include "exasync.h"
#if !lint
static char Sccsid[] = {"%Z% %M% %I% %G%"};
#endif /* !lint */
/*****************************************************************************
**
** defines used
**
*****************************************************************************/
#ifdef HAFAILOVER
/*
** Global variable for HAFailover
*/
CS_INT hafailoversucceed;
#endif /* HAFAILOVER */
/*
** sample query to send
*/
#define EX_QUERY "waitfor delay \"0:00:01\"\nselect name, dbid, version from sysdatabases"
/*
** Maximum memory allocation for result area
*/
#define MEM_POOL_SIZE (1000 * 32)
/*
** Enable error tracing
*/
#define ERROR_DEBUG 0
/*
** what async mode to use. Possible values are:
**
** CS_ASYNC_IO True async, runs at interrupt level.
** CS_DEFER_IO Deferred async, runs only when ct_poll() is called.
*/
#define EX_IO_MODE CS_DEFER_IO
/*
** Install a signal handler
*/
#ifndef USE_SIG_HANDLER
#define USE_SIG_HANDLER 1
#include <signal.h>
#endif /* USE_SIG_HANDLER */
/*****************************************************************************
**
** global data used
**
*****************************************************************************/
CS_CHAR *Ex_server = EX_SERVER;
CS_CHAR *Ex_username = EX_USERNAME;
CS_CHAR *Ex_password = EX_PASSWORD;
/*
** define a global context handle to use
*/
CS_STATIC CS_CONTEXT *Ex_context;
CS_STATIC CS_CONNECTION *Ex_connection;
/*
** The following variables are used to determine when a completion has
** occurred. "completed" is set to true by the installed async callback,
** "completed_retstat" reflects the status of the completion, once
** "completed" is set to true.
*/
CS_STATIC CS_BOOL completed = CS_FALSE;
CS_STATIC CS_RETCODE completed_retstat = CS_FAIL;
/*****************************************************************************
**
** static prototypes used
**
*****************************************************************************/
CS_RETCODE CS_PUBLIC CompletionCB PROTOTYPE((
CS_CONNECTION *connection,
CS_COMMAND *cmd,
CS_INT function,
CS_RETCODE status
));
CS_STATIC CS_VOID CS_INTERNAL BusyClear PROTOTYPE((void));
CS_STATIC CS_RETCODE CS_INTERNAL BusyWait PROTOTYPE((
CS_CONNECTION *connection,
char *where
));
CS_STATIC CS_VOID CS_INTERNAL Init PROTOTYPE((void));
CS_STATIC CS_VOID CS_INTERNAL DoConnect PROTOTYPE((void));
CS_STATIC CS_VOID CS_INTERNAL Cleanup PROTOTYPE((
CS_CONNECTION *connection
));
CS_STATIC CS_VOID CS_INTERNAL Display PROTOTYPE((
EX_RESULTS *results
));
/*****************************************************************************
**
** functions
**
*****************************************************************************/
#ifdef USE_SIG_HANDLER
void
send_attn(sig)
int sig;
{
CS_RETCODE retstat;
fprintf(stdout, "\nCalling ct_cancel\n");
fflush(stdout);
retstat = ct_cancel(Ex_connection, NULL, CS_CANCEL_ATTN);
if (retstat != CS_SUCCEED && retstat != CS_TRYING)
{
fprintf(stdout,
"ct_cancel returned unexpected status of %d\n",
retstat);
fflush(stdout);
}
return;
}
#endif /* USE_SIG_HANDLER */
/*
** CompletionCB()
**
** Type of function:
** async example program client completion handler
**
** Purpose:
** Installed as a callback into Open Client. Mark the completed
** flag for the busy functions to use.
**
** Returns:
** CS_SUCCEED
**
** Side Effects:
** None
**
*/
CS_RETCODE CS_PUBLIC
CompletionCB(connection, cmd, function, status)
CS_CONNECTION *connection;
CS_COMMAND *cmd;
CS_INT function;
CS_RETCODE status;
{
fprintf(stdout, "\nCompletionCB: function %d Completed\n",
function);
fflush(stdout);
completed_retstat = status;
completed = CS_TRUE;
return CS_SUCCEED;
}
/*
** BusyClear()
**
** Type of function:
** async example program api
**
** Purpose:
** Resets the completed flag used by the busy_wait function.
**
** Returns:
** Nothing.
**
** Side Effects:
** None
*/
CS_STATIC CS_VOID CS_INTERNAL
BusyClear()
{
completed = CS_FALSE;
}
/*
** BusyWait()
**
** Type of function:
** async example program api
**
** Purpose:
** Silly routine that prints out dots while waiting for an async
** operation to complete. It demonstrates the ability to do other work
** while an async operation is pending.
**
** Returns:
** completion status.
**
** Side Effects:
** None
*/
CS_STATIC CS_RETCODE CS_INTERNAL
BusyWait(connection, where)
CS_CONNECTION *connection;
char *where;
{
CS_COMMAND *compcmd;
CS_INT compid;
CS_RETCODE compstat;
CS_RETCODE retstat;
fprintf(stdout, "\nWaiting [%s]", where);
fflush(stdout);
do
{
fprintf(stdout, ".");
fflush(stdout);
retstat = ct_poll(NULL, connection, 100, NULL, &compcmd,
&compid, &compstat);
if (retstat != CS_SUCCEED && retstat != CS_TIMED_OUT &&
retstat != CS_INTERRUPT)
{
#ifdef HAFAILOVER
if (hafailoversucceed && (retstat == CS_QUIET))
{
printf("\nFailover occured, ct_poll returned CS_QUIET\n");
return CS_SUCCEED;
}
#endif /* HAFAILOVER */
fprintf(stdout,
"\nct_poll returned unexpected status of %d\n",
retstat);
fflush(stdout);
break;
}
} while (retstat != CS_SUCCEED);
if (retstat == CS_SUCCEED)
{
fprintf(stdout,
"\nct_poll completed: compid = %d, compstat = %d\n",
compid, compstat);
fflush(stdout);
}
return compstat;
}
/*
** Init()
**
** Type of function:
** async example program api
**
** Purpose:
** Set up context related information.
**
** Returns:
** Nothing.
**
** Side Effects:
** None
*/
CS_STATIC CS_VOID CS_INTERNAL
Init()
{
CS_RETCODE retstat;
/*
** Get a context handle to use.
*/
retstat = cs_ctx_alloc(EX_CTLIB_VERSION, &Ex_context);
if (retstat != CS_SUCCEED)
{
ex_panic("cs_ctx_alloc failed");
}
/*
** Initialize Open Client.
*/
retstat = ct_init(Ex_context, EX_CTLIB_VERSION);
if (retstat != CS_SUCCEED)
{
ex_panic("ct_init failed");
}
/*
** ct_debug stuff. Enable this function right before any call to
** Client-Library that is returning failure.
*/
#if ERROR_DEBUG
retstat = ct_debug(Ex_context, NULL, CS_SET_FLAG, CS_DBG_ERROR,
NULL, CS_UNUSED);
if (retstat != CS_SUCCEED)
{
ex_panic("ct_debug failed");
}
#endif
#ifdef API_DEBUG
retstat = ct_debug(Ex_context, NULL, CS_SET_FLAG, CS_DBG_API_STATES,
NULL, CS_UNUSED);
if (retstat != CS_SUCCEED)
{
ex_panic("ct_debug failed");
}
#endif
#ifdef ASYNC_DEBUG
/*
** Open a file to write to it
*/
retstat = ct_debug(Ex_context, NULL, CS_SET_DBG_FILE, CS_UNUSED,
"ctdebug.out", CS_NULLTERM);
if (retstat != CS_SUCCEED)
{
ex_panic("ct_debug CS_SET_DBG_FILE failed");
}
retstat = ct_debug(Ex_context, NULL, CS_SET_FLAG, CS_DBG_ASYNC,
NULL, CS_UNUSED);
if (retstat != CS_SUCCEED)
{
ex_panic("ct_debug failed");
}
#endif
/*
** Install message and completion handlers.
*/
retstat = ct_callback(Ex_context, NULL, CS_SET, CS_CLIENTMSG_CB,
(CS_VOID *)ex_clientmsg_cb);
if (retstat != CS_SUCCEED)
{
ex_panic("ct_callback failed");
}
retstat = ct_callback(Ex_context, NULL, CS_SET, CS_SERVERMSG_CB,
(CS_VOID *)ex_servermsg_cb);
if (retstat != CS_SUCCEED)
{
ex_panic("ct_callback failed");
}
retstat = ct_callback(Ex_context, NULL, CS_SET, CS_COMPLETION_CB,
(CS_VOID *)CompletionCB);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -