当前位置:网站首页>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";
}
边栏推荐
- How to write a good title of 10w+?
- Pointer concept & character pointer & pointer array yyds dry inventory
- Exclusive download! Alibaba cloud native brings 10 + technical experts to bring "new possibilities of cloud native and cloud future"
- 炒股开户佣金优惠怎么才能获得,网上开户安全吗
- Pyqt5 sensitive word detection tool production, operator's Gospel
- How to prevent malicious crawling of information by one-to-one live broadcast source server
- Get current JVM data
- "Learning notes" recursive & recursive
- Shiftvit uses the precision of swing transformer to outperform the speed of RESNET, and discusses that the success of Vit does not lie in attention!
- Alibaba cloud container service differentiation SLO hybrid technology practice
猜你喜欢

The difference between single power amplifier and dual power amplifier
![[Android reverse] use the DB browser to view and modify the SQLite database (copy the database file from the Android application data directory | use the DB browser tool to view the data block file)](/img/6e/3ffa91154a718b6ace6c8ca87c5995.jpg)
[Android reverse] use the DB browser to view and modify the SQLite database (copy the database file from the Android application data directory | use the DB browser tool to view the data block file)

Ningde times and BYD have refuted rumors one after another. Why does someone always want to harm domestic brands?

SDMU OJ#P19. Stock trading

How to write a good title of 10w+?

Ningde times and BYD have refuted rumors one after another. Why does someone always want to harm domestic brands?

Analysis of refrigeration and air conditioning equipment operation in 2022 and examination question bank of refrigeration and air conditioning equipment operation

Shiftvit uses the precision of swing transformer to outperform the speed of RESNET, and discusses that the success of Vit does not lie in attention!

Meta metauniverse female safety problems occur frequently, how to solve the relevant problems in the metauniverse?

Pyqt5 sensitive word detection tool production, operator's Gospel
随机推荐
Pandaoxi's video
ADB related commands
炒股开户佣金优惠怎么才能获得,网上开户安全吗
[untitled]
MLX90614 driver, function introduction and PEC verification
Mongoose the table associated with the primary key, and automatically bring out the data of another table
在恒泰证券开户怎么样?安全吗?
[source code] VB6 chat robot
Interpretation of corolla sub low configuration, three cylinder power configuration, CVT fuel saving and smooth, safety configuration is in place
2022 Guangdong Provincial Safety Officer a certificate third batch (main person in charge) simulated examination and Guangdong Provincial Safety Officer a certificate third batch (main person in charg
pycuda._ driver. LogicError: explicit_ context_ dependent failed: invalid device context - no currently
C # basic knowledge (2)
What are the common computer problems and solutions
How to quickly build high availability of service discovery
[note] IPC traditional interprocess communication and binder interprocess communication principle
Fashion cloud interview questions series - JS high-frequency handwritten code questions
Recursion and recursion
IO flow review
C summary of knowledge point definitions, summary notes
Opengauss database log management guide