Skip to content

Commit a199ccd

Browse files
committed
test libev well
1 parent 0a02dc1 commit a199ccd

File tree

370 files changed

+80009
-16
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

370 files changed

+80009
-16
lines changed

libev/example/entry.cpp

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
#include <iostream>
2+
#include <sys/socket.h>
3+
#include <arpa/inet.h>
4+
#include <netinet/in.h>
5+
#include <cstring>
6+
#include <cstdio>
7+
#include <signal.h>
8+
#include <unistd.h>
9+
#include <string.h>
10+
#include <stdlib.h>
11+
#include <ev.h>
12+
13+
#include "asyn_server.h"
14+
#include "nb_net_tool.h"
15+
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+
48+
int main()
49+
{
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);
58+
59+
struct sockaddr_in serv_addr;
60+
bzero(&serv_addr, sizeof(struct sockaddr_in));
61+
serv_addr.sin_family = AF_INET;
62+
serv_addr.sin_addr.s_addr = htonl(INADDR_ANY);
63+
serv_addr.sin_port = htons(10026);
64+
65+
sigset_t new_mask;
66+
sigset_t old_mask;
67+
sigfillset(&new_mask);
68+
pthread_sigmask(SIG_BLOCK, &new_mask, &old_mask);
69+
printf("start binding\n");
70+
if (0 != bind(server, (struct sockaddr*)&serv_addr, sizeof(serv_addr)))
71+
{
72+
printf("error happen when use bind\n");
73+
}
74+
75+
printf("start listening\n");
76+
if (0 != listen(server, 10))
77+
{
78+
printf("error happen when use listen\n");
79+
}
80+
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");
90+
91+
sigset_t wait_mask;
92+
sigemptyset(&wait_mask);
93+
sigaddset(&wait_mask, SIGINT);
94+
sigaddset(&wait_mask, SIGQUIT);
95+
sigaddset(&wait_mask, SIGTERM);
96+
pthread_sigmask(SIG_BLOCK, &wait_mask, 0);
97+
int flag = 0;
98+
sigwait(&wait_mask, &flag);
99+
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+
105+
return 0;
106+
}
107+
108+

libev/example/libev++.cpp

Lines changed: 213 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,213 @@
1+
#include <unistd.h>
2+
#include <fcntl.h>
3+
#include <string.h>
4+
#include <stdlib.h>
5+
#include <ev++.h>
6+
#include <netinet/in.h>
7+
#include <sys/socket.h>
8+
#include <resolv.h>
9+
#include <errno.h>
10+
#include <list>
11+
12+
//
13+
// Buffer class - allow for output buffering such that it can be written out
14+
// into async pieces
15+
//
16+
struct Buffer {
17+
char *data;
18+
ssize_t len;
19+
ssize_t pos;
20+
21+
Buffer(const char *bytes, ssize_t nbytes) {
22+
pos = 0;
23+
len = nbytes;
24+
data = new char[nbytes];
25+
memcpy(data, bytes, nbytes);
26+
}
27+
28+
virtual ~Buffer() {
29+
delete [] data;
30+
}
31+
32+
char *dpos() {
33+
return data + pos;
34+
}
35+
36+
ssize_t nbytes() {
37+
return len - pos;
38+
}
39+
};
40+
41+
//
42+
// A single instance of a non-blocking Echo handler
43+
//
44+
class EchoInstance {
45+
private:
46+
ev::io io;
47+
static int total_clients;
48+
49+
// Buffers that are pending write
50+
std::list<Buffer*> write_queue;
51+
52+
// Generic callback
53+
void callback(ev::io &watcher, int revents) {
54+
if (EV_ERROR & revents) {
55+
perror("got invalid event");
56+
return;
57+
}
58+
59+
if (revents & EV_READ)
60+
read_cb(watcher);
61+
62+
if (revents & EV_WRITE)
63+
write_cb(watcher);
64+
65+
if (write_queue.empty()) {
66+
io.set(ev::READ);
67+
} else {
68+
io.set(ev::READ|ev::WRITE);
69+
}
70+
}
71+
72+
// Socket is writable
73+
void write_cb(ev::io &watcher) {
74+
if (write_queue.empty()) {
75+
io.set(ev::READ);
76+
return;
77+
}
78+
79+
Buffer* buffer = write_queue.front();
80+
81+
ssize_t written = write(watcher.fd, buffer->dpos(), buffer->nbytes());
82+
if (written < 0) {
83+
perror("read error");
84+
return;
85+
}
86+
87+
buffer->pos += written;
88+
if (buffer->nbytes() == 0) {
89+
write_queue.pop_front();
90+
delete buffer;
91+
}
92+
}
93+
94+
// Receive message from client socket
95+
void read_cb(ev::io &watcher) {
96+
char buffer[1024];
97+
98+
ssize_t nread = recv(watcher.fd, buffer, sizeof(buffer), 0);
99+
100+
if (nread < 0) {
101+
perror("read error");
102+
return;
103+
}
104+
105+
if (nread == 0) {
106+
// Gack - we're deleting ourself inside of ourself!
107+
delete this;
108+
} else {
109+
// Send message bach to the client
110+
write_queue.push_back(new Buffer(buffer, nread));
111+
}
112+
}
113+
114+
// effictivly a close and a destroy
115+
virtual ~EchoInstance() {
116+
// Stop and free watcher if client socket is closing
117+
io.stop();
118+
119+
printf("%d client(s) connected.\n", --total_clients);
120+
}
121+
122+
public:
123+
EchoInstance(int s) {
124+
fcntl(s, F_SETFL, fcntl(s, F_GETFL, 0) | O_NONBLOCK);
125+
126+
printf("Got connection\n");
127+
total_clients++;
128+
129+
io.set<EchoInstance, &EchoInstance::callback>(this);
130+
131+
io.start(s, ev::READ);
132+
}
133+
};
134+
135+
class EchoServer {
136+
private:
137+
ev::io io;
138+
ev::sig sio;
139+
int s;
140+
141+
public:
142+
143+
void io_accept(ev::io &watcher, int revents) {
144+
if (EV_ERROR & revents) {
145+
perror("got invalid event");
146+
return;
147+
}
148+
149+
struct sockaddr_in client_addr;
150+
socklen_t client_len = sizeof(client_addr);
151+
152+
int client_sd = accept(watcher.fd, (struct sockaddr *)&client_addr, &client_len);
153+
154+
if (client_sd < 0) {
155+
perror("accept error");
156+
return;
157+
}
158+
159+
EchoInstance *client = new EchoInstance(client_sd);
160+
}
161+
162+
static void signal_cb(ev::sig &signal, int revents) {
163+
signal.loop.break_loop();
164+
}
165+
166+
EchoServer(int port) {
167+
printf("Listening on port %d\n", port);
168+
169+
struct sockaddr_in addr;
170+
171+
s = socket(PF_INET, SOCK_STREAM, 0);
172+
173+
addr.sin_family = AF_INET;
174+
addr.sin_port = htons(port);
175+
addr.sin_addr.s_addr = INADDR_ANY;
176+
177+
if (bind(s, (struct sockaddr *)&addr, sizeof(addr)) != 0) {
178+
perror("bind");
179+
}
180+
181+
fcntl(s, F_SETFL, fcntl(s, F_GETFL, 0) | O_NONBLOCK);
182+
183+
listen(s, 5);
184+
185+
io.set<EchoServer, &EchoServer::io_accept>(this);
186+
io.start(s, ev::READ);
187+
188+
sio.set<&EchoServer::signal_cb>();
189+
sio.start(SIGINT);
190+
}
191+
192+
virtual ~EchoServer() {
193+
shutdown(s, SHUT_RDWR);
194+
close(s);
195+
}
196+
};
197+
198+
int EchoInstance::total_clients = 0;
199+
200+
int main(int argc, char **argv)
201+
{
202+
int port = 8192;
203+
204+
if (argc > 1)
205+
port = atoi(argv[1]);
206+
207+
ev::default_loop loop;
208+
EchoServer echo(port);
209+
210+
loop.run(0);
211+
212+
return 0;
213+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
bin/
2+
obj/
3+
*.dSYM
57 Bytes
Binary file not shown.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
default
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
929fc180866cb7315af6c33abc0d4f5f852e4004 61
2+
929fc180866cb7315af6c33abc0d4f5f852e4004 default
1.83 KB
Binary file not shown.

libev/example/libev-examples/.hg/hgrc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
[paths]
2+
default = https://bitbucket.org/scassidy/libev-examples
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
revlogv1
2+
store
3+
fncache
4+
dotencode
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.

0 commit comments

Comments
 (0)