summaryrefslogtreecommitdiff
path: root/common/src/nx_tcp_socket_state_wait.c
blob: 5c633f567f34a62c859d2af51f24aa1758ba995e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
/***************************************************************************
 * Copyright (c) 2024 Microsoft Corporation
 * Copyright (c) 2025-present Eclipse ThreadX Contributors
 *
 * This program and the accompanying materials are made available under the
 * terms of the MIT License which is available at
 * https://opensource.org/licenses/MIT.
 *
 * SPDX-License-Identifier: MIT
 **************************************************************************/


/**************************************************************************/
/**************************************************************************/
/**                                                                       */
/** NetX Component                                                        */
/**                                                                       */
/**   Transmission Control Protocol (TCP)                                 */
/**                                                                       */
/**************************************************************************/
/**************************************************************************/

#define NX_SOURCE_CODE


/* Include necessary system files.  */

#include "nx_api.h"
#include "nx_tcp.h"
#ifdef NX_ENABLE_HTTP_PROXY
#include "nx_http_proxy_client.h"
#endif /* NX_ENABLE_HTTP_PROXY */


/**************************************************************************/
/*                                                                        */
/*  FUNCTION                                               RELEASE        */
/*                                                                        */
/*    _nx_tcp_socket_state_wait                           PORTABLE C      */
/*                                                           6.4.3        */
/*  AUTHOR                                                                */
/*                                                                        */
/*    Yuxin Zhou, Microsoft Corporation                                   */
/*                                                                        */
/*  DESCRIPTION                                                           */
/*                                                                        */
/*    This function waits for the specified socket to reach the specified */
/*    TCP state.                                                          */
/*                                                                        */
/*  INPUT                                                                 */
/*                                                                        */
/*    socket_ptr                            Pointer to socket             */
/*    desired_state                         Desired TCP state             */
/*    wait_option                           Suspension option             */
/*                                                                        */
/*  OUTPUT                                                                */
/*                                                                        */
/*    status                                Completion status             */
/*                                                                        */
/*  CALLS                                                                 */
/*                                                                        */
/*    tx_thread_sleep                       Sleep to wait for state change*/
/*                                                                        */
/*  CALLED BY                                                             */
/*                                                                        */
/*    Application                                                         */
/*                                                                        */
/**************************************************************************/
UINT  _nx_tcp_socket_state_wait(NX_TCP_SOCKET *socket_ptr, UINT desired_state, ULONG wait_option)
{

    /* If trace is enabled, insert this event into the trace buffer.  */
    NX_TRACE_IN_LINE_INSERT(NX_TRACE_TCP_SOCKET_STATE_WAIT, socket_ptr -> nx_tcp_socket_ip_ptr, socket_ptr, desired_state, socket_ptr -> nx_tcp_socket_state, NX_TRACE_TCP_EVENTS, 0, 0);

    /* Loop to wait for the desired socket state.   */
    for (;;)
    {

        /* Determine if the socket pointer is still valid.  */
        if (socket_ptr -> nx_tcp_socket_id != NX_TCP_ID)
        {

            /* Not still valid, return an error.  */
            return(NX_PTR_ERROR);
        }

        /* Determine if the desired state is present.  */
        if (socket_ptr -> nx_tcp_socket_state == desired_state)
        {

#ifdef NX_ENABLE_HTTP_PROXY
            if ((desired_state != NX_TCP_ESTABLISHED) ||
                (!socket_ptr -> nx_tcp_socket_ip_ptr -> nx_ip_http_proxy_enable) ||
                (!socket_ptr -> nx_tcp_socket_client_type) ||
                (socket_ptr -> nx_tcp_socket_http_proxy_state == NX_HTTP_PROXY_STATE_CONNECTED))
#endif /* NX_ENABLE_HTTP_PROXY */
            {
                /* The desired state is present, return success!  */
                return(NX_SUCCESS);
            }
        }

        /* Check to see if there is more time to wait.  */
        if (wait_option)
        {

            /* Yes, there is more time... sleep for a tick.  */
            tx_thread_sleep(1);

            /* Decrease the wait time.  */
            wait_option--;
        }
        else
        {

            /* Timeout, just return an error.  */
            return(NX_NOT_SUCCESSFUL);
        }
    }
}