forked from Miaoshuai/netlib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
base_loop.cpp
83 lines (69 loc) · 2.18 KB
/
base_loop.cpp
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
/*======================================================
> File Name: base_loop.cpp
> Author: MiaoShuai
> E-mail:
> Other :
> Created Time: 2015年12月20日 星期日 13时59分48秒
=======================================================*/
#include "base_loop.h"
#include <string>
#include "event_loop_thread_pool.h"
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <assert.h>
#include <sys/types.h>
#include <stdio.h>
#include <string.h>
#include <memory>
using namespace netlib;
BaseLoop::BaseLoop(std::string ip,int port,int loopNumber)
:ip_(ip),
port_(port),
loopThreadPoolPtr_(std::make_shared<LoopThreadPool>(loopNumber))
{
}
BaseLoop::~BaseLoop()
{
close(listenFd_);
}
int BaseLoop::createListenFd(void)
{
struct sockaddr_in address;
bzero(&address,sizeof(address));
address.sin_family = AF_INET;
inet_pton(AF_INET,ip_.c_str(),&address.sin_addr);
address.sin_port = htons(port_);
int sock = socket(PF_INET,SOCK_STREAM,0);
assert(sock >= 0);
int ret = bind(sock,(struct sockaddr*)&address,sizeof(address));
assert(ret == 0);
ret = listen(sock,100);
assert(ret == 0);
return sock;
}
void BaseLoop::start(void)
{
struct sockaddr_in clientAddress; //保存客户端地址
socklen_t len = sizeof(clientAddress); //客户端地址长度
int connfd;
listenFd_ = createListenFd(); //获得监听套接字
loopThreadPoolPtr_->setMessageCallback(messageCallback_); //设置读回调函数
loopThreadPoolPtr_->setCloseCallback(closeCallback_); //设置关闭回调函数
loopThreadPoolPtr_->setWriteCompleteCallback(writeCompleteCallback_);
loopThreadPoolPtr_->start(); //开启loop线程池
while(1)
{
connfd = accept(listenFd_,(struct sockaddr *)&clientAddress,&len);
connectionHandle(connfd);
}
}
void BaseLoop::connectionHandle(int connfd)
{
int fd = loopThreadPoolPtr_->getNextLoop();
uint64_t buff = connfd;
int ret = write(fd,&buff,sizeof(buff)); //添加到指定的loop循环中
assert(ret == sizeof(buff));
//下面可以加入用户自定义的连接回调
}