273 lines
7.0 KiB
C
273 lines
7.0 KiB
C
|
//˵<><CBB5><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
|
|||
|
//ϵͳ
|
|||
|
#include "stdafx.h"
|
|||
|
#include <string>
|
|||
|
#include <queue>
|
|||
|
|
|||
|
//Boost
|
|||
|
#define BOOST_PYTHON_STATIC_LIB
|
|||
|
#include <boost/python/module.hpp> //python<6F><6E>װ
|
|||
|
#include <boost/python/def.hpp> //python<6F><6E>װ
|
|||
|
#include <boost/python/dict.hpp> //python<6F><6E>װ
|
|||
|
#include <boost/python/object.hpp> //python<6F><6E>װ
|
|||
|
#include <boost/python.hpp> //python<6F><6E>װ
|
|||
|
#include <boost/thread.hpp> //<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>е<EFBFBD><D0B5>̹߳<DFB3><CCB9><EFBFBD>
|
|||
|
#include <boost/bind.hpp> //<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>е<EFBFBD><D0B5>̹߳<DFB3><CCB9><EFBFBD>
|
|||
|
#include <boost/any.hpp> //<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>е<EFBFBD><D0B5><EFBFBD><EFBFBD><EFBFBD>ʵ<EFBFBD><CAB5>
|
|||
|
|
|||
|
//API
|
|||
|
#include "SecurityFtdcMdApi.h"
|
|||
|
|
|||
|
//<2F><><EFBFBD><EFBFBD><EFBFBD>ռ<EFBFBD>
|
|||
|
using namespace std;
|
|||
|
using namespace boost::python;
|
|||
|
using namespace boost;
|
|||
|
|
|||
|
//<2F><><EFBFBD><EFBFBD>
|
|||
|
#define ONFRONTCONNECTED 0
|
|||
|
#define ONFRONTDISCONNECTED 1
|
|||
|
#define ONHEARTBEATWARNING 2
|
|||
|
#define ONRSPERROR 3
|
|||
|
#define ONRSPUSERLOGIN 4
|
|||
|
#define ONRSPUSERLOGOUT 5
|
|||
|
#define ONRSPSUBMARKETDATA 6
|
|||
|
#define ONRSPUNSUBMARKETDATA 7
|
|||
|
#define ONRTNDEPTHMARKETDATA 8
|
|||
|
#define EXIT 0
|
|||
|
|
|||
|
|
|||
|
///-------------------------------------------------------------------------------------
|
|||
|
///API<50>еIJ<D0B5><C4B2><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
///-------------------------------------------------------------------------------------
|
|||
|
|
|||
|
//GILȫ<4C><C8AB><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ȡ<EFBFBD>ã<EFBFBD>
|
|||
|
//<2F><><EFBFBD>ڰ<EFBFBD><DAB0><EFBFBD>C++<2B>̻߳<DFB3><CCBB><EFBFBD>GIL<49><4C><EFBFBD><EFBFBD><EFBFBD>Ӷ<EFBFBD><D3B6><EFBFBD>ֹpython<6F><6E><EFBFBD><EFBFBD>
|
|||
|
class PyLock
|
|||
|
{
|
|||
|
private:
|
|||
|
PyGILState_STATE gil_state;
|
|||
|
|
|||
|
public:
|
|||
|
//<2F><>ij<EFBFBD><C4B3><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>д<EFBFBD><D0B4><EFBFBD><EFBFBD>ö<EFBFBD><C3B6><EFBFBD>ʱ<EFBFBD><CAB1><EFBFBD><EFBFBD><EFBFBD><EFBFBD>GIL<49><4C>
|
|||
|
PyLock()
|
|||
|
{
|
|||
|
gil_state = PyGILState_Ensure();
|
|||
|
}
|
|||
|
|
|||
|
//<2F><>ij<EFBFBD><C4B3><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ɺ<EFBFBD><C9BA><EFBFBD><EFBFBD>ٸö<D9B8><C3B6><EFBFBD>ʱ<EFBFBD><CAB1><EFBFBD><EFBFBD><EFBFBD><EFBFBD>GIL<49><4C>
|
|||
|
~PyLock()
|
|||
|
{
|
|||
|
PyGILState_Release(gil_state);
|
|||
|
}
|
|||
|
};
|
|||
|
|
|||
|
|
|||
|
//<2F><><EFBFBD><EFBFBD><EFBFBD>ṹ<EFBFBD><E1B9B9>
|
|||
|
struct Task
|
|||
|
{
|
|||
|
int task_name; //<2F>ص<EFBFBD><D8B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ƶ<EFBFBD>Ӧ<EFBFBD>ij<EFBFBD><C4B3><EFBFBD>
|
|||
|
any task_data; //<2F><><EFBFBD>ݽṹ<DDBD><E1B9B9>
|
|||
|
any task_error; //<2F><><EFBFBD><EFBFBD><EFBFBD>ṹ<EFBFBD><E1B9B9>
|
|||
|
int task_id; //<2F><><EFBFBD><EFBFBD>id
|
|||
|
bool task_last; //<2F>Ƿ<EFBFBD>Ϊ<EFBFBD><CEAA><EFBFBD><EFBFBD>
|
|||
|
};
|
|||
|
|
|||
|
|
|||
|
///<2F>̰߳<DFB3>ȫ<EFBFBD>Ķ<EFBFBD><C4B6><EFBFBD>
|
|||
|
template<typename Data>
|
|||
|
|
|||
|
class ConcurrentQueue
|
|||
|
{
|
|||
|
private:
|
|||
|
queue<Data> the_queue; //<2F><><EFBFBD><D7BC><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
mutable mutex the_mutex; //boost<73><74><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
condition_variable the_condition_variable; //boost<73><74><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
|
|||
|
public:
|
|||
|
|
|||
|
//<2F><><EFBFBD><EFBFBD><EFBFBD>µ<EFBFBD><C2B5><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
void push(Data const& data)
|
|||
|
{
|
|||
|
mutex::scoped_lock lock(the_mutex); //<2F><>ȡ<EFBFBD><C8A1><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
the_queue.push(data); //<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>д<EFBFBD><D0B4><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
lock.unlock(); //<2F>ͷ<EFBFBD><CDB7><EFBFBD>
|
|||
|
the_condition_variable.notify_one(); //֪ͨ<CDA8><D6AA><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ȴ<EFBFBD><C8B4><EFBFBD><EFBFBD>߳<EFBFBD>
|
|||
|
}
|
|||
|
|
|||
|
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ƿ<EFBFBD>Ϊ<EFBFBD><CEAA>
|
|||
|
bool empty() const
|
|||
|
{
|
|||
|
mutex::scoped_lock lock(the_mutex);
|
|||
|
return the_queue.empty();
|
|||
|
}
|
|||
|
|
|||
|
//ȡ<><C8A1>
|
|||
|
Data wait_and_pop()
|
|||
|
{
|
|||
|
mutex::scoped_lock lock(the_mutex);
|
|||
|
|
|||
|
while (the_queue.empty()) //<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϊ<EFBFBD><CEAA>ʱ
|
|||
|
{
|
|||
|
the_condition_variable.wait(lock); //<2F>ȴ<EFBFBD><C8B4><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>֪ͨ
|
|||
|
}
|
|||
|
|
|||
|
Data popped_value = the_queue.front(); //<2F><>ȡ<EFBFBD><C8A1><EFBFBD><EFBFBD><EFBFBD>е<EFBFBD><D0B5><EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD><D2BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
the_queue.pop(); //ɾ<><C9BE><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
return popped_value; //<2F><><EFBFBD>ظ<EFBFBD><D8B8><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
}
|
|||
|
|
|||
|
};
|
|||
|
|
|||
|
|
|||
|
//<2F><><EFBFBD>ֵ<EFBFBD><D6B5>л<EFBFBD>ȡij<C8A1><C4B3><EFBFBD><EFBFBD>ֵ<EFBFBD><D6B5>Ӧ<EFBFBD><D3A6><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֵ<EFBFBD><D6B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ṹ<EFBFBD><E1B9B9><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֵ<EFBFBD><D6B5>
|
|||
|
void getInt(dict d, string key, int* value);
|
|||
|
|
|||
|
|
|||
|
//<2F><><EFBFBD>ֵ<EFBFBD><D6B5>л<EFBFBD>ȡij<C8A1><C4B3><EFBFBD><EFBFBD>ֵ<EFBFBD><D6B5>Ӧ<EFBFBD>ĸ<EFBFBD><C4B8><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֵ<EFBFBD><D6B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ṹ<EFBFBD><E1B9B9><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֵ<EFBFBD><D6B5>
|
|||
|
void getDouble(dict d, string key, double* value);
|
|||
|
|
|||
|
|
|||
|
//<2F><><EFBFBD>ֵ<EFBFBD><D6B5>л<EFBFBD>ȡij<C8A1><C4B3><EFBFBD><EFBFBD>ֵ<EFBFBD><D6B5>Ӧ<EFBFBD><D3A6><EFBFBD>ַ<EFBFBD><D6B7><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֵ<EFBFBD><D6B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ṹ<EFBFBD><E1B9B9><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֵ<EFBFBD><D6B5>
|
|||
|
void getChar(dict d, string key, char* value);
|
|||
|
|
|||
|
|
|||
|
|
|||
|
///-------------------------------------------------------------------------------------
|
|||
|
///C++ SPI<50>Ļص<C4BB><D8B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʵ<EFBFBD><CAB5>
|
|||
|
///-------------------------------------------------------------------------------------
|
|||
|
|
|||
|
//API<50>ļ̳<C4BC>ʵ<EFBFBD><CAB5>
|
|||
|
class MdApi : public CSecurityFtdcMdSpi
|
|||
|
{
|
|||
|
private:
|
|||
|
CSecurityFtdcMdApi* api; //API<50><49><EFBFBD><EFBFBD>
|
|||
|
thread *task_thread; //<2F><><EFBFBD><EFBFBD><EFBFBD>߳<EFBFBD>ָ<EFBFBD>루<EFBFBD><EBA3A8>python<6F><6E><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ݣ<EFBFBD>
|
|||
|
ConcurrentQueue<Task> task_queue; //<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
|
|||
|
public:
|
|||
|
MdApi()
|
|||
|
{
|
|||
|
function0<void> f = boost::bind(&MdApi::processTask, this);
|
|||
|
thread t(f);
|
|||
|
this->task_thread = &t;
|
|||
|
};
|
|||
|
|
|||
|
~MdApi()
|
|||
|
{
|
|||
|
};
|
|||
|
|
|||
|
//-------------------------------------------------------------------------------------
|
|||
|
//API<50>ص<EFBFBD><D8B5><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
//-------------------------------------------------------------------------------------
|
|||
|
|
|||
|
//<2F><><EFBFBD>ͻ<EFBFBD><CDBB><EFBFBD><EFBFBD>뽻<EFBFBD><EFBFBD>̨<EFBFBD><CCA8><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ͨ<EFBFBD><CDA8><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʱ<EFBFBD><CAB1><EFBFBD><EFBFBD>δ<EFBFBD><CEB4>¼ǰ<C2BC><C7B0><EFBFBD><EFBFBD><EFBFBD>÷<EFBFBD><C3B7><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>á<EFBFBD>
|
|||
|
virtual void OnFrontConnected();
|
|||
|
|
|||
|
//<2F><><EFBFBD>ͻ<EFBFBD><CDBB><EFBFBD><EFBFBD>뽻<EFBFBD><EFBFBD>̨ͨ<CCA8><CDA8><EFBFBD><EFBFBD><EFBFBD>ӶϿ<D3B6>ʱ<EFBFBD><CAB1><EFBFBD>÷<EFBFBD><C3B7><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>á<EFBFBD><C3A1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>API<50><49><EFBFBD>Զ<EFBFBD><D4B6><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ӣ<EFBFBD><D3A3>ͻ<EFBFBD><CDBB>˿ɲ<CBBF><C9B2><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
//@param nReason <20><><EFBFBD><EFBFBD>ԭ<EFBFBD><D4AD>
|
|||
|
// 0x1001 <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʧ<EFBFBD><CAA7>
|
|||
|
// 0x1002 <20><><EFBFBD><EFBFBD>дʧ<D0B4><CAA7>
|
|||
|
// 0x2001 <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʱ
|
|||
|
// 0x2002 <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʧ<EFBFBD><CAA7>
|
|||
|
// 0x2003 <20>յ<EFBFBD><D5B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
virtual void OnFrontDisconnected(int nReason);
|
|||
|
|
|||
|
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʱ<EFBFBD><CAB1><EFBFBD>档<EFBFBD><E6A1A3><EFBFBD><EFBFBD>ʱ<EFBFBD><CAB1>δ<EFBFBD>յ<EFBFBD><D5B5><EFBFBD><EFBFBD><EFBFBD>ʱ<EFBFBD><CAB1><EFBFBD>÷<EFBFBD><C3B7><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>á<EFBFBD>
|
|||
|
//@param nTimeLapse <20><><EFBFBD><EFBFBD><EFBFBD>ϴν<CFB4><CEBD>ձ<EFBFBD><D5B1>ĵ<EFBFBD>ʱ<EFBFBD><CAB1>
|
|||
|
virtual void OnHeartBeatWarning(int nTimeLapse);
|
|||
|
|
|||
|
//<2F><><EFBFBD><EFBFBD>Ӧ<EFBFBD><D3A6>
|
|||
|
virtual void OnRspError(CSecurityFtdcRspInfoField *pRspInfo, int nRequestID, bool bIsLast);
|
|||
|
|
|||
|
//<2F><>¼<EFBFBD><C2BC><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ӧ
|
|||
|
virtual void OnRspUserLogin(CSecurityFtdcRspUserLoginField *pRspUserLogin, CSecurityFtdcRspInfoField *pRspInfo, int nRequestID, bool bIsLast);
|
|||
|
|
|||
|
//<2F>dz<EFBFBD><C7B3><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ӧ
|
|||
|
virtual void OnRspUserLogout(CSecurityFtdcUserLogoutField *pUserLogout, CSecurityFtdcRspInfoField *pRspInfo, int nRequestID, bool bIsLast);
|
|||
|
|
|||
|
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ӧ<EFBFBD><D3A6>
|
|||
|
virtual void OnRspSubMarketData(CSecurityFtdcSpecificInstrumentField *pSpecificInstrument, CSecurityFtdcRspInfoField *pRspInfo, int nRequestID, bool bIsLast);
|
|||
|
|
|||
|
//ȡ<><C8A1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ӧ<EFBFBD><D3A6>
|
|||
|
virtual void OnRspUnSubMarketData(CSecurityFtdcSpecificInstrumentField *pSpecificInstrument, CSecurityFtdcRspInfoField *pRspInfo, int nRequestID, bool bIsLast);
|
|||
|
|
|||
|
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>֪ͨ
|
|||
|
virtual void OnRtnDepthMarketData(CSecurityFtdcDepthMarketDataField *pDepthMarketData);
|
|||
|
|
|||
|
|
|||
|
//-------------------------------------------------------------------------------------
|
|||
|
//task<73><6B><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
//-------------------------------------------------------------------------------------
|
|||
|
|
|||
|
void processTask();
|
|||
|
|
|||
|
void processFrontConnected(Task task);
|
|||
|
|
|||
|
void processFrontDisconnected(Task task);
|
|||
|
|
|||
|
void processHeartBeatWarning(Task task);
|
|||
|
|
|||
|
void processRspError(Task task);
|
|||
|
|
|||
|
void processRspUserLogin(Task task);
|
|||
|
|
|||
|
void processRspUserLogout(Task task);
|
|||
|
|
|||
|
void processRspSubMarketData(Task task);
|
|||
|
|
|||
|
void processRspUnSubMarketData(Task task);
|
|||
|
|
|||
|
void processRtnDepthMarketData(Task task);
|
|||
|
|
|||
|
//-------------------------------------------------------------------------------------
|
|||
|
//data<74><61><EFBFBD>ص<EFBFBD><D8B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֵ<EFBFBD>
|
|||
|
//error<6F><72><EFBFBD>ص<EFBFBD><D8B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ĵ<EFBFBD><C4B4><EFBFBD><EFBFBD>ֵ<EFBFBD>
|
|||
|
//id<69><64><EFBFBD><EFBFBD><EFBFBD><EFBFBD>id
|
|||
|
//last<73><74><EFBFBD>Ƿ<EFBFBD>Ϊ<EFBFBD><CEAA><EFBFBD><EFBFBD>
|
|||
|
//i<><69><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
//-------------------------------------------------------------------------------------
|
|||
|
|
|||
|
virtual void onFrontConnected(){};
|
|||
|
|
|||
|
virtual void onFrontDisconnected(int i){};
|
|||
|
|
|||
|
virtual void onHeartBeatWarning(int i){};
|
|||
|
|
|||
|
virtual void onRspError(dict data, int id, bool last) {};
|
|||
|
|
|||
|
virtual void onRspUserLogin(dict data, dict error, int id, bool last) {};
|
|||
|
|
|||
|
virtual void onRspUserLogout(dict data, dict error, int id, bool last) {};
|
|||
|
|
|||
|
virtual void onRspSubMarketData(dict data, dict error, int id, bool last) {};
|
|||
|
|
|||
|
virtual void onRspUnSubMarketData(dict data, dict error, int id, bool last) {};
|
|||
|
|
|||
|
virtual void onRtnDepthMarketData(dict data) {};
|
|||
|
|
|||
|
//-------------------------------------------------------------------------------------
|
|||
|
//req:<3A><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֵ<EFBFBD>
|
|||
|
//-------------------------------------------------------------------------------------
|
|||
|
|
|||
|
void createFtdcMdApi(string pszFlowPath = "");
|
|||
|
|
|||
|
void release();
|
|||
|
|
|||
|
void init();
|
|||
|
|
|||
|
int join();
|
|||
|
|
|||
|
string getTradingDay();
|
|||
|
|
|||
|
void registerFront(string pszFrontAddress);
|
|||
|
|
|||
|
int subscribeMarketData(dict req);
|
|||
|
|
|||
|
int unSubscribeMarketData(dict req);
|
|||
|
|
|||
|
int reqUserLogin(dict req, int nRequestID);
|
|||
|
|
|||
|
int reqUserLogout(dict req, int nRequestID);
|
|||
|
};
|