public Result DecodeMessages( ProtocolMessage protocolMessage, IEnumerable <IMessage> messages, ChannelOptions options) { var context = options.ToDecodingContext(); return(DecodeMessages(protocolMessage, messages, context)); }
public void Send( ProtocolMessage message, Action <bool, ErrorInfo> callback = null, ChannelOptions channelOptions = null) { if (Logger.IsDebug) { Logger.Debug($"Current state: {Connection.State}. Sending message: {message}"); } if (message.ConnectionId.IsNotEmpty()) { Logger.Warning( "Setting ConnectionId to null. ConnectionId should never be included in an outbound message on a realtime connection, it’s always implicit"); message.ConnectionId = null; } Result result = VerifyMessageHasCompatibleClientId(message); if (result.IsFailure) { callback?.Invoke(false, result.Error); return; } Result encodingResult = Handler.EncodeProtocolMessage(message, channelOptions.ToDecodingContext()); if (encodingResult.IsFailure) { Logger.Error($"Failed to encode protocol message: {encodingResult.Error.Message}"); } if (State.CanSend == false && State.CanQueue == false) { throw new AblyException($"The current state [{State.State}] does not allow messages to be sent."); } if (State.CanSend == false && State.CanQueue && Options.QueueMessages == false) { throw new AblyException( $"Current state is [{State.State}] which supports queuing but Options.QueueMessages is set to False.", Connection.ConnectionState.DefaultErrorInfo.Code, HttpStatusCode.ServiceUnavailable); } ExecuteCommand(SendMessageCommand.Create(message, callback).TriggeredBy("ConnectionManager.Send()")); Result VerifyMessageHasCompatibleClientId(ProtocolMessage protocolMessage) { var messagesResult = RestClient.AblyAuth.ValidateClientIds(protocolMessage.Messages); var presenceResult = RestClient.AblyAuth.ValidateClientIds(protocolMessage.Presence); return(Result.Combine(messagesResult, presenceResult)); } }
internal static T[] FromEncodedArray <T>(T[] encodedArray, ChannelOptions options = null) where T : IMessage { var context = options.ToDecodingContext(); foreach (var encoded in encodedArray) { DecodePayload(encoded, context, logger: DefaultLogger.LoggerInstance); } return(encodedArray); }
internal static T FromEncoded <T>(T encoded, ChannelOptions options = null) where T : IMessage { var context = options.ToDecodingContext(); var result = DecodePayload(encoded, context, logger: DefaultLogger.LoggerInstance); if (result.IsFailure) { throw new AblyException(result.Error); } return(encoded); }
public void WithInvalidKey_Throws() { var options = new ChannelOptions(new CipherParams(Crypto.DefaultAlgorithm, new byte[] { 1, 2, 3 })); var encoder = new CipherEncoder(); var error = Assert.Throws <AblyException>(delegate { encoder.Encode(new Message { Data = "string" }, options.ToDecodingContext()); }); error.InnerException.Should().BeOfType <CryptographicException>(); }
public void WithInvalidAlgorithm_Throws() { var keyGen = new Rfc2898DeriveBytes("password", 8); var key = keyGen.GetBytes(Crypto.DefaultKeylength / 8); var options = new ChannelOptions(new CipherParams("mgg", key)); var encoder = new CipherEncoder(); var error = Assert.Throws <AblyException>(() => { encoder.Encode(new Message { Data = "string" }, options.ToDecodingContext()); }); error.Message.Should().Contain("Currently only the AES encryption algorithm is supported"); }
public void WithFailedEncoding_ShouldLeaveOriginalDataAndEncodingInPayload() { const string initialEncoding = "utf-8/cipher+aes-128-cbc"; const string encryptedValue = "test"; var payload = new Message { Data = encryptedValue, Encoding = initialEncoding }; var channelOptions = new ChannelOptions(true, new CipherParams( Crypto.DefaultAlgorithm, GenerateKey(Crypto.DefaultKeylength), Encryption.CipherMode.CBC)); var context = channelOptions.ToDecodingContext(); var result = MessageHandler.DecodePayload(payload, context); result.IsFailure.Should().BeTrue(); payload.Encoding.Should().Be(initialEncoding); payload.Data.Should().Be(encryptedValue); }