Example #1
0
 public override Task HandleMessage(AlphaWebSocketConnection connection, IncomingMessage message)
 {
     //run it in the separate thread to avoid blocking quanta handling
     Task.Factory.StartNew(async() =>
     {
         try
         {
             ResultMessage result;
             try
             {
                 var request                     = message.Envelope.Message as EffectsRequest;
                 var effectsResponse             = await connection.Context.PersistenceManager.LoadEffects(request.Cursor, request.IsDesc, request.Limit, request.Account);
                 effectsResponse.OriginalMessage = message.Envelope;
                 effectsResponse.Effects         = new List <Effect>();
                 effectsResponse.Status          = ResultStatusCodes.Success;
                 result = effectsResponse;
             }
             catch (Exception exc)
             {
                 result = message.Envelope.CreateResult(exc.GetStatusCode());
             }
             await connection.SendMessage(result);
         }
         catch (Exception exc)
         {
             logger.Error(exc, "Error on sending effects.");
         }
     });
     return(Task.CompletedTask);
 }
        private async Task HandleClientHandshake(AlphaWebSocketConnection connection, MessageEnvelope envelope)
        {
            if (connection.Context.AppState.State != ApplicationState.Ready)
            {
                throw new ConnectionCloseException(WebSocketCloseStatus.ProtocolError, "Alpha is not in Ready state.");
            }
            connection.Account = connection.Context.AccountStorage.GetAccount(connection.ClientPubKey);
            if (connection.Account == null)
            {
                throw new ConnectionCloseException(WebSocketCloseStatus.NormalClosure, "Account is not registered.");
            }
            connection.ConnectionState = ConnectionState.Ready;
            var result = (HandshakeResult)envelope.CreateResult(ResultStatusCodes.Success);

            result.AccountId = connection.Account.Account.Id;
            await connection.SendMessage(result);
        }
        private async Task HandleAuditorHandshake(AlphaWebSocketConnection connection)
        {
            connection.SetAuditor();
            Message message;

            if (connection.Context.AppState.State == ApplicationState.Rising)
            {
                message = new AuditorStateRequest {
                    TargetApex = await connection.Context.PersistenceManager.GetLastApex()
                }
            }
            ;
            else
            {
                message = connection.Context.GetCurrentState();
            }
            await connection.SendMessage(message);
        }
        private async Task HandleAuditorHandshake(AlphaWebSocketConnection connection)
        {
            connection.MaxMessageSize = connection.MaxMessageSize * Global.MaxMessageBatchSize;
            Message message;

            if (Global.AppState.State == ApplicationState.Rising)
            {
                message = new AuditorStateRequest {
                    TargetApex = await Global.PersistenceManager.GetLastApex()
                }
            }
            ;
            else
            {
                message = AlphaStateHelper.GetCurrentState();
            }
            await connection.SendMessage(message);
        }