Skip to content

Commit 8232ddd

Browse files
talih0carlescufi
authored andcommitted
shell: telnet: Don't close the connection on EAGAIN error
EAGAIN error is returned if the tcp window size is full. Retry sending the packet instead of closing the connection if this error occurs. Also the full payload may not be sent in a single call to net_context_send(). Keep track of the number of bytes remaining and try to send the full payload. Signed-off-by: Andriy Gelman <[email protected]>
1 parent 995824a commit 8232ddd

File tree

1 file changed

+42
-15
lines changed

1 file changed

+42
-15
lines changed

subsys/shell/backends/shell_telnet.c

+42-15
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ struct shell_telnet *sh_telnet;
3333
#define TELNET_MIN_COMMAND_LEN 2
3434
#define TELNET_WILL_DO_COMMAND_LEN 3
3535

36+
#define TELNET_RETRY_SEND_SLEEP_MS 50
37+
3638
/* Basic TELNET implementation. */
3739

3840
static void telnet_end_client_connection(void)
@@ -64,17 +66,29 @@ static void telnet_sent_cb(struct net_context *client,
6466

6567
static void telnet_command_send_reply(uint8_t *msg, uint16_t len)
6668
{
67-
int err;
68-
6969
if (sh_telnet->client_ctx == NULL) {
7070
return;
7171
}
7272

73-
err = net_context_send(sh_telnet->client_ctx, msg, len, telnet_sent_cb,
74-
K_FOREVER, NULL);
75-
if (err < 0) {
76-
LOG_ERR("Failed to send command %d, shutting down", err);
77-
telnet_end_client_connection();
73+
while (len > 0) {
74+
int ret;
75+
76+
ret = net_context_send(sh_telnet->client_ctx, msg, len, telnet_sent_cb,
77+
K_FOREVER, NULL);
78+
79+
if (ret == -EAGAIN) {
80+
k_sleep(K_MSEC(TELNET_RETRY_SEND_SLEEP_MS));
81+
continue;
82+
}
83+
84+
if (ret < 0) {
85+
LOG_ERR("Failed to send command %d, shutting down", ret);
86+
telnet_end_client_connection();
87+
break;
88+
}
89+
90+
msg += ret;
91+
len -= ret;
7892
}
7993
}
8094

@@ -175,7 +189,9 @@ static void telnet_reply_command(struct telnet_simple_command *cmd)
175189

176190
static int telnet_send(void)
177191
{
178-
int err;
192+
int ret;
193+
uint8_t *msg = sh_telnet->line_out.buf;
194+
uint16_t len = sh_telnet->line_out.len;
179195

180196
if (sh_telnet->line_out.len == 0) {
181197
return 0;
@@ -185,13 +201,24 @@ static int telnet_send(void)
185201
return -ENOTCONN;
186202
}
187203

188-
err = net_context_send(sh_telnet->client_ctx, sh_telnet->line_out.buf,
189-
sh_telnet->line_out.len, telnet_sent_cb,
190-
K_FOREVER, NULL);
191-
if (err < 0) {
192-
LOG_ERR("Failed to send %d, shutting down", err);
193-
telnet_end_client_connection();
194-
return err;
204+
while (len > 0) {
205+
ret = net_context_send(sh_telnet->client_ctx, msg,
206+
len, telnet_sent_cb,
207+
K_FOREVER, NULL);
208+
209+
if (ret == -EAGAIN) {
210+
k_sleep(K_MSEC(TELNET_RETRY_SEND_SLEEP_MS));
211+
continue;
212+
}
213+
214+
if (ret < 0) {
215+
LOG_ERR("Failed to send %d, shutting down", ret);
216+
telnet_end_client_connection();
217+
return ret;
218+
}
219+
220+
msg += ret;
221+
len -= ret;
195222
}
196223

197224
/* We reinitialize the line buffer */

0 commit comments

Comments
 (0)