disconnection

This commit is contained in:
y4my4my4m 2023-04-29 22:17:14 +09:00
parent a16030394a
commit f4ed63ce70

View file

@ -50,6 +50,7 @@ U0 GetColorString(I64 color_code, U8 *buf) {
U0 TelnetClient(U8 *host, U16 port) { U0 TelnetClient(U8 *host, U16 port) {
I64 sock, bytes_received, input_len; I64 sock, bytes_received, input_len;
U8 buffer[BUF_SIZE], input_buffer[BUF_SIZE], *ptr, ch; U8 buffer[BUF_SIZE], input_buffer[BUF_SIZE], *ptr, ch;
Bool force_disconnect;
sock = TelnetOpen(host, port); sock = TelnetOpen(host, port);
if (sock <= 0) { if (sock <= 0) {
@ -58,7 +59,8 @@ U0 TelnetClient(U8 *host, U16 port) {
"Connected to %s:%d.\n", host, port; "Connected to %s:%d.\n", host, port;
while (1) { force_disconnect = FALSE;
while (!force_disconnect) {
bytes_received = TCPSocketReceive(sock, buffer, BUF_SIZE - 1); bytes_received = TCPSocketReceive(sock, buffer, BUF_SIZE - 1);
if (bytes_received > 0) { if (bytes_received > 0) {
buffer[bytes_received] = '\0'; buffer[bytes_received] = '\0';
@ -98,8 +100,9 @@ U0 TelnetClient(U8 *host, U16 port) {
ptr++; ptr++;
} }
} }
"\n";
"\n";
Sleep(200); // Add a 200 ms delay
// Prompt user for input and send it to the remote host // Prompt user for input and send it to the remote host
"Enter your choice: "; "Enter your choice: ";
@ -109,14 +112,22 @@ U0 TelnetClient(U8 *host, U16 port) {
if (ch == '\r' || ch == '\n') { if (ch == '\r' || ch == '\n') {
break; break;
} }
if (ch == 27) { // ESC key
force_disconnect = TRUE;
break;
}
input_buffer[input_len++] = ch; input_buffer[input_len++] = ch;
} }
input_buffer[input_len] = '\0'; input_buffer[input_len] = '\0';
"Sending: %s\n", input_buffer; // Debugging line if (!force_disconnect) {
SysLog("Sending: %s\n", input_buffer); // Debugging line
input_buffer[input_len++] = '\r'; input_buffer[input_len++] = '\r';
input_buffer[input_len++] = '\n'; input_buffer[input_len++] = '\n';
TCPSocketSend(sock, input_buffer, input_len); TCPSocketSend(sock, input_buffer, input_len);
} else {
"Force disconnecting...\n";
}
} else { } else {
"Error: Connection closed by the remote host.\n"; "Error: Connection closed by the remote host.\n";
break; break;
@ -141,7 +152,3 @@ U0 TelnetPrompt() {
TelnetClient(form.host, form.port); TelnetClient(form.host, form.port);
} }
} }
public U0 Telnet(U8 *hostname, U16 port = TELNET_PORT) {
TelnetClient(hostname, port);
}