Пример #1
0
        string IGangTokenService.Create(GangTokenData data, string salt)
        {
            var serialized  = _serializationService.Serialize(data);
            var encodedData = Base64Encode(serialized);

            return($"{encodedData}.{Hash(encodedData, salt)}");
        }
Пример #2
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);
Пример #3
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);
        }
Пример #4
0
        public static byte[] SerializeCommandData(
            this IGangSerializationService service,
            string type, object data,
            uint?replySequence = null)
        {
            if (data is null)
            {
                throw new ArgumentNullException(nameof(data));
            }

            return(service.Serialize(new GangCommandWrapper(
                                         type,
                                         data,
                                         replySequence
                                         )));
        }
Пример #5
0
        Task SaveFileAsync(
            string filePath, object content,
            bool overwrite)
        {
            return(_ioTasks.Enqueue(async() =>
            {
                using var fileStream = File.Open(
                          filePath,
                          overwrite ? FileMode.Create : FileMode.CreateNew,
                          FileAccess.Write,
                          FileShare.Read
                          );
                var bytes = _serializer.Serialize(content);

                await fileStream.WriteAsync(bytes);
            }));
        }