server结构体在antweb起到非常重要的作用,它主要记录着整个服务器在运行的信息,其中包括监听套接字、mempool内存池、配置信息、请求连接集合、以及工作进程集合等等。
server结构体:
typedef struct {
//定义监听套接字
int listenfd;
//定义监听套接字的地址结构
struct sockaddr_in listenfd_addr;
//记录内存池
mempool *mpool;
//记录全部配置信息
configure *conf;
//记录全部的连接
connection_array *cons;
//记录所有工作进程的状态
worker *workers;
//记录当前的工作进程id,即workers数组对应的位置
int worker_id;
}server;
该结构体在初始化时,会读取配置文件(日记05),创建内存池(日记02),初始化连接集合(日记08),以及创建一些工作进程(默认10个)。而在服务器结束时,server结构体会调用释放函数,把antweb所占有的内存全部归还给系统。
该操作的函数实现:
server* server_init()
{
server *srv;
srv = (server *)calloc(1,sizeof(server));
srv->conf = configure_init();
srv->mpool = mempool_create();
srv->cons = connection_array_init();
srv->workers = (worker*)calloc(10,sizeof(worker));
return srv;
}
void server_free(server * srv)
{
configure_free(srv->conf);
connection_array_free(srv->mpool,srv->cons);
mempool_free(srv->mpool);
free(srv);
}