1、web socket请求数据从服务器回来时,服务器会关闭连接,此时AT socket在读取返回的数据时不应该判断socket为连接的状态

2、sal socket在支持多网卡时,sal_getaddrinfo从A网卡获取的地址信息,在sal_freeaddrinfo释放时必须由A网卡进行释放
This commit is contained in:
longtengmcu 2020-11-22 12:36:07 +08:00
parent 9dc0bbb814
commit aac5e9da4c
2 changed files with 68 additions and 31 deletions

View File

@ -821,24 +821,17 @@ int at_recvfrom(int socket, void *mem, size_t len, int flags, struct sockaddr *f
goto __exit;
}
else
{
if (sock->state == AT_SOCKET_CONNECT)
{
/* get receive buffer to receiver ring buffer */
rt_mutex_take(sock->recv_lock, RT_WAITING_FOREVER);
recv_len = at_recvpkt_get(&(sock->recvpkt_list), (char *) mem, len);
rt_mutex_release(sock->recv_lock);
if (recv_len > 0)
{
/* get receive buffer to receiver ring buffer */
rt_mutex_take(sock->recv_lock, RT_WAITING_FOREVER);
recv_len = at_recvpkt_get(&(sock->recvpkt_list), (char *) mem, len);
rt_mutex_release(sock->recv_lock);
if (recv_len > 0)
{
break;
}
}
else
{
LOG_D("received data exit, current socket (%d) is closed by remote.", socket);
result = 0;
goto __exit;
break;
}
}
}

View File

@ -41,6 +41,13 @@ struct sal_socket_table
struct sal_socket **sockets;
};
/* record the netdev and res table*/
struct sal_netdev_res_table
{
struct addrinfo *res;
struct netdev *netdev;
};
#ifdef SAL_USING_TLS
/* The global TLS protocol options */
static struct sal_proto_tls *proto_tls;
@ -50,6 +57,7 @@ static struct sal_proto_tls *proto_tls;
static struct sal_socket_table socket_table;
static struct rt_mutex sal_core_lock;
static rt_bool_t init_ok = RT_FALSE;
static struct sal_netdev_res_table sal_dev_res_tbl[SAL_SOCKETS_NUM];
#define IS_SOCKET_PROTO_TLS(sock) (((sock)->protocol == PROTOCOL_TLS) || \
((sock)->protocol == PROTOCOL_DTLS))
@ -90,6 +98,11 @@ do {
((pf) = (struct sal_proto_family *) (netdev)->sal_user_data) != RT_NULL && \
(pf)->netdb_ops->ops) \
#define SAL_NETDBOPS_VALID(netdev, pf, ops) \
((netdev) && \
((pf) = (struct sal_proto_family *) (netdev)->sal_user_data) != RT_NULL && \
(pf)->netdb_ops->ops) \
/**
* SAL (Socket Abstraction Layer) initialize.
*
@ -116,6 +129,9 @@ int sal_init(void)
return -1;
}
/*init the dev_res table */
rt_memset(sal_dev_res_tbl, 0, sizeof(sal_dev_res_tbl));
/* create sal socket lock */
rt_mutex_init(&sal_core_lock, "sal_lock", RT_IPC_FLAG_FIFO);
@ -1086,10 +1102,12 @@ int sal_getaddrinfo(const char *nodename,
{
struct netdev *netdev = netdev_default;
struct sal_proto_family *pf;
int ret = 0;
rt_uint32_t i = 0;
if (SAL_NETDEV_NETDBOPS_VALID(netdev, pf, getaddrinfo))
{
return pf->netdb_ops->getaddrinfo(nodename, servname, hints, res);
ret = pf->netdb_ops->getaddrinfo(nodename, servname, hints, res);
}
else
{
@ -1097,30 +1115,56 @@ int sal_getaddrinfo(const char *nodename,
netdev = netdev_get_first_by_flags(NETDEV_FLAG_UP);
if (SAL_NETDEV_NETDBOPS_VALID(netdev, pf, getaddrinfo))
{
return pf->netdb_ops->getaddrinfo(nodename, servname, hints, res);
ret = pf->netdb_ops->getaddrinfo(nodename, servname, hints, res);
}
else
{
ret = -1;
}
}
return -1;
if(ret == RT_EOK)
{
/*record the netdev and res*/
for(i = 0; i < SAL_SOCKETS_NUM; i++)
{
if(sal_dev_res_tbl[i].res == RT_NULL)
{
sal_dev_res_tbl[i].res = *res;
sal_dev_res_tbl[i].netdev = netdev;
break;
}
}
RT_ASSERT((i < SAL_SOCKETS_NUM));
}
return ret;
}
void sal_freeaddrinfo(struct addrinfo *ai)
{
struct netdev *netdev = netdev_default;
struct sal_proto_family *pf;
struct netdev *netdev = RT_NULL;
struct sal_proto_family *pf = RT_NULL;
rt_uint32_t i = 0;
if (SAL_NETDEV_NETDBOPS_VALID(netdev, pf, freeaddrinfo))
/*when use the multi netdev, it must free the ai use the getaddrinfo netdev */
for(i = 0; i < SAL_SOCKETS_NUM; i++)
{
if(sal_dev_res_tbl[i].res == ai)
{
netdev = sal_dev_res_tbl[i].netdev;
sal_dev_res_tbl[i].res = RT_NULL;
sal_dev_res_tbl[i].netdev = RT_NULL;
break;
}
}
RT_ASSERT((i < SAL_SOCKETS_NUM));
if (SAL_NETDBOPS_VALID(netdev, pf, freeaddrinfo))
{
pf->netdb_ops->freeaddrinfo(ai);
}
else
{
/* get the first network interface device with up status */
netdev = netdev_get_first_by_flags(NETDEV_FLAG_UP);
if (SAL_NETDEV_NETDBOPS_VALID(netdev, pf, freeaddrinfo))
{
pf->netdb_ops->freeaddrinfo(ai);
}
}
}