From 34372fbc5df92f87adb4277d12abd13b57b86c7d Mon Sep 17 00:00:00 2001 From: Mark Riddoch Date: Fri, 14 Jun 2013 17:55:31 +0200 Subject: [PATCH] Addition of connect function to dcb functions and added dcb_connect routine --- core/dcb.c | 41 +++++++++++++++++++++++++++++++++++++++++ include/dcb.h | 5 +++++ include/session.h | 2 +- 3 files changed, 47 insertions(+), 1 deletion(-) diff --git a/core/dcb.c b/core/dcb.c index d3647026c..95d5c8a0c 100644 --- a/core/dcb.c +++ b/core/dcb.c @@ -30,6 +30,8 @@ #include #include #include +#include +#include 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 * diff --git a/include/dcb.h b/include/dcb.h index 5c7fc036e..13efaee5f 100644 --- a/include/dcb.h +++ b/include/dcb.h @@ -21,6 +21,7 @@ #include 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 */ diff --git a/include/session.h b/include/session.h index f6e3dea03..083f7ee21 100644 --- a/include/session.h +++ b/include/session.h @@ -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