forked from kulv2012/myrelay
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cli_pool.c
171 lines (139 loc) · 3.47 KB
/
cli_pool.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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
/*
* Copyright 2011-2013 Alibaba Group Holding Limited. All rights reserved.
* Use and distribution licensed under the GPL license.
*
* Authors: XiaoJinliang <xiaoshi.xjl@taobao.com>
*
*/
#include <stdio.h>
#include <list.h>
#include <time.h>
#include <genpool.h>
#include <log.h>
#include <handler.h>
#include "cli_pool.h"
#include "my_buf.h"
#include "conn_pool.h"
#include "passwd.h"
#include "mysql_com.h"
extern log_t *g_log;
static genpool_handler_t *cli_pool;
/*
* fun: init client connection pool
* arg: max client connection number
* ret: success return 0, error return -1
*
*/
int cli_pool_init(int count)
{
cli_pool = genpool_init(sizeof(cli_conn_t), count);
if(cli_pool == NULL){
log(g_log, "genpool init error\n");
return -1;
}
return 0;
}
int cli_pool_destroy( )
{
if( cli_pool != NULL){
genpool_destroy( cli_pool) ;
cli_pool = NULL ;
}
return 0;
}
/*
* fun: alloc client connection
* arg: connection pointer, connection fd, ip and port
* ret: success return client connection, error return NULL
*
*/
static cli_conn_t *
cli_conn_alloc(conn_t *conn, int fd, uint32_t ip, uint16_t port)
{//申请一个客户端连接结构,跟当前的搭桥的连接结构挂接
int res;
cli_conn_t *c;
if( (c = genpool_alloc_page(cli_pool)) == NULL ){
log(g_log, "genpool alloc page error\n");
return NULL;
}
c->fd = fd;//初始化相关结构
c->ip = ip;
c->port = port;
c->conn = conn;
INIT_LIST_HEAD(&(c->link));
// 初始化buffer
if( (res = buf_init(&(c->buf))) < 0 ){
log(g_log, "buf_init error\n");
genpool_release_page(cli_pool, c);
return NULL;
}
// 创建一个随机的scram
if( (res = make_rand_scram(c->scram, SCRAMBLE_LENGTH)) < 0 ){
log(g_log, "make rand scram error\n");
genpool_release_page(cli_pool, c);
return NULL;
}
c->scram[SCRAMBLE_LENGTH] = '\0';
return c;
}
/*
* fun: open client connection
* arg: connection pointer, connection fd, ip and port
* ret: success return 0, error return -1
*
*/
int cli_conn_open(conn_t *conn, int fd, uint32_t ip, uint16_t port)
{//调用cli_conn_alloc申请一个客户端连接结构,跟当前的中间conn挂接
cli_conn_t *c;
if(conn == NULL){
return -1;
}
if( (c = cli_conn_alloc(conn, fd, ip, port)) == NULL ){
log(g_log, "cli conn alloc error\n");
return -1;
}
conn->cli = c;//对应这个连接结构的客户端连接
return 0;
}
/*
* fun: release client connection
* arg: client connection
* ret: success 0, error -1
*
*/
static int cli_conn_release(cli_conn_t *conn)
{
int res;
if(conn == NULL){
return -1;
}
conn->fd = -1;
conn->ip = 0;
conn->port = 0;
list_del_init(&(conn->link));
conn->conn = NULL;
if( (res = buf_reset(&(conn->buf))) < 0 ){
return -1;
}
return genpool_release_page(cli_pool, conn);
}
/*
* fun: close client connection
* arg: client connection
* ret: success 0, error -1
*
*/
int cli_conn_close(cli_conn_t *conn)
{
int res;
if(conn == NULL){
return -1;
}
if(conn->fd >= 0){
if( (res = del_handler(conn->fd)) < 0 ){
log(g_log, "del_handler error\n");
}
close(conn->fd);
}
return cli_conn_release(conn);
}