您的位置:首页 > 其它

CharpServer简单分析

2004-12-20 11:11 316 查看
                                                 CharpServer 的设计与实现<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

 

目标:

1.       设计一高性能的WEBserver ,支持HTTP协议,简单的动态页面功能

2.       初始版本做为试验性的设计,用来熟悉c语言 和HttpServer构架

3.       支持多cpu的结构

 

一: 概括

       开发的主要平台在linux,内核版本为2.6以上。该server采取事件驱动调度策略。用到的技术有:Epoll,Aio,SmallLangage,Libevnet. 在MemCached的基础上进行设计改进,采用Tux的构架设计方案。

图一为整体够架图

<?xml:namespace prefix = v ns = "urn:schemas-microsoft-com:vml" />

 

 

 

二: 事件驱动机制的实现方法
所涉及到的事件列表
1.  EV_CONNREAD 事件 该事件表示已经建立的Socket连接有数据可以读取
2.  EV_HTTPPARSE  事件 该事件表示已经从Socket连接上读取完数据,需要进行数据分析
3.  EV_HTTPHANDLE 事件 该事件表示已经初步的分析完数据需要进行下一步处理
4.  EV_CACHEMISS 事件 该事件表示请求的数据在Cache中不存在,需要进行下一步处理
5.  EV_AIOWRITE事件,该事件表示可以往硬盘上写数据
6.  EV_AIOREAD 事件, 该事件表示可
4000
以从硬盘上读取数据
7.  EV_CONNWRITE  事件,该事件表示服务器端已经处理完毕,可以往客户端发送数据
 

各个队列之间的关系和作用
主线成负责轮训的检查Epoll Event Queue, Event Queue,Aio Active Queue 三个队列。主线程利用Epoll的Event_Wait函数来监视已经建立连接的事件发生情况。具体代码可以参见LibEvent的Epoll.c中的Epoll_Dispach函数。该函数应该进行更改,每处理一个event,必须检查其它Queue中是否有数据,并进行相应的处理。ServerThread负责把Epoll事件转化为Event事件并把它加入到EventQueue中
主线程检查Epoll Event Queue 处理完一个Event后,接下来检查Event Queue中是否有任务如果有,则从队列中得到一个任务并进行处理。
检查完Event Queue后, 接着检查Aio ActiveQueue检查其中是否有读写磁盘的事件如果有则从AIO事件队列中得到一个Io事件,并把该事件转化为Event的事件加入到EventQueue中等待下一步处理。
Aio的事件驱动机制,可以参见
 

执行完一上三个步骤是一个轮训策略,直到EpollEventQueue,EventQueue,AioQueue
中没有可以处理的任务。则主线程Block.监听是否有新的连接到达。
相关连接:
    http://developer.osdl.org/daniel/AIO/
http://lse.sourceforge.net/io/aio.html
http://www.monkey.org/~provos/libevent
http://www.danga.com/memcached/
 

 

 

 

 

 

 

 

 

 

 

 

1.  各种结构的简单设计结构
可以参见LibEvent的事件结构的设计,各个事件对应的十六进制码为
EV_CONNREAD     0x00
EV_HTTPPARSE     0x01
EV_HTTPHANDLE   0x02
EV_CONNWRITE     0x04
EV_AIOREAD        0x08
EV_AIOWRITE         0x0F
Event的结构
      struct event {
         TAILQ_ENTRY (event) ev_next;
      TAILQ_ENTRY (event) ev_Prev;
           Struct Conn_Struct  *conn;
              int event_count_active;  /* 在EventAtiveQueue中需要处理 */
              struct event_list **activequeues;  /*处于活动中的有优先级事件列表 */
              int nactivequeues; /*活动的事件的个数*/
              void (*ev_callback)(int short, void *arg);
          short ev_flags;
      };
 

ServerThread结构
Struct ServerThread {
       pthread_t  pthread_id;
       struct epoll_event *events; //Epoll 的队列
       struct event_list **activequeues; event优先级队列
       Int nactivequeues;
       struct io_event events[aio_maxio]; //读写磁盘的事件队列
}
 

 

Struct Conn_Struct{
    int    sfd;
    int    state;
    struct event_list event;
       struct socket *sock;
#define MAX_USERNAME_LEN 16
       char username[MAX_USERNAME_LEN];
       unsigned int username_len;
 

       // http部分
       http_method_t method;
       const char *method_str;
       unsigned int method_len;
 

       http_version_t version;
       const char *version_str;
       unsigned int version_len;
 

       /* requested URI: */
 

       const char *uri_str;
       unsigned int uri_len;
 

       /* Objectname (filename/scriptname) this URI refers to: */
 

#define MAX_OBJECTNAME_LEN 256
       char objectname[MAX_OBJECTNAME_LEN + 4]; // space for .gz as well
       unsigned int objectname_len;
 

       /* Query string within the URI: */
 

       const char *query_str;
       unsigned int query_len;
 

       /* Cookies: */
 

       const char *cookies_str;
       unsigned int cookies_len;
       unsigned int parse_cookies;
 

       /* Content-TYpe */
       const char *content_type_str;
       unsigned int content_type_len;
 

       /* Content-Length: */
 

       const char *contentlen_str;
       unsigned int contentlen_len;
       unsigned int content_len;
 

       /* User-Agent: */
 

       const char *user_agent_str;
       unsigned int user_agent_len;
 

       /* Accept: */
 

       const char *accept_str;
       unsigned int accept_len;
 

       /* Accept-Charset: */
 

       const char *accept_charset_str;
       unsigned int accept_charset_len;
 

       /* Accept-Language: */
 

       const char *accept_language_str;
       unsigned int accept_language_len;
 

       /* Cache-Control: */
 

       const char *cache_control_str;
       unsigned int cache_control_len;
 

       /* If-Modified-Since: */
 

       const char *if_modified_since_str;
       unsigned int if_modified_since_len;
 

       /* If-None-Match: */
       const char *if_none_match_str;
       unsigned int if_none_match_len;
 

       /* If-Range: */
 

       const char *if_range_str;
       unsigned int if_range_len;
 

       /* Negotiate: */
 

       const char *negotiate_str;
       unsigned int negotiate_len;
 

       /* Pragma: */
 

       const char *pragma_str;
       unsigned int pragma_len;
 

       /* Referer: */
 

       const char *referer_str;
       unsigned int referer_len;
 

       /* Accept-Encoding: */
 

       const char *accept_encoding_str;
       unsigned int accept_encoding_len;
       unsigned int may_send_gzip;
       unsigned int content_gzipped;
 

       /* Host */
 

#define MAX_HOST_LEN 128
       char host[MAX_HOST_LEN];
       unsigned int host_len;
 

       /* POSTed data: */
 

       const char *post_data_str;
       unsigned int post_data_len;
 

       unsigned int status;
 

       /* the file being sent */
 

       unsigned int bytes_sent;
#if CONFIG_TUX_DEBUG
       unsigned int bytes_expected;
#endif
       unsigned long first_timestamp;
       unsigned int body_len;
 

       unsigned int user_error;
 

       char error;
       char postponed;
 

       char had_cachemiss;
       char lookup_dir;
       char lookup_404;
 

       char keep_alive;
       struct timer_list keepalive_timer;
       unsigned int total_bytes;
       struct timer_list output_timer;
 

       unsigned int nr_keepalives;
 

       unsigned int event;
       u64 private;
 

       unsigned int magic;
#if CONFIG_TUX_EXTENDED_LOG
       unsigned long accept_timestamp;
       unsigned long parse_timestamp;
       unsigned long output_timestamp;
       unsigned long flush_timestamp;
# define SET_TIMESTAMP(x) do { (x) = jiffies; } while (0)
#else
# define SET_TIMESTAMP(x) do { } while (0)
#endif
}
事件循环的方法
EventLoop()
EventHandle()
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息