コード例 #1
0
        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);
コード例 #2
0
        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)));
        }
コード例 #3
0
ファイル: GangControllerFactory.cs プロジェクト: MrAntix/gang
 IGangController IGangControllerFactory
 .Create(
     IGangManager manager, string gangId, IGangMember member,
     GangMemberReceiveAsync receiveAsync, GangMemberSendAsync sendAsync
     )
 {
     return(new GangController(
                manager, gangId, member,
                receiveAsync, sendAsync,
                _serializer
                ));
 }
コード例 #4
0
 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);
 }
コード例 #5
0
        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)));
        }
コード例 #6
0
 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;
 }