最新文章專(zhuān)題視頻專(zhuān)題問(wèn)答1問(wèn)答10問(wèn)答100問(wèn)答1000問(wèn)答2000關(guān)鍵字專(zhuān)題1關(guān)鍵字專(zhuān)題50關(guān)鍵字專(zhuān)題500關(guān)鍵字專(zhuān)題1500TAG最新視頻文章推薦1 推薦3 推薦5 推薦7 推薦9 推薦11 推薦13 推薦15 推薦17 推薦19 推薦21 推薦23 推薦25 推薦27 推薦29 推薦31 推薦33 推薦35 推薦37視頻文章20視頻文章30視頻文章40視頻文章50視頻文章60 視頻文章70視頻文章80視頻文章90視頻文章100視頻文章120視頻文章140 視頻2關(guān)鍵字專(zhuān)題關(guān)鍵字專(zhuān)題tag2tag3文章專(zhuān)題文章專(zhuān)題2文章索引1文章索引2文章索引3文章索引4文章索引5123456789101112131415文章專(zhuān)題3
問(wèn)答文章1 問(wèn)答文章501 問(wèn)答文章1001 問(wèn)答文章1501 問(wèn)答文章2001 問(wèn)答文章2501 問(wèn)答文章3001 問(wèn)答文章3501 問(wèn)答文章4001 問(wèn)答文章4501 問(wèn)答文章5001 問(wèn)答文章5501 問(wèn)答文章6001 問(wèn)答文章6501 問(wèn)答文章7001 問(wèn)答文章7501 問(wèn)答文章8001 問(wèn)答文章8501 問(wèn)答文章9001 問(wèn)答文章9501
當(dāng)前位置: 首頁(yè) - 科技 - 知識(shí)百科 - 正文

MySQL服務(wù)器監(jiān)聽(tīng)客戶(hù)端連接源碼

來(lái)源:懂視網(wǎng) 責(zé)編:小采 時(shí)間:2020-11-09 12:03:09
文檔

MySQL服務(wù)器監(jiān)聽(tīng)客戶(hù)端連接源碼

MySQL服務(wù)器監(jiān)聽(tīng)客戶(hù)端連接源碼:在下面的代碼有,有些結(jié)構(gòu)體還不清楚,如THD,st_vio,pollfd等。但可以看出MySQL使用Select模型來(lái)接收客戶(hù)端的連接。而且也在網(wǎng)上 在下面的代碼有,有些結(jié)構(gòu)體還不清楚,如THD,st_vio,pollfd等。但可以看出MySQL使用Select模型來(lái)接收客戶(hù)端的連接
推薦度:
導(dǎo)讀MySQL服務(wù)器監(jiān)聽(tīng)客戶(hù)端連接源碼:在下面的代碼有,有些結(jié)構(gòu)體還不清楚,如THD,st_vio,pollfd等。但可以看出MySQL使用Select模型來(lái)接收客戶(hù)端的連接。而且也在網(wǎng)上 在下面的代碼有,有些結(jié)構(gòu)體還不清楚,如THD,st_vio,pollfd等。但可以看出MySQL使用Select模型來(lái)接收客戶(hù)端的連接

在下面的代碼有,有些結(jié)構(gòu)體還不清楚,如THD,st_vio,pollfd等。但可以看出MySQL使用Select模型來(lái)接收客戶(hù)端的連接。而且也在網(wǎng)上

在下面的代碼有,有些結(jié)構(gòu)體還不清楚,如THD,st_vio,pollfd等。但可以看出MySQL使用Select模型來(lái)接收客戶(hù)端的連接。而且也在網(wǎng)上查清,unix_sock是指同一機(jī)器中不同進(jìn)程間的通信,如命名管道。而ip_sock指的是不同主機(jī)間的通信。

void handle_connections_sockets()

{

my_socket UNINIT_VAR(sock), UNINIT_VAR(new_sock);

uint error_count=0;

THD *thd;

struct sockaddr_storage cAddr;

int ip_flags=0,socket_flags=0,flags=0,retval;

st_vio *vio_tmp;

#ifdef HAVE_POLL

int socket_count= 0;

struct pollfd fds[2]; // for ip_sock and unix_sock

#else

fd_set readFDs,clientFDs;

uint max_used_connection= (uint) (max(ip_sock,unix_sock)+1);

#endif

DBUG_ENTER("handle_connections_sockets");

#ifndef HAVE_POLL

FD_ZERO(&clientFDs);

#endif

if (ip_sock != INVALID_SOCKET)

{

#ifdef HAVE_POLL

fds[socket_count].fd= ip_sock;

fds[socket_count].events= POLLIN;

socket_count++;

#else

FD_SET(ip_sock,&clientFDs);

#endif

#ifdef HAVE_FCNTL

ip_flags = fcntl(ip_sock, F_GETFL, 0);

#endif

}

#ifdef HAVE_SYS_UN_H

#ifdef HAVE_POLL

fds[socket_count].fd= unix_sock;

fds[socket_count].events= POLLIN;

socket_count++;

#else

FD_SET(unix_sock,&clientFDs);

#endif

#ifdef HAVE_FCNTL

socket_flags=fcntl(unix_sock, F_GETFL, 0);

#endif

#endif

DBUG_PRINT("general",("Waiting for connections."));

MAYBE_BROKEN_SYSCALL;

while (!abort_loop)

{

#ifdef HAVE_POLL

retval= poll(fds, socket_count, -1);

#else

readFDs=clientFDs;

retval= select((int) max_used_connection,&readFDs,0,0,0);//等待客戶(hù)端連接,反回錯(cuò)誤或有連接到來(lái)

#endif

if (retval < 0)

{

if (socket_errno != SOCKET_EINTR)

{

if (!select_errors++ && !abort_loop) /* purecov: inspected */

sql_print_error("mysqld: Got error %d from select",socket_errno); /* purecov: inspected */

}

MAYBE_BROKEN_SYSCALL

continue;

}

if (abort_loop)

{

MAYBE_BROKEN_SYSCALL;

break;

}

/* Is this a new connection request ? */

#ifdef HAVE_POLL

for (int i= 0; i < socket_count; ++i)

{

if (fds[i].revents & POLLIN)//讀取事件類(lèi)型

{

sock= fds[i].fd;

#ifdef HAVE_FCNTL

flags= fcntl(sock, F_GETFL, 0);

#else

flags= 0;

#endif // HAVE_FCNTL

break;

}

}

#else // HAVE_POLL

#ifdef HAVE_SYS_UN_H

if (FD_ISSET(unix_sock,&readFDs))

{

sock = unix_sock;

flags= socket_flags;

}

else

#endif // HAVE_SYS_UN_H

{

sock = ip_sock;

flags= ip_flags;

}

#endif // HAVE_POLL

#if !defined(NO_FCNTL_NONBLOCK)

if (!(test_flags & TEST_BLOCKING))

{

#if defined(O_NONBLOCK)

fcntl(sock, F_SETFL, flags | O_NONBLOCK);//設(shè)置為非阻塞模式

#elif defined(O_NDELAY)

fcntl(sock, F_SETFL, flags | O_NDELAY);

#endif

}

#endif /* NO_FCNTL_NONBLOCK */

for (uint retry=0; retry < MAX_ACCEPT_RETRY; retry++)

{

size_socket length= sizeof(struct sockaddr_storage);

new_sock= accept(sock, (struct sockaddr *)(&cAddr),//接受連接,連接使用阻塞方式

&length);

if (new_sock != INVALID_SOCKET ||

(socket_errno != SOCKET_EINTR && socket_errno != SOCKET_EAGAIN))

break;

MAYBE_BROKEN_SYSCALL;

#if !defined(NO_FCNTL_NONBLOCK)

if (!(test_flags & TEST_BLOCKING))

{

if (retry == MAX_ACCEPT_RETRY - 1)

fcntl(sock, F_SETFL, flags); // Try without O_NONBLOCK

}

#endif

}

#if !defined(NO_FCNTL_NONBLOCK)

if (!(test_flags & TEST_BLOCKING))

fcntl(sock, F_SETFL, flags);

#endif

if (new_sock == INVALID_SOCKET)

{

if ((error_count++ & 255) == 0) // This can happen often

sql_perror("Error in accept");

MAYBE_BROKEN_SYSCALL;

if (socket_errno == SOCKET_ENFILE || socket_errno == SOCKET_EMFILE)

sleep(1); // Give other threads some time

continue;

}

#ifdef HAVE_LIBWRAP

{

if (sock == ip_sock)

{

struct request_info req;

signal(SIGCHLD, SIG_DFL);

request_init(&req, RQ_DAEMON, libwrapName, RQ_FILE, new_sock, NULL);

my_fromhost(&req);

if (!my_hosts_access(&req))//判斷主機(jī)是否具有訪問(wèn)權(quán)限

{//如果主機(jī)沒(méi)有被賦予給予權(quán)限,,則拒絕連接

/*

This may be stupid but refuse() includes an exit(0)

which we surely don't want...

clean_exit() - same stupid thing ...

*/

syslog(deny_severity, "refused connect from %s",

my_eval_client(&req));

/*

C++ sucks (the gibberish in front just translates the supplied

sink function pointer in the req structure from a void (*sink)();

to a void(*sink)(int) if you omit the cast, the C++ compiler

will cry...

*/

if (req.sink)

((void (*)(int))req.sink)(req.fd);

(void) shutdown(new_sock, SHUT_RDWR);//關(guān)閉連接

(void) closesocket(new_sock);

continue;

}

}

}

#endif /* HAVE_LIBWRAP */

{

size_socket dummyLen;

struct sockaddr_storage dummy;

dummyLen = sizeof(dummy);

if ( getsockname(new_sock,(struct sockaddr *)&dummy,

(SOCKET_SIZE_TYPE *)&dummyLen) < 0 )

{

sql_perror("Error on new connection socket");

(void) shutdown(new_sock, SHUT_RDWR);

(void) closesocket(new_sock);

continue;

}

}

/*

** Don't allow too many connections

*/

if (!(thd= new THD))//為該連接分配處理的結(jié)構(gòu),在以后用于給線程使用

{

(void) shutdown(new_sock, SHUT_RDWR);

(void) closesocket(new_sock);

continue;

}

if (!(vio_tmp=vio_new(new_sock,

sock == unix_sock ? VIO_TYPE_SOCKET :

VIO_TYPE_TCPIP,

sock == unix_sock ? VIO_LOCALHOST: 0)) ||

my_net_init(&thd->net,vio_tmp))//初始化網(wǎng)絡(luò)

{

/*

Only delete the temporary vio if we didn't already attach it to the

NET object. The destructor in THD will delete any initialized net

structure.

*/

if (vio_tmp && thd->net.vio != vio_tmp)

vio_delete(vio_tmp);

else

{

(void) shutdown(new_sock, SHUT_RDWR);

(void) closesocket(new_sock);

}

delete thd;

continue;

}

if (sock == unix_sock)

thd->security_ctx->host=(char*) my_localhost;

create_new_thread(thd);//準(zhǔn)備為連接分配線程

}

DBUG_VOID_RETURN;

}

linux

聲明:本網(wǎng)頁(yè)內(nèi)容旨在傳播知識(shí),若有侵權(quán)等問(wèn)題請(qǐng)及時(shí)與本網(wǎng)聯(lián)系,我們將在第一時(shí)間刪除處理。TEL:177 7030 7066 E-MAIL:11247931@qq.com

文檔

MySQL服務(wù)器監(jiān)聽(tīng)客戶(hù)端連接源碼

MySQL服務(wù)器監(jiān)聽(tīng)客戶(hù)端連接源碼:在下面的代碼有,有些結(jié)構(gòu)體還不清楚,如THD,st_vio,pollfd等。但可以看出MySQL使用Select模型來(lái)接收客戶(hù)端的連接。而且也在網(wǎng)上 在下面的代碼有,有些結(jié)構(gòu)體還不清楚,如THD,st_vio,pollfd等。但可以看出MySQL使用Select模型來(lái)接收客戶(hù)端的連接
推薦度:
  • 熱門(mén)焦點(diǎn)

最新推薦

猜你喜歡

熱門(mén)推薦

專(zhuān)題
Top