1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274
| ##include <stdio.h> ##include <errno.h> ##include <unistd.h> ##include <string.h> ##include <stdlib.h> ##include <sys/socket.h> ##include <netinet/in.h>
##include <fcntl.h> ##include <pthread.h> ##include <sys/poll.h> ##include <sys/epoll.h>
##define BUFFER_LENGTH 1024 ##define EPOLL_EVENT_LENGTH 1024
typedef int (*ZVCALLBACK)(int fd, int events, void *arg);
typedef struct zv_connect_s{ int fd;
ZVCALLBACK cb; char rbuffer[BUFFER_LENGTH]; int rc; int count; char wbuffer[BUFFER_LENGTH]; int wc; }zv_connect_t;
typedef struct zv_connblock_s { zv_connect_t * block; struct zv_connblock_s *next; }zv_connblock_t;
typedef struct zv_reactor_s{ int epfd; int blkcnt; zv_connblock_t *blockheader; }zv_reactor_t;
int zv_init_reactor(zv_reactor_t *reactor){ if(!reactor) return -1;
##if 0 reactor -> blockheader = malloc(sizeof(zv_connblock_t)); if(reactor->blockheader == NULL) return -1;
reactor -> blockheader -> block = calloc(1024, sizeof(zv_connect_t)); if(reactor->blockheader->block == NULL) return -1; ##else reactor->blockheader = malloc (sizeof(zv_connblock_t) + EPOLL_EVENT_LENGTH * sizeof(zv_connect_t)); if(reactor->blockheader == NULL) return -1; reactor->blockheader->block = (zv_connect_t*)(reactor->blockheader + 1); reactor->blkcnt = 1; reactor->blockheader->next = NULL; ##endif
reactor->epfd = epoll_create(1);
}
void zv_dest_reactor(zv_reactor_t *reactor){ if(!reactor) return;
if(!reactor->blockheader) free(reactor->blockheader);
close(reactor->epfd); }
int zv_connect_block(zv_reactor_t *reactor){ if(!reactor) return -1; zv_connblock_t *blk = reactor->blockheader; while(blk->next != NULL) blk = blk->next;
zv_connblock_t *connblock = malloc (sizeof(zv_connblock_t) + EPOLL_EVENT_LENGTH * sizeof(zv_connect_t)); if(connblock == NULL) return -1; connblock->block = (zv_connect_t*)(reactor->blockheader + 1); connblock->next = NULL;
blk->next = connblock;
reactor->blkcnt++;
return 0; }
zv_connect_t* zv_connect_idx(zv_reactor_t *reactor, int fd){ if(!reactor) return NULL;
int blkidx = fd / EPOLL_EVENT_LENGTH;
while(blkidx >= reactor->blkcnt){ int ret = zv_connect_block(reactor); if(ret != 0) return NULL; }
int i = 0; zv_connblock_t* blk = reactor->blockheader; while(i++ < blkidx){ blk = blk->next; } return blk->block +(fd % EPOLL_EVENT_LENGTH); }
int init_server(short port) {
int sockfd = socket(AF_INET, SOCK_STREAM, 0);
struct sockaddr_in servaddr; memset(&servaddr, 0, sizeof(struct sockaddr_in)); servaddr.sin_family = AF_INET; servaddr.sin_addr.s_addr = htonl(INADDR_ANY); servaddr.sin_port = htons(port);
if(-1 == bind(sockfd, (struct sockaddr*)&servaddr, sizeof(struct sockaddr))){ printf("bind failed: %s", strerror(errno)); return -1; } listen(sockfd, -1);
return sockfd; }
int recv_cb(int fd, int events, void *arg);
int send_cb(int fd, int events, void *arg){ printf("send_cb \n "); zv_reactor_t * reactor = (zv_reactor_t *)arg; if(!reactor || !reactor->blockheader) return -1;
zv_connect_t *conn = zv_connect_idx(reactor, fd); send(fd, conn->wbuffer, conn->wc, 0); conn->cb = recv_cb; struct epoll_event ev; ev.events = EPOLLIN; ev.data.fd = fd; epoll_ctl(reactor->epfd, EPOLL_CTL_MOD, fd, &ev); }
int recv_cb(int fd, int events, void *arg){ printf("recv_cb \n "); zv_reactor_t * reactor = (zv_reactor_t *)arg; if(!reactor || !reactor->blockheader) return -1;
zv_connect_t* conn = zv_connect_idx(reactor, fd); int ret = recv(fd, conn->rbuffer+conn->rc, conn->count, 0); if(ret < 0){ }else if(ret == 0){ conn->fd = -1; conn->rc = 0; conn->wc = 0;
epoll_ctl(reactor->epfd, EPOLL_CTL_DEL, fd, NULL); close(fd); } conn->rc += ret; conn->count = 20;
memcpy(conn->wbuffer, conn->rbuffer, conn->rc); conn->wc = conn->rc;
conn->cb = send_cb; struct epoll_event ev; ev.events = EPOLLOUT; ev.data.fd = fd; epoll_ctl(reactor->epfd, EPOLL_CTL_MOD, fd, &ev); }
int accept_cb(int fd, int events, void *arg) {
printf("accept_cb \n ");
struct sockaddr_in clientaddr; socklen_t len = sizeof(clientaddr); int clientfd = accept(fd, (struct sockaddr*)&clientaddr, &len);
if(clientfd < 0) { printf("accept err ,%d \n",errno); return -1; }
zv_reactor_t * reactor = (zv_reactor_t *)arg; if(!reactor || !reactor->blockheader) return -1;
zv_connect_t* conn = zv_connect_idx(reactor, clientfd); conn->fd = clientfd; conn->cb = recv_cb; conn->count = 2;
printf("client fd : %d", clientfd); struct epoll_event ev; ev.events = EPOLLIN; ev.data.fd = clientfd; epoll_ctl(reactor->epfd, EPOLL_CTL_ADD, clientfd, &ev); }
int set_listener(zv_reactor_t *reactor, int fd, ZVCALLBACK cb){ if(!reactor || !reactor->blockheader) return -1; reactor->blockheader->block[fd].fd = fd; reactor->blockheader->block[fd].cb = cb;
struct epoll_event ev; ev.events = EPOLLIN; ev.data.fd = fd;
epoll_ctl(reactor->epfd, EPOLL_CTL_ADD, fd, &ev); }
int main(int argc, char* argv[]) { if(argc < 2) return -1;
int port = atoi(argv[1]); int sockfd = init_server(port); zv_reactor_t reactor; zv_init_reactor(&reactor);
set_listener(&reactor,sockfd, accept_cb);
struct epoll_event events[EPOLL_EVENT_LENGTH] = {0}; while(1){ int nready = epoll_wait(reactor.epfd, events, EPOLL_EVENT_LENGTH, -1); if(nready < 0) continue;
int i = 0;
for(i = 0;i < nready;i++){ int connfd = events[i].data.fd; zv_connect_t *conn = zv_connect_idx(&reactor, connfd);
if(events[i].events & EPOLLIN){ conn->cb(connfd, events[i].events, &reactor); } if(events[i].events & EPOLLOUT){ conn->cb(connfd, events[i].events, &reactor); } } }
}
|