mysql_client_server_protocol.h, new file name that avoids conflicts with previous one

This commit is contained in:
Massimiliano Pinto
2013-06-17 22:47:14 +02:00
parent f4711f5980
commit 4d5215e267
5 changed files with 330 additions and 32 deletions

View File

@ -24,10 +24,21 @@
* 17/06/2013 Massimiliano Pinto Common MySQL protocol routines
*/
#include "mysql_protocol.h"
#include "mysql_client_server_protocol.h"
static char *version_str = "V1.0.0";
//static int gw_create_backend_connection(DCB *client_dcb, int efd);
static MySQLProtocol *gw_mysql_init(MySQLProtocol *data);
static void gw_mysql_close(MySQLProtocol **ptr);
extern gw_read_backend_event(DCB* dcb, int epfd);
extern gw_write_backend_event(DCB *dcb, int epfd);
extern int gw_MySQLWrite_backend(DCB *dcb, GWBUF *queue);
extern int gw_error_backend_event(DCB *dcb, int epfd, int event);
///////////////////////////////
// Initialize mysql protocol struct
///////////////////////////////////////
MySQLProtocol *gw_mysql_init(MySQLProtocol *data) {
int rv = -1;
@ -53,7 +64,7 @@ MySQLProtocol *gw_mysql_init(MySQLProtocol *data) {
//////////////////////////////////////
// close a connection if opened
// free data scructure
// free data scructure for MySQLProtocol
//////////////////////////////////////
void gw_mysql_close(MySQLProtocol **ptr) {
MySQLProtocol *conn = *ptr;
@ -85,3 +96,79 @@ void gw_mysql_close(MySQLProtocol **ptr) {
fprintf(stderr, "mysqlgw_mysql_close() free(conn) done\n");
#endif
}
/*
* Create a new MySQL backend connection.
*
* This routine performs the MySQL connection to the backend and fills the session->backends of the callier dcb
* with the new allocatetd dcb and adds the new socket to the epoll set
*
* - backend dcb allocation
* - MySQL session data fetch
* - backend connection using data in MySQL session
*
* @param client_dcb The client DCB struct
* @param epfd The epoll set to add the new connection
* @return 0 on Success or 1 on Failure.
*/
/*
* This function cannot work as it will be called from mysql_client.c but it needs function pointers from mysql_backend.c
* They are modules loaded separately!!
*
int gw_create_backend_connection(DCB *client_dcb, int efd) {
struct epoll_event ee;
DCB *backend = NULL;
MySQLProtocol *ptr_proto = NULL;
MySQLProtocol *client_protocol = NULL;
SESSION *session = NULL;
MYSQL_session *s_data = NULL;
backend = (DCB *) calloc(1, sizeof(DCB));
backend->state = DCB_STATE_ALLOC;
backend->session = NULL;
backend->protocol = (MySQLProtocol *)gw_mysql_init(NULL);
ptr_proto = (MySQLProtocol *)backend->protocol;
client_protocol = (MySQLProtocol *)client_dcb->protocol;
session = DCB_SESSION(client_dcb);
s_data = (MYSQL_session *)session->data;
// this is blocking until auth done
if (gw_mysql_connect("127.0.0.1", 3306, s_data->db, s_data->user, s_data->client_sha1, backend->protocol) == 0) {
fprintf(stderr, "Connected to backend mysql server\n");
backend->fd = ptr_proto->fd;
setnonblocking(backend->fd);
} else {
fprintf(stderr, "<<<< NOT Connected to backend mysql server!!!\n");
backend->fd = -1;
}
// edge triggering flag added
ee.events = EPOLLIN | EPOLLET | EPOLLOUT;
ee.data.ptr = backend;
// if connected, add it to the epoll
if (backend->fd > 0) {
if (epoll_ctl(efd, EPOLL_CTL_ADD, backend->fd, &ee) == -1) {
perror("epoll_ctl: backend sock");
} else {
fprintf(stderr, "--> Backend conn added, bk_fd [%i], scramble [%s], is session with client_fd [%i]\n", ptr_proto->fd, ptr_proto->scramble, client_dcb->fd);
backend->state = DCB_STATE_POLLING;
backend->session = DCB_SESSION(client_dcb);
(backend->func).read = gw_read_backend_event;
(backend->func).write = gw_MySQLWrite_backend;
(backend->func).write_ready = gw_write_backend_event;
(backend->func).error = gw_error_backend_event;
// assume here one backend only.
// in session.h
// struct dcb *backends;
// instead of a list **backends;
client_dcb->session->backends = backend;
}
return 0;
}
return 1;
}
*/