PageRenderTime 69ms CodeModel.GetById 31ms RepoModel.GetById 0ms app.codeStats 0ms

/IB_966/Shared/EClientSocketBase.h

http://github.com/Komnomnomnom/swigibpy
C++ Header | 170 lines | 115 code | 45 blank | 10 comment | 0 complexity | 63b3a5ad6f5b289d8aa30ff2de0f6574 MD5 | raw file
Possible License(s): BSD-3-Clause
  1. #ifndef eclientsocketbase_h__INCLUDED
  2. #define eclientsocketbase_h__INCLUDED
  3. #include "EClient.h"
  4. #include <memory>
  5. #include <string>
  6. #include <vector>
  7. class EWrapper;
  8. class EClientSocketBase : public EClient
  9. {
  10. public:
  11. explicit EClientSocketBase(EWrapper *ptr);
  12. ~EClientSocketBase();
  13. virtual bool eConnect(const char *host, unsigned int port, int clientId=0) = 0;
  14. virtual void eDisconnect() = 0;
  15. int clientId() const { return m_clientId; }
  16. protected:
  17. void eConnectBase();
  18. void eDisconnectBase();
  19. public:
  20. // connection state
  21. bool isConnected() const;
  22. protected:
  23. // access to protected variables
  24. EWrapper * getWrapper() const;
  25. void setClientId( int clientId);
  26. public:
  27. bool isInBufferEmpty() const;
  28. bool isOutBufferEmpty() const;
  29. // override virtual funcs from EClient
  30. int serverVersion();
  31. IBString TwsConnectionTime();
  32. void reqMktData(TickerId id, const Contract &contract,
  33. const IBString &genericTicks, bool snapshot);
  34. void cancelMktData(TickerId id);
  35. void placeOrder(OrderId id, const Contract &contract, const Order &order);
  36. void cancelOrder(OrderId id) ;
  37. void reqOpenOrders();
  38. void reqAccountUpdates(bool subscribe, const IBString& acctCode);
  39. void reqExecutions(int reqId, const ExecutionFilter& filter);
  40. void reqIds(int numIds);
  41. bool checkMessages();
  42. void reqContractDetails(int reqId, const Contract &contract);
  43. void reqMktDepth(TickerId tickerId, const Contract &contract, int numRows);
  44. void cancelMktDepth(TickerId tickerId);
  45. void reqNewsBulletins(bool allMsgs);
  46. void cancelNewsBulletins();
  47. void setServerLogLevel(int level);
  48. void reqAutoOpenOrders(bool bAutoBind);
  49. void reqAllOpenOrders();
  50. void reqManagedAccts();
  51. void requestFA(faDataType pFaDataType);
  52. void replaceFA(faDataType pFaDataType, const IBString& cxml);
  53. void reqHistoricalData( TickerId id, const Contract &contract,
  54. const IBString &endDateTime, const IBString &durationStr,
  55. const IBString & barSizeSetting, const IBString &whatToShow,
  56. int useRTH, int formatDate);
  57. void exerciseOptions(TickerId tickerId, const Contract &contract,
  58. int exerciseAction, int exerciseQuantity,
  59. const IBString &account, int override);
  60. void cancelHistoricalData(TickerId tickerId );
  61. void reqRealTimeBars(TickerId id, const Contract &contract, int barSize,
  62. const IBString &whatToShow, bool useRTH);
  63. void cancelRealTimeBars(TickerId tickerId );
  64. void cancelScannerSubscription(int tickerId);
  65. void reqScannerParameters();
  66. void reqScannerSubscription(int tickerId, const ScannerSubscription &subscription);
  67. void reqCurrentTime();
  68. void reqFundamentalData(TickerId reqId, const Contract&, const IBString& reportType);
  69. void cancelFundamentalData(TickerId reqId);
  70. void calculateImpliedVolatility(TickerId reqId, const Contract &contract, double optionPrice, double underPrice);
  71. void calculateOptionPrice(TickerId reqId, const Contract &contract, double volatility, double underPrice);
  72. void cancelCalculateImpliedVolatility(TickerId reqId);
  73. void cancelCalculateOptionPrice(TickerId reqId);
  74. void reqGlobalCancel();
  75. void reqMarketDataType(int marketDataType);
  76. private:
  77. virtual int send(const char* buf, size_t sz) = 0;
  78. virtual int receive(char* buf, size_t sz) = 0;
  79. protected:
  80. int sendBufferedData();
  81. private:
  82. int bufferedSend(const char* buf, size_t sz);
  83. int bufferedSend(const std::string& msg);
  84. // read and buffer what's available
  85. int bufferedRead();
  86. // try to process connection request ack
  87. int processConnectAck(const char*& ptr, const char* endPtr);
  88. // try to process single msg
  89. int processMsg(const char*& ptr, const char* endPtr);
  90. static bool CheckOffset(const char* ptr, const char* endPtr);
  91. static const char* FindFieldEnd(const char* ptr, const char* endPtr);
  92. // decoders
  93. static bool DecodeField(bool&, const char*& ptr, const char* endPtr);
  94. static bool DecodeField(int&, const char*& ptr, const char* endPtr);
  95. static bool DecodeField(long&, const char*& ptr, const char* endPtr);
  96. static bool DecodeField(double&, const char*& ptr, const char* endPtr);
  97. static bool DecodeField(IBString&, const char*& ptr, const char* endPtr);
  98. static bool DecodeFieldMax(int&, const char*& ptr, const char* endPtr);
  99. static bool DecodeFieldMax(long&, const char*& ptr, const char* endPtr);
  100. static bool DecodeFieldMax(double&, const char*& ptr, const char* endPtr);
  101. // encoders
  102. template<class T> static void EncodeField(std::ostream&, T);
  103. // "max" encoders
  104. static void EncodeFieldMax(std::ostream& os, int);
  105. static void EncodeFieldMax(std::ostream& os, double);
  106. // socket state
  107. virtual bool isSocketOK() const = 0;
  108. protected:
  109. void onConnectBase();
  110. private:
  111. typedef std::vector<char> BytesVec;
  112. private:
  113. static void CleanupBuffer(BytesVec&, int processed);
  114. private:
  115. EWrapper *m_pEWrapper;
  116. BytesVec m_inBuffer;
  117. BytesVec m_outBuffer;
  118. int m_clientId;
  119. bool m_connected;
  120. int m_serverVersion;
  121. IBString m_TwsTime;
  122. };
  123. template<> void EClientSocketBase::EncodeField<bool>(std::ostream& os, bool);
  124. template<> void EClientSocketBase::EncodeField<double>(std::ostream& os, double);
  125. #endif