📄 ex_amain.c
字号:
if (retstat != CS_SUCCEED)
{
ex_panic("ct_callback failed");
}
}
/*
** DoConnect()
**
** Type of function:
** async example program api
**
** Purpose:
** Established an asynchronous connection to a server.
**
** Returns:
** Nothing.
**
** Side Effects:
** None
*/
CS_STATIC CS_VOID CS_INTERNAL
DoConnect()
{
CS_INT netio_type = EX_IO_MODE;
CS_RETCODE retstat;
CS_INT slen;
#ifdef HAFAILOVER
CS_INT bhafailover;
#endif /* HAFAILOVER */
/*
** Open a connection to the server.
*/
retstat = ct_con_alloc(Ex_context, &Ex_connection);
if (retstat != CS_SUCCEED)
{
ex_panic("ct_con_alloc failed");
}
#if USE_SIG_HANDLER
signal(SIGINT, send_attn);
#endif
retstat = ct_con_props(Ex_connection, CS_SET, CS_USERNAME,
Ex_username, CS_NULLTERM, NULL);
if (retstat != CS_SUCCEED)
{
ex_panic("ct_con_props failed");
}
retstat = ct_con_props(Ex_connection, CS_SET, CS_PASSWORD,
Ex_password, CS_NULLTERM, NULL);
if (retstat != CS_SUCCEED)
{
ex_panic("ct_con_props failed");
}
retstat = ct_con_props(Ex_connection, CS_SET, CS_NETIO,
(CS_VOID *)&netio_type, CS_UNUSED, NULL);
if (retstat != CS_SUCCEED)
{
ex_panic("ct_con_props failed");
}
#ifdef HAFAILOVER
bhafailover = CS_TRUE;
retstat = ct_con_props(Ex_connection, CS_SET, CS_HAFAILOVER,
&bhafailover, CS_UNUSED, NULL);
if (retstat != CS_SUCCEED)
{
ex_panic("ct_con_props setting CS_HAFAILOVER failed");
}
#endif /* HAFAILOVER */
BusyClear();
printf("TRYING TO CONNECT TO %s.\n", (Ex_server == NULL)
? "NULL" : Ex_server);
slen = (Ex_server == NULL) ? 0 : CS_NULLTERM;
retstat = ct_connect(Ex_connection, Ex_server, slen);
if (retstat != CS_SUCCEED && retstat != CS_PENDING)
{
ex_panic("ct_connect failed");
}
retstat = BusyWait(Ex_connection, "ct_connect");
if (retstat != CS_SUCCEED && retstat != CS_PENDING)
{
ex_panic("ct_connect failed");
}
printf("WE ARE CONNECTED NOW\n");
return;
}
/*
** Cleanup()
**
** Type of function:
** async example program api
**
** Purpose:
** Closes the connection and shuts down CT-Lib.
**
** Returns:
** Nothing.
**
** Side Effects:
** None
*/
CS_STATIC CS_VOID CS_INTERNAL
Cleanup(connection)
CS_CONNECTION *connection;
{
CS_RETCODE retstat;
BusyClear();
retstat = ct_close(connection, CS_UNUSED);
if (retstat != CS_SUCCEED && retstat != CS_PENDING)
{
ex_panic("ct_close failed");
}
retstat = BusyWait(connection, "ct_close");
if (retstat != CS_SUCCEED && retstat != CS_PENDING)
{
ex_panic("BusyWait (ct_close) failed");
}
if (ct_con_drop(connection) != CS_SUCCEED)
{
ex_panic("ct_con_drop failed");
}
if (ct_exit(Ex_context, CS_UNUSED) != CS_SUCCEED)
{
ex_panic("ct_exit failed");
}
if (cs_ctx_drop(Ex_context) != CS_SUCCEED)
{
ex_panic("cs_ctx_drop failed");
}
}
/*
** Display()
**
** Type of function:
** async example program api
**
** Purpose:
**
** Returns:
** Nothing.
**
** Side Effects:
** None
*/
CS_STATIC CS_VOID CS_INTERNAL
Display(results)
EX_RESULTS *results;
{
CS_INT row;
CS_INT col;
ex_display_header(results->numcols, results->colfmts);
/*
** Macro to access the column data the was fetched in the async library.
** Probably should be moved to exasync.h
*/
#define DATA_OFFSET(RES, COL, ROW) \
((RES)->data[COL] + (ROW * (RES)->colfmts[COL].maxlength))
for (row = 0; row < results->numrows; row++)
{
for (col = 0; col < results->numcols; col++)
{
ex_display_column(Ex_context, &results->colfmts[col],
DATA_OFFSET(results, col, row),
results->datalen[col][row],
results->indicator[col][row]);
}
fputc('\n', stdout);
}
}
/*
** main()
**
** Type of function:
** main routine for async example program
**
** Parameters:
** None, argc and argv will not be used.
**
** Purpose:
** This routine will call the async library to send a query to a server
** and retrieve any rows sent back. It then displays the fetched rows.
**
** Returns:
** EX_EXIT_ERROR or EX_EXIT_SUCCEED
**
*/
int
main()
{
CS_RETCODE retstat;
EX_ASYNC *ex_async;
EX_RESULTS results;
CS_VOID *mempool;
CS_CHAR buf[MAX_CHAR_BUF];
#if USE_SIG_HANDLER
signal(SIGINT, SIG_IGN);
#endif
EX_SCREEN_INIT();
fprintf(stdout, "Asynchronous IO Example\n");
fflush(stdout);
/*
** Initialize global stuff
*/
Init();
/*
** establish a connection
*/
DoConnect();
/*
** Allocate a memory pool to use for results
*/
mempool = (CS_VOID *)malloc(MEM_POOL_SIZE);
if (mempool == NULL)
{
ex_panic("malloc failed");
}
/*
** allocate an async handle
*/
retstat = ex_async_alloc(Ex_connection, &results,
mempool, MEM_POOL_SIZE, &ex_async);
if (retstat != CS_SUCCEED)
{
ex_panic("ex_async_alloc failed");
}
for (;;)
{
#ifdef HAFAILOVER
hafailoversucceed = CS_FALSE;
#endif /* HAFAILOVER */
/*
** call ex_async to send a query.
*/
BusyClear();
fprintf(stdout, "\nEnter query to send or type quit: ");
fflush(stdout);
fgets(buf, sizeof (buf), stdin);
if (strncmp(buf, "quit", 4) == 0)
{
break;
}
retstat = ex_async_query(ex_async, buf, CS_NULLTERM);
if (retstat != CS_SUCCEED && retstat != CS_PENDING)
{
ex_panic("ex_async_query failed");
}
retstat = BusyWait(Ex_connection, "ex_async_query");
if (retstat != CS_SUCCEED && retstat != CS_PENDING &&
retstat != CS_CANCELED)
{
ex_panic("BusyWait (ex_async_query) failed");
}
#ifdef HAFAILOVER
/*
** if HA has succeeded , start afresh - that means
** bring it to the state of a new connection.
** clean up the old ex_async structure and get a new one.
*/
if(hafailoversucceed)
{
/*
** Re-install user completion routine
*/
retstat = ct_callback(NULL, Ex_connection, CS_SET, CS_COMPLETION_CB,
(CS_VOID *)CompletionCB);
if (retstat != CS_SUCCEED)
{
ex_panic("ct_callback failed");
}
if (mempool != (char *)NULL)
{
free(mempool);
}
if (ex_async != (EX_ASYNC *)NULL)
{
free(ex_async);
}
/*
** Allocate a memory pool to use for results
*/
mempool = (CS_VOID *)malloc(MEM_POOL_SIZE);
if (mempool == NULL)
{
ex_panic("malloc failed");
}
/*
** allocate an async handle
*/
retstat = ex_async_alloc(Ex_connection, &results,
mempool, MEM_POOL_SIZE, &ex_async);
if (retstat != CS_SUCCEED)
{
ex_panic("ex_async_alloc failed");
}
/*
** Print a message to tell the user to resend the query.
*/
fprintf(stdout,"Please resend the query\n");
}
else
{
#endif /* HAFAILOVER */
/*
** display results
*/
Display(&results);
#ifdef HAFAILOVER
}
#endif /* HAFAILOVER */
}
/*
** drop async handle and free memory pool
*/
ex_async_free(ex_async);
free(mempool);
/*
** clean up
*/
Cleanup(Ex_connection);
return EX_EXIT_SUCCEED;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -