/// <summary> /// Handler for incoming <see cref="AuthResponsePacket"/>s. /// </summary> /// <param name="connectionId">Original connection ID</param> /// <param name="packet">Incoming <see cref="AuthResponsePacket"/></param> private void authResponsePacketHandler(string connectionId, AuthResponsePacket packet) { // Was authentication successful? if (packet.Success) { // Set authenticated state and session ID Authenticated = true; SessionId = packet.SessionId; double timerInterval; if (packet.Expiry != null) { // COMPAT: Maintains backward-compatibility for 0.6.0 servers // This requires that the clock be properly synchronised or the interval may be calculated as negative timerInterval = (packet.Expiry.ToDateTime() - DateTime.UtcNow).TotalMilliseconds / 2; if (timerInterval < 0) { RaiseLogEntry(new LogEventArgs("Got a negative interval until session expiry. Check your system's clock is set correctly and try connecting again.", LogLevel.ERROR)); } } else { timerInterval = packet.ExpiresIn / 2; } // Reset the session extension timer for halfway between now and the expiry sessionExtendTimer.Stop(); sessionExtendTimer.Interval = timerInterval; sessionExtendTimer.Start(); // Raise successful auth event OnAuthenticationSuccess?.Invoke(this, new AuthenticationEventArgs()); } else { // Set authenticated state Authenticated = false; // Raise failed auth event OnAuthenticationFailure?.Invoke(this, new AuthenticationEventArgs(packet.FailureReason, packet.FailureMessage)); } }
/// <summary> /// Sends a failed <see cref="AuthResponsePacket"/> to a connection. /// </summary> /// <param name="connectionId">Recipient's connection ID</param> /// <param name="failureReason">Failure reason enum</param> /// <param name="failureMessage">Failure reason message</param> private void sendFailedAuthResponsePacket(string connectionId, AuthFailureReason failureReason, string failureMessage) { RaiseLogEntry(new LogEventArgs(string.Format("Sending failed AuthResponsePacket to connection {0}", connectionId.Highlight(HighlightType.ConnectionID)), LogLevel.DEBUG)); // Construct an AuthResponsePacket in failure configuration AuthResponsePacket response = new AuthResponsePacket { Success = false, FailureReason = failureReason, FailureMessage = failureMessage }; // Pack it into an Any for transmission Any packedResponse = ProtobufPacketHelper.Pack(response); // Send it if (!netServer.TrySend(connectionId, MODULE_NAME, packedResponse.ToByteArray())) { RaiseLogEntry(new LogEventArgs("Failed to send AuthResponsePacket to connection " + connectionId.Highlight(HighlightType.ConnectionID), LogLevel.ERROR)); } }
/// <summary> /// Sends a successful <see cref="AuthResponsePacket"/> to a connection. /// </summary> /// <param name="connectionId">Recipient's connection ID</param> /// <param name="sessionId">New session ID</param> /// <param name="expiresIn">Milliseconds until session expiry</param> private void sendSuccessfulAuthResponsePacket(string connectionId, string sessionId, int expiresIn) { RaiseLogEntry(new LogEventArgs(string.Format("Sending successful AuthResponsePacket to connection {0}", connectionId.Highlight(HighlightType.ConnectionID)), LogLevel.DEBUG)); // Construct an AuthResponsePacket in success configuration AuthResponsePacket response = new AuthResponsePacket { Success = true, SessionId = sessionId, ExpiresIn = expiresIn }; // Pack it into an Any for transmission Any packedResponse = ProtobufPacketHelper.Pack(response); // Send it if (!netServer.TrySend(connectionId, MODULE_NAME, packedResponse.ToByteArray())) { RaiseLogEntry(new LogEventArgs("Failed to send AuthResponsePacket to connection " + connectionId.Highlight(HighlightType.ConnectionID), LogLevel.ERROR)); } }