ut)
{
// 如果 BT 客户端没有传递 IP,则通过 Context 获得。
if (string.IsNullOrEmpty(input.Ip)) input.Ip = _httpContextAccessor.HttpContext.Connection.RemoteIpAddress.MapToIPv4().ToString();
// 本机测试用。
input.Ip = "127.0.0.1";
AnnounceInputParameters inputPara = input;
var resultDict = new BDictionary();
// 如果产生了错误,则不执行其他操作,直接返回结果。
if (inputPara.Error.Count == 0)
{
_bitTorrentManager.UpdatePeer(input.Info_Hash,inputPara);
_bitTorrentManager.ClearZombiePeers(input.Info_Hash,TimeSpan.FromMinutes(10));
var peers = _bitTorrentManager.GetPeers(input.Info_Hash);
HandlePeersData(resultDict,peers,inputPara);
// 构建剩余字段信息
// 客户端等待时间
resultDict.Add(TrackerServerConsts.IntervalKey,new BNumber((int)TimeSpan.FromSeconds(30).TotalSeconds));
// 最小等待间隔
resultDict.Add(TrackerServerConsts.MinIntervalKey,new BNumber((int)TimeSpan.FromSeconds(30).TotalSeconds));
// Tracker 服务器的 Id
resultDict.Add(TrackerServerConsts.TrackerIdKey,new BString("Tracker-DEMO"));
// 已完成的 Peer 数量
resultDict.Add(TrackerServerConsts.CompleteKey,new BNumber(_bitTorrentManager.GetComplete(input.Info_Hash)));
// 非做种状态的 Peer 数量
resultDict.Add(TrackerServerConsts.IncompleteKey,new BNumber(_bitTorrentManager.GetInComplete(input.Info_Hash)));
}
else
{
resultDict = inputPara.Error;
}
// 写入响应结果。
var resultDictBytes = resultDict.EncodeAsBytes();
var response = _httpContextAccessor.HttpContext.Response;
response.ContentType = "text/plain;";
response.StatusCode = 200;
response.ContentLength = resultDictBytes.Length;
await response.Body.WriteAsync(resultDictBytes);
}
5.5 测试效果
六、源码下载
本 DEMO 已经托管到 Github 上,有需要的朋友可以自行前往以下地址进行 clone 。
GitHub 仓库地址: https://github.com/GameBelial/BTTrackerDemo
|