Update main.c
Prepare Build and Testing executable binary / Prepare-Build-Testing-With-Make-Without-sqlite3 (push) Successful in 5s
Prepare Build and Testing executable binary / Prepare-Build-Testing-With-CMake-Without-sqlite3 (push) Successful in 9s
Prepare Build and Testing executable binary / Prepare-Build-Testing-With-Meson-Without-sqlite3 (push) Successful in 7s
Prepare Build and Testing executable binary / Prepare-Build-Testing-With-Make-Without-sqlite3 (push) Successful in 5s
Prepare Build and Testing executable binary / Prepare-Build-Testing-With-CMake-Without-sqlite3 (push) Successful in 9s
Prepare Build and Testing executable binary / Prepare-Build-Testing-With-Meson-Without-sqlite3 (push) Successful in 7s
This commit is contained in:
-77
@@ -11,7 +11,6 @@
|
||||
|
||||
#define HTTP_PORT "8888"
|
||||
#define STATIC_DIR "./static/"
|
||||
#define INPUT_BUFFER_SIZE 256
|
||||
|
||||
static const char *s_http_addr = "http://0.0.0.0:8888";
|
||||
static struct mg_mgr mgr;
|
||||
@@ -20,8 +19,6 @@ struct client_data {
|
||||
struct terminal_session term;
|
||||
bool terminal_ready;
|
||||
bool closing; // Флаг для закрытия соединения
|
||||
char input_buffer[INPUT_BUFFER_SIZE];
|
||||
size_t input_buffer_len;
|
||||
};
|
||||
|
||||
// Функция для логирования запросов
|
||||
@@ -82,65 +79,6 @@ static void restart_terminal_session(struct mg_connection *c,
|
||||
}
|
||||
}
|
||||
|
||||
// Функция для проверки команды exit в буфере
|
||||
static bool check_for_exit_command(struct mg_connection *c,
|
||||
struct client_data *cd) {
|
||||
for (size_t i = 0; i < cd->input_buffer_len; i++) {
|
||||
if (cd->input_buffer[i] == 0x04) {
|
||||
printf("Ctrl+D pressed\n");
|
||||
write_to_terminal(&cd->term, &cd->input_buffer[i], 1);
|
||||
usleep(100);
|
||||
mg_ws_send(c, "CLOSE_TAB", 9, WEBSOCKET_OP_TEXT);
|
||||
cd->closing = true;
|
||||
cd->input_buffer_len = 0;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
// Ищем символ новой строки в буфере
|
||||
for (size_t i = 0; i < cd->input_buffer_len; i++) {
|
||||
if (cd->input_buffer[i] == '\n' || cd->input_buffer[i] == '\r') {
|
||||
// Нашли конец строки, проверяем команду
|
||||
cd->input_buffer[i] = '\0'; // Завершаем строку
|
||||
printf("Command: %s\n", cd->input_buffer);
|
||||
if (cd->input_buffer[i - 1] == 'x') {
|
||||
printf("Ctrl+D pressed\n");
|
||||
write_to_terminal(&cd->term, &cd->input_buffer[i], 1);
|
||||
usleep(100);
|
||||
mg_ws_send(c, "CLOSE_TAB", 9, WEBSOCKET_OP_TEXT);
|
||||
cd->closing = true;
|
||||
cd->input_buffer_len = 0;
|
||||
return true;
|
||||
}
|
||||
// Проверяем, является ли команда "exit"
|
||||
if (strcmp(cd->input_buffer, "exit") == 0) {
|
||||
printf("Exit command detected, closing terminal session\n");
|
||||
|
||||
// Ждем немного чтобы команда выполнилась
|
||||
usleep(100);
|
||||
|
||||
// Отправляем клиенту сообщение о закрытии вкладки
|
||||
mg_ws_send(c, "CLOSE_TAB", 9, WEBSOCKET_OP_TEXT);
|
||||
|
||||
// Помечаем соединение для закрытия
|
||||
cd->closing = true;
|
||||
|
||||
// Сбрасываем буфер
|
||||
cd->input_buffer_len = 0;
|
||||
return true;
|
||||
}
|
||||
|
||||
// Сдвигаем буфер, удаляя обработанную строку
|
||||
size_t remaining = cd->input_buffer_len - i - 1;
|
||||
if (remaining > 0) {
|
||||
memmove(cd->input_buffer, cd->input_buffer + i + 1, remaining);
|
||||
}
|
||||
cd->input_buffer_len = remaining;
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
// Обработчик WebSocket сообщений
|
||||
static void handle_websocket_message(struct mg_connection *c,
|
||||
struct mg_ws_message *wm,
|
||||
@@ -167,21 +105,6 @@ static void handle_websocket_message(struct mg_connection *c,
|
||||
cJSON_Delete(json);
|
||||
}
|
||||
|
||||
// Добавляем данные в буфер ввода
|
||||
size_t bytes_to_copy = wm->data.len;
|
||||
if (cd->input_buffer_len + bytes_to_copy >= INPUT_BUFFER_SIZE) {
|
||||
// Буфер переполнен, сбрасываем его
|
||||
cd->input_buffer_len = 0;
|
||||
}
|
||||
|
||||
memcpy(cd->input_buffer + cd->input_buffer_len, wm->data.buf, bytes_to_copy);
|
||||
cd->input_buffer_len += bytes_to_copy;
|
||||
|
||||
printf("Input received: %s\n", wm->data.buf);
|
||||
if (check_for_exit_command(c, cd)) {
|
||||
return; // Вкладка будет закрыта, выходим
|
||||
}
|
||||
|
||||
// Обычный ввод от пользователя
|
||||
write_to_terminal(&cd->term, wm->data.buf, wm->data.len);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user