/// <summary> /// Handles the error. /// </summary> private T HandleError <T>(ClientStatusCode status, string msg) { switch (status) { case ClientStatusCode.CacheDoesNotExist: throw new IgniteClientException("Cache doesn't exist: " + Name, null, status); default: throw new IgniteClientException(msg, null, status); } }
/// <summary> /// Performs client protocol handshake. /// </summary> private void Handshake(IgniteClientConfiguration clientConfiguration, ClientProtocolVersion version) { bool auth = version.CompareTo(Ver110) >= 0 && clientConfiguration.UserName != null; // Send request. int messageLen; var buf = WriteMessage(stream => { // Handshake. stream.WriteByte(OpHandshake); // Protocol version. stream.WriteShort(version.Major); stream.WriteShort(version.Minor); stream.WriteShort(version.Maintenance); // Client type: platform. stream.WriteByte(ClientType); // Authentication data. if (auth) { var writer = BinaryUtils.Marshaller.StartMarshal(stream); writer.WriteString(clientConfiguration.UserName); writer.WriteString(clientConfiguration.Password); BinaryUtils.Marshaller.FinishMarshal(writer); } }, 12, out messageLen); SocketWrite(buf, messageLen); // Decode response. var res = ReceiveMessage(); using (var stream = new BinaryHeapStream(res)) { // Read input. var success = stream.ReadBool(); if (success) { if (version.CompareTo(Ver140) >= 0) { ServerNodeId = BinaryUtils.Marshaller.Unmarshal <Guid>(stream); } ServerVersion = version; return; } ServerVersion = new ClientProtocolVersion(stream.ReadShort(), stream.ReadShort(), stream.ReadShort()); var errMsg = BinaryUtils.Marshaller.Unmarshal <string>(stream); ClientStatusCode errCode = ClientStatusCode.Fail; if (stream.Remaining > 0) { errCode = (ClientStatusCode)stream.ReadInt(); } // Authentication error is handled immediately. if (errCode == ClientStatusCode.AuthenticationFailed) { throw new IgniteClientException(errMsg, null, ClientStatusCode.AuthenticationFailed); } // Re-try if possible. bool retry = ServerVersion.CompareTo(version) < 0 && ServerVersion.Equals(Ver100); if (retry) { Handshake(clientConfiguration, ServerVersion); } else { throw new IgniteClientException(string.Format( "Client handshake failed: '{0}'. Client version: {1}. Server version: {2}", errMsg, version, ServerVersion), null, errCode); } } }
/// <summary> /// Performs client protocol handshake. /// </summary> private ClientFeatures Handshake(IgniteClientConfiguration clientConfiguration, ClientProtocolVersion version) { var hasAuth = version >= Ver110 && clientConfiguration.UserName != null; var hasFeatures = version >= Ver170; // Send request. int messageLen; var buf = WriteMessage(stream => { // Handshake. stream.WriteByte(OpHandshake); // Protocol version. stream.WriteShort(version.Major); stream.WriteShort(version.Minor); stream.WriteShort(version.Maintenance); // Client type: platform. stream.WriteByte(ClientType); // Writing features. if (hasFeatures) { BinaryUtils.Marshaller.Marshal(stream, w => w.WriteByteArray(ClientFeatures.AllFeatures)); } // Authentication data. if (hasAuth) { BinaryUtils.Marshaller.Marshal(stream, writer => { writer.WriteString(clientConfiguration.UserName); writer.WriteString(clientConfiguration.Password); }); } }, 12, out messageLen); SocketWrite(buf, messageLen); // Decode response. var res = ReceiveMessage(); using (var stream = new BinaryHeapStream(res)) { // Read input. var success = stream.ReadBool(); if (success) { BitArray featureBits = null; if (hasFeatures) { featureBits = new BitArray(BinaryUtils.Marshaller.Unmarshal <byte[]>(stream)); } if (version >= Ver140) { ServerNodeId = BinaryUtils.Marshaller.Unmarshal <Guid>(stream); } ServerVersion = version; _logger.Debug("Handshake completed on {0}, protocol version = {1}", _socket.RemoteEndPoint, version); return(new ClientFeatures(version, featureBits)); } ServerVersion = new ClientProtocolVersion(stream.ReadShort(), stream.ReadShort(), stream.ReadShort()); var errMsg = BinaryUtils.Marshaller.Unmarshal <string>(stream); ClientStatusCode errCode = ClientStatusCode.Fail; if (stream.Remaining > 0) { errCode = (ClientStatusCode)stream.ReadInt(); } _logger.Debug("Handshake failed on {0}, requested protocol version = {1}, " + "server protocol version = {2}, status = {3}, message = {4}", _socket.RemoteEndPoint, version, ServerVersion, errCode, errMsg); // Authentication error is handled immediately. if (errCode == ClientStatusCode.AuthenticationFailed) { throw new IgniteClientException(errMsg, null, ClientStatusCode.AuthenticationFailed); } // Retry if server version is different and falls within supported version range. var retry = ServerVersion != version && ServerVersion >= Ver100 && ServerVersion <= CurrentProtocolVersion; if (retry) { _logger.Debug("Retrying handshake on {0} with protocol version {1}", _socket.RemoteEndPoint, ServerVersion); return(Handshake(clientConfiguration, ServerVersion)); } throw new IgniteClientException(string.Format( "Client handshake failed: '{0}'. Client version: {1}. Server version: {2}", errMsg, version, ServerVersion), null, errCode); } }
/// <summary> /// Handles the error. /// </summary> private static T HandleError <T>(ClientStatusCode status, string msg) { throw new IgniteClientException(msg, null, status); }
/// <summary> /// Initializes a new instance of the <see cref="IgniteClientException"/> class. /// </summary> /// <param name="info">Serialization information.</param> /// <param name="ctx">Streaming context.</param> protected IgniteClientException(SerializationInfo info, StreamingContext ctx) : base(info, ctx) { _statusCode = (ClientStatusCode)info.GetInt32(ErrorCodeField); }
/// <summary> /// Initializes a new instance of the <see cref="IgniteClientException" /> class. /// </summary> /// <param name="message">The message.</param> /// <param name="cause">The cause.</param> /// <param name="statusCode">The error code.</param> public IgniteClientException(string message, Exception cause, ClientStatusCode statusCode) : base(message, cause) { _statusCode = statusCode; }