This commit is contained in:
Massimiliano Pinto 2013-06-14 18:13:23 +02:00
commit 936ccfeda3
3 changed files with 47 additions and 1 deletions

View File

@ -30,6 +30,8 @@
#include <string.h>
#include <dcb.h>
#include <spinlock.h>
#include <server.h>
#include <session.h>
static DCB *allDCBs = NULL; /* Diagnotics need a list of DCBs */
static SPINLOCK *dcbspin = NULL;
@ -104,6 +106,45 @@ free_dcb(DCB *dcb)
free(dcb);
}
/*
* Connect to a server
*
* @param server The server to connect to
* @param session The session this connection is being made for
* @param protcol The protocol module to use
*/
DCB *
connect_dcb(SERVER *server, SESSION *session, const char *protocol)
{
DCB *dcb;
GWPROTOCOL *funcs;
int epollfd = -1; // Need to work out how to get this
if ((dcb = alloc_dcb()) == NULL)
{
return NULL;
}
if ((funcs = load_module(protocol, "Protocol")) == NULL)
{
free(dcb);
return NULL;
}
memcpy(&(dcb->func), funcs, sizeof(GWPROTOCOL));
dcb->session = session;
if ((dcb->fd = dcb->func.connect(server, session, epollfd)) == -1)
{
free(dcb);
return NULL;
}
/*
* We are now connected, the authentication etc will happen as
* part of the EPOLLOUT event that will be received once the connection
* is established.
*/
return dcb;
}
/*
* Diagnostic to print a DCB
*

View File

@ -21,6 +21,7 @@
#include <buffer.h>
struct session;
struct server;
/*
* The function pointer table used by descriptors to call relevant functions
@ -48,6 +49,8 @@ typedef struct gw_protocol {
* error EPOLLERR handler for the socket
* hangup EPOLLHUP handler for the socket
* accept Accept handler for listener socket only
* connect Create a connection to the specified server
* for the session pased in
* close Gateway close entry point for the socket
*/
int (*read)(struct dcb *, int);
@ -56,6 +59,7 @@ typedef struct gw_protocol {
int (*error)(struct dcb *, int);
int (*hangup)(struct dcb *, int);
int (*accept)(struct dcb *, int);
int (*connect)(struct server *, struct session *, int);
int (*close)(struct dcb *, int);
} GWPROTOCOL;
@ -98,6 +102,7 @@ typedef struct dcb {
extern DCB *alloc_dcb(); /* Allocate a DCB */
extern void free_dcb(DCB *); /* Free a DCB */
extern DCB *connect_dcb(struct server *, struct session *, const char *);
extern void printAllDCBs(); /* Debug to print all DCB in the system */
extern void printDCB(DCB *); /* Debug print routine */
extern const char *gw_dcb_state2string(int); /* DCB state to string */

View File

@ -36,7 +36,7 @@ typedef struct session {
int state; /* Current descriptor state */
struct dcb *client; /* The client connection */
struct dcb *backends; /* The set of backend servers */
void *data; /* The session data */
void *data; /* The session data */
} SESSION;
#define SESSION_STATE_ALLOC 0