当前位置:网站首页>Gossip about redis source code 81
Gossip about redis source code 81
2022-07-03 23:24:00 【Tao song remains the same】
fd_set It is a very important structure in network programming ,rfds Is read description set , wfds Is to write a description set , This is the use needs to be very clear , Look at the last select, Know how it is :
#include <sys/select.h>
#include <string.h>
typedef struct aeApiState {
fd_set rfds, wfds;
/* We need to have a copy of the fd sets as it's not safe to reuse
* FD sets after select(). */
fd_set _rfds, _wfds;
} aeApiState;
static int aeApiCreate(aeEventLoop *eventLoop) {
aeApiState *state = zmalloc(sizeof(aeApiState));
if (!state) return -1;
FD_ZERO(&state->rfds);
FD_ZERO(&state->wfds);
eventLoop->apidata = state;
return 0;
}
static int aeApiResize(aeEventLoop *eventLoop, int setsize) {
/* Just ensure we have enough room in the fd_set type. */
if (setsize >= FD_SETSIZE) return -1;
return 0;
}
static void aeApiFree(aeEventLoop *eventLoop) {
zfree(eventLoop->apidata);
}
static int aeApiAddEvent(aeEventLoop *eventLoop, int fd, int mask) {
aeApiState *state = eventLoop->apidata;
if (mask & AE_READABLE) FD_SET(fd,&state->rfds);
if (mask & AE_WRITABLE) FD_SET(fd,&state->wfds);
return 0;
}
static void aeApiDelEvent(aeEventLoop *eventLoop, int fd, int mask) {
aeApiState *state = eventLoop->apidata;
if (mask & AE_READABLE) FD_CLR(fd,&state->rfds);
if (mask & AE_WRITABLE) FD_CLR(fd,&state->wfds);
}
static int aeApiPoll(aeEventLoop *eventLoop, struct timeval *tvp) {
aeApiState *state = eventLoop->apidata;
int retval, j, numevents = 0;
memcpy(&state->_rfds,&state->rfds,sizeof(fd_set));
memcpy(&state->_wfds,&state->wfds,sizeof(fd_set));
retval = select(eventLoop->maxfd+1,
&state->_rfds,&state->_wfds,NULL,tvp);
if (retval > 0) {
for (j = 0; j <= eventLoop->maxfd; j++) {
int mask = 0;
aeFileEvent *fe = &eventLoop->events[j];
if (fe->mask == AE_NONE) continue;
if (fe->mask & AE_READABLE && FD_ISSET(j,&state->_rfds))
mask |= AE_READABLE;
if (fe->mask & AE_WRITABLE && FD_ISSET(j,&state->_wfds))
mask |= AE_WRITABLE;
eventLoop->fired[numevents].fd = j;
eventLoop->fired[numevents].mask = mask;
numevents++;
}
}
return numevents;
}
static char *aeApiName(void) {
return "select";
}
边栏推荐
- Opengauss database log management guide
- Pyqt5 sensitive word detection tool production, operator's Gospel
- Loop compensation - explanation and calculation of first-order, second-order and op amp compensation
- [network security] what is emergency response? What indicators should you pay attention to in emergency response?
- [15th issue] Tencent PCG background development internship I, II and III (OC)
- Creation of the template of the password management software keepassdx
- Sort merge sort
- What are the securities companies with the lowest Commission for stock account opening? Would you recommend it? Is it safe to open an account on your mobile phone
- [Happy Valentine's day] "I still like you very much, like sin ² a+cos ² A consistent "(white code in the attached table)
- "Learning notes" recursive & recursive
猜你喜欢
[Happy Valentine's day] "I still like you very much, like sin ² a+cos ² A consistent "(white code in the attached table)
2022 t elevator repair registration examination and the latest analysis of T elevator repair
Take you to master the formatter of visual studio code
Ningde times and BYD have refuted rumors one after another. Why does someone always want to harm domestic brands?
Unity shader visualizer shader graph
How to make icons easily
Qtoolbutton - menu and popup mode
Opengauss database log management guide
Hcip day 12 notes
leetcode-43. String multiplication
随机推荐
Shell script three swordsman awk
Sword finger offer day 4 (Sword finger offer 03. duplicate numbers in the array, sword finger offer 53 - I. find the number I in the sorted array, and the missing numbers in sword finger offer 53 - ii
[note] glide process and source code analysis
Exclusive download! Alibaba cloud native brings 10 + technical experts to bring "new possibilities of cloud native and cloud future"
Fudan 961 review
Enter MySQL in docker container by command under Linux
D24:divisor and multiple (divisor and multiple, translation + solution)
Interesting 10 CMD commands
finalize finalization finally final
SDMU OJ#P19. Stock trading
Gossip about redis source code 73
Day30-t540-2022-02-14-don't answer by yourself
Summary of fluent systemchrome
Runtime. getRuntime(). totalMemory/maxMemory()
Amway by head has this project management tool to improve productivity in a straight line
Learning notes of raspberry pie 4B - IO communication (SPI)
Blue Bridge Cup -- Mason prime
EPF: a fuzzy testing framework for network protocols based on evolution, protocol awareness and coverage guidance
How to prevent malicious crawling of information by one-to-one live broadcast source server
Arc135 partial solution