KCBP异步调用开发范例

// 异步调用的KCBPCLI编程例子
#include "stdafx.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "KCBPCli.h"
#define KCBP_OPTION_AUTHENTICATION	14

int main(int argc, char* argv[])
{
	int nRet, nUseGroup=3, nCount=0, nReqCount=3, i, nRetry;
	int nRow;
	char szCode[13], szMsg[256];
	char szColNames[256], szDat[1024];
	int  nCol = 0, nVal = 0;
	char szTmp[256];
	char szGroupId[32]={"GROUP0002"}, szServerName[32]={"KCBP01"};
	tagCallCtrl		stControl ;
	tagKCBPConnectOption stKCBPConnection;
	KCBPCLIHANDLE hHandle = NULL;
	nRet = KCBPCLI_Init(&hHandle);
	if(nRet != 0)
	{
		printf("error: KCBPCLI_Init return %d", nRet);
		return 1;
	}

	memset(&stKCBPConnection, 0, sizeof(stKCBPConnection));
	strcpy(stKCBPConnection.szServerName, szServerName);
	stKCBPConnection.nProtocal = 0;
	strcpy(stKCBPConnection.szAddress, "127.0.0.1");
	stKCBPConnection.nPort = 21000;
	strcpy(stKCBPConnection.szSendQName, "req-1");
	strcpy(stKCBPConnection.szReceiveQName, "ans-1");
	nRet = KCBPCLI_SetOptions(hHandle, KCBP_OPTION_CONNECT ,&stKCBPConnection, sizeof(stKCBPConnection));
	if (nRet != 0)
	{
		printf("KCBPCLI_SetOptions return %d\n", nRet);
		goto L_Exit;
	}

	//不登录
	KCBPCLI_SetOptions(hHandle, KCBP_OPTION_AUTHENTICATION ,&nVal, sizeof(nVal));
	nRet = KCBPCLI_ConnectServer(hHandle, szServerName, "KCXP00", "888888");
	if(nRet != 0)
	{
		szMsg[0] = 0;
		KCBPCLI_GetErrorMsg(hHandle, szMsg);
		printf("KCBPCLI_ConnectServer return %d, msg=%s\n", nRet, szMsg);
		goto L_Exit;
	}
	printf("KCBPCLI_ConnectServer OK.\n");
	//使用GROUPID
	KCBPCLI_SetOption(hHandle, KCBP_OPTION_GROUPID, szGroupId);
	KCBPCLI_SetOption(hHandle, KCBP_OPTION_USE_GROUPID, &nUseGroup);
	/*调用服务端程序*/
	for (i = 0; i < nReqCount; i++)
	{
		printf("KCBPCLI_BeginWrite.\n");
		/*设置发送缓冲区*/
		KCBPCLI_BeginWrite(hHandle);
		
		/*设置固定参数*/
		printf("KCBPCLI_SetValue.\n");
		KCBPCLI_SetValue(hHandle, "zjzh", "11010000");
		KCBPCLI_SetValue(hHandle, "jys", "898989");
		KCBPCLI_SetValue(hHandle, "bankcode", "0050");
		memset(&stControl, 0, sizeof(stControl));
		stControl.nExpiry = 50;
		nRet = KCBPCLI_ACallProgramAndCommit(hHandle, "11001001", &stControl);
		if(nRet != 0)
		{
			printf("KCBPCLI_ACallProgramAndCommit return %d\n", nRet);
			//有错,取错误信息
			szMsg[0] = 0;
			KCBPCLI_GetErrorMsg( hHandle, szMsg );
			printf("KCBPCLI_CallProgramAndCommit ErrMsg:%s\n", szMsg );
			continue;
		}
		nCount++;
	}
	
	i = 0;
	while (i < nCount)
	{
		nRetry = 0;
L_Loop:
		memset(&stControl, 0, sizeof(stControl));
		stControl.nExpiry = 100;
		strncpy(stControl.szMsgId, "0", sizeof(stControl.szMsgId)-1);
		strncpy(stControl.szId, szServerName, sizeof(stControl.szId)-1);
		nRet = KCBPCLI_GetReply(hHandle, &stControl);
		nRetry++;
		if (nRet != 0 && nRetry <= 3)
			goto L_Loop;
			
		if (nRet == 0)
		{
			KCBPCLI_GetSystemParam(hHandle, KCBP_PARAM_MSGID, szTmp, 30);
			printf("Index=%d MSGID=%s\n", i, szTmp);

			//结果集处理
			nRet = KCBPCLI_RsOpen(hHandle);
			if(nRet != 0)
			{
				printf("error: KCBPCLI_RsOpen return %d\n", nRet);
				goto L_Next;
			}
		
			nRet = KCBPCLI_RsFetchRow(hHandle);
			if(nRet != 0)
			{
				printf("error: KCBPCLI_RsFetchRow return %d\n", nRet);
				goto L_Next;
			}
		
			//通用取应答首结果集
			szColNames[0] = 0;
			KCBPCLI_RsGetColNames(hHandle, szColNames, sizeof(szColNames) - 1);
			if(strstr(szColNames, "MSG_CODE") != NULL)
			{	//UNIX风格
				//KCBPCLI_RsGetColByName( hHandle, "MSG_LEVEL", szLevel );
				KCBPCLI_RsGetColByName( hHandle, "MSG_TEXT", szMsg );
				nRet = KCBPCLI_RsGetColByName( hHandle, "MSG_CODE", szCode);
			}
			else
			{	//Win风格
				//KCBPCLI_RsGetColByName( hHandle, "LEVEL", szLevel );
				KCBPCLI_RsGetColByName( hHandle, "MSG", szMsg );
				nRet = KCBPCLI_RsGetColByName( hHandle, "CODE", szCode);
			}
			
			if (nRet != 0)
			{
				printf( "get code fail, ret=%d\n ", nRet);
				goto L_Next;
			}
			if (atoi(szCode) != 0)
			{
				printf( "LBM Exec error code:%s, msg:%s\n ", szCode, szMsg);
				goto L_Next;
			}
		
			//查询是否存在后续结果集
			nRet = KCBPCLI_RsMore(hHandle);
			if(nRet != 0)
			{
				goto L_Next;
			}
		
			nRow = 0;
			while(1)
			{
				nRet = KCBPCLI_RsFetchRow(hHandle);
				if(nRet != 0)
				{
					break;
				}
				printf("row %d\n", nRow++);
				nCol = 0;
				KCBPCLI_RsGetColNum(hHandle, &nCol);
				for( int j = 1; j <= nCol; j++ )
				{
					KCBPCLI_RsGetColName( hHandle, j, szDat, sizeof(szDat) );
					printf( "%s", szDat );
					KCBPCLI_RsGetColByName( hHandle, szDat, szDat );
					printf( " = %s\n", szDat );
				}
				printf( "\n" );
			}
		
L_Next:
			KCBPCLI_RsClose(hHandle);
			i++;
		}
	}
	printf("KCBPCLI_GetReply OK.\n");


L_Exit:
	KCBPCLI_DisConnect(hHandle);
	KCBPCLI_Exit(hHandle);
	return 0;
}