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

📄 net_bsd.c

📁 ucos+lwip用于AT91SAM7X256
💻 C
📖 第 1 页 / 共 3 页
字号:
*                                              listen()
*
* Description : Set socket to listen for connection requests.
*
* Argument(s) : sock_id         Socket descriptor/handle identifier of socket to listen.
*
*               sock_q_size     Number of connection requests to queue on listen socket.
*
* Return(s)   :  0, if NO errors.
*
*               -1, otherwise.
*
* Caller(s)   : Application.
*
* Note(s)     : none.
*********************************************************************************************************
*/

#ifdef  NET_SOCK_MODULE_PRESENT
#if    (NET_SOCK_CFG_TYPE_STREAM_EN == DEF_ENABLED)
int  listen (int  sock_id,
             int  sock_q_size)
{
    NET_ERR  err;
    int      rtn_code;


    rtn_code = (int)NetSock_Listen((NET_SOCK_ID    ) sock_id,
                                   (NET_SOCK_Q_SIZE) sock_q_size,
                                   (NET_ERR       *)&err);

    return (rtn_code);
}
#endif
#endif


/*$PAGE*/
/*
*********************************************************************************************************
*                                              accept()
*
* Description : Get a new socket accepted from a socket set to listen for connection requests.
*
* Argument(s) : sock_id         Socket descriptor/handle identifier of listen socket.
*
*               paddr_remote    Pointer to an address buffer that will receive the socket address structure
*                                   of the accepted socket's remote address (see Note #1), if NO errors.
*
*               paddr_len       Pointer to a variable to ... :
*
*                                   (a) Pass the size of the address buffer pointed to by 'paddr_remote'.
*                                   (b) (1) Return the actual size of socket address structure with the 
*                                               accepted socket's remote address, if NO errors;
*                                       (2) Return 0,                             otherwise.
*
* Return(s)   : Socket descriptor/handle identifier of new accepted socket, if NO errors.
*
*               -1,                                                         otherwise.
*
* Caller(s)   : Application.
*
* Note(s)     : (1) (a) Socket address structure 'Family' member returned in host-order & SHOULD NOT be
*                       converted to network-order.
*
*                   (b) Socket address structure addresses MUST be converted from host-order to network-
*                       order.
*
*                   See also 'net_sock.h  NETWORK SOCKET ADDRESS DATA TYPES  Note #2'.
*********************************************************************************************************
*/

#ifdef  NET_SOCK_MODULE_PRESENT
#if    (NET_SOCK_CFG_TYPE_STREAM_EN == DEF_ENABLED)
int  accept (        int        sock_id,
             struct  sockaddr  *paddr_remote,
                     int       *paddr_len)
{
    int                rtn_code;
    NET_SOCK_ADDR_LEN  addr_len;
    NET_ERR            err;


    addr_len  = (NET_SOCK_ADDR_LEN)*paddr_len;
    rtn_code  = (int)NetSock_Accept((NET_SOCK_ID        ) sock_id,
                                    (NET_SOCK_ADDR     *) paddr_remote,
                                    (NET_SOCK_ADDR_LEN *)&addr_len,
                                    (NET_ERR           *)&err);

   *paddr_len = (int)addr_len;

    return (rtn_code);
}
#endif
#endif


/*$PAGE*/
/*
*********************************************************************************************************
*                                             recvfrom()
*
* Description : Receive data from a socket.
*
* Argument(s) : sock_id         Socket descriptor/handle identifier of socket to receive data.
*
*               pdata_buf       Pointer to a data buffer that will receive the socket's received data.
*
*               data_buf_len    Size of the  data buffer (in octets) [see Note #1].
*
*               flags           Flags to select receive options (see Note #2); bit-field flags logically OR'd :
*
*                                   0                           No socket flags selected.
*                                   MSG_PEEK                    Receive socket data without consuming
*                                                                   the socket data.
*                                   MSG_DONTWAIT                Receive socket data without blocking.
*
*               paddr_remote    Pointer to an address buffer that will receive the socket address structure 
*                                   with the received data's remote address, if NO errors (see Note #3).
*
*               paddr_len       Pointer to a variable to ... :
*
*                                   (a) Pass the size of the address buffer pointed to by 'paddr_remote'.
*                                   (b) (1) Return the actual size of socket address structure with the 
*                                               received data's remote address, if NO errors;
*                                       (2) Return 0,                           otherwise.
*
* Return(s)   : Number of data octets received, if NO errors.
*
*               -1,                             otherwise.
*
* Caller(s)   : Application.
*
* Note(s)     : (1) (a) (1) Datagram-type sockets transmit & receive all data atomically -- i.e. every single, 
*                           complete datagram transmitted MUST be received as a single, complete datagram.
*
*                       (2) Thus, if the socket's type is datagram & the receive data buffer size is NOT 
*                           large enough for the received data, the receive data buffer is maximally filled 
*                           with receive data but the remaining data octets are silently discarded & NO
*                           error is returned.
*
*                   (b) (1) Stream-type sockets receive all data octets in one or more non-distinct packets.
*                           In other words, the application data is NOT bounded by any specific packet(s); 
*                           rather, it is contiguous & sequenced from one packet to the next.
*                   
*                       (2) It is REQUIRED that only a single application task may receive or request to 
*                           receive application data from a stream-type socket.
*
*                           However, since a socket is typically accessed by only a single application task,
*                           this restriction is not prohibitive.
*
*               (2) Only some recvfrom() flag options are implemented.  If other flag options are requested,
*                   recvfrom() returns an error so that flag options are NOT silently ignored.
*
*               (3) (a) Socket address structure 'Family' member returned in host-order & SHOULD NOT be
*                       converted to network-order.
*
*                   (b) Socket address structure addresses MUST be converted from host-order to network-
*                       order.
*
*                   See also 'net_sock.h  NETWORK SOCKET ADDRESS DATA TYPES  Note #2'.
*********************************************************************************************************
*/
/*$PAGE*/
#ifdef  NET_SOCK_MODULE_PRESENT
int  recvfrom (        int        sock_id,
                       void      *pdata_buf,
                       int        data_buf_len,
                       int        flags,
               struct  sockaddr  *paddr_remote,
                       int       *paddr_len)
{
    int                rtn_code;
    NET_SOCK_ADDR_LEN  addr_len;
    NET_ERR            err;


    addr_len  = (NET_SOCK_ADDR_LEN)*paddr_len;
    rtn_code  = (int)NetSock_RxDataFrom((NET_SOCK_ID        ) sock_id,
                                        (void              *) pdata_buf,
                                        (CPU_INT16S         ) data_buf_len,
                                        (CPU_INT16S         ) flags,
                                        (NET_SOCK_ADDR     *) paddr_remote,
                                        (NET_SOCK_ADDR_LEN *)&addr_len,
                                        (void              *) 0,
                                        (CPU_INT08U         ) 0,
                                        (CPU_INT08U        *) 0,
                                        (NET_ERR           *)&err);

   *paddr_len = (int)addr_len;

    return (rtn_code);
}
#endif


/*$PAGE*/
/*
*********************************************************************************************************
*                                               recv()
*
* Description : Receive data from a socket.
*
* Argument(s) : sock_id         Socket descriptor/handle identifier of socket to receive data.
*
*               pdata_buf       Pointer to a data buffer that will receive the socket's received data.
*
*               data_buf_len    Size of the  data buffer (in octets) [see Note #1].
*
*               flags           Flags to select receive options (see Note #2); bit-field flags logically OR'd :
*
*                                   0                           No socket flags selected.
*                                   MSG_PEEK                    Receive socket data without consuming
*                                                                   the socket data.
*                                   MSG_DONTWAIT                Receive socket data without blocking.
*
* Return(s)   : Number of data octets received, if NO errors.
*
*               -1,                             otherwise.
*
* Caller(s)   : Application.
*
* Note(s)     : (1) (a) (1) Datagram-type sockets transmit & receive all data atomically -- i.e. every single, 
*                           complete datagram transmitted MUST be received as a single, complete datagram.
*
*                       (2) Thus, if the socket's type is datagram & the receive data buffer size is NOT 
*                           large enough for the received data, the receive data buffer is maximally filled 
*                           with receive data but the remaining data octets are silently discarded & NO
*                           error is returned.
*
*                   (b) (1) Stream-type sockets receive all data octets in one or more non-distinct packets.
*                           In other words, the application data is NOT bounded by any specific packet(s); 
*                           rather, it is contiguous & sequenced from one packet to the next.
*                   
*                       (2) It is REQUIRED that only a single application task may receive or request to 
*                           receive application data from a stream-type socket.
*
*                           However, since a socket is typically accessed by only a single application task,
*                           this restriction is not prohibitive.
*
*               (2) Only some recv() flag options are implemented.  If other flag options are requested,
*                   recv() returns an error so that flag options are NOT silently ignored.
*********************************************************************************************************
*/

#ifdef  NET_SOCK_MODULE_PRESENT
int  recv (int    sock_id,
           void  *pdata_buf,
           int    data_buf_len,
           int    flags)
{
    int      rtn_code;
    NET_ERR  err;


    rtn_code = (int)NetSock_RxData((NET_SOCK_ID  ) sock_id,
                                   (void        *) pdata_buf,
                                   (CPU_INT16S   ) data_buf_len,
                                   (CPU_INT16S   ) flags,
                                   (NET_ERR     *)&err);

    return (rtn_code);
}
#endif


/*$PAGE*/
/*
*********************************************************************************************************
*                                              sendto()
*
* Description : Send data through a socket.
*
* Argument(s) : sock_id         Socket descriptor/handle identifier of socket to send data.
*
*               p_data          Pointer to application/socket data to send.
*
*               data_len        Length of  application/socket data (in octets) [see Note #1].
*
*               flags           Flags to select send options (see Note #2); bit-field flags logically OR'd :
*
*                                   0                           No socket flags selected.
*                                   MSG_DONTWAIT                Send socket data without blocking.
*
*               paddr_remote    Pointer to destination address buffer (see Note #3).
*
*               addr_len        Length of  destination address buffer (in octets).
*
* Return(s)   : Number of data octets sent, if NO errors.

⌨️ 快捷键说明

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