namespace Easychart.Finance.DataClient { using Easychart.Finance; using Easychart.Finance.DataProvider; using System; using System.Collections; using System.ComponentModel; using System.IO; using System.Net; using System.Reflection; using System.Runtime.CompilerServices; using System.Text; using System.Threading; public class DataClientBase { protected ArrayList alStreamingSymbols = new ArrayList(); protected bool Canceled; private Hashtable htExtraData = new Hashtable(); [Browsable(false)] public Exception LastError; private string proxy; private Thread StreamingThread; protected string Ticket; public bool UtcStreamingTime = true; public Easychart.Finance.DataClient.WorkingMode WorkingMode; public event DataProgress OnProgress; public event StreamingDataChanged OnStreamingData; public event EventHandler OnStreamingStopped; public DataClientBase() { this.OnProgress = (DataProgress) Delegate.Remove(this.OnProgress, null); this.OnStreamingData = (StreamingDataChanged) Delegate.Remove(this.OnStreamingData, null); this.OnStreamingStopped = (EventHandler) Delegate.Remove(this.OnStreamingStopped, null); } public void AddStreamingSymbol(string Symbol) { foreach (string str in Symbol.Split(new char[] { ',', ';' })) { string str2 = str.ToUpper(); if (this.alStreamingSymbols.IndexOf(str2) < 0) { this.alStreamingSymbols.Add(str2); } } this.SteamingSymbolChanged(); } public void ClearLastError() { this.LastError = null; } protected byte[] DownloadBinary(string Symbol, string URL) { byte[] buffer4; if ((URL.Length > 2) && (URL[1] == ':')) { using (FileStream stream = File.OpenRead(URL)) { byte[] buffer = new byte[stream.Length]; stream.Read(buffer, 0, buffer.Length); return buffer; } } HttpWebRequest request = (HttpWebRequest)WebRequest.Create(URL); try { byte[] buffer2; request.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; .NET CLR 1.0.3705; .NET CLR 1.1.4322)"; request.Accept = "*/*"; request.KeepAlive = false; request.Referer = "http://subscribe.easychart.net"; if ((this.Proxy != null) && (this.Proxy != "")) { request.Proxy = new WebProxy(this.Proxy); } else if (FormulaHelper.DownloadProxy != null) { request.Proxy = FormulaHelper.DownloadProxy; } if (this.Logined && (this.Ticket != null)) { request.CookieContainer = new CookieContainer(); request.CookieContainer.Add(new Cookie("dc", this.Ticket, "/", "subscribe.easychart.net")); } HttpWebResponse response = (HttpWebResponse)request.GetResponse(); BinaryReader reader = new BinaryReader(response.GetResponseStream()); int currentValue = 0; this.Canceled = false; ArrayList list = new ArrayList(); Label_0125: buffer2 = reader.ReadBytes(0x3e8); if ((response.ContentLength > 0L) && (this.OnProgress != null)) { currentValue += buffer2.Length; this.OnProgress(this, new DataProgressArgs(Symbol, currentValue, (int) response.ContentLength)); } if (!this.Canceled && (buffer2.Length > 0)) { list.AddRange(buffer2); goto Label_0125; } buffer4 = (byte[]) list.ToArray(typeof(byte)); } finally { request.Abort(); } return buffer4; } public virtual void DownloadStreaming() { } protected string DownloadString(string URL) { byte[] bytes = this.DownloadBinary(null, URL); if (bytes != null) { return Encoding.UTF8.GetString(bytes); } return null; } public static DataClientBase[] GetAllDataFeeds() { Assembly[] assemblyArray; assemblyArray = assemblyArray = AppDomain.CurrentDomain.GetAssemblies(); ArrayList list = new ArrayList(); foreach (Assembly assembly in assemblyArray) { if (assembly.FullName.ToUpper().IndexOf("EASYCHART") >= 0) { try { foreach (Type type in assembly.GetTypes()) { if (type.BaseType == typeof(DataClientBase)) { list.Add(Activator.CreateInstance(type)); } } } catch { } } } if (list.Count == 0) { list.Add(new EasyChartDataClient()); } return (DataClientBase[]) list.ToArray(typeof(DataClientBase)); } public CommonDataProvider GetCachedHistoricalData(string Symbol) { string path = Environment.CurrentDirectory + @"\Cache\"; if (!Directory.Exists(path)) { Directory.CreateDirectory(path); } string str2 = path + Symbol + ".dat"; CommonDataProvider historicalData = null; if (Symbol != "") { if (File.Exists(str2) && (File.GetLastWriteTime(str2).Date == DateTime.Now.Date)) { historicalData = new CommonDataProvider(null); historicalData.LoadBinary(str2); historicalData.SetStringData("Code", Symbol.ToUpper()); return historicalData; } historicalData = this.GetHistoricalData(Symbol); if ((historicalData != null) && (historicalData.Count > 0)) { historicalData.SaveBinary(str2); } } return historicalData; } public virtual CommonDataProvider GetData(string symbol, DataCycle dataCycle, DateTime startTime, DateTime endTime) { return this.GetDatas(new string[] { symbol }, dataCycle, startTime, endTime)[0]; } public virtual CommonDataProvider[] GetDatas(string[] symbols, DataCycle dataCycle, DateTime startTime, DateTime endTime) { if (symbols.Length > 0) { return new CommonDataProvider[] { this.GetData(symbols[0], dataCycle, startTime, endTime) }; } return null; } public virtual DataPacket[] GetEodData(string Exchanges, string[] symbols, DateTime time) { return null; } public virtual string[] GetExchanges() { return null; } public CommonDataProvider GetHistoricalData(string Symbol) { return this.GetHistoricalData(Symbol, DateTime.Today.AddYears(-10), DateTime.Today); } public CommonDataProvider GetHistoricalData(string Symbol, DateTime StartTime, DateTime EndTime) { return this.GetData(Symbol, DataCycle.Day, StartTime, EndTime); } public virtual string[] GetIndustry() { return null; } public CommonDataProvider GetIntradayData(string Symbol, int Interval, DateTime StartTime, DateTime EndTime) { return this.GetData(Symbol, new DataCycle(DataCycleBase.MINUTE, Interval), StartTime, EndTime); } public virtual string[] GetMarket() { return null; } public virtual string[] GetStockType() { return null; } public string GetStreamingSymbol(string separator) { return string.Join(separator, (string[]) this.alStreamingSymbols.ToArray(typeof(string))); } public bool HasStreamingSymbol() { return (this.alStreamingSymbols.Count > 0); } public static void LoadDataFeed() { LoadDataFeed(FormulaHelper.DataFeedRoot); } public static void LoadDataFeed(string FilePath) { if (Directory.Exists(FilePath)) { foreach (string str in Directory.GetFiles(FilePath, "*.dll")) { if (Path.GetFileNameWithoutExtension(str).ToUpper().IndexOf("EASYCHART") >= 0) { Assembly.LoadFrom(str); } } } } public static void LoadDataFeeds() { } public virtual void LoadExtraData() { } public virtual bool Login(string Username, string Password) { return false; } public virtual string[] LookupSymbols(string Key, string Exchanges, string StockType, string Market) { return null; } public void RemoveStreamingSymbol(string Symbol) { this.alStreamingSymbols.Remove(Symbol); this.SteamingSymbolChanged(); } public virtual void ResetActiveX() { } public virtual void SaveExtraData() { } public void SetStreamingSymbol(string Symbol) { this.alStreamingSymbols.Clear(); this.AddStreamingSymbol(Symbol); } public void StartStreaming(string Symbols) { this.SetStreamingSymbol(Symbols); if (this.ThreadStreaming) { if (this.StreamingThread == null) { this.StreamingThread = new Thread(new ThreadStart(this.DownloadStreaming)); this.StreamingThread.Start(); } } else { this.DownloadStreaming(); } } public virtual void SteamingSymbolChanged() { if (!this.ThreadStreaming) { this.DownloadStreaming(); } } public virtual void StopDownload() { } public virtual void StopStreaming() { if (this.ThreadStreaming && (this.StreamingThread != null)) { this.StreamingThread.Abort(); this.StreamingThread.Join(); this.StreamingThread = null; } } public override string ToString() { return this.DataFeedName; } private string TrimStreamingSymbol(string s) { int num = 0; for (int i = 0; i < s.Length; i++) { if (s[i] == ',') { num++; } if (num >= this.MaxSymbolsForStreaming) { return s.Substring(0, i); } } return s; } [Category("Properties")] public virtual string DataFeedName { get { return ""; } } [Category("Properties")] public virtual string Description { get { return this.DataFeedName; } } [Browsable(false)] public Hashtable ExtraData { get { return this.htExtraData; } } [Category("Properties")] public virtual string HomePage { get { return ""; } } [Category("Features")] public virtual bool IsFree { get { return false; } } [Category("Features")] public virtual bool IsLocal { get { return false; } } [Browsable(false)] public bool Logined { get { return (!this.NeedLogin || ((this.Ticket != null) && (this.Ticket != ""))); } } [Category("Properties")] public virtual string LoginURL { get { return this.HomePage; } } [Category("Features")] public virtual int MaxSymbolsForData { get { return 1; } } [Category("Features")] public virtual int MaxSymbolsForStreaming { get { return 1; } } [Category("Features")] public virtual bool NeedLogin { get { return true; } } [Browsable(false)] public string Proxy { get { return this.proxy; } set { this.proxy = value; } } [Category("Properties")] public virtual string RegURL { get { return this.HomePage; } } [Category("Features")] public virtual bool SupportEod { get { return false; } } [Category("Features")] public virtual bool SupportIndustry { get { return false; } } [Category("Features")] public virtual bool SupportIntraday { get { return false; } } [Category("Features")] public virtual bool SupportStreaming { get { return false; } } [Category("Features")] public virtual bool SupportSymbolList { get { return false; } } public virtual bool ThreadStreaming { get { return true; } } } }