-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathclient.c
130 lines (113 loc) · 3.31 KB
/
client.c
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <event2/bufferevent.h>
#include <sdnet.h>
#include <logging.h>
#include <stdlib.h>
#include <event2/event.h>
#include <sys/socket.h>
#include <sys/types.h>
void input_event_cb(struct bufferevent *bev, short ev_type, void *arg)
{
if (ev_type & BEV_EVENT_ERROR) {
LOG_ERROR("input error\n");
exit(1);
} else if (ev_type & BEV_EVENT_READING) {
LOG_INFO("bev reading\n");
}
}
void input_data_cb(struct bufferevent *bev, void *ctx)
{
char buf[1024];
struct bufferevent *net = ctx;
struct evbuffer *in = NULL;
size_t len;
in = bufferevent_get_input(bev);
len = evbuffer_get_length(in);
if (len) {
evbuffer_remove(in, buf, len);
bufferevent_write(net, buf, len);
}
}
void net_event_cb(struct bufferevent *bev, short events, void *ptr)
{
struct event_base *base = ptr;
struct bufferevent *input = NULL;
int flags = -1;
if (events & BEV_EVENT_CONNECTED) {
flags = fcntl(STDIN_FILENO, F_GETFL);
if (flags < 0) {
LOG_ERROR("Failed to get stdin flags\n");
LOG_ERRNO(errno);
exit(1);
}
if (fcntl(STDIN_FILENO, F_SETFL, flags | O_NONBLOCK) < 0) {
LOG_ERROR("Failed to set stdin with NONBLOCK\n");
exit(1);
}
input = bufferevent_socket_new(base, STDIN_FILENO, BEV_OPT_DEFER_CALLBACKS);
if (input == NULL) {
LOG_ERROR("Failed to new an bufferevent\n");
exit(1);
}
bufferevent_setcb(input, input_data_cb, input_data_cb, input_event_cb, bev);
LOG_INFO("Connection has been established, ready for input:\n");
bufferevent_enable(input, EV_READ);
} else if (events & BEV_EVENT_ERROR) {
LOG_ERROR("Connect error\n");
exit(1);
} else if (events & BEV_EVENT_EOF) {
LOG_ERROR("The other side closed connection\n");
exit(1);
} else {
LOG_ERROR("UNKNOW\n");
exit(1);
}
}
void net_data_cb(struct bufferevent *bev, void *ptr)
{
char buf[1024];
size_t len;
struct evbuffer *in = bufferevent_get_input(bev);
len = evbuffer_get_length(in);
if (len) {
evbuffer_remove(in, buf, len);
buf[len] = 0;
LOG_INFO("%s\n", buf);
}
}
extern int cmagent(int , char **);
int main(int argc, char **argv)
{
struct event_base * base = NULL;
struct bufferevent * bev = NULL, *input = NULL;
struct sockaddr_in sa;
int flags = -1;
char buf[128] = "hello";
log_init(NULL);
return cmagent(argc, argv);
base = event_base_new();
if (base == NULL) {
LOG_ERROR("event_base_new() error\n");
return -1;
}
bev = bufferevent_socket_new(base, -1, BEV_OPT_CLOSE_ON_FREE);
if (bev == NULL) {
LOG_ERROR("event_base_new() error\n");
return -1;
}
sa.sin_family = AF_INET;
sa.sin_addr.s_addr = htonl(0x7f000001);
sa.sin_port = htons(9999);
bufferevent_setcb(bev, net_data_cb, NULL, net_event_cb, base);
bufferevent_enable(bev, EV_READ | EV_WRITE);
if (bufferevent_socket_connect(bev, (struct sockaddr *)&sa, sizeof sa) < 0) {
bufferevent_free(bev);
LOG_ERROR("connect error\n");
return -1;
}
event_base_dispatch(base);
return 0;
}