static	char	sccsid[]="%Z% %M% %I% %E% %U%";
/****************************************************************
 *								*
 *	処理目的	: パケットチェック			*
 *								*
 *	処理内容	: 受け取りパケットのチェック		*
 *								*
 ****************************************************************/
#include <colmn.h>

#define False		0
#define True		1

extern int giOptions[];
extern CLPRTBL  *pGLprocTable;
extern CLPRTBL  *pCLprocTable;
extern GlobalCt *pGlobTable;

extern CLCOMMON CLcommon;

/****************************************************************
 *								*
 *	機能名		: パケットチェック処理			*
 *	関数形式	: int cl_packet_check()			*
 *								*
 *	処理内容	: パケットチェック			*
 *								*
 *	引数の説明	: 					*
 *								*
 *	リターン値	: 		*
 *								*
 ****************************************************************/
int cl_packet_check (lInstanceHandle,cpInstanceData,tpRecvMsgCom)
long lInstanceHandle;
char *cpInstanceData;
AKAMSGCOM *tpRecvMsgCom;
{
	AKAMSGCOM    *tpMsgCom;
	int           iRc;
	int			time_out,i,len,size,*pSize;
	qDbToCol	*DTDHead;
	char *p,*pm,buf[64];
	ushort  usw;
	long    lw;
	char cM_QUOTE1 = CLcommon.Quot[0];

DEBUGOUTL3(150,"cl_packet_check lInstanceHandle=%08x cpInstanceData=%08x tpRecvMsgCom=%08x",
lInstanceHandle,cpInstanceData,tpRecvMsgCom);

	p = cpInstanceData;
	if (tpRecvMsgCom->msg_disp == 0) {
		/* パケットがコマンドの時 */

		DEBUGOUTL(101,FORMAT(11));	/* >> コマンド・パケットです。 */

		/* パケットの解析 */
#ifdef TIME
akb_ptime('S',"col_mn_analz");
#endif
#ifdef TIME
akb_ptime('E',"col_mn_analz");
#endif
/*
akxaxdump("C RecvMsgCom",tpRecvMsgCom,sizeof(AKAMSGCOM));
akxaxdump("C Msg",tpRecvMsgCom->msg_pmsg,tpRecvMsgCom->msg_mlen);
*/
		/* 関連テーブルの初期化 */
#if 1
		pCLprocTable = (CLPRTBL *)Malloc(sizeof(CLPRTBL));
		pGlobTable   = (GlobalCt *)Malloc(sizeof(GlobalCt));
DEBUGOUTL2(110,"cl_packet_check:cmd: pCLprocTable=%08x pGlobTable=%08x",
pCLprocTable,pGlobTable);
		memset(pGlobTable,0,sizeof(GlobalCt));
#else
		pCLprocTable = &CLprocTable;
		pGlobTable   = &GlobTable;
#endif
		memcpy(pGlobTable->options,giOptions,sizeof(int)*MAX_OPTIONS);
		cl_mod_option(1,0);
		cl_mod_option(9,0);

		memset(pCLprocTable,0,sizeof(CLPRTBL));
		pCLprocTable->GlobCt = pGlobTable;
		pCLprocTable->lInstanceHandle = lInstanceHandle;
		pCLprocTable->iThread = lInstanceHandle>>16;
		/* set start time */
		aka_get_msec(&pCLprocTable->tScrTimeVal[1]);
/*
printf("cl_packet_check iThread=%08x\n",pCLprocTable->iThread);
*/
/* 10.26 Add Koba */
		i = htonl(time(0));
		memcpy(pCLprocTable->CurCmdId,(char *)&i,4);
		usw = htonl(pCLprocTable->iThread);
		memcpy(pCLprocTable->CurCmdId,(char *)&usw,2);

/* 2000.6.16 Add Koba */
		pCLprocTable->usDbProcId  = CLcommon.usDbProcId
		                    + ((pCLprocTable->iThread - 1) % CLcommon.iDbProcNum);
		pCLprocTable->usDbClassId = CLcommon.usDbClassId;

		DEBUGOUTL(101,FORMAT(12));	/* >> 現環境の初期化を行ないました。 */

		memcpy((char *)&i,pCLprocTable->CurCmdId,4);
		DEBUGOUTL1(101,"CurCmdId=%08x",i);

		memcpy(p,(char *)tpRecvMsgCom,sizeof(AKAMSGCOM));
/*
printf("sizeof(AKAMSGCOM)=%d lMsgLen=%d\n",sizeof(AKAMSGCOM),tpRecvMsgCom->msg_mlen);
*/
		if (!(pm=Malloc(tpRecvMsgCom->msg_mlen))) {
			return -1;
		}
/*
akxaxdump("C Msg2",tpRecvMsgCom->msg_pmsg,tpRecvMsgCom->msg_mlen);
*/
		memcpy(pm,tpRecvMsgCom->msg_pmsg,tpRecvMsgCom->msg_mlen);
		tpMsgCom = (AKAMSGCOM *)p;
		tpMsgCom->msg_pmsg = pm;

		pCLprocTable->CmdPacketp = p;
		pCLprocTable->PrPacketp = NULL;
/*
akxaxdump("C CmdPacket",p,sizeof(AKAMSGCOM));
akxaxdump("C Msg",pm,tpRecvMsgCom->msg_mlen);
*/
		/* 処理フラグＯＮ */
		cmn_set_stat( COM_PR ,&pCLprocTable->PrSt , L_ON );
 
		/* スクリプト処理 */
		cmn_set_stat( SCR_PR ,&pCLprocTable->PrSt ,  L_ON );
		pGlobTable->Trnshid[0] = '\0';
		pCLprocTable->SlstatusSW = 0;
	/*
		memcpy(pGlobTable->options,giOptions,sizeof(int)*MAX_OPTIONS);
		cl_mod_option(1,0);
		cl_mod_option(9,0);
	*/
		cl_tmp_const_ct_set(NULL);

		if (!(pCLprocTable->pha_vnam=akxs_xhash_new(0,MAX_VAR_IY,0))) {
			return -1;
		}
		len = sizeof(tdtInfoParm **)*(MAX_VAR_IX+1);
		if (!(pCLprocTable->pTBL_vnam=(tdtInfoParm ***)Malloc(len))) {
			akxs_xhash_free(pCLprocTable->pha_vnam);
			return -1;
		}
		memset(pCLprocTable->pTBL_vnam,0,len);
		if (!(pSize=(int *)Malloc(cl_var_size_len(0)))) {
			akxs_xhash_free(pCLprocTable->pha_vnam);
			Free(pCLprocTable->pTBL_vnam);
			return -1;
		}
		pCLprocTable->pTBL_vnam[0] = (tdtInfoParm **)pSize;
		cl_var_set_size(pSize,MAX_VAR_IX,MAX_VAR_IY,"PVN");		/* PUBLIC */

		pCLprocTable->bxobj = (GXObject **)cl_const_malloc(sizeof(GXObject *)*4);
		memset(pCLprocTable->bxobj,0,sizeof(GXObject *)*4);
#if 1	/* 2021.7.13 */
		sprintf(buf,"$%s = %d",D_NAM_MAX_LOOP_WHILE,MAX_LOOP_WHILE);
	/*	if ((iRc=cl_set_public_var(buf,&pCLprocTable->bxobj[0])) < 0) return iRc;	*/
		if ((iRc=cl_set_public_var(buf,NULL)) < 0) return iRc;
		sprintf(buf,"$%s = %c%c",D_NAM_TRNSHID,cM_QUOTE1,cM_QUOTE1);
	/*	if ((iRc=cl_set_public_var(buf,&pCLprocTable->bxobj[1])) < 0) return iRc;	*/
		if ((iRc=cl_set_public_var(buf,NULL)) < 0) return iRc;
		sprintf(buf,"$%s = %c%s%c",D_NAM_UNX_DATE_FORMAT,cM_QUOTE1,D_UNX_DATE_FORMAT,cM_QUOTE1);
		if ((iRc=cl_set_public_var(buf,NULL)) < 0) return iRc;
		sprintf(buf,"$%s = %c%s%c",D_NAM_SQL_DATE_FORMAT,cM_QUOTE1,D_SQL_DATE_FORMAT,cM_QUOTE1);
		if ((iRc=cl_set_public_var(buf,NULL)) < 0) return iRc;
#endif
#if 1	/* 2021.7.13 */
		pCLprocTable->myGid = ++pGLprocTable->gid;
		akxs_xhasl(pGLprocTable->pha_gid,'S',pCLprocTable->myGid,0);
#else
		pCLprocTable->pha_gid = akxs_xhasl_new(sizeof(long),20,19,0);
#endif
		pCLprocTable->pha_fp = akxs_hasl_new(sizeof(FILE *),akb_set_fd_setsize(0),0);
		pCLprocTable->pha_env = akxs_xhash_new2(0,10,7,0);

		DEBUGOUTL(101,FORMAT(13));	/* >> コマンドを発行します。 */
	}
	else {
		DEBUGOUTL(101,FORMAT(14));	/* >> R 返却パケットです。 */

		/* 保存していた環境を元に戻す */
		p += sizeof(AKAMSGCOM);
#if 1
		memcpy((char*)&pCLprocTable,p,sizeof(CLPRTBL *));
		p += sizeof(CLPRTBL *);
		memcpy((char*)&pGlobTable,p , sizeof(GlobalCt *));
DEBUGOUTL2(110,"cl_packet_check:ret: pCLprocTable=%08x pGlobTable=%08x",
pCLprocTable,pGlobTable);
#else
		memcpy((char*)&CLprocTable,p,sizeof(CLPRTBL));
		p += sizeof(CLPRTBL);
		memcpy((char*)&GlobTable,p , sizeof(GlobalCt));

		pCLprocTable = &CLprocTable;
		pGlobTable   = &GlobTable;
#endif

		cl_tmp_const_ct_set(NULL);

		/* リードした返答パケット部だけ上書きする */
		pCLprocTable->PrPacketp = (char *)tpRecvMsgCom;

		/* 後処理 */
		DEBUGOUTL(101,FORMAT(15));	/* >> R 環境を戻すことができました。 */

		/* リターンパケットか？ */
		if((cmn_chk_stat(RTN_PR , &pCLprocTable->PrSt))) {
			cl_packet_error_check(tpRecvMsgCom);
		}
		else {
			ERROROUT(FORMAT(16));	/* リターン・パケット待ちの状態でない。 */
			return 2;
		}
	}
	return NORMAL;
}

/****************************************************************
 *								*
 *	機能名		: パケット保存処理			*
 *	関数形式	: int packet_save()			*
 *								*
 *	処理内容	: 受け取りパケットの保存		*
 *								*
 *	引数の説明	: なし					*
 *								*
 *	リターン値	: NORMAL 正常終了			*
 *			  -1     異常終了			*
 *								*
 ****************************************************************/
int packet_save(p)
char *p;
{
	/* 保存領域に現環境を保存する */
DEBUGOUTL3(110,"cl_packet_check:save: pCLprocTable=%08x pGlobTable=%08x p=%08x",
pCLprocTable,pGlobTable,p);
	p += sizeof(AKAMSGCOM);
#if 1
/*
printf("packet_save: p=%08x pCLprocTable=%08x\n",p,pCLprocTable);
*/
	memcpy(p,(char*)&pCLprocTable,sizeof(CLPRTBL *));
	p += sizeof(CLPRTBL *);
	memcpy(p,(char*)&pGlobTable,sizeof(GlobalCt *));
#else
	memcpy(p,(char*)&CLprocTable,sizeof(CLPRTBL));
	p += sizeof(CLPRTBL);
	memcpy(p,(char*)&GlobTable,sizeof(GlobalCt));
#endif

	DEBUGOUTL(101,FORMAT(17));	/* >> 環境を保存しました。 */

	return(NORMAL);
}

int cl_packet_error_check(tpRecvMsg)
AKAMSGCOM *tpRecvMsg;
{
	qColToDb *pDTD;		/* 送信パケット 共通部分   */
	int lw;

	if (tpRecvMsg) {
		if (tpRecvMsg->msg_pret) {
			pGlobTable->error = tpRecvMsg->msg_pret;
			strnzcpy(pGlobTable->errmsg,akb_str_error(pGlobTable->error),
			         sizeof(pGlobTable->errmsg)-1);

DEBUGOUTL2(100,"cl_packet_error_check: ERROR=%d ERRMSG=[%s]",
pGlobTable->error,pGlobTable->errmsg);

		}
		if (tpRecvMsg->msg_mlen >= sizeof(qColToDb)) {
			pDTD = (qColToDb *)tpRecvMsg->msg_pmsg;
			/* エラーパケットのチェック */
			if((pDTD->cMsgId[0]=='C') && (pDTD->cMsgId[1]=='M')) {
				if( tpRecvMsg->msg_pret)
					lw = tpRecvMsg->msg_pret;
				else
					lw = PACKET_RECEIVE_ERROR;
				pDTD->lret = htonl(lw);
			}
		}
	}
	else return -1;
	return 0;
}
