Reduce code duplication in auto-generated API protocol code (#9097)

This commit is contained in:
J. Nick Koston 2025-06-19 02:10:01 +02:00 committed by GitHub
parent b7b1d17ecb
commit a08d021f77
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 151 additions and 360 deletions

View File

@ -620,544 +620,300 @@ void APIServerConnection::on_ping_request(const PingRequest &msg) {
} }
} }
void APIServerConnection::on_device_info_request(const DeviceInfoRequest &msg) { void APIServerConnection::on_device_info_request(const DeviceInfoRequest &msg) {
if (!this->is_connection_setup()) { if (this->check_connection_setup_()) {
this->on_no_setup_connection(); DeviceInfoResponse ret = this->device_info(msg);
return; if (!this->send_message(ret)) {
} this->on_fatal_error();
DeviceInfoResponse ret = this->device_info(msg); }
if (!this->send_message(ret)) {
this->on_fatal_error();
} }
} }
void APIServerConnection::on_list_entities_request(const ListEntitiesRequest &msg) { void APIServerConnection::on_list_entities_request(const ListEntitiesRequest &msg) {
if (!this->is_connection_setup()) { if (this->check_authenticated_()) {
this->on_no_setup_connection(); this->list_entities(msg);
return;
} }
if (!this->is_authenticated()) {
this->on_unauthenticated_access();
return;
}
this->list_entities(msg);
} }
void APIServerConnection::on_subscribe_states_request(const SubscribeStatesRequest &msg) { void APIServerConnection::on_subscribe_states_request(const SubscribeStatesRequest &msg) {
if (!this->is_connection_setup()) { if (this->check_authenticated_()) {
this->on_no_setup_connection(); this->subscribe_states(msg);
return;
} }
if (!this->is_authenticated()) {
this->on_unauthenticated_access();
return;
}
this->subscribe_states(msg);
} }
void APIServerConnection::on_subscribe_logs_request(const SubscribeLogsRequest &msg) { void APIServerConnection::on_subscribe_logs_request(const SubscribeLogsRequest &msg) {
if (!this->is_connection_setup()) { if (this->check_authenticated_()) {
this->on_no_setup_connection(); this->subscribe_logs(msg);
return;
} }
if (!this->is_authenticated()) {
this->on_unauthenticated_access();
return;
}
this->subscribe_logs(msg);
} }
void APIServerConnection::on_subscribe_homeassistant_services_request( void APIServerConnection::on_subscribe_homeassistant_services_request(
const SubscribeHomeassistantServicesRequest &msg) { const SubscribeHomeassistantServicesRequest &msg) {
if (!this->is_connection_setup()) { if (this->check_authenticated_()) {
this->on_no_setup_connection(); this->subscribe_homeassistant_services(msg);
return;
} }
if (!this->is_authenticated()) {
this->on_unauthenticated_access();
return;
}
this->subscribe_homeassistant_services(msg);
} }
void APIServerConnection::on_subscribe_home_assistant_states_request(const SubscribeHomeAssistantStatesRequest &msg) { void APIServerConnection::on_subscribe_home_assistant_states_request(const SubscribeHomeAssistantStatesRequest &msg) {
if (!this->is_connection_setup()) { if (this->check_authenticated_()) {
this->on_no_setup_connection(); this->subscribe_home_assistant_states(msg);
return;
} }
if (!this->is_authenticated()) {
this->on_unauthenticated_access();
return;
}
this->subscribe_home_assistant_states(msg);
} }
void APIServerConnection::on_get_time_request(const GetTimeRequest &msg) { void APIServerConnection::on_get_time_request(const GetTimeRequest &msg) {
if (!this->is_connection_setup()) { if (this->check_connection_setup_()) {
this->on_no_setup_connection(); GetTimeResponse ret = this->get_time(msg);
return; if (!this->send_message(ret)) {
} this->on_fatal_error();
GetTimeResponse ret = this->get_time(msg); }
if (!this->send_message(ret)) {
this->on_fatal_error();
} }
} }
void APIServerConnection::on_execute_service_request(const ExecuteServiceRequest &msg) { void APIServerConnection::on_execute_service_request(const ExecuteServiceRequest &msg) {
if (!this->is_connection_setup()) { if (this->check_authenticated_()) {
this->on_no_setup_connection(); this->execute_service(msg);
return;
} }
if (!this->is_authenticated()) {
this->on_unauthenticated_access();
return;
}
this->execute_service(msg);
} }
#ifdef USE_API_NOISE #ifdef USE_API_NOISE
void APIServerConnection::on_noise_encryption_set_key_request(const NoiseEncryptionSetKeyRequest &msg) { void APIServerConnection::on_noise_encryption_set_key_request(const NoiseEncryptionSetKeyRequest &msg) {
if (!this->is_connection_setup()) { if (this->check_authenticated_()) {
this->on_no_setup_connection(); NoiseEncryptionSetKeyResponse ret = this->noise_encryption_set_key(msg);
return; if (!this->send_message(ret)) {
} this->on_fatal_error();
if (!this->is_authenticated()) { }
this->on_unauthenticated_access();
return;
}
NoiseEncryptionSetKeyResponse ret = this->noise_encryption_set_key(msg);
if (!this->send_message(ret)) {
this->on_fatal_error();
} }
} }
#endif #endif
#ifdef USE_BUTTON #ifdef USE_BUTTON
void APIServerConnection::on_button_command_request(const ButtonCommandRequest &msg) { void APIServerConnection::on_button_command_request(const ButtonCommandRequest &msg) {
if (!this->is_connection_setup()) { if (this->check_authenticated_()) {
this->on_no_setup_connection(); this->button_command(msg);
return;
} }
if (!this->is_authenticated()) {
this->on_unauthenticated_access();
return;
}
this->button_command(msg);
} }
#endif #endif
#ifdef USE_ESP32_CAMERA #ifdef USE_ESP32_CAMERA
void APIServerConnection::on_camera_image_request(const CameraImageRequest &msg) { void APIServerConnection::on_camera_image_request(const CameraImageRequest &msg) {
if (!this->is_connection_setup()) { if (this->check_authenticated_()) {
this->on_no_setup_connection(); this->camera_image(msg);
return;
} }
if (!this->is_authenticated()) {
this->on_unauthenticated_access();
return;
}
this->camera_image(msg);
} }
#endif #endif
#ifdef USE_CLIMATE #ifdef USE_CLIMATE
void APIServerConnection::on_climate_command_request(const ClimateCommandRequest &msg) { void APIServerConnection::on_climate_command_request(const ClimateCommandRequest &msg) {
if (!this->is_connection_setup()) { if (this->check_authenticated_()) {
this->on_no_setup_connection(); this->climate_command(msg);
return;
} }
if (!this->is_authenticated()) {
this->on_unauthenticated_access();
return;
}
this->climate_command(msg);
} }
#endif #endif
#ifdef USE_COVER #ifdef USE_COVER
void APIServerConnection::on_cover_command_request(const CoverCommandRequest &msg) { void APIServerConnection::on_cover_command_request(const CoverCommandRequest &msg) {
if (!this->is_connection_setup()) { if (this->check_authenticated_()) {
this->on_no_setup_connection(); this->cover_command(msg);
return;
} }
if (!this->is_authenticated()) {
this->on_unauthenticated_access();
return;
}
this->cover_command(msg);
} }
#endif #endif
#ifdef USE_DATETIME_DATE #ifdef USE_DATETIME_DATE
void APIServerConnection::on_date_command_request(const DateCommandRequest &msg) { void APIServerConnection::on_date_command_request(const DateCommandRequest &msg) {
if (!this->is_connection_setup()) { if (this->check_authenticated_()) {
this->on_no_setup_connection(); this->date_command(msg);
return;
} }
if (!this->is_authenticated()) {
this->on_unauthenticated_access();
return;
}
this->date_command(msg);
} }
#endif #endif
#ifdef USE_DATETIME_DATETIME #ifdef USE_DATETIME_DATETIME
void APIServerConnection::on_date_time_command_request(const DateTimeCommandRequest &msg) { void APIServerConnection::on_date_time_command_request(const DateTimeCommandRequest &msg) {
if (!this->is_connection_setup()) { if (this->check_authenticated_()) {
this->on_no_setup_connection(); this->datetime_command(msg);
return;
} }
if (!this->is_authenticated()) {
this->on_unauthenticated_access();
return;
}
this->datetime_command(msg);
} }
#endif #endif
#ifdef USE_FAN #ifdef USE_FAN
void APIServerConnection::on_fan_command_request(const FanCommandRequest &msg) { void APIServerConnection::on_fan_command_request(const FanCommandRequest &msg) {
if (!this->is_connection_setup()) { if (this->check_authenticated_()) {
this->on_no_setup_connection(); this->fan_command(msg);
return;
} }
if (!this->is_authenticated()) {
this->on_unauthenticated_access();
return;
}
this->fan_command(msg);
} }
#endif #endif
#ifdef USE_LIGHT #ifdef USE_LIGHT
void APIServerConnection::on_light_command_request(const LightCommandRequest &msg) { void APIServerConnection::on_light_command_request(const LightCommandRequest &msg) {
if (!this->is_connection_setup()) { if (this->check_authenticated_()) {
this->on_no_setup_connection(); this->light_command(msg);
return;
} }
if (!this->is_authenticated()) {
this->on_unauthenticated_access();
return;
}
this->light_command(msg);
} }
#endif #endif
#ifdef USE_LOCK #ifdef USE_LOCK
void APIServerConnection::on_lock_command_request(const LockCommandRequest &msg) { void APIServerConnection::on_lock_command_request(const LockCommandRequest &msg) {
if (!this->is_connection_setup()) { if (this->check_authenticated_()) {
this->on_no_setup_connection(); this->lock_command(msg);
return;
} }
if (!this->is_authenticated()) {
this->on_unauthenticated_access();
return;
}
this->lock_command(msg);
} }
#endif #endif
#ifdef USE_MEDIA_PLAYER #ifdef USE_MEDIA_PLAYER
void APIServerConnection::on_media_player_command_request(const MediaPlayerCommandRequest &msg) { void APIServerConnection::on_media_player_command_request(const MediaPlayerCommandRequest &msg) {
if (!this->is_connection_setup()) { if (this->check_authenticated_()) {
this->on_no_setup_connection(); this->media_player_command(msg);
return;
} }
if (!this->is_authenticated()) {
this->on_unauthenticated_access();
return;
}
this->media_player_command(msg);
} }
#endif #endif
#ifdef USE_NUMBER #ifdef USE_NUMBER
void APIServerConnection::on_number_command_request(const NumberCommandRequest &msg) { void APIServerConnection::on_number_command_request(const NumberCommandRequest &msg) {
if (!this->is_connection_setup()) { if (this->check_authenticated_()) {
this->on_no_setup_connection(); this->number_command(msg);
return;
} }
if (!this->is_authenticated()) {
this->on_unauthenticated_access();
return;
}
this->number_command(msg);
} }
#endif #endif
#ifdef USE_SELECT #ifdef USE_SELECT
void APIServerConnection::on_select_command_request(const SelectCommandRequest &msg) { void APIServerConnection::on_select_command_request(const SelectCommandRequest &msg) {
if (!this->is_connection_setup()) { if (this->check_authenticated_()) {
this->on_no_setup_connection(); this->select_command(msg);
return;
} }
if (!this->is_authenticated()) {
this->on_unauthenticated_access();
return;
}
this->select_command(msg);
} }
#endif #endif
#ifdef USE_SIREN #ifdef USE_SIREN
void APIServerConnection::on_siren_command_request(const SirenCommandRequest &msg) { void APIServerConnection::on_siren_command_request(const SirenCommandRequest &msg) {
if (!this->is_connection_setup()) { if (this->check_authenticated_()) {
this->on_no_setup_connection(); this->siren_command(msg);
return;
} }
if (!this->is_authenticated()) {
this->on_unauthenticated_access();
return;
}
this->siren_command(msg);
} }
#endif #endif
#ifdef USE_SWITCH #ifdef USE_SWITCH
void APIServerConnection::on_switch_command_request(const SwitchCommandRequest &msg) { void APIServerConnection::on_switch_command_request(const SwitchCommandRequest &msg) {
if (!this->is_connection_setup()) { if (this->check_authenticated_()) {
this->on_no_setup_connection(); this->switch_command(msg);
return;
} }
if (!this->is_authenticated()) {
this->on_unauthenticated_access();
return;
}
this->switch_command(msg);
} }
#endif #endif
#ifdef USE_TEXT #ifdef USE_TEXT
void APIServerConnection::on_text_command_request(const TextCommandRequest &msg) { void APIServerConnection::on_text_command_request(const TextCommandRequest &msg) {
if (!this->is_connection_setup()) { if (this->check_authenticated_()) {
this->on_no_setup_connection(); this->text_command(msg);
return;
} }
if (!this->is_authenticated()) {
this->on_unauthenticated_access();
return;
}
this->text_command(msg);
} }
#endif #endif
#ifdef USE_DATETIME_TIME #ifdef USE_DATETIME_TIME
void APIServerConnection::on_time_command_request(const TimeCommandRequest &msg) { void APIServerConnection::on_time_command_request(const TimeCommandRequest &msg) {
if (!this->is_connection_setup()) { if (this->check_authenticated_()) {
this->on_no_setup_connection(); this->time_command(msg);
return;
} }
if (!this->is_authenticated()) {
this->on_unauthenticated_access();
return;
}
this->time_command(msg);
} }
#endif #endif
#ifdef USE_UPDATE #ifdef USE_UPDATE
void APIServerConnection::on_update_command_request(const UpdateCommandRequest &msg) { void APIServerConnection::on_update_command_request(const UpdateCommandRequest &msg) {
if (!this->is_connection_setup()) { if (this->check_authenticated_()) {
this->on_no_setup_connection(); this->update_command(msg);
return;
} }
if (!this->is_authenticated()) {
this->on_unauthenticated_access();
return;
}
this->update_command(msg);
} }
#endif #endif
#ifdef USE_VALVE #ifdef USE_VALVE
void APIServerConnection::on_valve_command_request(const ValveCommandRequest &msg) { void APIServerConnection::on_valve_command_request(const ValveCommandRequest &msg) {
if (!this->is_connection_setup()) { if (this->check_authenticated_()) {
this->on_no_setup_connection(); this->valve_command(msg);
return;
} }
if (!this->is_authenticated()) {
this->on_unauthenticated_access();
return;
}
this->valve_command(msg);
} }
#endif #endif
#ifdef USE_BLUETOOTH_PROXY #ifdef USE_BLUETOOTH_PROXY
void APIServerConnection::on_subscribe_bluetooth_le_advertisements_request( void APIServerConnection::on_subscribe_bluetooth_le_advertisements_request(
const SubscribeBluetoothLEAdvertisementsRequest &msg) { const SubscribeBluetoothLEAdvertisementsRequest &msg) {
if (!this->is_connection_setup()) { if (this->check_authenticated_()) {
this->on_no_setup_connection(); this->subscribe_bluetooth_le_advertisements(msg);
return;
} }
if (!this->is_authenticated()) {
this->on_unauthenticated_access();
return;
}
this->subscribe_bluetooth_le_advertisements(msg);
} }
#endif #endif
#ifdef USE_BLUETOOTH_PROXY #ifdef USE_BLUETOOTH_PROXY
void APIServerConnection::on_bluetooth_device_request(const BluetoothDeviceRequest &msg) { void APIServerConnection::on_bluetooth_device_request(const BluetoothDeviceRequest &msg) {
if (!this->is_connection_setup()) { if (this->check_authenticated_()) {
this->on_no_setup_connection(); this->bluetooth_device_request(msg);
return;
} }
if (!this->is_authenticated()) {
this->on_unauthenticated_access();
return;
}
this->bluetooth_device_request(msg);
} }
#endif #endif
#ifdef USE_BLUETOOTH_PROXY #ifdef USE_BLUETOOTH_PROXY
void APIServerConnection::on_bluetooth_gatt_get_services_request(const BluetoothGATTGetServicesRequest &msg) { void APIServerConnection::on_bluetooth_gatt_get_services_request(const BluetoothGATTGetServicesRequest &msg) {
if (!this->is_connection_setup()) { if (this->check_authenticated_()) {
this->on_no_setup_connection(); this->bluetooth_gatt_get_services(msg);
return;
} }
if (!this->is_authenticated()) {
this->on_unauthenticated_access();
return;
}
this->bluetooth_gatt_get_services(msg);
} }
#endif #endif
#ifdef USE_BLUETOOTH_PROXY #ifdef USE_BLUETOOTH_PROXY
void APIServerConnection::on_bluetooth_gatt_read_request(const BluetoothGATTReadRequest &msg) { void APIServerConnection::on_bluetooth_gatt_read_request(const BluetoothGATTReadRequest &msg) {
if (!this->is_connection_setup()) { if (this->check_authenticated_()) {
this->on_no_setup_connection(); this->bluetooth_gatt_read(msg);
return;
} }
if (!this->is_authenticated()) {
this->on_unauthenticated_access();
return;
}
this->bluetooth_gatt_read(msg);
} }
#endif #endif
#ifdef USE_BLUETOOTH_PROXY #ifdef USE_BLUETOOTH_PROXY
void APIServerConnection::on_bluetooth_gatt_write_request(const BluetoothGATTWriteRequest &msg) { void APIServerConnection::on_bluetooth_gatt_write_request(const BluetoothGATTWriteRequest &msg) {
if (!this->is_connection_setup()) { if (this->check_authenticated_()) {
this->on_no_setup_connection(); this->bluetooth_gatt_write(msg);
return;
} }
if (!this->is_authenticated()) {
this->on_unauthenticated_access();
return;
}
this->bluetooth_gatt_write(msg);
} }
#endif #endif
#ifdef USE_BLUETOOTH_PROXY #ifdef USE_BLUETOOTH_PROXY
void APIServerConnection::on_bluetooth_gatt_read_descriptor_request(const BluetoothGATTReadDescriptorRequest &msg) { void APIServerConnection::on_bluetooth_gatt_read_descriptor_request(const BluetoothGATTReadDescriptorRequest &msg) {
if (!this->is_connection_setup()) { if (this->check_authenticated_()) {
this->on_no_setup_connection(); this->bluetooth_gatt_read_descriptor(msg);
return;
} }
if (!this->is_authenticated()) {
this->on_unauthenticated_access();
return;
}
this->bluetooth_gatt_read_descriptor(msg);
} }
#endif #endif
#ifdef USE_BLUETOOTH_PROXY #ifdef USE_BLUETOOTH_PROXY
void APIServerConnection::on_bluetooth_gatt_write_descriptor_request(const BluetoothGATTWriteDescriptorRequest &msg) { void APIServerConnection::on_bluetooth_gatt_write_descriptor_request(const BluetoothGATTWriteDescriptorRequest &msg) {
if (!this->is_connection_setup()) { if (this->check_authenticated_()) {
this->on_no_setup_connection(); this->bluetooth_gatt_write_descriptor(msg);
return;
} }
if (!this->is_authenticated()) {
this->on_unauthenticated_access();
return;
}
this->bluetooth_gatt_write_descriptor(msg);
} }
#endif #endif
#ifdef USE_BLUETOOTH_PROXY #ifdef USE_BLUETOOTH_PROXY
void APIServerConnection::on_bluetooth_gatt_notify_request(const BluetoothGATTNotifyRequest &msg) { void APIServerConnection::on_bluetooth_gatt_notify_request(const BluetoothGATTNotifyRequest &msg) {
if (!this->is_connection_setup()) { if (this->check_authenticated_()) {
this->on_no_setup_connection(); this->bluetooth_gatt_notify(msg);
return;
} }
if (!this->is_authenticated()) {
this->on_unauthenticated_access();
return;
}
this->bluetooth_gatt_notify(msg);
} }
#endif #endif
#ifdef USE_BLUETOOTH_PROXY #ifdef USE_BLUETOOTH_PROXY
void APIServerConnection::on_subscribe_bluetooth_connections_free_request( void APIServerConnection::on_subscribe_bluetooth_connections_free_request(
const SubscribeBluetoothConnectionsFreeRequest &msg) { const SubscribeBluetoothConnectionsFreeRequest &msg) {
if (!this->is_connection_setup()) { if (this->check_authenticated_()) {
this->on_no_setup_connection(); BluetoothConnectionsFreeResponse ret = this->subscribe_bluetooth_connections_free(msg);
return; if (!this->send_message(ret)) {
} this->on_fatal_error();
if (!this->is_authenticated()) { }
this->on_unauthenticated_access();
return;
}
BluetoothConnectionsFreeResponse ret = this->subscribe_bluetooth_connections_free(msg);
if (!this->send_message(ret)) {
this->on_fatal_error();
} }
} }
#endif #endif
#ifdef USE_BLUETOOTH_PROXY #ifdef USE_BLUETOOTH_PROXY
void APIServerConnection::on_unsubscribe_bluetooth_le_advertisements_request( void APIServerConnection::on_unsubscribe_bluetooth_le_advertisements_request(
const UnsubscribeBluetoothLEAdvertisementsRequest &msg) { const UnsubscribeBluetoothLEAdvertisementsRequest &msg) {
if (!this->is_connection_setup()) { if (this->check_authenticated_()) {
this->on_no_setup_connection(); this->unsubscribe_bluetooth_le_advertisements(msg);
return;
} }
if (!this->is_authenticated()) {
this->on_unauthenticated_access();
return;
}
this->unsubscribe_bluetooth_le_advertisements(msg);
} }
#endif #endif
#ifdef USE_BLUETOOTH_PROXY #ifdef USE_BLUETOOTH_PROXY
void APIServerConnection::on_bluetooth_scanner_set_mode_request(const BluetoothScannerSetModeRequest &msg) { void APIServerConnection::on_bluetooth_scanner_set_mode_request(const BluetoothScannerSetModeRequest &msg) {
if (!this->is_connection_setup()) { if (this->check_authenticated_()) {
this->on_no_setup_connection(); this->bluetooth_scanner_set_mode(msg);
return;
} }
if (!this->is_authenticated()) {
this->on_unauthenticated_access();
return;
}
this->bluetooth_scanner_set_mode(msg);
} }
#endif #endif
#ifdef USE_VOICE_ASSISTANT #ifdef USE_VOICE_ASSISTANT
void APIServerConnection::on_subscribe_voice_assistant_request(const SubscribeVoiceAssistantRequest &msg) { void APIServerConnection::on_subscribe_voice_assistant_request(const SubscribeVoiceAssistantRequest &msg) {
if (!this->is_connection_setup()) { if (this->check_authenticated_()) {
this->on_no_setup_connection(); this->subscribe_voice_assistant(msg);
return;
} }
if (!this->is_authenticated()) {
this->on_unauthenticated_access();
return;
}
this->subscribe_voice_assistant(msg);
} }
#endif #endif
#ifdef USE_VOICE_ASSISTANT #ifdef USE_VOICE_ASSISTANT
void APIServerConnection::on_voice_assistant_configuration_request(const VoiceAssistantConfigurationRequest &msg) { void APIServerConnection::on_voice_assistant_configuration_request(const VoiceAssistantConfigurationRequest &msg) {
if (!this->is_connection_setup()) { if (this->check_authenticated_()) {
this->on_no_setup_connection(); VoiceAssistantConfigurationResponse ret = this->voice_assistant_get_configuration(msg);
return; if (!this->send_message(ret)) {
} this->on_fatal_error();
if (!this->is_authenticated()) { }
this->on_unauthenticated_access();
return;
}
VoiceAssistantConfigurationResponse ret = this->voice_assistant_get_configuration(msg);
if (!this->send_message(ret)) {
this->on_fatal_error();
} }
} }
#endif #endif
#ifdef USE_VOICE_ASSISTANT #ifdef USE_VOICE_ASSISTANT
void APIServerConnection::on_voice_assistant_set_configuration(const VoiceAssistantSetConfiguration &msg) { void APIServerConnection::on_voice_assistant_set_configuration(const VoiceAssistantSetConfiguration &msg) {
if (!this->is_connection_setup()) { if (this->check_authenticated_()) {
this->on_no_setup_connection(); this->voice_assistant_set_configuration(msg);
return;
} }
if (!this->is_authenticated()) {
this->on_unauthenticated_access();
return;
}
this->voice_assistant_set_configuration(msg);
} }
#endif #endif
#ifdef USE_ALARM_CONTROL_PANEL #ifdef USE_ALARM_CONTROL_PANEL
void APIServerConnection::on_alarm_control_panel_command_request(const AlarmControlPanelCommandRequest &msg) { void APIServerConnection::on_alarm_control_panel_command_request(const AlarmControlPanelCommandRequest &msg) {
if (!this->is_connection_setup()) { if (this->check_authenticated_()) {
this->on_no_setup_connection(); this->alarm_control_panel_command(msg);
return;
} }
if (!this->is_authenticated()) {
this->on_unauthenticated_access();
return;
}
this->alarm_control_panel_command(msg);
} }
#endif #endif

View File

@ -379,6 +379,26 @@ class ProtoService {
// Send the buffer // Send the buffer
return this->send_buffer(buffer, message_type); return this->send_buffer(buffer, message_type);
} }
// Authentication helper methods
bool check_connection_setup_() {
if (!this->is_connection_setup()) {
this->on_no_setup_connection();
return false;
}
return true;
}
bool check_authenticated_() {
if (!this->check_connection_setup_()) {
return false;
}
if (!this->is_authenticated()) {
this->on_unauthenticated_access();
return false;
}
return true;
}
}; };
} // namespace api } // namespace api

View File

@ -1424,25 +1424,40 @@ def main() -> None:
hpp_protected += f" void {on_func}(const {inp} &msg) override;\n" hpp_protected += f" void {on_func}(const {inp} &msg) override;\n"
hpp += f" virtual {ret} {func}(const {inp} &msg) = 0;\n" hpp += f" virtual {ret} {func}(const {inp} &msg) = 0;\n"
cpp += f"void {class_name}::{on_func}(const {inp} &msg) {{\n" cpp += f"void {class_name}::{on_func}(const {inp} &msg) {{\n"
body = ""
if needs_conn:
body += "if (!this->is_connection_setup()) {\n"
body += " this->on_no_setup_connection();\n"
body += " return;\n"
body += "}\n"
if needs_auth:
body += "if (!this->is_authenticated()) {\n"
body += " this->on_unauthenticated_access();\n"
body += " return;\n"
body += "}\n"
if is_void: # Start with authentication/connection check if needed
body += f"this->{func}(msg);\n" if needs_auth or needs_conn:
else: # Determine which check to use
body += f"{ret} ret = this->{func}(msg);\n" if needs_auth:
body += "if (!this->send_message(ret)) {\n" check_func = "this->check_authenticated_()"
body += " this->on_fatal_error();\n" else:
check_func = "this->check_connection_setup_()"
body = f"if ({check_func}) {{\n"
# Add the actual handler code, indented
handler_body = ""
if is_void:
handler_body = f"this->{func}(msg);\n"
else:
handler_body = f"{ret} ret = this->{func}(msg);\n"
handler_body += "if (!this->send_message(ret)) {\n"
handler_body += " this->on_fatal_error();\n"
handler_body += "}\n"
body += indent(handler_body) + "\n"
body += "}\n" body += "}\n"
else:
# No auth check needed, just call the handler
body = ""
if is_void:
body += f"this->{func}(msg);\n"
else:
body += f"{ret} ret = this->{func}(msg);\n"
body += "if (!this->send_message(ret)) {\n"
body += " this->on_fatal_error();\n"
body += "}\n"
cpp += indent(body) + "\n" + "}\n" cpp += indent(body) + "\n" + "}\n"
if ifdef is not None: if ifdef is not None: