jcs
/subtext
/amendments
/6
telnet: Buffer input into ibuf
jcs made amendment 6 over 2 years ago
--- telnet.c Tue Nov 30 15:01:24 2021
+++ telnet.c Tue Nov 30 20:49:46 2021
@@ -70,11 +70,8 @@ telnet_init(void)
for (i = 0; i < TELNET_SLOTS; i++) {
node = &telnet_nodes[i];
- node->id = i;
memset(node, 0, sizeof(struct telnet_node));
-
- if (i > 1)
- continue;
+ node->id = i;
/* docs say buf should be 4*MTU+1024 */
/* TODO: actually lookup MTU */
@@ -107,15 +104,12 @@ telnet_atexit(void)
error = _TCPClose(&telnet_exit_pb, node->stream, nil, nil,
false);
if (error == noErr)
- error = _TCPAbort(&telnet_exit_pb, node->stream, nil, nil,
- false);
- if (error == noErr)
error = _TCPRelease(&telnet_exit_pb, node->stream, nil,
nil, false);
}
if (node->tcp_buf)
- free(node->tcp_buf);
+ ;//free(node->tcp_buf);
}
}
@@ -192,8 +186,8 @@ telnet_output(struct session *session)
error = _TCPSend(&node->pb, node->stream, node->tcp_wds, nil, nil,
true);
if (error)
+ /* TODO: make this not fatal */
err(1, "TCPSend[%d] failed: %d", node->id, error);
- /* XXX: make this not fatal */
return session->obuflen;
}
@@ -201,7 +195,37 @@ telnet_output(struct session *session)
short
telnet_input(struct session *session)
{
- uthread_yield();
- return 0;
-}
+ struct telnet_node *node = (struct telnet_node *)session->cookie;
+ unsigned short rlen;
+ short error;
+
+ if (session->ibuflen >= sizeof(session->ibuf))
+ return 0;
+
+ if (node->pb.ioResult > 0)
+ /* previous _TCPSend/_TCPRecv has not completed yet */
+ return 0;
+
+ error = _TCPStatus(&node->pb, node->stream, &telnet_status_pb, nil,
+ nil, false);
+ if (error)
+ /* TODO: make this not fatal */
+ err(1, "TCPRecv[%d] failed: %d", node->id, error);
+
+ if (telnet_status_pb.amtUnreadData == 0)
+ return 0;
+
+ rlen = telnet_status_pb.amtUnreadData;
+ if (session->ibuflen + rlen >= sizeof(session->ibuf))
+ rlen = sizeof(session->ibuf) - session->ibuflen;
+ error = _TCPRcv(&node->pb, node->stream,
+ (Ptr)(session->ibuf + session->ibuflen), &rlen, nil, nil, false);
+ if (error)
+ /* TODO: make this not fatal */
+ err(1, "TCPRecv[%d] failed: %d", node->id, error);
+
+ session->ibuflen += rlen;
+
+ return rlen;
+}