Skip to content

Commit d9b8d81

Browse files
committed
imple server by libev
1 parent a199ccd commit d9b8d81

File tree

7 files changed

+111
-56
lines changed

7 files changed

+111
-56
lines changed

libev/include/nb_net_tool.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#include <netdb.h>
2020
#include <ifaddrs.h>
2121
#include <vector>
22+
#include <fcntl.h>
2223

2324
//#include <boost/noncopyable.hpp>
2425

@@ -117,4 +118,14 @@ inline std::string nb_getifaddr_v4()
117118
}
118119
}
119120

121+
//add by wuxiang
122+
int setnoblock(int fd)
123+
{
124+
int flags = 0;
125+
flags = fcntl(fd, F_GETFL);
126+
flags = flags | O_NONBLOCK;
127+
return fcntl(fd, F_SETFL, flags);
128+
}
129+
130+
120131
#endif /* _NB_NET_TOOL_H_ */

libev/source/include/configuration.h

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#ifndef CONFIGURATION_H_
2+
#define CONFIGURATION_H_
3+
4+
class configuration
5+
{
6+
public:
7+
configuration& instance();
8+
void parse_option(const int ac, const char* argv[]);
9+
std::size_t get_ev_loop_size();
10+
void dump();
11+
12+
private:
13+
configuration();
14+
~configuration();
15+
configuration(const configuration&);
16+
configuration operator=(const configuration&);
17+
18+
private:
19+
void init_default_options();
20+
21+
void load_config_file();
22+
23+
private:
24+
std::size_t m_ev_loop_size;
25+
};
26+
27+
#endif //CONFIGURATION_H_

libev/source/include/io_loop_pool.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,16 @@
66
class io_loop_pool: public singleton_base<io_loop_pool>
77
{
88
public:
9+
io_loop_pool& instance();
10+
io_loop& get_io_loop();
11+
bool run();
12+
bool stop();
13+
14+
private:
915
io_loop_pool();
1016
io_loop_pool(const std::size_t& size);
1117
~io_loop_pool();
1218

13-
io_loop& get_io_loop();
1419

1520
private:
1621
std::size_t m_size;

libev/source/include/loop.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#ifndef LOOP_H_
22
#define LOOP_H_
3+
#include <ev.h>
34

45
class io_loop
56
{
@@ -8,6 +9,11 @@ class io_loop
89
io_loop(const unsigned size, const std::size_t* array);
910
~io_loop();
1011

12+
bool run();
13+
bool stop();
14+
bool add_service(const ev_io* service);
15+
bool del_service(const ev_io* service);
16+
1117
private:
1218
//make it can not be copied
1319
io_loop(const io_loop&);

libev/source/src/configuration.cpp

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
#include "configuration.h"
2+
3+
configuration& instance()
4+
{
5+
static configuration cfg;
6+
return cfg;
7+
}
8+
9+
configuration::configuration()
10+
{
11+
init_default_options();
12+
}
13+
14+
configuration::~configuration()
15+
{
16+
}
17+
18+
void configuration::parse_option(const int ac, const char* argv[])
19+
{
20+
}
21+
22+
std::size_t configuration::get_ev_loop_size()
23+
{
24+
}
25+
26+
void configuration::dump()
27+
{
28+
}
29+
30+
void configuration::init_default_options()
31+
{
32+
m_ev_loop_size = 4;
33+
}
34+
35+
void configuration::load_config_file()
36+
{
37+
}
38+

libev/source/src/loop.cpp

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,25 @@ io_loop::~io_loop()
2121
ev_loop_destroy(loop);
2222
}
2323

24+
bool io_loop::run()
25+
{
26+
return ev_run(loop, 0);
27+
}
28+
29+
bool io_loop::stop()
30+
{
31+
return ev_break(loop, EVBREAK_ALL);
32+
}
33+
34+
bool io_service::add_service(const ev_io* service)
35+
{
36+
ev_io_start(loop, service);
37+
return true;
38+
}
39+
40+
bool io_service::del_service(const ev_io* service)
41+
{
42+
ev_io_start(loop, service);
43+
return true;
44+
}
45+

libev/test/entry.cpp

Lines changed: 1 addition & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -8,53 +8,13 @@
88
#include <unistd.h>
99
#include <string.h>
1010
#include <stdlib.h>
11-
#include <ev.h>
1211

1312
#include "asyn_server.h"
1413
#include "nb_net_tool.h"
1514

16-
struct ev_loop* loop = NULL;
17-
int server = 0;
18-
int cli_fd = 0;
19-
20-
//void accept_cb(EV_P_ ev_io* p, int enent)
21-
void accept_cb(struct ev_loop* loop, struct ev_io* io, int event)
22-
{
23-
printf("accept handler\n");
24-
struct sockaddr_in cli_addr;
25-
socklen_t len = sizeof(cli_addr);
26-
cli_fd = accept(server, (struct sockaddr*)&cli_addr, &len);
27-
if (-1 == cli_fd)
28-
{
29-
printf("accept failed\n");
30-
}
31-
32-
//ev_io_stop(EV_A_ p);
33-
//ev_break(EV_A_ EVBREAK_ONE);
34-
35-
//p = (ev_io*)malloc(sizeof(ev_io));
36-
//ev_io_init(p, accept_cb, 0, EV_READ);
37-
//ev_io_start(loop, p);
38-
}
39-
40-
41-
//void cb(EV_P_ ev_io* p, int enent)
42-
//{
43-
// printf("stdin read happen\n");
44-
// //ev_io_stop(EV_A_ p);
45-
// //ev_break(EV_A_ EVBREAK_ONE);
46-
//}
47-
4815
int main()
4916
{
50-
// prepare io
51-
//struct ev_loop* loop = EV_DEFAULT;
52-
loop = EV_DEFAULT;
53-
ev_io accept_id;
54-
//ev_io tmp;
55-
56-
//int server = socket(AF_INET, SOCK_STREAM, 0);
57-
server = socket(AF_INET, SOCK_STREAM, 0);
17+
int server = socket(AF_INET, SOCK_STREAM, 0);
5818

5919
struct sockaddr_in serv_addr;
6020
bzero(&serv_addr, sizeof(struct sockaddr_in));
@@ -78,15 +38,6 @@ int main()
7838
printf("error happen when use listen\n");
7939
}
8040

81-
printf("ev io starting\n");
82-
ev_io_init(&accept_id, accept_cb, server, EV_READ);
83-
ev_io_start(loop, &accept_id);
84-
85-
//ev_io_init(&tmp, cb, 0, EV_READ);
86-
//ev_io_start(loop, &tmp);
87-
88-
ev_run(loop, 0);
89-
printf("\nev io end\n");
9041

9142
sigset_t wait_mask;
9243
sigemptyset(&wait_mask);
@@ -97,11 +48,6 @@ int main()
9748
int flag = 0;
9849
sigwait(&wait_mask, &flag);
9950

100-
printf("ev io stoping\n");
101-
//ev_io_stop(EV_A_ &accept_id);
102-
//ev_break(EV_A_ EVBREAK_ALL);
103-
printf("ev io has stoped\n");
104-
10551
return 0;
10652
}
10753

0 commit comments

Comments
 (0)