Skip to content

Commit

Permalink
西井姐姐说要把CancellationToken放在最后面
Browse files Browse the repository at this point in the history
  • Loading branch information
dogdie233 committed Oct 15, 2024
1 parent 2b8d104 commit 5e26cf3
Show file tree
Hide file tree
Showing 9 changed files with 42 additions and 42 deletions.
4 changes: 2 additions & 2 deletions Lagrange.Core/Common/Interface/Api/OperationExt.cs
Original file line number Diff line number Diff line change
Expand Up @@ -414,10 +414,10 @@ public static Task<BotGroupClockInResult> GroupClockIn(this BotContext bot, uint
=> bot.ContextCollection.Business.OperationLogic.GroupClockIn(groupUin, cancellationToken);

public static Task<BotUserInfo?> FetchUserInfo(this BotContext bot, uint uin, bool refreshCache = false)
=> bot.ContextCollection.Business.OperationLogic.FetchUserInfo(uin, CancellationToken.None, refreshCache);
=> bot.ContextCollection.Business.OperationLogic.FetchUserInfo(uin, refreshCache, CancellationToken.None);

public static Task<BotUserInfo?> FetchUserInfo(this BotContext bot, uint uin, bool refreshCache, CancellationToken cancellationToken)
=> bot.ContextCollection.Business.OperationLogic.FetchUserInfo(uin, cancellationToken, refreshCache);
=> bot.ContextCollection.Business.OperationLogic.FetchUserInfo(uin, refreshCache, cancellationToken);

public static Task<List<string>?> FetchCustomFace(this BotContext bot)
=> bot.ContextCollection.Business.OperationLogic.FetchCustomFace(CancellationToken.None);
Expand Down
12 changes: 6 additions & 6 deletions Lagrange.Core/Internal/Context/HighwayContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public HighwayContext(ContextCollection collection, BotKeystore keystore, BotApp
_concurrent = config.HighwayConcurrent;
}

public async Task UploadResources(MessageChain chain, CancellationToken cancellationToken)
public async Task UploadResources(MessageChain chain, CancellationToken cancellationToken = default)
{
foreach (var entity in chain)
{
Expand All @@ -76,7 +76,7 @@ public async Task UploadResources(MessageChain chain, CancellationToken cancella
}
}

public async Task ManualUploadEntity(IMessageEntity entity, CancellationToken cancellationToken)
public async Task ManualUploadEntity(IMessageEntity entity, CancellationToken cancellationToken = default)
{
if (_uploaders.TryGetValue(entity.GetType(), out var uploader))
{
Expand All @@ -95,7 +95,7 @@ public async Task ManualUploadEntity(IMessageEntity entity, CancellationToken ca
}
}

public async Task<bool> UploadSrcByStreamAsync(int commonId, Stream data, byte[] ticket, byte[] md5, CancellationToken cancellation, byte[]? extendInfo = null)
public async Task<bool> UploadSrcByStreamAsync(int commonId, Stream data, byte[] ticket, byte[] md5, byte[]? extendInfo = null, CancellationToken cancellation = default)
{
if (_uri == null)
{
Expand Down Expand Up @@ -134,7 +134,7 @@ public async Task<bool> UploadSrcByStreamAsync(int commonId, Stream data, byte[]
return success;
}

private async Task<bool> SendUpBlockAsync(UpBlock upBlock, Uri server, CancellationToken cancellation)
private async Task<bool> SendUpBlockAsync(UpBlock upBlock, Uri server, CancellationToken cancellation = default)
{
var head = new DataHighwayHead
{
Expand Down Expand Up @@ -171,15 +171,15 @@ private async Task<bool> SendUpBlockAsync(UpBlock upBlock, Uri server, Cancellat
};

bool isEnd = upBlock.Offset + (ulong)upBlock.Block.Length == upBlock.FileSize;
var payload = await SendPacketAsync(highwayHead, new BinaryPacket(upBlock.Block), server, cancellation, end: isEnd);
var payload = await SendPacketAsync(highwayHead, new BinaryPacket(upBlock.Block), server, end: isEnd, cancellation: cancellation);
var (respHead, resp) = ParsePacket(payload);

Collection.Log.LogDebug(Tag, $"Highway Block Result: {respHead.ErrorCode} | {respHead.MsgSegHead?.RetCode} | {respHead.BytesRspExtendInfo?.Hex()} | {resp.ToArray().Hex()}");

return respHead.ErrorCode == 0;
}

private Task<BinaryPacket> SendPacketAsync(ReqDataHighwayHead head, BinaryPacket buffer, Uri server, CancellationToken cancellation, bool end = true)
private Task<BinaryPacket> SendPacketAsync(ReqDataHighwayHead head, BinaryPacket buffer, Uri server, bool end = true, CancellationToken cancellation = default)
{
using var stream = new MemoryStream();
Serializer.Serialize(stream, head);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,9 @@ public override Task Incoming(ProtocolEvent e, CancellationToken cancellationTok
{
return e switch
{
GroupSysDecreaseEvent groupSysDecreaseEvent when groupSysDecreaseEvent.MemberUid != Collection.Keystore.Uid => CacheUid(groupSysDecreaseEvent.GroupUin, cancellationToken, force: true),
GroupSysIncreaseEvent groupSysIncreaseEvent => CacheUid(groupSysIncreaseEvent.GroupUin, cancellationToken, force: true),
GroupSysAdminEvent groupSysAdminEvent => CacheUid(groupSysAdminEvent.GroupUin, cancellationToken, force: true),
GroupSysDecreaseEvent groupSysDecreaseEvent when groupSysDecreaseEvent.MemberUid != Collection.Keystore.Uid => CacheUid(groupSysDecreaseEvent.GroupUin, force: true, cancellationToken: cancellationToken),
GroupSysIncreaseEvent groupSysIncreaseEvent => CacheUid(groupSysIncreaseEvent.GroupUin, force: true, cancellationToken: cancellationToken),
GroupSysAdminEvent groupSysAdminEvent => CacheUid(groupSysAdminEvent.GroupUin, force: true, cancellationToken: cancellationToken),
_ => Task.CompletedTask,
};
}
Expand All @@ -70,17 +70,17 @@ public async Task<List<BotGroup>> GetCachedGroups(bool refreshCache, Cancellatio
if (_uinToUid.Count == 0) await ResolveFriendsUidAndFriendGroups(cancellationToken);
if (groupUin == null) return _uinToUid.GetValueOrDefault(friendUin);

await CacheUid(groupUin.Value, cancellationToken);
await CacheUid(groupUin.Value, false, cancellationToken);

return _uinToUid.GetValueOrDefault(friendUin);
}

public async Task<uint?> ResolveUin(uint? groupUin, string friendUid, CancellationToken cancellationToken, bool force = false)
public async Task<uint?> ResolveUin(uint? groupUin, string friendUid, bool force, CancellationToken cancellationToken)
{
if (_uinToUid.Count == 0) await ResolveFriendsUidAndFriendGroups(cancellationToken);
if (groupUin == null) return _uinToUid.FirstOrDefault(x => x.Value == friendUid).Key;

await CacheUid(groupUin.Value, cancellationToken, force: force);
await CacheUid(groupUin.Value, force: force, cancellationToken: cancellationToken);

return _uinToUid.FirstOrDefault(x => x.Value == friendUid).Key;
}
Expand Down Expand Up @@ -108,7 +108,7 @@ public async Task<List<BotFriend>> GetCachedFriends(bool refreshCache, Cancellat
return info;
}

private async Task CacheUid(uint groupUin, CancellationToken cancellationToken, bool force = false)
private async Task CacheUid(uint groupUin, bool force, CancellationToken cancellationToken)
{
if (!_cachedGroups.Contains(groupUin) || force)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,32 +94,32 @@ public override async Task Incoming(ProtocolEvent e, CancellationToken cancellat
}
case GroupSysInviteEvent invite:
{
uint invitorUin = await Collection.Business.CachingLogic.ResolveUin(null, invite.InvitorUid, cancellationToken) ?? 0;
uint invitorUin = await Collection.Business.CachingLogic.ResolveUin(null, invite.InvitorUid, false, cancellationToken) ?? 0;
var inviteArgs = new GroupInvitationEvent(invite.GroupUin, invitorUin);
Collection.Invoker.PostEvent(inviteArgs);
break;
}
case GroupSysAdminEvent admin:
{
uint adminUin = await Collection.Business.CachingLogic.ResolveUin(admin.GroupUin, admin.Uid, cancellationToken) ?? 0;
uint adminUin = await Collection.Business.CachingLogic.ResolveUin(admin.GroupUin, admin.Uid, false, cancellationToken) ?? 0;
var adminArgs = new GroupAdminChangedEvent(admin.GroupUin, adminUin, admin.IsPromoted);
Collection.Invoker.PostEvent(adminArgs);
break;
}
case GroupSysIncreaseEvent increase:
{
uint memberUin = await Collection.Business.CachingLogic.ResolveUin(increase.GroupUin, increase.MemberUid, cancellationToken, true) ?? 0;
uint memberUin = await Collection.Business.CachingLogic.ResolveUin(increase.GroupUin, increase.MemberUid, true, cancellationToken) ?? 0;
uint? invitorUin = null;
if (increase.InvitorUid != null) invitorUin = await Collection.Business.CachingLogic.ResolveUin(increase.GroupUin, increase.InvitorUid, cancellationToken);
if (increase.InvitorUid != null) invitorUin = await Collection.Business.CachingLogic.ResolveUin(increase.GroupUin, increase.InvitorUid, false, cancellationToken);
var increaseArgs = new GroupMemberIncreaseEvent(increase.GroupUin, memberUin, invitorUin, increase.Type);
Collection.Invoker.PostEvent(increaseArgs);
break;
}
case GroupSysDecreaseEvent decrease:
{
uint memberUin = await Collection.Business.CachingLogic.ResolveUin(decrease.GroupUin, decrease.MemberUid, cancellationToken) ?? 0;
uint memberUin = await Collection.Business.CachingLogic.ResolveUin(decrease.GroupUin, decrease.MemberUid, false, cancellationToken) ?? 0;
uint? operatorUin = null;
if (decrease.OperatorUid != null) operatorUin = await Collection.Business.CachingLogic.ResolveUin(decrease.GroupUin, decrease.OperatorUid, cancellationToken);
if (decrease.OperatorUid != null) operatorUin = await Collection.Business.CachingLogic.ResolveUin(decrease.GroupUin, decrease.OperatorUid, false, cancellationToken);
var decreaseArgs = new GroupMemberDecreaseEvent(decrease.GroupUin, memberUin, operatorUin, decrease.Type);
Collection.Invoker.PostEvent(decreaseArgs);
break;
Expand All @@ -138,7 +138,7 @@ public override async Task Incoming(ProtocolEvent e, CancellationToken cancellat
}
case GroupSysReactionEvent reaction:
{
uint operatorUin = await Collection.Business.CachingLogic.ResolveUin(reaction.TargetGroupUin, reaction.OperatorUid, cancellationToken) ?? 0;
uint operatorUin = await Collection.Business.CachingLogic.ResolveUin(reaction.TargetGroupUin, reaction.OperatorUid, false, cancellationToken) ?? 0;
var pokeArgs = new GroupReactionEvent(reaction.TargetGroupUin, reaction.TargetSequence, operatorUin, reaction.IsAdd, reaction.Code, reaction.Count);
Collection.Invoker.PostEvent(pokeArgs);
break;
Expand All @@ -158,25 +158,25 @@ public override async Task Incoming(ProtocolEvent e, CancellationToken cancellat
case GroupSysMuteEvent groupMute:
{
uint? operatorUin = null;
if (groupMute.OperatorUid != null) operatorUin = await Collection.Business.CachingLogic.ResolveUin(groupMute.GroupUin, groupMute.OperatorUid, cancellationToken);
if (groupMute.OperatorUid != null) operatorUin = await Collection.Business.CachingLogic.ResolveUin(groupMute.GroupUin, groupMute.OperatorUid, false, cancellationToken);
var muteArgs = new GroupMuteEvent(groupMute.GroupUin, operatorUin, groupMute.IsMuted);
Collection.Invoker.PostEvent(muteArgs);
break;
}
case GroupSysMemberMuteEvent memberMute:
{
uint memberUin = await Collection.Business.CachingLogic.ResolveUin(memberMute.GroupUin, memberMute.TargetUid, cancellationToken) ?? 0;
uint memberUin = await Collection.Business.CachingLogic.ResolveUin(memberMute.GroupUin, memberMute.TargetUid, false, cancellationToken) ?? 0;
uint? operatorUin = null;
if (memberMute.OperatorUid != null) operatorUin = await Collection.Business.CachingLogic.ResolveUin(memberMute.GroupUin, memberMute.OperatorUid, cancellationToken);
if (memberMute.OperatorUid != null) operatorUin = await Collection.Business.CachingLogic.ResolveUin(memberMute.GroupUin, memberMute.OperatorUid, false, cancellationToken);
var muteArgs = new GroupMemberMuteEvent(memberMute.GroupUin, memberUin, operatorUin, memberMute.Duration);
Collection.Invoker.PostEvent(muteArgs);
break;
}
case GroupSysRecallEvent recall:
{
uint authorUin = await Collection.Business.CachingLogic.ResolveUin(recall.GroupUin, recall.AuthorUid, cancellationToken) ?? 0;
uint authorUin = await Collection.Business.CachingLogic.ResolveUin(recall.GroupUin, recall.AuthorUid, false, cancellationToken) ?? 0;
uint operatorUin = 0;
if (recall.OperatorUid != null) operatorUin = await Collection.Business.CachingLogic.ResolveUin(recall.GroupUin, recall.OperatorUid, cancellationToken) ?? 0;
if (recall.OperatorUid != null) operatorUin = await Collection.Business.CachingLogic.ResolveUin(recall.GroupUin, recall.OperatorUid, false, cancellationToken) ?? 0;
var recallArgs = new GroupRecallEvent(recall.GroupUin, authorUin, operatorUin, recall.Sequence, recall.Time, recall.Random, recall.Tip);
Collection.Invoker.PostEvent(recallArgs);
break;
Expand All @@ -193,7 +193,7 @@ public override async Task Incoming(ProtocolEvent e, CancellationToken cancellat
}
case GroupSysRequestInvitationEvent invitation:
{
uint invitorUin = await Collection.Business.CachingLogic.ResolveUin(invitation.GroupUin, invitation.InvitorUid, cancellationToken) ?? 0;
uint invitorUin = await Collection.Business.CachingLogic.ResolveUin(invitation.GroupUin, invitation.InvitorUid, false, cancellationToken) ?? 0;

var fetchUidEvent = FetchUserInfoEvent.Create(invitation.TargetUid);
var results = await Collection.Business.SendEvent(fetchUidEvent, cancellationToken);
Expand All @@ -205,7 +205,7 @@ public override async Task Incoming(ProtocolEvent e, CancellationToken cancellat
}
case FriendSysRecallEvent recall:
{
uint fromUin = await Collection.Business.CachingLogic.ResolveUin(null, recall.FromUid, cancellationToken) ?? 0;
uint fromUin = await Collection.Business.CachingLogic.ResolveUin(null, recall.FromUid, false, cancellationToken) ?? 0;
var recallArgs = new FriendRecallEvent(fromUin, recall.ClientSequence, recall.Time, recall.Random, recall.Tip);
Collection.Invoker.PostEvent(recallArgs);
break;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -550,7 +550,7 @@ public async Task<bool> GroupSetSpecialTitle(uint groupUin, uint targetUin, stri
return events.Count != 0 && ((GroupSetSpecialTitleEvent)events[0]).ResultCode == 0;
}

public async Task<BotUserInfo?> FetchUserInfo(uint uin, CancellationToken cancellationToken, bool refreshCache = false)
public async Task<BotUserInfo?> FetchUserInfo(uint uin, bool refreshCache, CancellationToken cancellationToken)
{
return await Collection.Business.CachingLogic.GetCachedUsers(uin, refreshCache, cancellationToken);
}
Expand Down Expand Up @@ -618,7 +618,7 @@ public async Task<bool> SetAvatar(ImageEntity avatar, CancellationToken cancella

var ticket = ((HighwayUrlEvent)highwayUrlResults[0]).SigSession;
var md5 = avatar.ImageStream.Value.Md5().UnHex();
return await Collection.Highway.UploadSrcByStreamAsync(90, avatar.ImageStream.Value, ticket, md5, cancellationToken, extendInfo: Array.Empty<byte>());
return await Collection.Highway.UploadSrcByStreamAsync(90, avatar.ImageStream.Value, ticket, md5, extendInfo: Array.Empty<byte>(), cancellation: cancellationToken);
}

public async Task<bool> GroupSetAvatar(uint groupUin, ImageEntity avatar, CancellationToken cancellationToken)
Expand All @@ -639,7 +639,7 @@ public async Task<bool> GroupSetAvatar(uint groupUin, ImageEntity avatar, Cancel
Field5 = 3,
Field6 = 1
}.Serialize().ToArray();
return await Collection.Highway.UploadSrcByStreamAsync(3000, avatar.ImageStream.Value, ticket, md5, cancellationToken, extendInfo: extra);
return await Collection.Highway.UploadSrcByStreamAsync(3000, avatar.ImageStream.Value, ticket, md5, extendInfo: extra, cancellation: cancellationToken);
}

public async Task<(uint, uint)> GroupRemainAtAll(uint groupUin, CancellationToken cancellationToken)
Expand Down
4 changes: 2 additions & 2 deletions Lagrange.Core/Internal/Context/Uploader/FileUploader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public static async Task<bool> UploadPrivate(ContextCollection context, MessageC
file.FileUuid = uploadResp.FileId;

bool hwSuccess = await context.Highway.UploadSrcByStreamAsync(95, file.FileStream,
await Common.GetTicket(context, cancellationToken), file.FileMd5, cancellationToken, extendInfo: ext.Serialize().ToArray());
await Common.GetTicket(context, cancellationToken), file.FileMd5, extendInfo: ext.Serialize().ToArray(), cancellation: cancellationToken);
if (!hwSuccess) return false;
}
}
Expand Down Expand Up @@ -143,7 +143,7 @@ public static async Task<bool> UploadGroup(ContextCollection context, MessageCha
};

bool hwSuccess = await context.Highway.UploadSrcByStreamAsync(71, file.FileStream,
await Common.GetTicket(context, cancellationToken), file.FileMd5, cancellationToken, extendInfo: ext.Serialize().ToArray());
await Common.GetTicket(context, cancellationToken), file.FileMd5, extendInfo: ext.Serialize().ToArray(), cancellation: cancellationToken);
if (!hwSuccess) return false;

fileId = uploadResp.FileId;
Expand Down
4 changes: 2 additions & 2 deletions Lagrange.Core/Internal/Context/Uploader/ImageUploader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public async Task UploadPrivate(ContextCollection context, MessageChain chain, I
if (Common.GenerateExt(metaResult) is { } ext)
{
var hash = metaResult.MsgInfo.MsgInfoBody[0].Index.Info.FileHash.UnHex();
bool hwSuccess = await context.Highway.UploadSrcByStreamAsync(1003, stream.Value, await Common.GetTicket(context, cancellationToken), hash, cancellationToken, extendInfo: ext.Serialize().ToArray());
bool hwSuccess = await context.Highway.UploadSrcByStreamAsync(1003, stream.Value, await Common.GetTicket(context, cancellationToken), hash, extendInfo: ext.Serialize().ToArray(), cancellation: cancellationToken);
if (!hwSuccess)
{
await stream.Value.DisposeAsync();
Expand All @@ -44,7 +44,7 @@ public async Task UploadGroup(ContextCollection context, MessageChain chain, IMe
if (Common.GenerateExt(metaResult) is { } ext)
{
var hash = metaResult.MsgInfo.MsgInfoBody[0].Index.Info.FileHash.UnHex();
bool hwSuccess = await context.Highway.UploadSrcByStreamAsync(1004, stream.Value, await Common.GetTicket(context, cancellationToken), hash, cancellationToken, extendInfo: ext.Serialize().ToArray());
bool hwSuccess = await context.Highway.UploadSrcByStreamAsync(1004, stream.Value, await Common.GetTicket(context, cancellationToken), hash, extendInfo: ext.Serialize().ToArray(), cancellation: cancellationToken);
if (!hwSuccess)
{
await stream.Value.DisposeAsync();
Expand Down
Loading

0 comments on commit 5e26cf3

Please sign in to comment.