Skip to content

Commit

Permalink
beautify event code
Browse files Browse the repository at this point in the history
  • Loading branch information
guangqianpeng committed Mar 2, 2018
1 parent ed17f68 commit d839dde
Show file tree
Hide file tree
Showing 6 changed files with 19 additions and 16 deletions.
1 change: 0 additions & 1 deletion event/connection.c
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ int conn_pool_init(mem_pool *p, int size)
return FCY_OK;
}

/* 从空闲链表中取出一个连接 */
connection *conn_get()
{
list_node *head;
Expand Down
9 changes: 6 additions & 3 deletions event/connection.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
//
// Created by frank on 17-2-12.
// TCP connection pool
// the size of pool is determined by worker connection configuration
//

#ifndef FANCY_CONN_POOL_H
Expand All @@ -12,26 +14,27 @@
typedef struct connection connection;
typedef struct connection peer_connection;

/* call conn_get to get a connection */
struct connection {

int sockfd;
event read;
event write;

peer_connection *peer; // 上游/下游连接
peer_connection *peer; // user or upstream connection

void *app; // request, upstream
int app_count;

struct sockaddr_in addr;
struct sockaddr_in addr; // peer address

list_node node;
};

int conn_pool_init(mem_pool *p, int size);

/* conn_get is the only way to get a connection */
connection *conn_get();

void conn_free(connection *conn);
char *conn_str(connection *conn);

Expand Down
6 changes: 3 additions & 3 deletions event/event.c
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ int event_process(timer_msec timeout)
return FCY_ERROR;
}
else if (n_ev == 0) {
/* 超时 */
/* timeout */
return 0;
}

Expand All @@ -60,7 +60,7 @@ int event_process(timer_msec timeout)
events = e_event->events;
conn = e_event->data.ptr;

/* 检测到错误或者对端关闭连接,交给read_handler或者write_handler解决 */
/* error detected, throw it to read_handler or write_handler */
if (events & (EPOLLERR | EPOLLRDHUP)) {
events |= EPOLLIN | EPOLLOUT ;
}
Expand All @@ -72,7 +72,7 @@ int event_process(timer_msec timeout)

wevent = &conn->write;
if (wevent->active && (events & EPOLLOUT)) {
// 忽略过期事件
// ignore timeout event
if (conn->sockfd == -1) {
continue;
}
Expand Down
6 changes: 3 additions & 3 deletions event/event.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ typedef void (*event_handler)(event *);

struct event {

unsigned active:1; // 是否在epoll_wait中
unsigned timer_set:1; // 是否在定时器中
unsigned timeout:1; // 是否为超时事件
unsigned active:1; // is in epoll_wait ?
unsigned timer_set:1; // is in timer ?
unsigned timeout:1; // is timeout ?

rbtree_node rb_node;

Expand Down
7 changes: 4 additions & 3 deletions event/timer.c
Original file line number Diff line number Diff line change
Expand Up @@ -84,18 +84,19 @@ static timer_msec timer_recent()
rbtree_node *recent;
timer_msec current;

/* 计时器为空, 表示不会有任何事件发生 */
/* no event is waiting */
if (rbtree_empty(&timer)) {
return TIMER_INFINITE;
}

recent = rbtree_min(&timer);
current = current_msec();
if (current < recent->key) { /* 没有事件发生,返回最近事件的时间差值 */
if (current < recent->key) {
/* event will be timeout in the future */
return recent->key - current;
}

/* 有事件发生 */
/* event already timeout */
return 0;
}

Expand Down
6 changes: 3 additions & 3 deletions http/http.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,14 @@
#include "request.h"
#include "upstream.h"

/* 通用的handler */
/* generic handler */
static void accept_h(event *);
static void read_request_headers_h(event *);
static void parse_request_h(event *);
static void read_request_body(event *);
static void process_request_h(event *);

/* 专门处理动态内容的 handler */
/* dynamic content handler */
static void peer_connect_h(event *);
static void upstream_write_request_h(event *);
static void upstream_read_response_header_h(event *);
Expand All @@ -29,7 +29,7 @@ static void write_response_all_h(event *);
static void write_response_headers_h(event *);
static void send_file_h(event *);

/* 表示一个请求处理完成,可能关闭连接,也可能keep_alive */
/* a request is finished, connection can be closed or keep alive */
static void finalize_request_h(event *);

static void response_and_close(connection *conn, int status_code);
Expand Down

0 comments on commit d839dde

Please sign in to comment.