/// <summary> /// NewClientConsumer Constructor /// </summary> /// <param name="serverUrl">数据接收地址</param> /// <param name="fileAbsolutePath">日志文件的绝对路径</param> /// <param name="bulkSize">缓存事件数目,当缓存大于等于此数,将触发数据上报功能</param> /// <param name="requestTimeoutMillisecond">数据上报的网络请求超时时间,单位是 ms</param> public NewClientConsumer(String serverUrl, String fileAbsolutePath, int bulkSize, int requestTimeoutMillisecond, ICallback callback = null) { this.bulkSize = Math.Min(MAX_FLUSH_BULK_SIZE, bulkSize);; this.serverUrl = serverUrl; this.fileAbsolutePath = fileAbsolutePath; this.callback = callback; if (requestTimeoutMillisecond <= 0) { requestTimeoutMillisecond = DEFAULT_TIME_OUT_SECOND; } this.requestTimeoutMillisecond = requestTimeoutMillisecond; try { if (File.Exists(fileAbsolutePath)) { this.fileStream = new FileStream(fileAbsolutePath, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.ReadWrite); string mutexName = "Global\\SensorsAnalytics " + Path.GetFullPath(fileAbsolutePath).Replace('\\', '_').Replace('/', '_'); this.mutex = new Mutex(false, mutexName); LoadDataFromFile(); } else { if (callback != null) { callback.OnFailed(new FailedData(FailedData.TYPE_LOAD_FROME_FILE_ERROR, "Can not found log file: " + fileAbsolutePath)); } } } catch (Exception e) { if (callback != null) { callback.OnFailed(new FailedData(FailedData.TYPE_LOAD_FROME_FILE_ERROR, "Something error when load data from log file: " + e.ToString())); } logE(e.Message + "\n" + e.StackTrace); } }
public void Close() { try { if (cancellationTokenSource != null) { cancellationTokenSource.Cancel(); } if (globalTask != null && (!globalTask.IsCompleted && !globalTask.IsCanceled && !globalTask.IsFaulted)) { Task.WaitAll(globalTask); } SaveCacheToFile(); blockingList.Dispose(); if (fileStream != null) { fileStream.Close(); fileStream = null; } if (mutex != null) { mutex.Close(); mutex = null; } } catch (Exception e) { if (callback != null) { callback.OnFailed(new FailedData(FailedData.TYPE_OTHER_ERROR, "Something error when shutdown: " + e.ToString())); } logE("Something error when shutdown: " + e.ToString()); } }