Skip to content

Commit 67d5f64

Browse files
author
zhang xiao bin
committed
调试select
1 parent cfe12db commit 67d5f64

File tree

4 files changed

+303
-179
lines changed

4 files changed

+303
-179
lines changed

.gitignore

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
.vs
2+
x64
3+
async_mysql/x64
4+
5+
CMakeSettings.json
6+
out
7+

async_mysql/async_mysql.vcxproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,7 @@
146146
</ItemDefinitionGroup>
147147
<ItemGroup>
148148
<ClCompile Include="main.cpp" />
149+
<ClCompile Include="poll_uv.cpp" />
149150
</ItemGroup>
150151
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
151152
<ImportGroup Label="ExtensionTargets">

async_mysql/main.cpp

Lines changed: 58 additions & 179 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@
55
#include <cstdio>
66
#include <signal.h>
77
#include <mysql/mysql.h>
8-
#include <event.h>
9-
#include <uv.h>
108
#include <functional>
119
#include <thread>
1210
#include <mutex>
@@ -20,28 +18,22 @@ typedef int fd_t;
2018
#endif /* _WIN32 */
2119

2220
typedef struct mysql_context_s {
23-
uv_poll_t poll_handle;
2421
MYSQL* m ;
25-
int index = 0;
22+
int qindex = 0;
23+
int rindex = 0;
2624
char name[32];
2725

2826
} mysql_context_t;
2927

3028
std::vector<mysql_context_t*> conns;
31-
std::unordered_map<unsigned long long , event*> events;
3229
std::vector<std::string> query_sql;
3330
std::mutex mutex;
3431

3532
void query(const std::string& sql,MYSQL *m);
36-
void hand_libevent_event(intptr_t fd, short what, void* _userdata);
37-
void hand_libuv_event(uv_poll_t* handle, int status, int what);
3833
void mysql_write(MYSQL* m);
39-
4034
void mysql_read_result(MYSQL*);
41-
4235
int hand_times = 0;
4336

44-
4537
// ========================================================
4638
// select
4739

@@ -82,112 +74,48 @@ int init_poll_fds_info(void) {
8274
return ndfs + 1;
8375
}
8476

85-
void hand_libevent_event(intptr_t fd, short what, void* _userdata)
86-
{
87-
/*
88-
printf("Got an event on socket %d:%s%s%s%s \n",
89-
(int) fd,
90-
(what&EV_TIMEOUT) ? " timeout" : "",
91-
(what&EV_READ) ? " read" : "",
92-
(what&EV_WRITE) ? " write" : "",
93-
(what&EV_SIGNAL) ? " signal" : "");
94-
*/
95-
96-
if (what & EV_READ)
97-
{
98-
event* ev = events[fd];
99-
if (ev)
100-
{
101-
ev->ev_events |= EV_READ;
102-
ev->ev_events |= EV_WRITE;
103-
int ret = event_add(ev, NULL);
104-
}
105-
mysql_read_result((MYSQL*)_userdata);
106-
}
107-
if (what & EV_WRITE)
108-
{
109-
mysql_write((MYSQL*)_userdata);
110-
}
111-
112-
}
113-
114-
void hand_libuv_event(uv_poll_t* handle, int status, int what)
115-
{
116-
hand_times++;
117-
118-
mysql_context_t* context = (mysql_context_t*)handle->data;
119-
120-
/*
121-
printf("Got an uvevent on socket %d:%s%s%",
122-
(int) context->m->net.fd,
123-
(what&UV_READABLE) ? " read" : "",
124-
(what&UV_WRITABLE) ? " write" : "");
125-
*/
126-
127-
128-
129-
if (what & UV_READABLE)
130-
{
131-
printf("hand_libuv_event times %d\n", hand_times);
132-
mysql_read_result(context->m);
133-
}
134-
if (what & UV_WRITABLE)
135-
{
136-
mysql_write(context->m);
137-
}
138-
139-
}
14077

14178
void mysql_write(MYSQL* m)
14279
{
14380
query("select * from bag;", m);
14481
}
14582

14683
void mysql_read_result(MYSQL* m) {
147-
//mysql_read_query_result(m);
148-
149-
if (0 == mysql_read_query_result(m)) {
150-
MYSQL_RES *res = mysql_store_result(m);
151-
MYSQL_ROW row;
152-
int fields = mysql_num_fields(res);
153-
while ((row = mysql_fetch_row(res)))
154-
{
155-
for (int i = 0; i < fields; i++)
84+
while (true)
85+
{
86+
if (mysql_read_query_result(m) == 0) {
87+
MYSQL_RES *res = mysql_store_result(m);
88+
MYSQL_ROW row;
89+
int fields = mysql_num_fields(res);
90+
while ((row = mysql_fetch_row(res)))
15691
{
157-
printf("%s\t", row[i]);
92+
for (int i = 0; i < fields; i++)
93+
{
94+
printf("%s \t", row[i]);
95+
}
96+
printf("\n");
15897
}
98+
//printf("a result for m->net.fd = %d \n", m->net.fd);
99+
mysql_free_result(res);
100+
} else {
101+
std::cout <<" mysql_read_query_result() failed for fd = "<< m->net.fd << " "<<mysql_error(m)<< ":"<<mysql_errno(m)<< std::endl;
102+
return;
159103
}
160-
printf("a result for m->net.fd = %d \n", m->net.fd);
161-
mysql_free_result(res);
162-
} else {
163-
std::cout <<" mysql_read_query_result() failed for fd = "<< m->net.fd << " "<<mysql_error(m)<< ":"<<mysql_errno(m)<< std::endl;
164-
}
165-
}
166104

167-
void connect_mysql_server()
168-
{
169-
for (int i = 0; i < 4; i++)
170-
{
171-
mysql_context_t* c = new mysql_context_t;
172-
173-
c->m = mysql_init(nullptr);
174-
if (!c->m)
105+
switch(mysql_next_result(m))
175106
{
176-
std::cout<<"mysql init error"<< std::endl;
107+
case -1:
177108
return;
178-
}
179-
c->m->free_me = 1;
180-
auto ret = mysql_real_connect(c->m,"192.168.150.79", "root","123456","gamesh_zxb",3306,NULL,0);
181-
if (!ret)
182-
{
183-
std::cout<<"Mysql conn err : "<<mysql_error(c->m) << ", errno :"<< mysql_errno(c->m)<< std::endl;
109+
case 0:
110+
continue;
111+
default:
184112
return;
185113
}
186-
conns.push_back(c);
187-
m_tracked_sockets[c->m->net.fd] = c;
114+
188115
}
189116
}
190117

118+
191119
void connect_mysql_server(mysql_context_t* context)
192120
{
193121
context->m = mysql_init(nullptr);
@@ -207,16 +135,7 @@ void connect_mysql_server(mysql_context_t* context)
207135
conns.push_back(context);
208136
}
209137

210-
void bind_event(struct event_base* evbase)
211-
{
212-
for (auto c : conns)
213-
{
214-
auto e = event_new(evbase, c->m->net.fd, EV_READ | EV_PERSIST, hand_libevent_event, c->m);
215-
event_add(e, NULL);
216-
events[c->m->net.fd] = e;
217-
}
218138

219-
}
220139

221140
void query(const std::string& sql,MYSQL *m)
222141
{
@@ -226,81 +145,29 @@ void query(const std::string& sql,MYSQL *m)
226145

227146
void query(const std::string& sql, mysql_context_t* c)
228147
{
229-
c->index++;
230-
printf("query index %d\n", c->index);
148+
c->qindex++;
231149
auto ret = mysql_send_query(c->m, sql.c_str(), sql.size());
232-
printf("mysql_send_query result %d\n", ret);
233-
}
234-
235-
236-
void test_libevent()
237-
{
238-
printf("======== test_libevent ========\n");
239-
struct event_base* evbase = event_base_new();
240-
connect_mysql_server();
241-
query("select * from account;", conns[0]);
242-
query("select * from bag;", conns[1]);
243-
query("select * from account;", conns[2]);
244-
query("select * from bag;", conns[3]);
245-
bind_event(evbase);
246-
event_base_dispatch(evbase);
247-
}
248-
249-
250-
void bind_libuv(uv_loop_t* loop, mysql_context_t* context)
251-
{
252-
uv_poll_init_socket(loop, &context->poll_handle, context->m->net.fd);
253-
context->poll_handle.data = context;
254-
uv_poll_start(&context->poll_handle , UV_READABLE | UV_DISCONNECT, hand_libuv_event);
255-
}
256-
257-
void test_libuv()
258-
{
259-
printf("======== test_libuv ========\n");
260-
uv_loop_t* loop = uv_default_loop();
261-
262-
mysql_context_t* c1 = new mysql_context_t;
263-
mysql_context_t* c2 = new mysql_context_t;
264-
mysql_context_t* c3 = new mysql_context_t;
265-
mysql_context_t* c4 = new mysql_context_t;
266-
267-
connect_mysql_server(c1);
268-
bind_libuv(loop, c1);
269-
connect_mysql_server(c2);
270-
bind_libuv(loop, c2);
271-
connect_mysql_server(c3);
272-
bind_libuv(loop, c3);
273-
connect_mysql_server(c4);
274-
bind_libuv(loop, c4);
275-
276-
query("select * from account;",c1->m);
277-
query("select * from bag;",c2->m);
278-
query("select * from account;",c3->m);
279-
query("select * from bag;",c4->m);
280-
281-
#if 0
282-
283-
query("select * from account;",c2);
284-
query("select * from bag;",c2);
285-
query("select * from account;",c2);
286-
query("select * from bag;",c2);
287-
#endif
288-
int r = uv_run(loop, UV_RUN_DEFAULT);
150+
printf("mysql_send_query ret : %d \n", ret);
289151
}
290152

291153

292-
293154
void select_rd_event_cb(fd_t fd, mysql_context_t* c)
294155
{
295156
mysql_read_result(c->m);
157+
c->rindex++;
158+
//printf("Result Contesx index %d\n", c->rindex);
296159
}
297160

298161
void select_wr_event_cb(fd_t fd, mysql_context_t* c)
299162
{
300163

301164
}
302165

303-
void process_events(void) {
166+
167+
void hand_select_event(void) {
168+
static int index = 0;
169+
index++;
170+
printf("hand_select_event index %d\n", index);
304171

305172
for (const auto& fd : m_polled_fds) {
306173

@@ -321,12 +188,15 @@ void process_events(void) {
321188

322189
void process_query()
323190
{
324-
std::vector<std::string> querys;
325-
querys.swap(query_sql);
326-
for (auto& sql : querys)
191+
if (query_sql.empty())
327192
{
328-
query(sql, conns[0]);
193+
return;
329194
}
195+
196+
std::lock_guard<std::mutex> l(mutex);
197+
std::string sql = query_sql.back();
198+
query_sql.pop_back();
199+
query(sql, conns[0]);
330200
}
331201

332202

@@ -336,10 +206,9 @@ void poll()
336206
int ndfs = init_poll_fds_info();
337207
struct timeval tv;
338208
tv.tv_sec = 0;
339-
tv.tv_usec = 10000;
340-
209+
tv.tv_usec = 1000; //1ms
341210
if (select(ndfs, &m_rd_set, &m_wr_set, NULL, &tv) > 0) {
342-
process_events();
211+
hand_select_event();
343212
}
344213
else {
345214
process_query();
@@ -349,12 +218,20 @@ void poll()
349218

350219
void test_select()
351220
{
352-
mysql_context_t* c1 = new mysql_context_t;
353-
connect_mysql_server(c1);
354221
std::thread worker = std::thread(poll);
355-
query_in_loop(std::string("select * from account;"));
356-
worker.join();
357222

223+
for (int i = 0; i < 5; i++)
224+
{
225+
mysql_context_t* c = new mysql_context_t;
226+
connect_mysql_server(c);
227+
}
228+
229+
for (int i = 0; i < 100; i++)
230+
{
231+
query_in_loop(std::string("select * from account;"));
232+
//query("select * from account", conns[0]);
233+
}
234+
worker.join();
358235
}
359236

360237
int main(int argc, char** argv)
@@ -371,6 +248,8 @@ int main(int argc, char** argv)
371248

372249
//test_libevent();
373250
//test_libuv();
251+
mysql_library_init(0, nullptr, nullptr);
374252
test_select();
253+
mysql_server_end();
375254
return 0;
376255
}

0 commit comments

Comments
 (0)