2019-07-01 20:25:32 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* net/tcp/tcp_txdrain.c
|
|
|
|
*
|
2024-09-11 14:39:39 +02:00
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
|
|
*
|
2021-02-19 12:45:37 +01:00
|
|
|
* Licensed to the Apache Software Foundation (ASF) under one or more
|
|
|
|
* contributor license agreements. See the NOTICE file distributed with
|
|
|
|
* this work for additional information regarding copyright ownership. The
|
|
|
|
* ASF licenses this file to you under the Apache License, Version 2.0 (the
|
|
|
|
* "License"); you may not use this file except in compliance with the
|
|
|
|
* License. You may obtain a copy of the License at
|
2019-07-01 20:25:32 +02:00
|
|
|
*
|
2021-02-19 12:45:37 +01:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2019-07-01 20:25:32 +02:00
|
|
|
*
|
2021-02-19 12:45:37 +01:00
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
|
|
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
|
|
|
* License for the specific language governing permissions and limitations
|
|
|
|
* under the License.
|
2019-07-01 20:25:32 +02:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Included Files
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include <nuttx/config.h>
|
|
|
|
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sched.h>
|
|
|
|
#include <assert.h>
|
|
|
|
#include <errno.h>
|
|
|
|
|
2021-05-14 04:03:23 +02:00
|
|
|
#include <nuttx/net/net.h>
|
2020-02-01 08:17:32 +01:00
|
|
|
#include <nuttx/semaphore.h>
|
|
|
|
|
2021-05-14 04:03:23 +02:00
|
|
|
#include "utils/utils.h"
|
2019-07-01 20:25:32 +02:00
|
|
|
#include "tcp/tcp.h"
|
|
|
|
|
2019-12-31 16:26:14 +01:00
|
|
|
#if defined(CONFIG_NET_TCP_WRITE_BUFFERS) && defined(CONFIG_NET_TCP_NOTIFIER)
|
2019-07-01 20:25:32 +02:00
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: txdrain_worker
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Called with the write buffers have all been sent.
|
|
|
|
*
|
|
|
|
* Input Parameters:
|
2019-07-28 15:39:55 +02:00
|
|
|
* arg - The notifier entry.
|
2019-07-01 20:25:32 +02:00
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* None.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static void txdrain_worker(FAR void *arg)
|
|
|
|
{
|
2019-12-31 16:26:14 +01:00
|
|
|
FAR sem_t *waitsem = (FAR sem_t *)arg;
|
2019-07-27 18:26:52 +02:00
|
|
|
|
2019-12-31 16:26:14 +01:00
|
|
|
DEBUGASSERT(waitsem != NULL);
|
2019-07-27 18:26:52 +02:00
|
|
|
|
|
|
|
/* Then just post the semaphore, waking up tcp_txdrain() */
|
|
|
|
|
2019-12-31 16:26:14 +01:00
|
|
|
nxsem_post(waitsem);
|
2019-07-01 20:25:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Public Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: tcp_txdrain
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Wait for all write buffers to be sent (or for a timeout to occur).
|
|
|
|
*
|
|
|
|
* Input Parameters:
|
|
|
|
* psock - An instance of the internal socket structure.
|
2020-01-04 11:37:46 +01:00
|
|
|
* timeout - The relative time when the timeout will occur
|
2019-07-01 20:25:32 +02:00
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* Zero (OK) is returned on success; a negated errno value is returned
|
|
|
|
* on any failure.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2020-01-04 11:37:46 +01:00
|
|
|
int tcp_txdrain(FAR struct socket *psock, unsigned int timeout)
|
2019-07-01 20:25:32 +02:00
|
|
|
{
|
|
|
|
FAR struct tcp_conn_s *conn;
|
|
|
|
sem_t waitsem;
|
|
|
|
int ret;
|
|
|
|
|
2019-07-27 18:26:52 +02:00
|
|
|
DEBUGASSERT(psock->s_type == SOCK_STREAM);
|
2019-07-01 20:25:32 +02:00
|
|
|
|
2023-05-08 16:11:04 +02:00
|
|
|
conn = psock->s_conn;
|
2019-07-01 20:25:32 +02:00
|
|
|
|
|
|
|
/* Initialize the wait semaphore */
|
|
|
|
|
|
|
|
nxsem_init(&waitsem, 0, 0);
|
|
|
|
|
|
|
|
/* The following needs to be done with the network stable */
|
|
|
|
|
|
|
|
net_lock();
|
2019-07-27 18:26:52 +02:00
|
|
|
|
|
|
|
/* Get a notification when the write buffers are drained */
|
|
|
|
|
2019-07-01 20:25:32 +02:00
|
|
|
ret = tcp_writebuffer_notifier_setup(txdrain_worker, conn, &waitsem);
|
2019-07-27 18:26:52 +02:00
|
|
|
|
|
|
|
/* The special return value of 0 means that there is no Tx data to be
|
|
|
|
* drained. Otherwise it is a special 'key' that can be used to teardown
|
|
|
|
* the notification later
|
|
|
|
*/
|
|
|
|
|
2019-07-01 20:25:32 +02:00
|
|
|
if (ret > 0)
|
|
|
|
{
|
2019-07-27 18:26:52 +02:00
|
|
|
/* Save the drain key */
|
2019-07-01 20:25:32 +02:00
|
|
|
|
2019-07-27 18:26:52 +02:00
|
|
|
int drain_key = ret;
|
2019-07-01 20:25:32 +02:00
|
|
|
|
2019-07-27 18:26:52 +02:00
|
|
|
/* Also get a notification if we lose the connection.
|
|
|
|
* REVISIT: This really should not be necessary. tcp_send() should
|
|
|
|
* detect the disconnection event and signal the txdrain but I do not
|
|
|
|
* see that TCP disconnection event. This assures that the txdrain
|
|
|
|
* operation will not wait for the full timeout in that case.
|
|
|
|
*/
|
|
|
|
|
|
|
|
ret = tcp_disconnect_notifier_setup(txdrain_worker, conn, &waitsem);
|
|
|
|
|
2019-10-25 19:31:42 +02:00
|
|
|
/* Zero is a special value that means that no connection has been
|
|
|
|
* established. Otherwise it is a special 'key' that can be used
|
|
|
|
* to teardown the notification later
|
|
|
|
*/
|
2019-07-27 18:26:52 +02:00
|
|
|
|
2019-10-25 19:31:42 +02:00
|
|
|
if (ret > 0)
|
|
|
|
{
|
|
|
|
/* Save the disconnect key */
|
2019-07-27 18:26:52 +02:00
|
|
|
|
2019-10-25 19:31:42 +02:00
|
|
|
int disconn_key = ret;
|
2019-07-01 20:25:32 +02:00
|
|
|
|
2019-10-25 19:31:42 +02:00
|
|
|
/* There is pending write data and the socket is connected..
|
|
|
|
* wait for it to drain or be be disconnected.
|
|
|
|
*/
|
2019-07-01 20:25:32 +02:00
|
|
|
|
2023-01-13 07:51:38 +01:00
|
|
|
ret = net_sem_timedwait_uninterruptible(&waitsem, timeout);
|
2019-07-27 18:26:52 +02:00
|
|
|
|
|
|
|
/* Tear down the disconnect notifier */
|
|
|
|
|
|
|
|
tcp_notifier_teardown(disconn_key);
|
2019-07-01 20:25:32 +02:00
|
|
|
}
|
2019-07-27 18:26:52 +02:00
|
|
|
|
|
|
|
/* Tear down the disconnect notifier */
|
|
|
|
|
|
|
|
tcp_notifier_teardown(drain_key);
|
2019-07-01 20:25:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
net_unlock();
|
|
|
|
nxsem_destroy(&waitsem);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2019-12-31 16:26:14 +01:00
|
|
|
#endif /* CONFIG_NET_TCP_WRITE_BUFFERS && CONFIG_NET_TCP_NOTIFIER */
|