我正在努力解决一个问题,我的Linux socket接口运行完美, 但是在osx上,我没有接收/发送任何数据,使用gdb,我发现select永远不会设置fd_set,所以我的代码永远不会读取套接字: 这里是选择代码,可能是原因:
static inline int max_fd_plusone(t_list *fds)
{
int max;
t_list *tmp;
t_selfd *fd;
max = -1;
tmp = fds;
while (tmp)
{
fd = (t_selfd*)tmp->data;
max = fd->fd > max ? fd->fd : max;
tmp = tmp->next;
}
return (max + 1);
}
static inline void set_fdset(t_list *fds, fd_set *setr, fd_set *setw)
{
t_list *tmp;
t_selfd *fd;
FD_ZERO(setr);
FD_ZERO(setw);
tmp = fds;
while (tmp)
{
fd = (t_selfd*)tmp->data;
if ((fd->checktype & FDREAD) == FDREAD)
FD_SET(fd->fd, setr);
if ((fd->checktype & FDWRITE) == FDWRITE)
FD_SET(fd->fd, setw);
tmp = tmp->next;
}
}
t_selfd *create_fd(int fd, void *data, int (*call)())
{
t_selfd *res;
if ((res = malloc(1 * sizeof(t_selfd))) == NULL)
return (NULL);
res->fd = fd;
res->etype = 0;
res->checktype = FDREAD;
res->data = data;
res->callback = call;
res->to_close = 0;
res->rbuff = create_ring_buffer(BUFSIZ * 2);
res->wbuff = create_ring_buffer(BUFSIZ * 2);
if (!res->rbuff || !res->wbuff)
{
destroy_ring_buffer(res->rbuff);
destroy_ring_buffer(res->wbuff);
free(res);
return (NULL);
}
return (res);
}
void do_select(t_list *fds, struct timeval *tv, void *global_arg)
{
fd_set setr;
fd_set setw;
t_list *tmp;
t_list *nexttmp;
t_selfd *fd;
nexttmp = NULL;
set_fdset(fds, &setr, &setw);
if ((select(max_fd_plusone(fds), &setr, &setw, NULL, tv) == -1))
return ;
tmp = fds;
nexttmp = tmp->next;
while (tmp)
{
fd = (t_selfd*)tmp->data;
fd->etype = (FD_ISSET(fd->fd, &setr)) * FDREAD
+ (FD_ISSET(fd->fd, &setw)) * FDWRITE;
fd->checktype = 0;
fd->callback(fd, global_arg);
tmp = nexttmp;
nexttmp = tmp ? tmp->next : NULL;
}
}
答案 0 :(得分:2)
这看起来很可疑:
fd->etype = (FD_ISSET(fd->fd, &setr)) * FDREAD
+ (FD_ISSET(fd->fd, &setw)) * FDWRITE;
您假设FD_ISSET返回一个特定值(最可能是1),而它只记录为返回非零或零。这在操作系统之间肯定会有所不同。
Linux在这里返回一个标准化的布尔值(0或1),而MacOS则没有。
N.B。因为您似乎在设计一些库,所以使用更现代的界面(如poll
而不是select
)可能是个好主意。如果文件描述符太多,则选择性能很差,并且可能完全失败。