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); }
protected override async Task OnMemberDisconnectAsync( GangAudit audit) { var state = await StateAsync; await SetStateAsync(state, audit); }
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 } }); }
public CommitCall( string gangId, GangState <TodosState> state, GangAudit audit) { GangId = gangId; State = state; Audit = audit; }
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)); }
public GangStateCommandException( object commandData, GangAudit audit) : base(audit) { Type = commandData?.GetType().Name ?? "Unknown"; CommandData = commandData; }
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 GangManagerEvent( TEventData data, GangAudit audit ) { Data = data; Audit = audit; }
public GangStateEventWrapper( object data, string type, GangAudit audit ) { Data = data; Type = type; Audit = audit; }
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); }
async Task IGangController.SendStateAsync <T>( T state, IEnumerable <byte[]> memberIds) { var audit = new GangAudit(_gangId); await _sendAsync( GangMessageTypes.State, _serializer.Serialize(state), audit, memberIds); }
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 })); }
protected override async Task OnMemberConnectAsync( GangAudit audit) { var state = await StateAsync; await SetStateAsync(state, audit); await Controller.SendCommandAsync( _setSettings, memberIds : new[] { audit.MemberId } ); }
public static IGangStateEvent From( object data, GangAudit audit ) { var type = typeof(GangStateEvent <>) .MakeGenericType(data.GetType()); return((IGangStateEvent)Activator.CreateInstance( type, data, audit )); }
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)); }
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 ) )); }
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)); }
public IGangCommand Deserialize(byte[] bytes, GangAudit audit) { return(GangCommand.From(bytes, audit)); }
public GangStateExceptionBase( GangAudit audit) { Audit = audit; }
public GangStateVersionException( uint expectedVersion, GangAudit audit) : base(audit) { ExpectedVersion = expectedVersion; }
Task IGangMember.HandleAsync(GangMessageTypes type, byte[] data, GangAudit audit) { MessagesReceived.Add(new Message(type, data, audit?.Version)); return(Task.CompletedTask); }