⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 net_conn.c

📁 ucos的tcpip协议占
💻 C
📖 第 1 页 / 共 5 页
字号:
*                   (a) Close transport connection, if requested                            See Note #3c
*                   (b) Clear network   connection's reference to application connection    See Note #3c
*                   (c) Free  network   connection, if necessary
*
*
* Argument(s) : conn_id                 Handle identifier of network connection to close.
*
*               close_conn_transport    Indicate whether to close transport connection :
*
*                                           DEF_YES                    Close transport connection.
*                                           DEF_NO              Do NOT close transport connection.
*
* Return(s)   : none.
*
* Caller(s)   : NetSock_CloseConn(),
*               NetSock_CloseSockHandler(),
*               NetSock_ConnAcceptQ_Clr().
*
*               This function is an INTERNAL network protocol suite function & MUST NOT be called by 
*               application function(s).
*
* Note(s)     : (2) #### To prevent closing a network connection already closed via previous network
*                   connection close, NetConn_CloseFromApp() checks the connection's 'USED' flag 
*                   BEFORE closing the network connection.
*
*                   This prevention is only best-effort since any invalid duplicate network connection 
*                   closes MAY be asynchronous to potentially valid network connection gets.  Thus the 
*                   invalid network connection close(s) MAY corrupt the network connection's valid
*                   operation(s).
*
*                   However, since the primary tasks of the network protocol suite are prevented from
*                   running concurrently (see 'net.h  Note #2'), it is NOT necessary to protect network
*                   connection resources from possible corruption since no asynchronous access from
*                   other network tasks is possible.
*
*                   (a) Network connection(s) MAY already be closed AFTER other network connection 
*                       close operations & MUST be validated as used BEFORE any subsequent network 
*                       connection close operation(s).
*
*               (3) (a) Network connections are considered connected if any of the following network 
*                       connections are valid :
*
*                       (1) Application layer connection
*                       (2) Transport   layer connection
*
*                               (A) Network connections which ONLY reference application layer clone 
*                                   connection(s) are NOT considered connected since the actual non-
*                                   cloned application connection MAY or MAY NOT reference the cloned
*                                   network connection.
*
*                   (b) Since NetConn_CloseFromApp() actively closes the application layer connection,
*                       network connections need only validate the remaining transport layer connection
*                       as connected.
*
*                   (c) Since network connection(s) connection validation determines, in part, when to
*                       close the network connection (see Note #3a), & since NetConn_CloseFromTransport() 
*                       may indirectly call NetConn_CloseFromApp(); clearing the network connection's 
*                       application layer connection handle identifier MUST follow the closing of the 
*                       transport   layer connection to prevent re-closing the network connection.
*********************************************************************************************************
*/
/*$PAGE*/
void  NetConn_CloseFromApp (NET_CONN_ID  conn_id,
                            CPU_BOOLEAN  close_conn_transport)
{
#if ((NET_ERR_CFG_ARG_CHK_DBG_EN == DEF_ENABLED)                    && \
     (NET_CTR_CFG_ERR_EN         == DEF_ENABLED)                    && \
     (CPU_CFG_CRITICAL_METHOD    == CPU_CRITICAL_METHOD_STATUS_LOCAL))
    CPU_SR        cpu_sr;
#endif
    CPU_BOOLEAN   used;
    CPU_BOOLEAN   connd;
    CPU_BOOLEAN   free;
    NET_CONN     *pconn;
    NET_ERR       err;

                                                                    /* -------------- VALIDATE NET CONN --------------- */
    if (conn_id == NET_CONN_ID_NONE) {
        return;
    }

#if (NET_ERR_CFG_ARG_CHK_DBG_EN == DEF_ENABLED)
    if (conn_id < NET_CONN_ID_MIN) {
        NET_CTR_ERR_INC(NetConn_ErrInvalidConnCtr);
        return;
    }
    if (conn_id > NET_CONN_ID_MAX) {
        NET_CTR_ERR_INC(NetConn_ErrInvalidConnCtr);
        return;
    }
#endif

    pconn = &NetConn_Tbl[conn_id];

#if (NET_ERR_CFG_ARG_CHK_DBG_EN == DEF_ENABLED)
                                                                    /* ------------ VALIDATE NET CONN USED ------------ */
    used  =  DEF_BIT_IS_SET(pconn->Flags, NET_CONN_FLAG_USED);
    if (used != DEF_YES) {                                          /* If net conn NOT used, ...                        */
        NET_CTR_ERR_INC(NetConn_ErrNotUsedCtr);
        return;                                                     /* ... rtn but do NOT close (see Note #2).          */
    }
#endif


                                                                    /* ------------- CLOSE TRANSPORT CONN ------------- */
    if (close_conn_transport == DEF_YES) {                          
        NetConn_CloseTransport(pconn);                              /* Close transport conn, if req'd.                  */
        used  =  DEF_BIT_IS_SET(pconn->Flags, NET_CONN_FLAG_USED);  /* If still used (see Note #2a), free net conn ...  */
        free  = (used == DEF_YES) ? DEF_YES : DEF_NO;               /* ... since app & transport conns closed.          */

    } else {                                                        /* Else chk net conn conn'd (see Note #3b).         */
        connd = (pconn->ID_Transport != NET_CONN_ID_NONE) ? DEF_YES : DEF_NO;
        free  =  (connd != DEF_YES) ? DEF_YES : DEF_NO;             /* Free net conn, if NOT conn'd.                    */
    }

                                                                    /* ---------------- CLOSE APP CONN ---------------- */
   (void)NetConn_ID_AppSet(conn_id, NET_CONN_ID_NONE, &err);        /* Clr net conn's app conn id (see Note #3c).       */

                                                                    /* ---------------- FREE NET CONN ----------------- */
    if (free == DEF_YES) {
        NetConn_FreeHandler(pconn);                                 /* Free net conn, if req'd.                         */
    }
}


/*$PAGE*/
/*
*********************************************************************************************************
*                                    NetConn_CloseFromTransport()
*
* Description : (1) Close a network connection from transport layer :
*
*                   (a) Close application connection, if requested                          See Note #3c
*                   (b) Clear network     connection's reference to transport connection    See Note #3c
*                   (c) Free  network     connection, if necessary
*
*
* Argument(s) : conn_id             Handle identifier of network connection to close.
*
*               close_conn_app      Indicate whether to close application connection :
*
*                                       DEF_YES                        Close application connection.
*                                       DEF_NO                  Do NOT close application connection.
*
* Return(s)   : none.
*
* Caller(s)   : NetTCP_ConnClose().
*
*               This function is an INTERNAL network protocol suite function & MUST NOT be called by 
*               application function(s).
*
* Note(s)     : (2) #### To prevent closing a network connection already closed via previous network
*                   connection close, NetConn_CloseFromTransport() checks the connection's 'USED' flag 
*                   BEFORE closing the network connection.
*
*                   This prevention is only best-effort since any invalid duplicate network connection 
*                   closes MAY be asynchronous to potentially valid network connection gets.  Thus the 
*                   invalid network connection close(s) MAY corrupt the network connection's valid
*                   operation(s).
*
*                   However, since the primary tasks of the network protocol suite are prevented from
*                   running concurrently (see 'net.h  Note #2'), it is NOT necessary to protect network
*                   connection resources from possible corruption since no asynchronous access from
*                   other network tasks is possible.
*
*                   (a) Network connection(s) MAY already be closed AFTER other network connection 
*                       close operations & MUST be validated as used BEFORE any subsequent network 
*                       connection close operation(s).
*
*               (3) (a) Network connections are considered connected if any of the following network 
*                       connections are valid :
*
*                       (1) Application layer connection
*                       (2) Transport   layer connection
*
*                               (A) Network connections which ONLY reference application layer clone 
*                                   connection(s) are NOT considered connected since the actual non-
*                                   cloned application connection MAY or MAY NOT reference the cloned
*                                   network connection.
*
*                   (b) Since NetConn_CloseFromTransport() actively closes the transport layer connection,
*                       network connections need only validate the remaining application layer connection
*                       as connected.
*
*                   (c) Since network connection(s) connection validation determines, in part, when to
*                       close the network connection (see Note #3a), & since NetConn_CloseFromApp() may
*                       indirectly call NetConn_CloseFromTransport(); clearing the network connection's 
*                       transport   layer connection handle identifier MUST follow the closing of the 
*                       application layer connection to prevent re-closing the network connection.
*********************************************************************************************************
*/
/*$PAGE*/
void  NetConn_CloseFromTransport (NET_CONN_ID  conn_id,
                                  CPU_BOOLEAN  close_conn_app)
{
#if ((NET_ERR_CFG_ARG_CHK_DBG_EN == DEF_ENABLED)                    && \
     (NET_CTR_CFG_ERR_EN         == DEF_ENABLED)                    && \
     (CPU_CFG_CRITICAL_METHOD    == CPU_CRITICAL_METHOD_STATUS_LOCAL))
    CPU_SR        cpu_sr;
#endif
    CPU_BOOLEAN   used;
    CPU_BOOLEAN   connd;
    CPU_BOOLEAN   free;
    NET_CONN     *pconn;
    NET_ERR       err;

                                                                    /* -------------- VALIDATE NET CONN --------------- */
    if (conn_id == NET_CONN_ID_NONE) {
        return;
    }

#if (NET_ERR_CFG_ARG_CHK_DBG_EN == DEF_ENABLED)
    if (conn_id < NET_CONN_ID_MIN) {
        NET_CTR_ERR_INC(NetConn_ErrInvalidConnCtr);
        return;
    }
    if (conn_id > NET_CONN_ID_MAX) {
        NET_CTR_ERR_INC(NetConn_ErrInvalidConnCtr);
        return;
    }
#endif

    pconn = &NetConn_Tbl[conn_id];

#if (NET_ERR_CFG_ARG_CHK_DBG_EN == DEF_ENABLED)
                                                                    /* ------------ VALIDATE NET CONN USED ------------ */
    used  =  DEF_BIT_IS_SET(pconn->Flags, NET_CONN_FLAG_USED);
    if (used != DEF_YES) {                                          /* If net conn NOT used, ...                        */
        NET_CTR_ERR_INC(NetConn_ErrNotUsedCtr);
        return;                                                     /* ... rtn but do NOT close (see Note #2).          */
    }
#endif


                                                                    /* ---------------- CLOSE APP CONN ---------------- */
    if (close_conn_app == DEF_YES) {                          
        NetConn_CloseApp(pconn);                                    /* Close app conn, if req'd.                        */
        used  =  DEF_BIT_IS_SET(pconn->Flags, NET_CONN_FLAG_USED);  /* If still used (see Note #2a), free net conn ...  */
        free  = (used == DEF_YES) ? DEF_YES : DEF_NO;               /* ... since app & transport conns closed.          */

    } else {                                                        /* Else chk net conn conn'd (see Note #3b).         */
        connd = (pconn->ID_App != NET_CONN_ID_NONE) ? DEF_YES : DEF_NO;
        free  =  (connd != DEF_YES) ? DEF_YES : DEF_NO;             /* Free net conn, if NOT conn'd.                    */
    }

                                                                    /* ------------- CLOSE TRANSPORT CONN ------------- */
   (void)NetConn_ID_TransportSet(conn_id, NET_CONN_ID_NONE, &err);  /* Clr net conn's transport conn id (see Note #3c). */

                                                                    /* ---------------- FREE NET CONN ----------------- */
    if (free == DEF_YES) {
        NetConn_FreeHandler(pconn);                                 /* Free net conn, if req'd.                         */
    }
}


/*$PAGE*/
/*
*********************************************************************************************************
*                                       NetConn_CloseAllConns()
*
* Description : Close ALL network connections.
*
* Argument(s) : none.
*
* Return(s)   : none.
*
* Caller(s)   : Application.
*
*               This function is a network protocol suite application interface (API) function & MAY
*               be called by application function(s) [see also Note #2].
*
* Note(s)     : (1) (a) Certain circumstances may require that :
*
*                       (1) ALL network protocol suite connections close;
*                       (2) All pending network &/or application connection function(s) SHOULD :
*                           (A) Abort, immediately if possible;
*                           (B) Return appropriate closed error code(s).
*
*                   (b) The following example(s) list when to close all network connections :
*
*                       (1) RFC #2131, Section 4.4.5 states that "if the [DHCP] client is given a
*                           new network address, it MUST NOT continue using the previous network
*                           address and SHOULD notify the local users of the problem".
*
*                           Therefore, ALL network connections based on a previous network address
*                           MUST be closed.
*
*               (2) NetConn_CloseAllConns() is called by application function(s) & ... :
*
*                   (a) MUST NOT be called with the global network lock already acquired;
*                   (b) MUST block ALL other network protocol tasks by pending on & acquiring the global
*                       network lock (see 'net.h  Note #2').
*
*                   This is required since an application's network protocol suite API function access is 
*                   asynchronous to other network protocol tasks.
*
*                   See also 'NetConn_CloseAllConnsHandler()  Note #1'.
*********************************************************************************************************
*/

void  NetConn_CloseAllConns (void)
{
    NET_ERR  err;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -