DataClientBase.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547
  1. namespace Easychart.Finance.DataClient
  2. {
  3. using Easychart.Finance;
  4. using Easychart.Finance.DataProvider;
  5. using System;
  6. using System.Collections;
  7. using System.ComponentModel;
  8. using System.IO;
  9. using System.Net;
  10. using System.Reflection;
  11. using System.Runtime.CompilerServices;
  12. using System.Text;
  13. using System.Threading;
  14. public class DataClientBase
  15. {
  16. protected ArrayList alStreamingSymbols = new ArrayList();
  17. protected bool Canceled;
  18. private Hashtable htExtraData = new Hashtable();
  19. [Browsable(false)]
  20. public Exception LastError;
  21. private string proxy;
  22. private Thread StreamingThread;
  23. protected string Ticket;
  24. public bool UtcStreamingTime = true;
  25. public Easychart.Finance.DataClient.WorkingMode WorkingMode;
  26. public event DataProgress OnProgress;
  27. public event StreamingDataChanged OnStreamingData;
  28. public event EventHandler OnStreamingStopped;
  29. public DataClientBase()
  30. {
  31. this.OnProgress = (DataProgress) Delegate.Remove(this.OnProgress, null);
  32. this.OnStreamingData = (StreamingDataChanged) Delegate.Remove(this.OnStreamingData, null);
  33. this.OnStreamingStopped = (EventHandler) Delegate.Remove(this.OnStreamingStopped, null);
  34. }
  35. public void AddStreamingSymbol(string Symbol)
  36. {
  37. foreach (string str in Symbol.Split(new char[] { ',', ';' }))
  38. {
  39. string str2 = str.ToUpper();
  40. if (this.alStreamingSymbols.IndexOf(str2) < 0)
  41. {
  42. this.alStreamingSymbols.Add(str2);
  43. }
  44. }
  45. this.SteamingSymbolChanged();
  46. }
  47. public void ClearLastError()
  48. {
  49. this.LastError = null;
  50. }
  51. protected byte[] DownloadBinary(string Symbol, string URL)
  52. {
  53. byte[] buffer4;
  54. if ((URL.Length > 2) && (URL[1] == ':'))
  55. {
  56. using (FileStream stream = File.OpenRead(URL))
  57. {
  58. byte[] buffer = new byte[stream.Length];
  59. stream.Read(buffer, 0, buffer.Length);
  60. return buffer;
  61. }
  62. }
  63. HttpWebRequest request = (HttpWebRequest)WebRequest.Create(URL);
  64. try
  65. {
  66. byte[] buffer2;
  67. request.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; .NET CLR 1.0.3705; .NET CLR 1.1.4322)";
  68. request.Accept = "*/*";
  69. request.KeepAlive = false;
  70. request.Referer = "http://subscribe.easychart.net";
  71. if ((this.Proxy != null) && (this.Proxy != ""))
  72. {
  73. request.Proxy = new WebProxy(this.Proxy);
  74. }
  75. else if (FormulaHelper.DownloadProxy != null)
  76. {
  77. request.Proxy = FormulaHelper.DownloadProxy;
  78. }
  79. if (this.Logined && (this.Ticket != null))
  80. {
  81. request.CookieContainer = new CookieContainer();
  82. request.CookieContainer.Add(new Cookie("dc", this.Ticket, "/", "subscribe.easychart.net"));
  83. }
  84. HttpWebResponse response = (HttpWebResponse)request.GetResponse();
  85. BinaryReader reader = new BinaryReader(response.GetResponseStream());
  86. int currentValue = 0;
  87. this.Canceled = false;
  88. ArrayList list = new ArrayList();
  89. Label_0125:
  90. buffer2 = reader.ReadBytes(0x3e8);
  91. if ((response.ContentLength > 0L) && (this.OnProgress != null))
  92. {
  93. currentValue += buffer2.Length;
  94. this.OnProgress(this, new DataProgressArgs(Symbol, currentValue, (int) response.ContentLength));
  95. }
  96. if (!this.Canceled && (buffer2.Length > 0))
  97. {
  98. list.AddRange(buffer2);
  99. goto Label_0125;
  100. }
  101. buffer4 = (byte[]) list.ToArray(typeof(byte));
  102. }
  103. finally
  104. {
  105. request.Abort();
  106. }
  107. return buffer4;
  108. }
  109. public virtual void DownloadStreaming()
  110. {
  111. }
  112. protected string DownloadString(string URL)
  113. {
  114. byte[] bytes = this.DownloadBinary(null, URL);
  115. if (bytes != null)
  116. {
  117. return Encoding.UTF8.GetString(bytes);
  118. }
  119. return null;
  120. }
  121. public static DataClientBase[] GetAllDataFeeds()
  122. {
  123. Assembly[] assemblyArray;
  124. assemblyArray = assemblyArray = AppDomain.CurrentDomain.GetAssemblies();
  125. ArrayList list = new ArrayList();
  126. foreach (Assembly assembly in assemblyArray)
  127. {
  128. if (assembly.FullName.ToUpper().IndexOf("EASYCHART") >= 0)
  129. {
  130. try
  131. {
  132. foreach (Type type in assembly.GetTypes())
  133. {
  134. if (type.BaseType == typeof(DataClientBase))
  135. {
  136. list.Add(Activator.CreateInstance(type));
  137. }
  138. }
  139. }
  140. catch
  141. {
  142. }
  143. }
  144. }
  145. if (list.Count == 0)
  146. {
  147. list.Add(new EasyChartDataClient());
  148. }
  149. return (DataClientBase[]) list.ToArray(typeof(DataClientBase));
  150. }
  151. public CommonDataProvider GetCachedHistoricalData(string Symbol)
  152. {
  153. string path = Environment.CurrentDirectory + @"\Cache\";
  154. if (!Directory.Exists(path))
  155. {
  156. Directory.CreateDirectory(path);
  157. }
  158. string str2 = path + Symbol + ".dat";
  159. CommonDataProvider historicalData = null;
  160. if (Symbol != "")
  161. {
  162. if (File.Exists(str2) && (File.GetLastWriteTime(str2).Date == DateTime.Now.Date))
  163. {
  164. historicalData = new CommonDataProvider(null);
  165. historicalData.LoadBinary(str2);
  166. historicalData.SetStringData("Code", Symbol.ToUpper());
  167. return historicalData;
  168. }
  169. historicalData = this.GetHistoricalData(Symbol);
  170. if ((historicalData != null) && (historicalData.Count > 0))
  171. {
  172. historicalData.SaveBinary(str2);
  173. }
  174. }
  175. return historicalData;
  176. }
  177. public virtual CommonDataProvider GetData(string symbol, DataCycle dataCycle, DateTime startTime, DateTime endTime)
  178. {
  179. return this.GetDatas(new string[] { symbol }, dataCycle, startTime, endTime)[0];
  180. }
  181. public virtual CommonDataProvider[] GetDatas(string[] symbols, DataCycle dataCycle, DateTime startTime, DateTime endTime)
  182. {
  183. if (symbols.Length > 0)
  184. {
  185. return new CommonDataProvider[] { this.GetData(symbols[0], dataCycle, startTime, endTime) };
  186. }
  187. return null;
  188. }
  189. public virtual DataPacket[] GetEodData(string Exchanges, string[] symbols, DateTime time)
  190. {
  191. return null;
  192. }
  193. public virtual string[] GetExchanges()
  194. {
  195. return null;
  196. }
  197. public CommonDataProvider GetHistoricalData(string Symbol)
  198. {
  199. return this.GetHistoricalData(Symbol, DateTime.Today.AddYears(-10), DateTime.Today);
  200. }
  201. public CommonDataProvider GetHistoricalData(string Symbol, DateTime StartTime, DateTime EndTime)
  202. {
  203. return this.GetData(Symbol, DataCycle.Day, StartTime, EndTime);
  204. }
  205. public virtual string[] GetIndustry()
  206. {
  207. return null;
  208. }
  209. public CommonDataProvider GetIntradayData(string Symbol, int Interval, DateTime StartTime, DateTime EndTime)
  210. {
  211. return this.GetData(Symbol, new DataCycle(DataCycleBase.MINUTE, Interval), StartTime, EndTime);
  212. }
  213. public virtual string[] GetMarket()
  214. {
  215. return null;
  216. }
  217. public virtual string[] GetStockType()
  218. {
  219. return null;
  220. }
  221. public string GetStreamingSymbol(string separator)
  222. {
  223. return string.Join(separator, (string[]) this.alStreamingSymbols.ToArray(typeof(string)));
  224. }
  225. public bool HasStreamingSymbol()
  226. {
  227. return (this.alStreamingSymbols.Count > 0);
  228. }
  229. public static void LoadDataFeed()
  230. {
  231. LoadDataFeed(FormulaHelper.DataFeedRoot);
  232. }
  233. public static void LoadDataFeed(string FilePath)
  234. {
  235. if (Directory.Exists(FilePath))
  236. {
  237. foreach (string str in Directory.GetFiles(FilePath, "*.dll"))
  238. {
  239. if (Path.GetFileNameWithoutExtension(str).ToUpper().IndexOf("EASYCHART") >= 0)
  240. {
  241. Assembly.LoadFrom(str);
  242. }
  243. }
  244. }
  245. }
  246. public static void LoadDataFeeds()
  247. {
  248. }
  249. public virtual void LoadExtraData()
  250. {
  251. }
  252. public virtual bool Login(string Username, string Password)
  253. {
  254. return false;
  255. }
  256. public virtual string[] LookupSymbols(string Key, string Exchanges, string StockType, string Market)
  257. {
  258. return null;
  259. }
  260. public void RemoveStreamingSymbol(string Symbol)
  261. {
  262. this.alStreamingSymbols.Remove(Symbol);
  263. this.SteamingSymbolChanged();
  264. }
  265. public virtual void ResetActiveX()
  266. {
  267. }
  268. public virtual void SaveExtraData()
  269. {
  270. }
  271. public void SetStreamingSymbol(string Symbol)
  272. {
  273. this.alStreamingSymbols.Clear();
  274. this.AddStreamingSymbol(Symbol);
  275. }
  276. public void StartStreaming(string Symbols)
  277. {
  278. this.SetStreamingSymbol(Symbols);
  279. if (this.ThreadStreaming)
  280. {
  281. if (this.StreamingThread == null)
  282. {
  283. this.StreamingThread = new Thread(new ThreadStart(this.DownloadStreaming));
  284. this.StreamingThread.Start();
  285. }
  286. }
  287. else
  288. {
  289. this.DownloadStreaming();
  290. }
  291. }
  292. public virtual void SteamingSymbolChanged()
  293. {
  294. if (!this.ThreadStreaming)
  295. {
  296. this.DownloadStreaming();
  297. }
  298. }
  299. public virtual void StopDownload()
  300. {
  301. }
  302. public virtual void StopStreaming()
  303. {
  304. if (this.ThreadStreaming && (this.StreamingThread != null))
  305. {
  306. this.StreamingThread.Abort();
  307. this.StreamingThread.Join();
  308. this.StreamingThread = null;
  309. }
  310. }
  311. public override string ToString()
  312. {
  313. return this.DataFeedName;
  314. }
  315. private string TrimStreamingSymbol(string s)
  316. {
  317. int num = 0;
  318. for (int i = 0; i < s.Length; i++)
  319. {
  320. if (s[i] == ',')
  321. {
  322. num++;
  323. }
  324. if (num >= this.MaxSymbolsForStreaming)
  325. {
  326. return s.Substring(0, i);
  327. }
  328. }
  329. return s;
  330. }
  331. [Category("Properties")]
  332. public virtual string DataFeedName
  333. {
  334. get
  335. {
  336. return "";
  337. }
  338. }
  339. [Category("Properties")]
  340. public virtual string Description
  341. {
  342. get
  343. {
  344. return this.DataFeedName;
  345. }
  346. }
  347. [Browsable(false)]
  348. public Hashtable ExtraData
  349. {
  350. get
  351. {
  352. return this.htExtraData;
  353. }
  354. }
  355. [Category("Properties")]
  356. public virtual string HomePage
  357. {
  358. get
  359. {
  360. return "";
  361. }
  362. }
  363. [Category("Features")]
  364. public virtual bool IsFree
  365. {
  366. get
  367. {
  368. return false;
  369. }
  370. }
  371. [Category("Features")]
  372. public virtual bool IsLocal
  373. {
  374. get
  375. {
  376. return false;
  377. }
  378. }
  379. [Browsable(false)]
  380. public bool Logined
  381. {
  382. get
  383. {
  384. return (!this.NeedLogin || ((this.Ticket != null) && (this.Ticket != "")));
  385. }
  386. }
  387. [Category("Properties")]
  388. public virtual string LoginURL
  389. {
  390. get
  391. {
  392. return this.HomePage;
  393. }
  394. }
  395. [Category("Features")]
  396. public virtual int MaxSymbolsForData
  397. {
  398. get
  399. {
  400. return 1;
  401. }
  402. }
  403. [Category("Features")]
  404. public virtual int MaxSymbolsForStreaming
  405. {
  406. get
  407. {
  408. return 1;
  409. }
  410. }
  411. [Category("Features")]
  412. public virtual bool NeedLogin
  413. {
  414. get
  415. {
  416. return true;
  417. }
  418. }
  419. [Browsable(false)]
  420. public string Proxy
  421. {
  422. get
  423. {
  424. return this.proxy;
  425. }
  426. set
  427. {
  428. this.proxy = value;
  429. }
  430. }
  431. [Category("Properties")]
  432. public virtual string RegURL
  433. {
  434. get
  435. {
  436. return this.HomePage;
  437. }
  438. }
  439. [Category("Features")]
  440. public virtual bool SupportEod
  441. {
  442. get
  443. {
  444. return false;
  445. }
  446. }
  447. [Category("Features")]
  448. public virtual bool SupportIndustry
  449. {
  450. get
  451. {
  452. return false;
  453. }
  454. }
  455. [Category("Features")]
  456. public virtual bool SupportIntraday
  457. {
  458. get
  459. {
  460. return false;
  461. }
  462. }
  463. [Category("Features")]
  464. public virtual bool SupportStreaming
  465. {
  466. get
  467. {
  468. return false;
  469. }
  470. }
  471. [Category("Features")]
  472. public virtual bool SupportSymbolList
  473. {
  474. get
  475. {
  476. return false;
  477. }
  478. }
  479. public virtual bool ThreadStreaming
  480. {
  481. get
  482. {
  483. return true;
  484. }
  485. }
  486. }
  487. }