Browse Source

修改cache;完成trade request process回调功能

KarsusNeko 6 năm trước cách đây
mục cha
commit
34bbc02c70
2 tập tin đã thay đổi với 82 bổ sung0 xóa
  1. 74 0
      MT5MonkPAMM/PluginInstance.cpp
  2. 8 0
      MT5MonkPAMM/PluginInstance.h

+ 74 - 0
MT5MonkPAMM/PluginInstance.cpp

@@ -584,6 +584,80 @@ void CPluginInstance::OnTradeRequestProcess(const IMTRequest* request, const IMT
 	{
 		return;
 	}
+
+	UINT64 login = request->Login();
+	bool found = false;
+	for (auto follower : m_followers)
+	{
+		if (login == follower)
+		{
+			found = true;
+			break;
+		}
+	}
+
+	// 未找到直接退出
+	if (!found)	return;
+
+	// 获取request cache并删除
+	request_cache cache;
+	std::string cache_key = std::string("reqid_") + std::to_string(request->ID());
+	auto fut = m_redis_client->get(cache_key);
+	m_redis_client->sync_commit();
+	auto reply = fut.get();
+
+	m_api->LoggerOut(MTLogOK, L"request cache, key: %s", cache_key.c_str());
+
+	if (reply.ko())			return;
+	if (reply.is_null())	return;
+
+	memcpy(&cache, reply.as_string().c_str(), sizeof(request_cache));
+
+	std::vector<std::string> keys = { cache_key };
+	m_redis_client->del(keys, [this](cpp_redis::reply& r)
+	{
+		if (r.ko())
+		{
+			m_api->LoggerOut(MTLogErr, L"redis: %s", r.error().c_str());
+		}
+	});
+	
+	// 获取做单上下文
+	position_context context;
+
+	char login_buf[128] = { 0 };
+	char orig_pos_buf[128] = { 0 };
+	sprintf(orig_pos_buf, "%lld", cache.orig_position);
+	sprintf(login_buf, "%lld", cache.login);
+
+	auto ctx_fut = m_redis_client->hget(orig_pos_buf, login_buf);
+	m_redis_client->sync_commit();
+	auto ctx_reply = fut.get();
+
+	m_api->LoggerOut(MTLogOK, L"request cache, key: %s", cache_key.c_str());
+
+	if (ctx_reply.ko())			return;
+	if (ctx_reply.is_null())	return;
+
+	memcpy(&context, ctx_reply.as_string().c_str(), sizeof(position_context));
+
+	// 修改跟单上下文
+	int direction = 1;
+	if (order->Type() == IMTOrder::OP_SELL)	direction = -1;
+	context.volume += direction * order->VolumeCurrent();
+	context.position_id = position->Position();
+
+	m_api->LoggerOut(MTLogOK, L"writeback context, key: %s, field: %s", orig_pos_buf, login_buf);
+
+	// 写入
+	m_redis_client->hset(orig_pos_buf, login_buf, std::string((char*)&context, sizeof(position_context)), [this](cpp_redis::reply& r)
+	{
+		if (r.ko())
+		{
+			m_api->LoggerOut(MTLogErr, L"redis: %s", r.error().c_str());
+		}
+	});
+	m_redis_client->commit();
 }
 
 MTAPIRES CPluginInstance::LoadParam()

+ 8 - 0
MT5MonkPAMM/PluginInstance.h

@@ -24,6 +24,14 @@ struct position_context
 	UINT64	volume; // Õý±íʾbuy£¬¸º±íʾsell
 };
 
+struct request_cache
+{
+	UINT	request;
+	UINT64	orig_position;
+	//UINT64	dest_position;
+	UINT64	login;
+};
+
 class CPluginInstance
 	: public IMTServerPlugin
 	, public IMTConPluginSink