22 Star 36 Fork 15

蔡东赟/beanstalkd-win

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
linux.c 1.62 KB
一键复制 编辑 原始数据 按行查看 历史
#define _XOPEN_SOURCE 600
#include <stdint.h>
#include <fcntl.h>
#include <stdlib.h>
#include <errno.h>
#include <sys/epoll.h>
#include "dat.h"
#ifndef EPOLLRDHUP
#define EPOLLRDHUP 0x2000
#endif
static int epfd;
/* Allocate disk space.
* Expects fd's offset to be 0; may also reset fd's offset to 0.
* Returns 0 on success, and a positive errno otherwise. */
int
rawfalloc(int fd, int len)
{
return posix_fallocate(fd, 0, len);
}
int
sockinit(void)
{
epfd = epoll_create(1);
if (epfd == -1) {
twarn("epoll_create");
return -1;
}
return 0;
}
int
sockwant(Socket *s, int rw)
{
int op;
struct epoll_event ev = {};
if (!s->added && !rw) {
return 0;
} else if (!s->added && rw) {
s->added = 1;
op = EPOLL_CTL_ADD;
} else if (!rw) {
op = EPOLL_CTL_DEL;
} else {
op = EPOLL_CTL_MOD;
}
switch (rw) {
case 'r':
ev.events = EPOLLIN;
break;
case 'w':
ev.events = EPOLLOUT;
break;
}
ev.events |= EPOLLRDHUP | EPOLLPRI;
ev.data.ptr = s;
return epoll_ctl(epfd, op, s->fd, &ev);
}
int
socknext(Socket **s, int64 timeout)
{
int r;
struct epoll_event ev;
r = epoll_wait(epfd, &ev, 1, (int)(timeout/1000000));
if (r == -1 && errno != EINTR) {
twarn("epoll_wait");
exit(1);
}
if (r > 0) {
*s = ev.data.ptr;
if (ev.events & (EPOLLHUP|EPOLLRDHUP)) {
return 'h';
} else if (ev.events & EPOLLIN) {
return 'r';
} else if (ev.events & EPOLLOUT) {
return 'w';
}
}
return 0;
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
C
1
https://gitee.com/lomox/beanstalkd-win.git
git@gitee.com:lomox/beanstalkd-win.git
lomox
beanstalkd-win
beanstalkd-win
master

搜索帮助