int main(int argc, char* argv[])
{
	// create a CThostFtdcTraderApi instance
	CThostFtdcTraderApi *pUserApi = CThostFtdcTraderApi::CreateFtdcTraderApi();

	// create an event handler instance
	CSimpleHandler pSpi(pUserApi);

	// register an event handler instance
	pUserApi->RegisterSpi(&pSpi);

	pUserApi->SubscribePrivateTopic(THOST_TERT_QUICK);

	pUserApi->SubscribePublicTopic(THOST_TERT_QUICK);

	pUserApi->RegisterFront("tcp://10.253.117.107:13153");
	pUserApi->Init();

	printf ("\npress return to release...\n");
	getchar();

	// release the API instance
	pUserApi->Release();

	printf ("\npress return to quit...\n");
	getchar();

	return 0;
}
示例#2
0
  CTPWRAPPER_API int Process(void *instance,CTP_REQUEST_TYPE type,int p1,char *p2)
  {

    printf("Process %s","Process");

    switch(type)
    {

    case TraderApiCreate:
      {
        //C#回调函数
        CTPResponseCallback callback = (CTPResponseCallback)instance;

        //创建交易接口
        CThostFtdcTraderApi* pTrader = CThostFtdcTraderApi::CreateFtdcTraderApi(p2);
        //创建交易回调
        CTPTraderSpi *spi = new CTPTraderSpi(callback);

        //注册回调
        pTrader->RegisterSpi((CThostFtdcTraderSpi*)spi);

        //订阅流
        pTrader->SubscribePublicTopic((THOST_TE_RESUME_TYPE)p1);
        pTrader->SubscribePrivateTopic((THOST_TE_RESUME_TYPE)p1);

        return (int)pTrader;
      }


      ///删除接口对象本身
      ///@remark 不再使用本接口对象时,调用该函数删除接口对象
    case TraderApiRelease:
      {

        CThostFtdcTraderApi* pTrader = (CThostFtdcTraderApi *)instance;

        pTrader->RegisterSpi(NULL);
        pTrader->Release();

        //delete pTrader;
        instance = NULL;

        return 0;
      }

      ///初始化
      ///@remark 初始化运行环境,只有调用后,接口才开始工作
    case  TraderApiInit:
      {
        ((CThostFtdcTraderApi *)instance)->Init();
        return 0;
      }

      ///等待接口线程结束运行
      ///@return 线程退出代码
    case TraderApiJoin:
      {
        return ((CThostFtdcTraderApi *)instance)->Join();
      }

      ///获取当前交易日
      ///@retrun 获取到的交易日
      ///@remark 只有登录成功后,才能得到正确的交易日
    case TraderApiGetTradingDay:
      {
        const char* date = ((CThostFtdcTraderApi *)instance)->GetTradingDay();
        memcpy(p2,date,sizeof(date));
        return 0;
      }

      ///注册前置机网络地址
      ///@param pszFrontAddress:前置机网络地址。
      ///@remark 网络地址的格式为:“protocol://ipaddress:port”,如:”tcp://127.0.0.1:17001”。 
      ///@remark “tcp”代表传输协议,“127.0.0.1”代表服务器地址。”17001”代表服务器端口号。
    case TraderApiRegisterFront:
      {
        ((CThostFtdcTraderApi *)instance)->RegisterFront(p2);
        return 0;
      }

      ///注册前置机网络地址
      ///@param pszFrontAddress:前置机网络地址。
      ///@remark 网络地址的格式为:“protocol://ipaddress:port”,如:”tcp://127.0.0.1:17001”。 
      ///@remark “tcp”代表传输协议,“127.0.0.1”代表服务器地址。”17001”代表服务器端口号。
    case TraderApiRegisterNameServer:
      {
        ((CThostFtdcTraderApi *)instance)->RegisterNameServer(p2);
        return 0;
      }

      ///注册回调接口
      ///@param pSpi 派生自回调接口类的实例
    case TraderApiRegisterSpi:
      {
        ((CThostFtdcTraderApi *)instance)->RegisterSpi((CThostFtdcTraderSpi*)p1);
        return 0;
      }

      ///订阅私有流。
      ///@param nResumeType 私有流重传方式  
      ///        THOST_TERT_RESTART:从本交易日开始重传
      ///        THOST_TERT_RESUME:从上次收到的续传
      ///        THOST_TERT_QUICK:只传送登录后私有流的内容
      ///@remark 该方法要在Init方法前调用。若不调用则不会收到私有流的数据。
    case TraderApiSubscribePrivateTopic:
      {
        ((CThostFtdcTraderApi *)instance)->SubscribePrivateTopic((THOST_TE_RESUME_TYPE)p1);
        return 0;
      }

      ///订阅公共流。
      ///@param nResumeType 公共流重传方式  
      ///        THOST_TERT_RESTART:从本交易日开始重传
      ///        THOST_TERT_RESUME:从上次收到的续传
      ///        THOST_TERT_QUICK:只传送登录后公共流的内容
      ///@remark 该方法要在Init方法前调用。若不调用则不会收到公共流的数据。
    case TraderApiSubscribePublicTopic:
      {
        ((CThostFtdcTraderApi *)instance)->SubscribePublicTopic((THOST_TE_RESUME_TYPE)p1);
        return 0;
      }


    case MarketDataCreate:
      {
        //C#回调函数
        CTPResponseCallback callback = (CTPResponseCallback)instance;

        //创建行情接口
        CThostFtdcMdApi* pMdApi = CThostFtdcMdApi::CreateFtdcMdApi(p2);
        //创建交易回调
        CTPMarketDataSpi *spi = new CTPMarketDataSpi(callback);

        //注册回调
        pMdApi->RegisterSpi(spi);
        
        //订阅流
        //pMdApi->SubscribePublicTopic((THOST_TE_RESUME_TYPE)p1);
        //pMdApi->SubscribePrivateTopic((THOST_TE_RESUME_TYPE)p1);

        return (int)pMdApi;
      }

    case MarketDataRelease:
      {
        CThostFtdcMdApi* pMarketData = (CThostFtdcMdApi *)instance;

        pMarketData->RegisterSpi(NULL);
        pMarketData->Release();

        //delete instance;
        instance = NULL;

        return 0;
      }

    case MarketDataInit:
      {
        ((CThostFtdcMdApi *)instance)->Init();
        return 0;
      }

    case MarketDataRegisterFront:
      {
        ((CThostFtdcMdApi *)instance)->RegisterFront(p2);
        return 0;
      }

    case MarketDataRegisterNameServer:
      {
        ((CThostFtdcMdApi *)instance)->RegisterNameServer(p2);
        return 0;
      }
      
       ///订阅行情
    case MarketDataSubscribeMarketData:
      {
        char** p = (char**)calloc(p1, sizeof(char*));

        p = (char**)p2;

        p[0] = *p;

        for(int i=1;i<p1-1;i++)
        {
          p[i] = p[i-1] + strlen(p[i-1]) + 1;
        }

        return ((CThostFtdcMdApi*)instance)->SubscribeMarketData(p, p1);
      }

       ///退订行情
    case MarketDataUnSubscribeMarketData:
      {
        char** p = (char**)calloc(p1, sizeof(char*));

        

        //p = (char**)p2;
        OutputDebugStringA(p2);
        p[0] = p2;

        for(int i=1;i<p1-1;i++)
        {
          p[i] = p[i-1] + strlen(p[i-1]) + 1;
        }

        return ((CThostFtdcMdApi*)instance)->UnSubscribeMarketData(p, p1);
      }
    }
  }