Esempio n. 1
0
        async Task IGangController.SendAsync(
            GangMessageTypes?type, byte[] data, IEnumerable <byte[]> memberIds)
        {
            var audit = new GangAudit(_gangId, null, Member.Id, Member.Session?.User.Id);

            await _sendAsync(type, data, audit, memberIds);
        }
Esempio n. 2
0
        protected override async Task OnMemberDisconnectAsync(
            GangAudit audit)
        {
            var state = await StateAsync;

            await SetStateAsync(state, audit);
        }
Esempio n. 3
0
        async Task IGangMember.HandleAsync(GangMessageTypes type, byte[] data, GangAudit audit)
        {
            await _sendQueue.Enqueue(async() =>
            {
                try
                {
                    await _webSocket.SendAsync(
                        new[] { (byte)type },
                        WebSocketMessageType.Binary, data == null, CancellationToken.None);

                    if (audit?.Version != null)
                    {
                        await _webSocket.SendAsync(
                            BitConverter.GetBytes(audit.Version.Value),
                            WebSocketMessageType.Binary, data == null, CancellationToken.None);
                    }

                    if (data != null)
                    {
                        await _webSocket.SendAsync(
                            data,
                            WebSocketMessageType.Binary, true, CancellationToken.None);
                    }
                }
                catch (WebSocketException)
                {
                    // ignore this
                }
            });
        }
Esempio n. 4
0
 public CommitCall(
     string gangId, GangState <TodosState> state, GangAudit audit)
 {
     GangId = gangId;
     State  = state;
     Audit  = audit;
 }
Esempio n. 5
0
        async Task <GangState <TStateData> > IGangStateStore
        .CommitAsync <TStateData>(string gangId, GangState <TStateData> state, GangAudit audit)
        {
            var version = state.Version - (uint)state.Uncommitted.Count;

            foreach (var eventData in state.Uncommitted)
            {
                var e       = GangStateEvent.From(eventData, audit);
                var wrapper = new GangStateEventWrapper(
                    eventData,
                    GangStateEventMap.GetName <TStateData>(eventData.GetType()),
                    audit.SetVersion(++version)
                    );

                _events.OnNext(e);

                var key = GetEventKey(++_sequenceNumber);

                await _eventStore.SetAsync(
                    key, wrapper,
                    overwrite : false
                    );
            }

            await _cache.SetAsync(gangId,
                                  new { state.Data, state.Version }
                                  );

            await _sequenceNumberStore.SetAsync(KEY_SEQUENCE_NUMBER, _sequenceNumber);

            return(GangState.Create(state.Data, state.Version));
        }
Esempio n. 6
0
 public GangStateCommandException(
     object commandData,
     GangAudit audit) : base(audit)
 {
     Type        = commandData?.GetType().Name ?? "Unknown";
     CommandData = commandData;
 }
Esempio n. 7
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);
Esempio n. 8
0
 public GangManagerEvent(
     TEventData data,
     GangAudit audit
     )
 {
     Data  = data;
     Audit = audit;
 }
Esempio n. 9
0
 public GangStateEventWrapper(
     object data,
     string type,
     GangAudit audit
     )
 {
     Data  = data;
     Type  = type;
     Audit = audit;
 }
Esempio n. 10
0
        async Task IGangController.SendCommandAsync(
            string type, object data, IEnumerable <byte[]> memberIds, uint?replySequence)
        {
            var bytes = _serializer.SerializeCommandData(
                type, data,
                replySequence
                );
            var audit = new GangAudit(_gangId, ++_commandSequence, Member.Id, Member.Session?.User.Id);

            await _sendAsync(GangMessageTypes.Command, bytes, audit, memberIds);
        }
Esempio n. 11
0
        async Task IGangController.SendStateAsync <T>(
            T state, IEnumerable <byte[]> memberIds)
        {
            var audit = new GangAudit(_gangId);

            await _sendAsync(
                GangMessageTypes.State,
                _serializer.Serialize(state),
                audit,
                memberIds);
        }
Esempio n. 12
0
        public static IGangCommand From(object data, GangAudit audit)
        {
            if (data is null)
            {
                throw new System.ArgumentNullException(nameof(data));
            }

            return((IGangCommand)_from
                   .MakeGenericMethod(data.GetType())
                   .Invoke(null, new[] { data, audit }));
        }
Esempio n. 13
0
        protected override async Task OnMemberConnectAsync(
            GangAudit audit)
        {
            var state = await StateAsync;

            await SetStateAsync(state, audit);

            await Controller.SendCommandAsync(
                _setSettings,
                memberIds : new[] { audit.MemberId }
                );
        }
Esempio n. 14
0
        public static IGangStateEvent From(
            object data,
            GangAudit audit
            )
        {
            var type = typeof(GangStateEvent <>)
                       .MakeGenericType(data.GetType());

            return((IGangStateEvent)Activator.CreateInstance(
                       type, data, audit
                       ));
        }
Esempio n. 15
0
        public static GangCommand <TData> From <TData>(
            TData data, GangAudit audit)
            where TData : class
        {
            if (data is null)
            {
                throw new System.ArgumentNullException(nameof(data));
            }
            if (audit is null)
            {
                throw new System.ArgumentNullException(nameof(audit));
            }

            return(new GangCommand <TData>(data, audit));
        }
Esempio n. 16
0
        public Task <GangState <TStateData> > CommitAsync <TStateData>(
            string gangId, GangState <TStateData> state, GangAudit audit)
            where TStateData : class
        {
            var typedState = state as GangState <TodosState>;

            CommitCalls = CommitCalls.Add(
                new CommitCall(gangId, typedState, audit));

            return(Task.FromResult(
                       new GangState <TStateData>(
                           typedState.Data as TStateData,
                           typedState.Version
                           )
                       ));
        }
Esempio n. 17
0
        IGangCommand IGangCommandExecutor <TStateData> .Deserialize(
            byte[] bytes, GangAudit audit
            )
        {
            var wrapper = _serialization.Deserialize <GangCommandWrapper>(bytes);

            if (!_handlers.ContainsKey(wrapper.Type))
            {
                throw new GangCommandHandlerNotFoundExcetion();
            }

            var type = _handlers[wrapper.Type].DataType;

            return(GangCommand.From(
                       _serialization.Map(wrapper.Data, type),
                       audit));
        }
Esempio n. 18
0
 public IGangCommand Deserialize(byte[] bytes, GangAudit audit)
 {
     return(GangCommand.From(bytes, audit));
 }
Esempio n. 19
0
 public GangStateExceptionBase(
     GangAudit audit)
 {
     Audit = audit;
 }
Esempio n. 20
0
 public GangStateVersionException(
     uint expectedVersion,
     GangAudit audit) : base(audit)
 {
     ExpectedVersion = expectedVersion;
 }
Esempio n. 21
0
 Task IGangMember.HandleAsync(GangMessageTypes type,
                              byte[] data, GangAudit audit)
 {
     MessagesReceived.Add(new Message(type, data, audit?.Version));
     return(Task.CompletedTask);
 }