async Task <GangMemberConnectionState> IGangManager.ManageAsync( GangParameters parameters, IGangMember member) { if (parameters is null) { throw new ArgumentNullException(nameof(parameters)); } if (member is null) { throw new ArgumentNullException(nameof(member)); } var audit = new GangAudit(parameters.GangId, null, member.Id, member.Session?.User.Id); var auth = _serializer.Serialize( new GangAuth(member.Id, member.Session?.Token, _settings.Application) ); if (member.Session == null) { await member.HandleAsync(GangMessageTypes.Denied, auth, audit); await member.DisconnectAsync(RESULT_DENIED); return(GangMemberConnectionState.Disconnected); } var gangId = parameters.GangId; var gang = _gangs .AddMemberToGang(parameters.GangId, member, _ => RaiseEvent(new GangAdded(), gangId)); if (gang.HostMember == member) { await member.HandleAsync(GangMessageTypes.Host, auth, audit); } else { await member.HandleAsync(GangMessageTypes.Member, auth, audit); await gang.HostMember.HandleAsync(GangMessageTypes.Connect, null, audit); } RaiseEvent(new GangMemberAdded(), gangId, member.Id); var controller = _controllerFactory.Create( this, parameters.GangId, member, async(data) => { if (member == gang.HostMember) { throw new GangException("host member should not receive data"); } var sequenceNumber = BitConverter.ToUInt32(data.AsSpan()[0..4]); var audit = new GangAudit(parameters.GangId, sequenceNumber, member.Id, member.Session?.User.Id); await gang.HostMember .HandleAsync(GangMessageTypes.Command, data[4..], audit);
public GangMemberCollection RemoveMember(IGangMember member) { if (member == HostMember) { return(OtherMembers.Any() ? new GangMemberCollection(OtherMembers[0], OtherMembers.Skip(1)) : null); } return(new GangMemberCollection(HostMember, OtherMembers.Remove(member))); }
IGangController IGangControllerFactory .Create( IGangManager manager, string gangId, IGangMember member, GangMemberReceiveAsync receiveAsync, GangMemberSendAsync sendAsync ) { return(new GangController( manager, gangId, member, receiveAsync, sendAsync, _serializer )); }
public GangMemberCollection( IGangMember hostMember = null, IEnumerable <IGangMember> otherMembers = null) { HostMember = hostMember; OtherMembers = otherMembers == null ? ImmutableArray <IGangMember> .Empty : otherMembers.ToImmutableArray(); Members = HostMember == null ? OtherMembers : OtherMembers.Add(HostMember); }
public GangMemberCollection AddMember(IGangMember member) { if (Members.Any(m => m.Id.SequenceEqual(member.Id))) { throw new Exception("Member exists"); } if (HostMember == null) { return(new GangMemberCollection(member)); } return(new GangMemberCollection(HostMember, OtherMembers.Add(member))); }
public GangController( IGangManager manager, string gangId, IGangMember member, GangMemberReceiveAsync receiveAsync, GangMemberSendAsync sendAsync, IGangSerializationService serializer ) { _gangId = gangId; Member = member; _manager = manager; _receiveAsync = receiveAsync; _sendAsync = sendAsync; _serializer = serializer; }