ManagerExtension.h 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. #pragma once
  2. #include <memory>
  3. #include <mutex>
  4. #include <thread>
  5. #include <ScopeGuard.hpp>
  6. #include <cpp_redis/cpp_redis>
  7. #include <WinSock2.h>
  8. #pragma comment(lib, "ws2_32.lib")
  9. #pragma comment(lib, "tacopie.lib")
  10. #pragma comment(lib, "cpp_redis.lib")
  11. #include <log/logger.h>
  12. #include "Misc.h"
  13. struct position_context
  14. {
  15. UINT64 level;
  16. UINT64 position_id;
  17. UINT64 request_id;
  18. UINT64 cur_ord;
  19. UINT64 volume; // Õý±íʾbuy£¬¸º±íʾsell
  20. };
  21. struct request_cache
  22. {
  23. UINT64 request;
  24. UINT64 orig_position;
  25. //UINT64 dest_position;
  26. UINT64 login;
  27. };
  28. class CManagerExtension
  29. : public IMTManagerSink
  30. , public IMTOrderSink
  31. , public IMTDealerSink
  32. , public IMTDealSink
  33. {
  34. //--- constants
  35. enum
  36. {
  37. CONNECTION_TIMEOUT = 30000,
  38. };
  39. private:
  40. CMTManagerAPIFactory m_factory; // Manager API factory
  41. IMTManagerAPI *m_manager; // Manager interface
  42. IMTManagerAPI *m_manager_pumping; // Manager interface(pumping mode)
  43. //IMTAdminAPI *m_admin; // Administrator API
  44. public:
  45. CManagerExtension();
  46. ~CManagerExtension();
  47. //--- init and shutdown
  48. bool Initialize();
  49. void Shutdown();
  50. //--- connect, disconnect
  51. bool Connect(const CMTStr &server, const UINT64 login, const CMTStr &password);
  52. void Disconnect();
  53. //virtual void OnOrderAdd(const IMTOrder* order);
  54. virtual void OnOrderDelete(const IMTOrder* order);
  55. virtual void OnConnect();
  56. virtual void OnDisconnect();
  57. virtual void OnDealPerform(const IMTDeal* deal, IMTAccount* account, IMTPosition* position);
  58. void SetTrader(const UINT64 trader);
  59. void SetGroups(const std::string& groups);
  60. void SetStep(const int step);
  61. void SetTolerance(const int tolerance);
  62. void SetRedisServer(const std::string& server);
  63. void SetRedisPort(const int port);
  64. void SetRedisPassword(const std::string& password);
  65. void SetLogger(const std::shared_ptr<tools::logger>& logger);
  66. void LogOut(const tools::logger::log_level level, const char* log, ...);
  67. private:
  68. std::shared_ptr<tools::logger> m_logger;
  69. std::mutex m_lock;
  70. std::atomic<bool> m_run;
  71. std::shared_ptr<cpp_redis::client> m_redis_client;
  72. bool m_redis_conn;
  73. bool start_redis();
  74. void stop_redis();
  75. void keep_alive();
  76. bool m_redis_error_notified;
  77. std::thread m_work_thread;
  78. std::string m_redis_server;
  79. std::string m_redis_password;
  80. int m_redis_port;
  81. std::string m_groups;
  82. //std::wstring m_logins;
  83. UINT64 m_trader;
  84. int m_step;
  85. int m_tolerance;
  86. std::vector<UINT64> m_logins;
  87. MTAPIRES LoadLogins();
  88. UINT64 m_dealer_login;
  89. };