From 2687057ff0c7a1a84c4f32afe6a2c234375d3737 Mon Sep 17 00:00:00 2001 From: Aurimas Date: Mon, 13 Feb 2017 15:28:47 +0100 Subject: [PATCH 1/2] Some generic exceptions refactored into custom and specific exceptions --- TLSharp.Core/Network/MtProtoSender.cs | 34 ++++++++++++++++++++++++++- TLSharp.Core/Network/TcpTransport.cs | 20 ++++++++++++++-- 2 files changed, 51 insertions(+), 3 deletions(-) diff --git a/TLSharp.Core/Network/MtProtoSender.cs b/TLSharp.Core/Network/MtProtoSender.cs index 8a959ca..ee88acc 100644 --- a/TLSharp.Core/Network/MtProtoSender.cs +++ b/TLSharp.Core/Network/MtProtoSender.cs @@ -110,7 +110,7 @@ namespace TLSharp.Core.Network using (var inputReader = new BinaryReader(inputStream)) { if (inputReader.BaseStream.Length < 8) - throw new InvalidOperationException($"Can't decode packet"); + throw new DecodePacketException(); ulong remoteAuthKeyId = inputReader.ReadUInt64(); // TODO: check auth key id byte[] msgKey = inputReader.ReadBytes(16); // TODO: check msg_key correctness @@ -313,6 +313,15 @@ namespace TLSharp.Core.Network { throw new CloudPasswordNeededException("This Account has Cloud Password !"); } + else if (errorMessage == "AUTH_KEY_UNREGISTERED") + { + // + throw new AuthKeyUnregisteredException(); + } + else if (errorMessage == "RPC_MCGET_FAIL") + { + throw new RpcMcGetFailException(); + } else { throw new InvalidOperationException(errorMessage); @@ -535,6 +544,29 @@ namespace TLSharp.Core.Network } } + public class DecodePacketException : Exception + { + internal DecodePacketException() : base($"Can't decode packet.") + { + + } + } + public class AuthKeyUnregisteredException : Exception + { + internal AuthKeyUnregisteredException() : base($"Auth key is unregistered.") + { + + } + } + + public class RpcMcGetFailException : Exception + { + internal RpcMcGetFailException() : base($"Rpc Mc Get Fail.") + { + + } + } + internal abstract class DataCenterMigrationException : Exception { internal int DC { get; private set; } diff --git a/TLSharp.Core/Network/TcpTransport.cs b/TLSharp.Core/Network/TcpTransport.cs index 254153e..49fa874 100644 --- a/TLSharp.Core/Network/TcpTransport.cs +++ b/TLSharp.Core/Network/TcpTransport.cs @@ -28,7 +28,7 @@ namespace TLSharp.Core.Network public async Task Send(byte[] packet) { if (!_tcpClient.Connected) - throw new InvalidOperationException("Client not connected to server."); + throw new TcpClientNotConnectedException(); var tcpMessage = new TcpMessage(sendCounter, packet); @@ -42,7 +42,7 @@ namespace TLSharp.Core.Network var packetLengthBytes = new byte[4]; if (await stream.ReadAsync(packetLengthBytes, 0, 4) != 4) - throw new InvalidOperationException("Couldn't read the packet length"); + throw new TcpClientCouldntReadPacketLengthException(); int packetLength = BitConverter.ToInt32(packetLengthBytes, 0); var seqBytes = new byte[4]; @@ -92,4 +92,20 @@ namespace TLSharp.Core.Network _tcpClient.Close(); } } + public class TcpClientNotConnectedException : Exception + { + internal TcpClientNotConnectedException() : base($"Client not connected to server.") + { + + } + } + public class TcpClientCouldntReadPacketLengthException : Exception + { + internal TcpClientCouldntReadPacketLengthException() : base($"Couldn't read the packet length") + { + + } + } + + } From cac93b3b207bd35a842f2ee00eff3ca992859d6f Mon Sep 17 00:00:00 2001 From: Aurimas Date: Mon, 13 Feb 2017 15:28:47 +0100 Subject: [PATCH 2/2] Some generic exceptions refactored into custom and specific exceptions --- TLSharp.Core/Auth/Step3_CompleteDHExchange.cs | 9 +++- TLSharp.Core/Network/MtProtoSender.cs | 46 ++++++++++++++++++- TLSharp.Core/Network/TcpTransport.cs | 20 +++++++- 3 files changed, 70 insertions(+), 5 deletions(-) diff --git a/TLSharp.Core/Auth/Step3_CompleteDHExchange.cs b/TLSharp.Core/Auth/Step3_CompleteDHExchange.cs index 8ba8f5a..b881e40 100644 --- a/TLSharp.Core/Auth/Step3_CompleteDHExchange.cs +++ b/TLSharp.Core/Auth/Step3_CompleteDHExchange.cs @@ -175,7 +175,7 @@ namespace TLSharp.Core.Auth if (!newNonceHash1.SequenceEqual(newNonceHashCalculated)) { - throw new InvalidOperationException("invalid new nonce hash"); + throw new InvalidNewNonceHashException(); } //logger.info("generated new auth key: {0}", gab); @@ -206,4 +206,11 @@ namespace TLSharp.Core.Auth } } } + internal class InvalidNewNonceHashException : Exception + { + internal InvalidNewNonceHashException() : base($"invalid new nonce hash") + { + + } + } } diff --git a/TLSharp.Core/Network/MtProtoSender.cs b/TLSharp.Core/Network/MtProtoSender.cs index 8a959ca..8ecbd71 100644 --- a/TLSharp.Core/Network/MtProtoSender.cs +++ b/TLSharp.Core/Network/MtProtoSender.cs @@ -110,7 +110,7 @@ namespace TLSharp.Core.Network using (var inputReader = new BinaryReader(inputStream)) { if (inputReader.BaseStream.Length < 8) - throw new InvalidOperationException($"Can't decode packet"); + throw new DecodePacketException(); ulong remoteAuthKeyId = inputReader.ReadUInt64(); // TODO: check auth key id byte[] msgKey = inputReader.ReadBytes(16); // TODO: check msg_key correctness @@ -313,6 +313,15 @@ namespace TLSharp.Core.Network { throw new CloudPasswordNeededException("This Account has Cloud Password !"); } + else if (errorMessage == "AUTH_KEY_UNREGISTERED") + { + // + throw new AuthKeyUnregisteredException(); + } + else if (errorMessage == "RPC_MCGET_FAIL") + { + throw new RpcMcGetFailException(); + } else { throw new InvalidOperationException(errorMessage); @@ -378,7 +387,7 @@ namespace TLSharp.Core.Network case 20: throw new InvalidOperationException("message too old, and it cannot be verified whether the server has received a message with this msg_id or not"); case 32: - throw new InvalidOperationException("msg_seqno too low (the server has already received a message with a lower msg_id but with either a higher or an equal and odd seqno)"); + throw new MsgSeqnoTooLowException(); case 33: throw new InvalidOperationException(" msg_seqno too high (similarly, there is a message with a higher msg_id but with either a lower or an equal and odd seqno)"); case 34: @@ -535,6 +544,39 @@ namespace TLSharp.Core.Network } } + internal class DecodePacketException : Exception + { + internal DecodePacketException() : base($"Can't decode packet.") + { + + } + } + public class AuthKeyUnregisteredException : Exception + { + internal AuthKeyUnregisteredException() : base($"Auth key is unregistered.") + { + + } + } + + internal class RpcMcGetFailException : Exception + { + internal RpcMcGetFailException() : base($"Rpc Mc Get Fail.") + { + + } + } + + internal class MsgSeqnoTooLowException : Exception + { + internal MsgSeqnoTooLowException() : base($"msg_seqno too low (the server has already received a message with a lower msg_id but with either a higher or an equal and odd seqno)") + { + + } + } + + + internal abstract class DataCenterMigrationException : Exception { internal int DC { get; private set; } diff --git a/TLSharp.Core/Network/TcpTransport.cs b/TLSharp.Core/Network/TcpTransport.cs index 254153e..1cb5c23 100644 --- a/TLSharp.Core/Network/TcpTransport.cs +++ b/TLSharp.Core/Network/TcpTransport.cs @@ -28,7 +28,7 @@ namespace TLSharp.Core.Network public async Task Send(byte[] packet) { if (!_tcpClient.Connected) - throw new InvalidOperationException("Client not connected to server."); + throw new TcpClientNotConnectedException(); var tcpMessage = new TcpMessage(sendCounter, packet); @@ -42,7 +42,7 @@ namespace TLSharp.Core.Network var packetLengthBytes = new byte[4]; if (await stream.ReadAsync(packetLengthBytes, 0, 4) != 4) - throw new InvalidOperationException("Couldn't read the packet length"); + throw new TcpClientCouldntReadPacketLengthException(); int packetLength = BitConverter.ToInt32(packetLengthBytes, 0); var seqBytes = new byte[4]; @@ -92,4 +92,20 @@ namespace TLSharp.Core.Network _tcpClient.Close(); } } + internal class TcpClientNotConnectedException : Exception + { + internal TcpClientNotConnectedException() : base($"Client not connected to server.") + { + + } + } + internal class TcpClientCouldntReadPacketLengthException : Exception + { + internal TcpClientCouldntReadPacketLengthException() : base($"Couldn't read the packet length") + { + + } + } + + }