This commit is contained in:
y4my4my4m 2023-05-02 00:34:49 +09:00
parent 6d61aca360
commit acd9d71f6d

View file

@ -6,6 +6,7 @@
#define TIMEOUT_DURATION 5000 #define TIMEOUT_DURATION 5000
Bool skip_input = FALSE; Bool skip_input = FALSE;
Bool WaitForInputOrTimeout(I64 timeout_duration) { Bool WaitForInputOrTimeout(I64 timeout_duration) {
I64 start_jiffies = counts.jiffies; I64 start_jiffies = counts.jiffies;
I64 elapsed_jiffies; I64 elapsed_jiffies;
@ -31,6 +32,7 @@ I64 TelnetOpen(U8 *host, U16 port) {
} }
sock = TCPConnectionCreate(host, port); sock = TCPConnectionCreate(host, port);
"$$RED$$Conecting to %s:%d.$$FG$$$$BG$$\n", host, port;
if (sock <= 0) { if (sock <= 0) {
PrintErr("Failed to connect to %s:%d\n", host, port); PrintErr("Failed to connect to %s:%d\n", host, port);
return sock; return sock;
@ -90,7 +92,7 @@ U0 SendTerminalType(I64 sock, U8 *terminal_type) {
} }
U0 HandleControlCodes(U8 ch) { U0 HandleControlCodes(U8 ch) {
if (ch < 32) { // ASCII code below 32 (control character) if (ch < 32 && ch != 20) { // ASCII code below 32 (control character)
switch (ch) { switch (ch) {
case 0: // NUL (Null) case 0: // NUL (Null)
// Typically ignored // Typically ignored
@ -100,13 +102,13 @@ U0 HandleControlCodes(U8 ch) {
Beep; Beep;
break; break;
case 8: // BS (Backspace) case 8: // BS (Backspace)
"%c%c%c", 8, ' ', 8; // Move cursor back, erase character, move cursor back again // "%c%c%c", 8, ' ', 8; // Move cursor back, erase character, move cursor back again
break; break;
case 9: // HT (Horizontal Tab) case 9: // HT (Horizontal Tab)
// Move the cursor to the next tab stop (typically 8 spaces) // Move the cursor to the next tab stop (typically 8 spaces)
break; break;
case 10: // LF (Line Feed) case 10: // LF (Line Feed)
"%c", ch; // "%c", ch;
break; break;
case 11: // VT (Vertical Tab) case 11: // VT (Vertical Tab)
// Move the cursor down one line // Move the cursor down one line
@ -115,8 +117,7 @@ U0 HandleControlCodes(U8 ch) {
DocClear; DocClear;
break; break;
case 13: // CR (Carriage Return) case 13: // CR (Carriage Return)
// "\r\n\0"; "\r\n\0";
skip_input = TRUE;
break; break;
case 14: // SO (Shift Out) case 14: // SO (Shift Out)
// Switch to an alternate character set // Switch to an alternate character set
@ -128,45 +129,44 @@ U0 HandleControlCodes(U8 ch) {
break; break;
} }
} else { } else {
// dont print for now // SysLog("Code: 0x%02X\n", ch);
// "%c", ch; "%c", ch;
} }
// skip_input = TRUE;
} }
U0 Telnet(U8 *host, U16 port=TELNET_PORT) { U0 Telnet(U8 *host, U16 port=TELNET_PORT) {
I64 sock, bytes_received, input_len; I64 sock, bytes_received, input_len, sc = 0;
U8 buffer[BUF_SIZE], input_buffer[BUF_SIZE], *ptr, ch; U8 buffer[BUF_SIZE], input_buffer[BUF_SIZE], *ptr, ch;
Bool force_disconnect = FALSE; Bool force_disconnect = FALSE;
// would be nice to resize to 25x80 I64 window_width = 80;
// CTask *task; I64 window_height = 25;
// task=User;
// TaskWait(task); I64 window_left = (GR_WIDTH - window_width) / 2;
// task->border_src=BDS_CONST; I64 window_top = (Fs->win_bottom - Fs->win_top - window_height) / 2;
// task->border_attr=LTGRAY<<4+DriveTextAttrGet(':')&15;
// task->text_attr =LTGRAY<<4+BLUE; WinHorz(Fs->win_left, Fs->win_left+80, Fs);
// task->win_inhibit= WIG_TASK_DEFAULT-WIF_SELF_BORDER; WinVert(Fs->win_top + window_top, Fs->win_top + window_top + window_height, Fs);
// WinHorz(Fs->win_left,Fs->win_right,task); WinToTop(Fs);
// WinVert(Fs->win_top,(Fs->win_top+Fs->win_bottom)>>2-1,task);
// WinVert(task->win_bottom+3,Fs->win_bottom);
// WinToTop(Fs);
sock = TelnetOpen(host, port); sock = TelnetOpen(host, port);
if (sock <= 0) { if (sock <= 0) {
return; return;
} }
"$$BG,RED$$$$WHITE$$Connected to %s:%d.$$FG$$$$BG$$\n", host, port; "$$BG,RED$$$$WHITE$$Connected$$FG$$$$BG$$\n"
while (!force_disconnect) { 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';
// Basic Telnet protocol parser // Basic Telnet protocol parser
ptr = buffer; ptr = buffer;
while (*ptr) { while (*ptr) {
SysLog("BUF: 0x%02X\n", *ptr);
if (*ptr == 0xFF) { // Telnet negotiation sequence if (*ptr == 0xFF) { // Telnet negotiation sequence
U8 negotiation_code = *(ptr + 1); U8 negotiation_code = *(ptr + 1);
U8 option_code = *(ptr + 2); U8 option_code = *(ptr + 2);
@ -195,7 +195,7 @@ U0 Telnet(U8 *host, U16 port=TELNET_PORT) {
response[3] = '\0'; response[3] = '\0';
TCPSocketSendString(sock, response); TCPSocketSendString(sock, response);
SendTerminalType(sock, "ANSI"); SendTerminalType(sock, "ANSI");
// SendWindowSize(sock, 40, 80); // SendWindowSize(sock, 25, 80);
ptr += 3; ptr += 3;
} else { } else {
response[1] = 0xFC; // WONT response[1] = 0xFC; // WONT
@ -209,7 +209,7 @@ U0 Telnet(U8 *host, U16 port=TELNET_PORT) {
response[2] = option_code; response[2] = option_code;
response[3] = '\0'; response[3] = '\0';
// TCPSocketSendString(sock, response); TCPSocketSendString(sock, response);
ptr += 3; ptr += 3;
} else if (*ptr == 0x1B) { } else if (*ptr == 0x1B) {
// ANSI escape sequence // ANSI escape sequence
@ -221,6 +221,7 @@ U0 Telnet(U8 *host, U16 port=TELNET_PORT) {
ansi_code = 0; ansi_code = 0;
while (*ptr >= '0' && *ptr <= '9') { while (*ptr >= '0' && *ptr <= '9') {
ansi_code = ansi_code * 10 + (*ptr - '0'); ansi_code = ansi_code * 10 + (*ptr - '0');
// HandleControlCodes(*ptr);
ptr++; ptr++;
} }
// Process ansi_code // Process ansi_code
@ -273,8 +274,7 @@ U0 Telnet(U8 *host, U16 port=TELNET_PORT) {
} }
} }
skip_input = WaitForInputOrTimeout(TIMEOUT_DURATION); // skip_input = WaitForInputOrTimeout(TIMEOUT_DURATION);
if (!skip_input) if (!skip_input)
{ {
Sleep(200); Sleep(200);
@ -282,23 +282,59 @@ U0 Telnet(U8 *host, U16 port=TELNET_PORT) {
"\n$$RED$$$BK,1$Input$BK,0$$$BLACK$$: "; "\n$$RED$$$BK,1$Input$BK,0$$$BLACK$$: ";
U8 *line = input_buffer; U8 *line = input_buffer;
U8 *end_of_line = line;
while (1) { while (1) {
// switch (KeyGet(&sc))
// {
// case 0:
// switch (sc.u8[0])
// {
// case SC_CURSOR_UP:
// TCPSocketSendString(sock, "CU01");
// SysLog("Cursor up");
// break;
// case SC_CURSOR_DOWN:
// TCPSocketSendString(sock, "CD01");
// SysLog("Cursor down");
// break;
// case SC_CURSOR_LEFT:
// TCPSocketSendString(sock, "CB01");
// SysLog("Cursor left");
// if (line > input_buffer) {
// line--;
// "%c", 8;
// }
// break;
// case SC_CURSOR_RIGHT:
// SysLog("Cursor right");
// TCPSocketSendString(sock, "CF01");
// if (line < end_of_line) {
// "%c", *line++;
// }
// break;
// default:
// break;
// }
// default:
// break;
// }
ch = CharGet(, FALSE); ch = CharGet(, FALSE);
if (ch == '\r' || ch == '\n') { if (ch == '\r' || ch == '\n') {
break; break;
} }
if (ch == CH_SHIFT_ESC) { // ESC key else if (ch == CH_SHIFT_ESC) { // ESC key
force_disconnect = TRUE; force_disconnect = TRUE;
break; break;
} }
if (ch == CH_BACKSPACE || ch == 127) { // Backspace or Delete key // if (ch == CH_BACKSPACE || ch == 127) { // Backspace or Delete key
if (line != input_buffer) { // if (line != input_buffer) {
line--; // Move the pointer back // line--; // Move the pointer back
"%c%c%c", 8, ' ', 8; // Move the cursor back, erase the character, and move the cursor back again // "%c%c%c", 8, ' ', 8; // Move the cursor back, erase the character, and move the cursor back again
} // }
} else { // }
else {
*line++ = ch; *line++ = ch;
HandleControlCodes(ch); "%c", ch;
} }
} }
*line++ = '\r'; *line++ = '\r';
@ -306,15 +342,16 @@ U0 Telnet(U8 *host, U16 port=TELNET_PORT) {
*line = '\0'; *line = '\0';
if (!force_disconnect) { if (!force_disconnect) {
// SysLog("Sending: %s\n", input_buffer); // Debugging line
TCPSocketSendString(sock, line); TCPSocketSendString(sock, line);
// TCPSocketSend(sock, input_buffer, BUF_SIZE);
} else { } else {
"Force disconnecting...\n"; "Force disconnecting...\n";
break; break;
} }
} }
skip_input = FALSE; // skip_input = FALSE;
} else { } else {
SysLog("Error: %0x%02X\n", ch);
"Error: Connection closed by the remote host.\n"; "Error: Connection closed by the remote host.\n";
break; break;
} }
@ -342,9 +379,9 @@ U0 TelnetPrompt() {
// Dev auto-connect to test server // Dev auto-connect to test server
// Telnet("mbrserver.com"); // Telnet("mbrserver.com");
// Telnet("freechess.org"); Telnet("freechess.org");
// Telnet("dura-bbs.net", 6359); // Telnet("dura-bbs.net", 6359);
// Telnet("darkrealms.ca"); // Telnet("darkrealms.ca");
// ZealBBS dev server // ZealBBS dev server
Telnet("localhost", 8888); // Telnet("localhost", 8888);