📄 request.h
字号:
#ifndef _STOCK_REQUEST_H_
#define _STOCK_REQUEST_H_
#include "config.h"
#include <queue>
#include <loki/singleton.h>
#include <loki/Factory.h>
#include "tcpSocket.h"
#include "stock.h"
namespace StockMarket
{
using namespace std;
using namespace boost;
using namespace Loki;
#pragma pack(push)
#pragma pack(1)
extern recursive_mutex req_queue_mutex;
class Request
{
public:
Request();
virtual operator bool ();
virtual void send(TcpSocket& soc);
virtual ~Request();
virtual char* buff() = 0;
virtual ulong len() = 0;
void next();
static void res_seq_id(uint id);
static bool ready();
protected:
virtual Request& operator++();
private:
bool first;
static uint seq_id;
static bool received;
};
struct ReqHead
{
public:
friend class Request;
ReqHead(){};
ReqHead( ushort cmd_id, ushort packet_len/*the total packet len*/);
void set_len(ushort payload_len);
ushort get_len();
uint get_seq_id();
private:
void set_seq_id(uint id);
protected:
char zip; // always 0x0c: data-uncompressed
uint seq_id; // 同一种命令的 seq_id。
char packet_type; // 00: 回应。 1,2,3... request count
ushort len; // 数据长度
ushort len1; // 数据长度重复
ushort cmd; // b4 bf: 分钟线。。b5 bf 单笔成交
};
class StockHeartBeat : public Request
{
public:
char* buff();
ulong len();
private:
struct StockHeartBeatStruct
{
StockHeartBeatStruct() : header(CMD_HEART_BEAT, sizeof(StockHeartBeatStruct)), count(1)
{
memcpy(&stock_code[0],"000792", MarketInfo::StocksCodeLen);
location = MarketInfo::get_market_location(&stock_code[0]);
}
ReqHead header;
ushort count;
char location;
char stock_code[MarketInfo::StocksCodeLen];
}s;
};
// 0c 22 07 0a 00 01 0c 00 0c 00 24 05 00 00 00 00 00 00 1e 00 00 00
class StockListReq : public Request
{
public:
StockListReq(MarketInfo::MarketType market_code, ushort record_offset = 0, ushort record_count = 200, ushort record_total = 0);
char* buff();
ulong len();
operator bool ();
StockListReq& operator++();
protected:
struct StockListStruct
{
StockListStruct(MarketInfo::MarketType market_code, ushort record_offset, ushort record_count)
: header(CMD_STOCK_LIST, sizeof(StockListStruct)),
offset(record_offset), count(record_count),unknown1(0),unknown2(0)
{
block = MarketInfo::get_block_from_market_type(market_code);
}
ReqHead header;
ushort block; // 00 00 上A, 01 00 上B, 02 00 深A, 03 00 深B, 0x0d: 权证
ushort unknown1;
ushort offset;
ushort count;
ushort unknown2;
}s;
ushort total;
};
class StockHoldChgReq : public Request
{
public:
StockHoldChgReq(){};
StockHoldChgReq(const string& stock_code)
{
add_stock(stock_code);
}
char* buff();
ulong len();
bool add_stock(const string& stock_code);
static const int max_stocks_a_request = 30;
protected:
struct StockHoldStruct
{
char market_locale;
char stock_code[MarketInfo::StocksCodeLen];
};
struct StockHoldChgReqStruct
{
StockHoldChgReqStruct() : header(CMD_STOCKHOLD_CHANGE, 0), count(0){}
bool add_one_stock(const string& stock_code)
{
if(count >= max_stocks_a_request)
return false;
s_buff[count].market_locale = MarketInfo::get_market_location(stock_code);
memcpy(&s_buff[count].stock_code, stock_code.c_str(), MarketInfo::StocksCodeLen);
count++;
header.set_len( sizeof(StockHoldStruct) * count + 4);
return true;
}
ReqHead header;
ushort count;
StockHoldStruct s_buff[max_stocks_a_request];
}s;
};
#if 0
struct DealReqHead
{
public:
friend class Request;
DealReqHead(){};
DealReqHead( ushort cmd_id, ushort packet_len/*the total packet len*/);
void set_len(ushort payload_len);
ushort get_len();
uint get_seq_id();
private:
void set_seq_id(uint id);
protected:
const uint flag; // always 0xfdfdfdfd
char cmd[9]; // null-terminated, 8 byte long string
};
class DealConnectReq : public Request
{
public:
private:
static const char * data_ = "\0x04\0x01\0x1F\0x42\0x3D\0x9A\0x0C\0x34\0x23\0x32\0x00";
};
#endif
#pragma pack(pop)
typedef shared_ptr<Request> CmdData_t;
typedef queue<CmdData_t > CmdQueue_t;
typedef SingletonHolder< CmdQueue_t> CmdQueue;
}
#endif
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -