internal static void Send(TcpState e) { var startMsg = new StartMsg(); PoolTask poolCache = GetFreeMinerTask(); var startCmd = PoolCommand.CreateCommand(CommandNames.Start, poolCache.CurrentStartMsg); }
/// <summary> /// 接收开启命令 /// 接收到的json反序列化StartMiningMsg对象,构造PoolTask类,添加到Pool任务中 /// </summary> /// <param name="json"></param> public void ReceiveStartMsg(string json) { try { var msg = JsonConvert.DeserializeObject <StartMiningMsg>(json); if (msg == null) { return; } LogHelper.Info("Receive StartMsg"); PoolTask poolTask = new PoolTask(); poolTask.CurrentBlockHeight = msg.BlockHeight; poolTask.CurrentScoopNumber = msg.ScoopNumber; poolTask.CurrentStartMsg = msg.GetStartMsg(); poolTask.GeneratingBlock = RedisManager.Current.GetDataInRedis <BlockMsg>(msg.Id.ToString()); poolTask.BaseTarget = msg.BaseTarget; poolTask.StartTime = msg.StartTime; poolTask.State = MiningState.Wait; poolTask.Id = msg.Id; PoolCache.poolTasks.Clear(); PoolCache.poolTasks.Add(poolTask); } catch (Exception ex) { LogHelper.Error(ex.ToString()); return; } }
public static void AddTask(PoolTask task) { lock (Tasks) { ThreadPool.QueueUserWorkItem(new WaitCallback(delegate(object state) { task(); })); } }
public Task <byte[]> Run(string Url, CancellationToken Cancel) { lock (_locker) { Cancel.ThrowIfCancellationRequested(); var task = new PoolTask(Url, Cancel); _tasks.Enqueue(task); if (_currentTask == null) { Next(); } return(task.CompletionSource.Task); } }
private void Next() { lock (_locker) { if (_tasks.Count > 0) { var task = _tasks.Dequeue(); if (!task.Cancel.IsCancellationRequested) { _currentTask = task; _client.DownloadDataAsync(new Uri(_currentTask.Url)); _currentTaskCancelRegistration = _currentTask.Cancel.Register(_client.CancelAsync); } else { Next(); } } } }
private void ClientOnDownloadDataCompleted(object Sender, DownloadDataCompletedEventArgs e) { lock (_locker) { _currentTaskCancelRegistration.Dispose(); if (e.Error != null) { _currentTask.CompletionSource.SetException(e.Error); } else if (e.Cancelled) { _currentTask.CompletionSource.SetCanceled(); } else { _currentTask.CompletionSource.SetResult(e.Result); } _currentTask = null; Next(); } }
private static PoolTask GetFreeMinerTask() { PoolTask poolCache = new PoolTask(); return(poolCache); }