#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "gttrading.h"
#include "gt.h"
#include "gterror.h"
#include "gtmem.h"
#include "gtsystem.h"
#include "gtsender.h"
#include "gttoolkitext.h"

GtTrading *GtTrading::_instance = NULL;

GtTrading::GtTrading() {

}

GtTrading::~GtTrading() {

}

GtTrading *GtTrading::instance() {
    if (_instance == NULL) {
        _instance = new GtTrading();
    }
    return _instance;
}

/************************************************************
*功能：调用交易网络对数据进行请求与应答，串行阻塞模式
*输入：tradingCode=交易码
*输入：inData=请求数据
*输入：inLen=请求数据长度
*输出：outData=应答数据
*输出：outLen=应答数据长度
*返回：!=0错误
*/
int GtTrading::callNetwork(unsigned long tradingCode, unsigned char *inData, int inLen,
                           unsigned char *outData, int *outLen) {
    return GtSender::sendMessage(tradingCode, inData, inLen, outData, outLen);
}

/************************************************************
*功能：连接交易服务器
*输入：tradingCode=交易码
*输出：无
*返回：!=0错误
*请求报文数据：
*1）服务器IP
*2）服务器端口
*3）终端ID
*4）操作员ID
*5）离线模式
*应答报文数据：
*无
*/
int GtTrading::t100000ConnectServer(unsigned long tradingCode) {
    int result = 0;
    int commLine = 0;
    int inLength = 0;
    int outLength = 0;
    char serverIP[256];
    unsigned int serverPort = 0;
    unsigned char inData[MAX_MTU];
    unsigned char outData[MAX_MTU];
    stTerminal terminal;
    stRuntime runtime;

    //获取内存数据
    memset(&terminal, 0, sizeof(stTerminal));
    result = GtMem::instance()->getMemTerminal(&terminal);
    if (result != 0) {
        GtToolkitExt::logError(__FILE__, __LINE__, "读取终端配置内存数据错误<%d>", result);
        return result;
    }
    memset(&runtime, 0, sizeof(stRuntime));
    result = GtMem::instance()->getMemRuntime(&runtime);
    if (result != 0) {
        GtToolkitExt::logError(__FILE__, __LINE__, "读取终端运行内存数据错误<%d>", result);
        return result;
    }
    commLine = terminal.ucCommLineNo;
    strcpy(serverIP, terminal.commAttr[commLine].strIP1);
    serverPort = terminal.commAttr[commLine].unPort1;
    memset(inData, 0, sizeof(inData));
    inLength = sprintf((char *) inData, "%s%c%d%c%s%c%d%c%d%c", serverIP, Gt::_delimiter1,
                       serverPort, Gt::_delimiter1,  //serverPort端口
                       terminal.strTerminalID, Gt::_delimiter1,//站点编号
                       runtime.ucUserID, Gt::_delimiter1,//用户ID
                       terminal.ucOfflineMode, Gt::_delimiter1);//离线模式
    memset(outData, 0, sizeof(outData));
    result = callNetwork(tradingCode, inData, inLength, outData, &outLength);
    if (result != 0) {
        GtToolkitExt::logError(__FILE__, __LINE__, "连接服务器错误<%d>", result);
        return result;
    }
    return 0;
}

/************************************************************
*功能：获取通讯加密密匙
*输入：tradingCode=交易码
*输出：无
*返回：!=0错误
*请求报文数据：
*1）本地密钥公匙
*应答报文数据：
*1）通信加密密匙
*2）通信加密类型
*/
int GtTrading::t101000CommEncryptKey(unsigned long tradingCode) {
    int result = 0;
    int inLength = 0;
    int outLength = 0;
    unsigned char inData[MAX_MTU];
    unsigned char outData[MAX_MTU];

    //通信报文交换
    memset(inData, 0, sizeof(inData));
    memset(outData, 0, sizeof(outData));
    result = callNetwork(tradingCode, inData, inLength, outData, &outLength);
    if (result != 0) {
        GtToolkitExt::logError(__FILE__, __LINE__, "获取通信加密密匙错误<%d>", result);
        return result;
    }
    return 0;
}

/************************************************************
*功能：终端登录
*输入：tradingCode=交易码
*输出：无
*返回：!=0错误
*请求报文数据：
*1）操作员认证方式
*2）操作员认证码
*3）投注机认证方式
*4）投注机认证码
*应答报文数据：
*1）用户权限级别
*2）投注机认证码
*/
int GtTrading::t101001TerminalLogin(unsigned long tradingCode) {
    int result = 0;
    char tmpBuf[1024];
    int bufLen = 0;
    int inLength = 0;
    int outLength = 0;
    unsigned char inData[MAX_MTU];
    unsigned char outData[MAX_MTU];
    stTerminal terminal;
    stRuntime runtime;

    memset(&terminal, 0, sizeof(stTerminal));
    result = GtMem::instance()->getMemTerminal(&terminal);
    if (result != 0) {
        GtToolkitExt::logError(__FILE__, __LINE__, "读取终端配置内存数据错误<%d>", result);
        return result;
    }
    memset(&runtime, 0, sizeof(stRuntime));
    result = GtMem::instance()->getMemRuntime(&runtime);
    if (result != 0) {
        GtToolkitExt::logError(__FILE__, __LINE__, "读取终端运行内存数据错误<%d>", result);
        return result;
    }
    //请求报文
    memset(inData, 0, sizeof(inData));
    inLength = sprintf((char *) inData, "%d%c%s%c%d%c%s%c", runtime.ucUserAuthType, Gt::_delimiter1,
                       runtime.strUserPassword, Gt::_delimiter1,
                       terminal.ucTerminalAuthType, Gt::_delimiter1,
                       terminal.strTerminalCert, Gt::_delimiter1);
    //通信报文交换
    memset(outData, 0, sizeof(outData));
    result = callNetwork(tradingCode, inData, inLength, outData, &outLength);
    if (result != 0) {
        GtToolkitExt::logError(__FILE__, __LINE__, "终端登录错误<%d>", result);
        return result;
    }
    //解析应答报文
    //用户权限级别
    bufLen = sizeof(tmpBuf);
    memset(tmpBuf, 0, bufLen);
    result = GtToolkitExt::parseNChar((char *) outData, tmpBuf, bufLen, Gt::_delimiter1, 1);
    if (result != 0) {
        GtToolkitExt::logError(__FILE__, __LINE__, "用户权限级别字段解析错误<%d>", result);
        return result;
    }
    runtime.ucUserLevel = atoi(tmpBuf);
    //投注机认证码
    memset(tmpBuf, 0, bufLen);
    result = GtToolkitExt::parseNChar((char *) outData, tmpBuf, bufLen, Gt::_delimiter1, 2);
    if (result != 0) {
        GtToolkitExt::logError(__FILE__, __LINE__, "投注机认证码字段解析错误<%d>", result);
        return result;
    }
    if (strlen((char *) terminal.strTerminalCert) == 0) {
        strcpy((char *) terminal.strTerminalCert, tmpBuf);
        GtToolkitExt::logInfo(__FILE__, __LINE__, "strTerminalCert == 0");
        result = GtSystem::instance()->saveTerminalConfig(&terminal);
        if (result != 0) {
            GtToolkitExt::logError(__FILE__, __LINE__, "写入终端配置错误<%d>", result);
            return result;
        }
    }
    result = GtMem::instance()->setMemRuntime(&runtime);
    if (result != 0) {
        GtToolkitExt::logError(__FILE__, __LINE__, "保存终端运行内存数据错误<%d>", result);
        return result;
    }
    return 0;
}

/************************************************************
*功能：终端登出
*输入：tradingCode=交易码
*输出：无
*返回：!=0错误
*请求报文数据：
*无
*应答报文数据：
*无
*/
int GtTrading::t101002TerminalLogout(unsigned long tradingCode) {
    int result = 0;
    int inLength = 0;
    int outLength = 0;
    unsigned char inData[MAX_MTU];
    unsigned char outData[MAX_MTU];

    //请求报文
    memset(inData, 0, sizeof(inData));
    //通信报文交换
    memset(outData, 0, sizeof(outData));
    result = callNetwork(tradingCode, inData, inLength, outData, &outLength);
    if (result != 0) {
        GtToolkitExt::logError(__FILE__, __LINE__, "终端登出错误<%d>", result);
        return result;
    }
    //解析应答报文
    return 0;
}

/************************************************************
*功能：获取终端控制参数
*输入：tradingCode=交易码
*输出：无
*返回：!=0错误
*请求报文数据：
*无
*应答报文数据：
*1）系统备份标记
*2）强制通信线路
*3）数据链路模式
*4）心跳时间间隔
*5）数据链路保持联接时间
*6）服务器时间
*7）文件下载标记
*8）打印机切纸模式
*9）是否打印兑奖单
*10）是否打印注销单
*11）是否打印冲正单
*12）是否支持半票确认功能
*13）交易确认级别
*14）游戏个数
*15）游戏ID串
*16）编号|线路类型|线路名称|拨号号码|用户|密码|DNS1|DNS2|域名|IP1|IP2|IP3|IP4|PORT1|PORT2|PORT3|PORT4|
*17）编号|线路类型|线路名称|拨号号码|用户|密码|DNS1|DNS2|域名|IP1|IP2|IP3|IP4|PORT1|PORT2|PORT3|PORT4|
*18）编号|线路类型|线路名称|拨号号码|用户|密码|DNS1|DNS2|域名|IP1|IP2|IP3|IP4|PORT1|PORT2|PORT3|PORT4|
*19）编号|线路类型|线路名称|拨号号码|用户|密码|DNS1|DNS2|域名|IP1|IP2|IP3|IP4|PORT1|PORT2|PORT3|PORT4|
*20）编号|线路类型|线路名称|拨号号码|用户|密码|DNS1|DNS2|域名|IP1|IP2|IP3|IP4|PORT1|PORT2|PORT3|PORT4|
*21）编号|线路类型|线路名称|拨号号码|用户|密码|DNS1|DNS2|域名|IP1|IP2|IP3|IP4|PORT1|PORT2|PORT3|PORT4|
*22）操作员认证方式
*23）投注机认证方式
*24）即时消息标记
*25）单票保护金额
*26）管理员密码
*27）编号|线路类型|线路名称|拨号号码|用户|密码|DNS1|DNS2|域名|IP1|IP2|IP3|IP4|PORT1|PORT2|PORT3|PORT4|
*28）编号|线路类型|线路名称|拨号号码|用户|密码|DNS1|DNS2|域名|IP1|IP2|IP3|IP4|PORT1|PORT2|PORT3|PORT4|
*29）编号|线路类型|线路名称|拨号号码|用户|密码|DNS1|DNS2|域名|IP1|IP2|IP3|IP4|PORT1|PORT2|PORT3|PORT4|
*30）编号|线路类型|线路名称|拨号号码|用户|密码|DNS1|DNS2|域名|IP1|IP2|IP3|IP4|PORT1|PORT2|PORT3|PORT4|
*31）编号|线路类型|线路名称|拨号号码|用户|密码|DNS1|DNS2|域名|IP1|IP2|IP3|IP4|PORT1|PORT2|PORT3|PORT4|
*32）编号|线路类型|线路名称|拨号号码|用户|密码|DNS1|DNS2|域名|IP1|IP2|IP3|IP4|PORT1|PORT2|PORT3|PORT4|
*/
int GtTrading::t101027GetControlInfo(unsigned long tradingCode) {
    int i = 0;
    int n = 0;
    int result = 0;
    char tmpBuf[2048];
    char buf[2048];
    int bufLen = 0;
    int inLength = 0;
    int outLength = 0;
    unsigned char inData[MAX_MTU];
    unsigned char outData[MAX_MTU];
    stCtrlInfo ctrlInfo;
    stTerminal terminal;
    stGameRuntime gameRuntime;

    //请求报文
    memset(inData, 0, sizeof(inData));
    //通信报文交换
    memset(outData, 0, sizeof(outData));
    result = callNetwork(tradingCode, inData, inLength, outData, &outLength);
    if (result != 0) {
        GtToolkitExt::logError(__FILE__, __LINE__, "网络交易错误<%d>", result);
        return result;
    }
    //解析应答报文
    //获取内存数据
    memset(&terminal, 0, sizeof(stTerminal));
    result = GtMem::instance()->getMemTerminal(&terminal);
    if (result != 0) {
        GtToolkitExt::logError(__FILE__, __LINE__, "读取终端配置内存数据错误<%d>", result);
        return result;
    }
    memset(&ctrlInfo, 0, sizeof(stCtrlInfo));
    result = GtMem::instance()->getMemCtrlInfo(&ctrlInfo);
    if (result != 0) {
        GtToolkitExt::logError(__FILE__, __LINE__, "读取控制配置内存数据错误<%d>", result);
        return result;
    }
    memset(&gameRuntime, 0, sizeof(stGameRuntime));
    result = GtMem::instance()->getMemGameRuntime(&gameRuntime);
    if (result != 0) {
        GtToolkitExt::logError(__FILE__, __LINE__, "读取游戏内存数据错误<%d>", result);
        return result;
    }
    //系统备份标记
    bufLen = sizeof(tmpBuf);
    memset(tmpBuf, 0, bufLen);
    result = GtToolkitExt::parseNChar((char *) outData, tmpBuf, bufLen, Gt::_delimiter1, 1);
    if (result != 0) {
        GtToolkitExt::logError(__FILE__, __LINE__, "系统备份标记字段解析错误<%d>", result);
        return result;
    }
    ctrlInfo.ucBackupFlag = atoi(tmpBuf);
    //强制通信线路
    memset(tmpBuf, 0, bufLen);
    result = GtToolkitExt::parseNChar((char *) outData, tmpBuf, bufLen, Gt::_delimiter1, 2);
    if (result != 0) {
        GtToolkitExt::logError(__FILE__, __LINE__, "强制通信线路字段解析错误<%d>", result);
        return result;
    }
    ctrlInfo.ucConnLine = atoi(tmpBuf);
    //数据链路模式
    memset(tmpBuf, 0, bufLen);
    result = GtToolkitExt::parseNChar((char *) outData, tmpBuf, bufLen, Gt::_delimiter1, 3);
    if (result != 0) {
        GtToolkitExt::logError(__FILE__, __LINE__, "数据链路模式字段解析错误<%d>", result);
        return result;
    }

    //心跳时间间隔
    memset(tmpBuf, 0, bufLen);
    result = GtToolkitExt::parseNChar((char *) outData, tmpBuf, bufLen, Gt::_delimiter1, 4);
    if (result != 0) {
        GtToolkitExt::logError(__FILE__, __LINE__, "心跳时间字段解析错误<%d>", result);
        return result;
    }
    ctrlInfo.unHeartbeatTime = atol(tmpBuf);
    //数据链路保持联接时间
    memset(tmpBuf, 0, bufLen);
    result = GtToolkitExt::parseNChar((char *) outData, tmpBuf, bufLen, Gt::_delimiter1, 5);
    if (result != 0) {
        GtToolkitExt::logError(__FILE__, __LINE__, "数据链路时间字段解析错误<%d>", result);
        return result;
    }
    //terminal.ulConnTime = atol(tmpBuf);
    //服务器时间
    memset(tmpBuf, 0, bufLen);
    result = GtToolkitExt::parseNChar((char *) outData, tmpBuf, bufLen, Gt::_delimiter1, 6);
    if (result != 0) {
        GtToolkitExt::logError(__FILE__, __LINE__, "服务器时间字段解析错误<%d>", result);
        return result;
    }
    ctrlInfo.lServerTime = atol(tmpBuf);
    //文件下载标记
    memset(tmpBuf, 0, bufLen);
    result = GtToolkitExt::parseNChar((char *) outData, tmpBuf, bufLen, Gt::_delimiter1, 7);
    if (result != 0) {
        GtToolkitExt::logError(__FILE__, __LINE__, "文件下载标记字段解析错误<%d>", result);
        return result;
    }
    ctrlInfo.ucFileDownload = atoi(tmpBuf);
    //打印机切纸模式
    memset(tmpBuf, 0, bufLen);
    result = GtToolkitExt::parseNChar((char *) outData, tmpBuf, bufLen, Gt::_delimiter1, 8);
    if (result != 0) {
        GtToolkitExt::logError(__FILE__, __LINE__, "打印机切纸模式字段解析错误<%d>", result);
        return result;
    }
    ctrlInfo.ucPaperMarkMode = atoi(tmpBuf);
    //是否打印兑奖单
    memset(tmpBuf, 0, bufLen);
    result = GtToolkitExt::parseNChar((char *) outData, tmpBuf, bufLen, Gt::_delimiter1, 9);
    if (result != 0) {
        GtToolkitExt::logError(__FILE__, __LINE__, "打印兑奖单字段解析错误<%d>", result);
        return result;
    }
    ctrlInfo.ucPrnCashBill = atoi(tmpBuf);
    //是否打印注销单
    memset(tmpBuf, 0, bufLen);
    result = GtToolkitExt::parseNChar((char *) outData, tmpBuf, bufLen, Gt::_delimiter1, 10);
    if (result != 0) {
        GtToolkitExt::logError(__FILE__, __LINE__, "打印注销单字段解析错误<%d>", result);
        return result;
    }
    ctrlInfo.ucPrnCancelBill = atoi(tmpBuf);
    //是否打印冲正单
    memset(tmpBuf, 0, bufLen);
    result = GtToolkitExt::parseNChar((char *) outData, tmpBuf, bufLen, Gt::_delimiter1, 11);
    if (result != 0) {
        GtToolkitExt::logError(__FILE__, __LINE__, "打印冲正单字段解析错误<%d>", result);
        return result;
    }
    ctrlInfo.ucPrnSPCancelBill = atoi(tmpBuf);
    //是否支持半票确认功能
    memset(tmpBuf, 0, bufLen);
    result = GtToolkitExt::parseNChar((char *) outData, tmpBuf, bufLen, Gt::_delimiter1, 12);
    if (result != 0) {
        GtToolkitExt::logError(__FILE__, __LINE__, "半票确认功能字段解析错误<%d>", result);
        return result;
    }
    ctrlInfo.ucHalfTicketCtrl = atoi(tmpBuf);
    //交易确认级别
    memset(tmpBuf, 0, bufLen);
    result = GtToolkitExt::parseNChar((char *) outData, tmpBuf, bufLen, Gt::_delimiter1, 13);
    if (result != 0) {
        GtToolkitExt::logError(__FILE__, __LINE__, "交易确认级别字段解析错误<%d>", result);
        return result;
    }
    ctrlInfo.ucEnterLevel = atoi(tmpBuf);
    //游戏个数
    memset(tmpBuf, 0, bufLen);
    result = GtToolkitExt::parseNChar((char *) outData, tmpBuf, bufLen, Gt::_delimiter1, 14);
    if (result != 0) {
        GtToolkitExt::logError(__FILE__, __LINE__, "游戏个数字段解析错误<%d>", result);
        return result;
    }
    gameRuntime.unGameCount = atoi(tmpBuf);
    //游戏ID串
    memset(tmpBuf, 0, bufLen);
    result = GtToolkitExt::parseNChar((char *) outData, tmpBuf, bufLen, Gt::_delimiter1, 15);
    if (result != 0) {
        GtToolkitExt::logError(__FILE__, __LINE__, "游戏ID字段解析错误<%d>", result);
        return result;
    }
    for (i = 0; i < (int) gameRuntime.unGameCount; i++) {
        memset(buf, 0, sizeof(buf));
        result = GtToolkitExt::parseNChar(tmpBuf, buf, bufLen, Gt::_delimiter2, i + 1);
        if (result != 0) {
            GtToolkitExt::logError(__FILE__, __LINE__, "游戏ID字段解析错误<%d>", result);
            return result;
        }
        gameRuntime.gameAttr[i].unGameID = atoi(buf);
        gameRuntime.strGameSN[gameRuntime.gameAttr[i].unGameID] = i;
    }
    //操作员认证方式
    memset(tmpBuf, 0, bufLen);
    result = GtToolkitExt::parseNChar((char *) outData, tmpBuf, bufLen, Gt::_delimiter1, 22);
    if (result != 0) {
        GtToolkitExt::logError(__FILE__, __LINE__, "操作员认证方式字段解析错误<%d>", result);
        return result;
    }
    ctrlInfo.ucUserAuthType = atoi(tmpBuf);
    //投注机认证方式
    memset(tmpBuf, 0, bufLen);
    result = GtToolkitExt::parseNChar((char *) outData, tmpBuf, bufLen, Gt::_delimiter1, 23);
    if (result != 0) {
        GtToolkitExt::logError(__FILE__, __LINE__, "投注机认证方式字段解析错误<%d>", result);
        return result;
    }
    ctrlInfo.ucTerminalAuthType = atoi(tmpBuf);
    //即时消息标记
    memset(tmpBuf, 0, bufLen);
    result = GtToolkitExt::parseNChar((char *) outData, tmpBuf, bufLen, Gt::_delimiter1, 24);
    if (result != 0) {
        GtToolkitExt::logError(__FILE__, __LINE__, "即时消息标记字段解析错误<%d>", result);
        return result;
    }
    ctrlInfo.ucMsgDownload = atoi(tmpBuf);
    //单票保护金额
    memset(tmpBuf, 0, bufLen);
    result = GtToolkitExt::parseNChar((char *) outData, tmpBuf, bufLen, Gt::_delimiter1, 25);
    if (result != 0) {
        GtToolkitExt::logError(__FILE__, __LINE__, "单票保护金额字段解析错误<%d>", result);
        return result;
    }
    ctrlInfo.dTicketCtrlMoney = atof(tmpBuf);
    //管理员密码
    memset(tmpBuf, 0, bufLen);
    result = GtToolkitExt::parseNChar((char *) outData, tmpBuf, bufLen, Gt::_delimiter1, 26);
    if (result != 0) {
        GtToolkitExt::logError(__FILE__, __LINE__, "管理员密码字段解析错误<%d>", result);
        return result;
    }
    if (strlen(tmpBuf) >= sizeof(ctrlInfo.strAdminPwd)) {
        GtToolkitExt::logError(__FILE__, __LINE__, "管理员密码字段解析错误<%d>", GtError::ERROR_INVALID_VALUE);
        return GtError::ERROR_INVALID_VALUE;
    }
    strcpy(ctrlInfo.strAdminPwd, tmpBuf);
    //通信线路
    for (i = 0; i < 12; i++) {
        if (i < 6) {
            n = i;
        } else {
            n = i + 5;
        }
        memset(tmpBuf, 0, bufLen);
        result = GtToolkitExt::parseNChar((char *) outData, tmpBuf, bufLen, Gt::_delimiter1,
                                          16 + n);
        if (result != 0) {
            GtToolkitExt::logError(__FILE__, __LINE__, "通信线路字段解析错误<%d>", result);
            return result;
        }
        if (strlen(tmpBuf) == 0) {
            continue;
        }
        memset(buf, 0, sizeof(buf));
        result = GtToolkitExt::parseNChar(tmpBuf, buf, bufLen, Gt::_delimiter2, 1);
        if (result != 0) {
            GtToolkitExt::logError(__FILE__, __LINE__, "通信线路字段解析错误<%d>", result);
            return result;
        }
        terminal.commAttr[i].ucLineID = atoi(buf);
        memset(buf, 0, sizeof(buf));
        result = GtToolkitExt::parseNChar(tmpBuf, buf, bufLen, Gt::_delimiter2, 2);
        if (result != 0) {
            GtToolkitExt::logError(__FILE__, __LINE__, "通信线路字段解析错误<%d>", result);
            return result;
        }
        terminal.commAttr[i].ucType = atoi(buf);
        memset(buf, 0, sizeof(buf));
        result = GtToolkitExt::parseNChar(tmpBuf, buf, bufLen, Gt::_delimiter2, 3);
        if (result != 0) {
            GtToolkitExt::logError(__FILE__, __LINE__, "通信线路字段解析错误<%d>", result);
            return result;
        }
        strcpy(terminal.commAttr[i].strName, buf);
        memset(buf, 0, sizeof(buf));
        result = GtToolkitExt::parseNChar(tmpBuf, buf, bufLen, Gt::_delimiter2, 4);
        if (result != 0) {
            GtToolkitExt::logError(__FILE__, __LINE__, "通信线路字段解析错误<%d>", result);
            return result;
        }
        strcpy(terminal.commAttr[i].strDialNum, buf);
        memset(buf, 0, sizeof(buf));
        result = GtToolkitExt::parseNChar(tmpBuf, buf, bufLen, Gt::_delimiter2, 5);
        if (result != 0) {
            GtToolkitExt::logError(__FILE__, __LINE__, "通信线路字段解析错误<%d>", result);
            return result;
        }
        strcpy(terminal.commAttr[i].strUser, buf);
        memset(buf, 0, sizeof(buf));
        result = GtToolkitExt::parseNChar(tmpBuf, buf, bufLen, Gt::_delimiter2, 6);
        if (result != 0) {
            GtToolkitExt::logError(__FILE__, __LINE__, "通信线路字段解析错误<%d>", result);
            return result;
        }
        strcpy(terminal.commAttr[i].strPassword, buf);
        memset(buf, 0, sizeof(buf));
        result = GtToolkitExt::parseNChar(tmpBuf, buf, bufLen, Gt::_delimiter2, 7);
        if (result != 0) {
            GtToolkitExt::logError(__FILE__, __LINE__, "通信线路字段解析错误<%d>", result);
            return result;
        }
        strcpy(terminal.commAttr[i].strDns1, buf);
        memset(buf, 0, sizeof(buf));
        result = GtToolkitExt::parseNChar(tmpBuf, buf, bufLen, Gt::_delimiter2, 8);
        if (result != 0) {
            GtToolkitExt::logError(__FILE__, __LINE__, "通信线路字段解析错误<%d>", result);
            return result;
        }
        strcpy(terminal.commAttr[i].strDns2, buf);
        memset(buf, 0, sizeof(buf));
        result = GtToolkitExt::parseNChar(tmpBuf, buf, bufLen, Gt::_delimiter2, 9);
        if (result != 0) {
            GtToolkitExt::logError(__FILE__, __LINE__, "通信线路字段解析错误<%d>", result);
            return result;
        }
        strcpy(terminal.commAttr[i].strDomainName, buf);
        memset(buf, 0, sizeof(buf));
        result = GtToolkitExt::parseNChar(tmpBuf, buf, bufLen, Gt::_delimiter2, 10);
        if (result != 0) {
            GtToolkitExt::logError(__FILE__, __LINE__, "通信线路字段解析错误<%d>", result);
            return result;
        }
        //strcpy(terminal.commAttr[i].strIP1, buf);
        memset(buf, 0, sizeof(buf));
        result = GtToolkitExt::parseNChar(tmpBuf, buf, bufLen, Gt::_delimiter2, 11);
        if (result != 0) {
            GtToolkitExt::logError(__FILE__, __LINE__, "通信线路字段解析错误<%d>", result);
            return result;
        }
        //strcpy(terminal.commAttr[i].strIP2, buf);
        memset(buf, 0, sizeof(buf));
        result = GtToolkitExt::parseNChar(tmpBuf, buf, bufLen, Gt::_delimiter2, 12);
        if (result != 0) {
            GtToolkitExt::logError(__FILE__, __LINE__, "通信线路字段解析错误<%d>", result);
            return result;
        }
        //strcpy(terminal.commAttr[i].strIP3, buf);
        memset(buf, 0, sizeof(buf));
        result = GtToolkitExt::parseNChar(tmpBuf, buf, bufLen, Gt::_delimiter2, 13);
        if (result != 0) {
            GtToolkitExt::logError(__FILE__, __LINE__, "通信线路字段解析错误<%d>", result);
            return result;
        }
        //strcpy(terminal.commAttr[i].strIP4, buf);
        memset(buf, 0, sizeof(buf));
        result = GtToolkitExt::parseNChar(tmpBuf, buf, bufLen, Gt::_delimiter2, 14);
        if (result != 0) {
            GtToolkitExt::logError(__FILE__, __LINE__, "通信线路字段解析错误<%d>", result);
            return result;
        }
        //terminal.commAttr[i].unPort1 = atoi(buf);
        memset(buf, 0, sizeof(buf));
        result = GtToolkitExt::parseNChar(tmpBuf, buf, bufLen, Gt::_delimiter2, 15);
        if (result != 0) {
            GtToolkitExt::logError(__FILE__, __LINE__, "通信线路字段解析错误<%d>", result);
            return result;
        }
        //terminal.commAttr[i].unPort2 = atoi(buf);
        memset(buf, 0, sizeof(buf));
        result = GtToolkitExt::parseNChar(tmpBuf, buf, bufLen, Gt::_delimiter2, 16);
        if (result != 0) {
            GtToolkitExt::logError(__FILE__, __LINE__, "通信线路字段解析错误<%d>", result);
            return result;
        }
        //terminal.commAttr[i].unPort3 = atoi(buf);
        memset(buf, 0, sizeof(buf));
        result = GtToolkitExt::parseNChar(tmpBuf, buf, bufLen, Gt::_delimiter2, 17);
        if (result != 0) {
            GtToolkitExt::logError(__FILE__, __LINE__, "通信线路字段解析错误<%d>", result);
            return result;
        }
        //terminal.commAttr[i].unPort4 = atoi(buf);
    }
    result = GtSystem::instance()->saveTerminalConfig(&terminal);
    if (result != 0) {
        GtToolkitExt::logError(__FILE__, __LINE__, "写入终端配置错误<%d>", result);
        return result;
    }
    result = GtMem::instance()->setMemCtrlInfo(&ctrlInfo);
    if (result != 0) {
        GtToolkitExt::logError(__FILE__, __LINE__, "写入控制配置内存数据错误<%d>", result);
        return result;
    }
    result = GtMem::instance()->setMemGameRuntime(&gameRuntime);
    if (result != 0) {
        GtToolkitExt::logError(__FILE__, __LINE__, "写入游戏内存数据错误<%d>", result);
        return result;
    }
    return 0;
}

/************************************************************
*功能：同步服务器时间
*输入：tradingCode=交易码
*输出：cTime=服务器时间
*返回：!=0错误
*请求报文数据：
*无
*应答报文数据：
*1）服务器时间（time_t）
*/
int GtTrading::t101025GetServerTime(unsigned long tradingCode, time_t *cTime) {
    int result = 0;
    int inLength = 0;
    int outLength = 0;
    char tmpBuf[1024];
    unsigned char inData[MAX_MTU];
    unsigned char outData[MAX_MTU];

    //请求报文
    memset(inData, 0, sizeof(inData));
    //通信报文交换
    memset(outData, 0, sizeof(outData));
    result = callNetwork(tradingCode, inData, inLength, outData, &outLength);
    if (result != 0) {
        GtToolkitExt::logError(__FILE__, __LINE__, "同步服务器时间错误<%d>", result);
        return result;
    }
    //解析应答报文
    memset(tmpBuf, 0, sizeof(tmpBuf));
    result = GtToolkitExt::parseNChar((char *) outData, tmpBuf, sizeof(tmpBuf), Gt::_delimiter1, 1);
    if (result != 0) {
        GtToolkitExt::logError(__FILE__, __LINE__, "服务器时间字段解析错误<%d>", result);
        return result;
    }
    *cTime = atol(tmpBuf);
    return 0;
}

/************************************************************
*功能：终端升级包下载成功
*输入：tradingCode=交易码
*输出：无
*返回：!=0错误
*请求报文数据：
*无
*应答报文数据：
*无
*/
int GtTrading::t101023AppUpgradeCompleted(unsigned long tradingCode) {
    int result = 0;
    int inLength = 0;
    int outLength = 0;
    unsigned char inData[MAX_MTU];
    unsigned char outData[MAX_MTU];

    //请求报文
    memset(inData, 0, sizeof(inData));
    //通信报文交换
    memset(outData, 0, sizeof(outData));
    result = callNetwork(tradingCode, inData, inLength, outData, &outLength);
    if (result != 0) {
        GtToolkitExt::logError(__FILE__, __LINE__, "[%d]升级确认交易错误", result);
        return result;
    }
    return 0;
}

#if 0
/************************************************************
*功能：上传软件信息
*输入：tradingCode=交易码
*输入：softwareInfo=软件信息
*输出：无
*返回：!=0错误
*请求报文数据：
*1）操作系统版本
*2）app版本
*3）动态库个数
*4）动态库名称（多笔报文）
*5）动态库版本（多笔报文）
*/
int GtTrading::uploadSoftwareInfo(unsigned long tradingCode, stSoftware *softwareInfo)
{
    int result = 0;
    int inLength = 0;
    int outLength = 0;
    unsigned char inData[MAX_MTU];
    unsigned char outData[MAX_MTU];

    //请求报文
    memset(inData, 0, sizeof(inData));
    length = sprintf((char *)inData, "%s%c%s%c%d%c%c%c",
        softwareInfo->strOSVersion, Gt::_delimiter1,
        softwareInfo->strAppVersion, Gt::_delimiter1,
        0, Gt::_delimiter1,
        Gt::_delimiter1,
        Gt::_delimiter1);
    //通信报文交换
    memset(outData, 0, sizeof(outData));
    result = callNetwork(tradingCode, inData, inLength, outData, &outLength);
    if(result != 0)
    {
        GtToolkitExt::logError(__FILE__, __LINE__, "[%d]上传软件信息错误", result);
        return result;
    }
    return 0;
}

/************************************************************
*功能：上传硬件信息
*输入：tradingCode=交易码
*输入：hardwareInfo=硬件信息
*输出：无
*返回：!=0错误
*请求报文数据：
*1）投注机型号
*2）主板型号
*3）打印机型号
*4）阅读器型号
*5）LCD型号
*6）Modem型号
*7）显卡型号
*8）机顶盒型号
*9）电源型号
*10）键盘型号
*11）扫描枪型号
*12）DOM盘型号
*13）客显屏型号
*14）户外LED型号
*15）磁卡/IC卡型号
*16）串口转接型号
*17）条码登记方式
*18）终端整机条码
*19）BIOS版本号
*20）CPU主频
*21）内存大小
*22）打印机固件版本号
*23）打印机机头型号
*24）阅读器固件版本号
*25）hda1总空间大小|hda1可用空间大小
*26）hda2总空间大小|hda2可用空间大小
*27）hda3总空间大小|hda3可用空间大小
*28）主板ID
*29）打印机ID
*30）UPS ID
*31）UPS型号
*32）阅读器ID
*33）LCD ID
*34）监控版版本
*/
int GtTrading::uploadHardwareInfo(unsigned long tradingCode, stHardware *hardwareInfo)
{
    int result = 0;
    int inLength = 0;
    int outLength = 0;
    unsigned char inData[MAX_MTU];
    unsigned char outData[MAX_MTU];
    stTerminal terminal;

    result = GtMem::instance()->getMemTerminal(&terminal);
    if(result != 0)
    {
        GtToolkitExt::logError(__FILE__, __LINE__, "[%d]读取终端配置内存数据错误", result);
        return result;
    }
    //请求报文
    memset(inData, 0, sizeof(inData));
    length = sprintf((char *)inData, "%s%c%c%s%c%c%c%c%c%c%c%c%c%c%c%c%c%c%d%c%s%c%c%c%c%s%c%c%c%c%c%c%c%s%c%c%c%c%c%c",
        terminal.strTerminalType, Gt::_delimiter1,
        Gt::_delimiter1,
        hardwareInfo->strPrinterType,Gt::_delimiter1,
        Gt::_delimiter1, Gt::_delimiter1,
        Gt::_delimiter1, Gt::_delimiter1,
        Gt::_delimiter1, Gt::_delimiter1,
        Gt::_delimiter1, Gt::_delimiter1,
        Gt::_delimiter1, Gt::_delimiter1,
        Gt::_delimiter1, Gt::_delimiter1,
        Gt::_delimiter1,
        1, Gt::_delimiter1,
        hardwareInfo->strMachineBarCode, Gt::_delimiter1,
        Gt::_delimiter1, Gt::_delimiter1,
        Gt::_delimiter1,
        hardwareInfo->strPrinterFirmwareVer, Gt::_delimiter1,
        Gt::_delimiter1,
        Gt::_delimiter1,
        Gt::_delimiter1,
        Gt::_delimiter1,
        Gt::_delimiter1,
        Gt::_delimiter1,
        hardwareInfo->strPrinterID, Gt::_delimiter1,
        Gt::_delimiter1, Gt::_delimiter1,
        Gt::_delimiter1, Gt::_delimiter1,
        Gt::_delimiter1);
    //通信报文交换
    memset(outData, 0, sizeof(outData));
    result = callNetwork(tradingCode, inData, inLength, outData, &outLength);
    if(result != 0)
    {
        GtToolkitExt::logError(__FILE__, __LINE__, "[%d]网络交易错误", result);
        return result;
    }
    return 0;
}

#endif
