MSession now works with the new MCache server

This commit is contained in:
Mark L. Woodward 2006-01-30 16:18:40 +00:00
parent cf0736cab6
commit 8b24460752
7 changed files with 1395 additions and 733 deletions

View File

@ -1,8 +1,8 @@
This is msession, it is an interface to a stand-alone session
management system. The msession daemon can be found at
Mohawk Software's web site.
Mohawk Software's web site, under Project Phoenix
http://www.mohawksoft.com
http://www.mohawksoft.com/phoenix.html
Requirements:
Mohawk Software's Phoenix library.

View File

@ -6,12 +6,12 @@ PHP_ARG_WITH(msession, for msession support,
[ --with-msession[=DIR] Include msession support])
if test "$PHP_MSESSION" != "no"; then
if test -r $PHP_MSESSION/lib/libphoenix.a; then
if test -r $PHP_MSESSION/lib/libphoenix.so; then
PHOENIX_DIR=$PHP_MSESSION
else
AC_MSG_CHECKING(for msession in default path)
for i in /usr/local/phoenix /usr/local /usr; do
if test -r $i/lib/libphoenix.a; then
for i in /opt/mohawk /usr/local/phoenix /usr/local /usr; do
if test -r $i/lib/libphoenix.so; then
PHOENIX_DIR=$i
AC_MSG_RESULT(found in $i)
fi
@ -23,14 +23,13 @@ if test "$PHP_MSESSION" != "no"; then
AC_MSG_ERROR(Please reinstall the Phoenix / msession distribution)
fi
AC_DEFINE(HAVE_MSESSION, 1, [ ])
PHP_ADD_LIBRARY_WITH_PATH(phoenix, "$PHOENIX_DIR/lib", PHOENIX_LIB)
PHOENIX_INCLUDE="-I$PHOENIX_DIR"
PHOENIX_INCLUDE="-I$PHOENIX_DIR/src"
PHP_SUBST(PHOENIX_LIB)
PHP_REQUIRE_CXX
PHP_REQUIRE_CXX
PHP_NEW_EXTENSION(msession, msession.c, $ext_shared)
PHP_ADD_LIBRARY(stdc++)
PHP_ADD_LIBRARY(stdc++)
fi

View File

@ -15,50 +15,45 @@
# Start sessions, this will set a cookie.
session_start();
msession_connect('localhost','8086');
# Now, optional, use msession_uniq() to create a guarenteed
# uniq session name.
#
if(!$HTTP_COOKIE_VARS["PHPSESSID"])
{
# Use uniq to create the session. This is guarenteed to be
# uniq in the server.
$sid = msession_uniq(32);
setcookie ("PHPSESSID", $sid);
session_id($sid);
$HTTP_COOKIE_VARS["PHPSESSID"] = $sid;
# New session, set some variables
if(0) // One at a time
{
echo "Set Variable: " . msession_set($sid, 'time',time()) ."<p>\n";
echo "Set Variable: " . msession_set($sid, 'name1','test1') ."<p>\n";
echo "Set Variable: " . msession_set($sid, 'name2','test2') ."<p>\n";
echo "Set Variable: " . msession_set($sid, 'name3','test3') ."<p>\n";
echo "Set Variable: " . msession_set($sid, 'name4','test4') ."<p>\n";
echo "Set Variable: " . msession_set($sid, 'name5','test5') ."<p>\n";
echo "Set Variable: " . msession_set($sid, 'name6','test6') ."<p>\n";
echo "Set Variable: " . msession_set($sid, 'name7','test7') ."<p>\n";
}
else // All at once in an array
{
$setarray = array();
$setarray['time']=time();
$setarray['name1'] = 'test1';
$setarray['name2'] = 'test2';
$setarray['name3'] = 'test3';
$setarray['name4'] = 'test4';
$setarray['name5'] = 'test5';
$setarray['name6'] = 'test6';
$setarray['name7'] = 'test7';
msession_set_array($sid, $setarray);
}
}
else
{
# Use uniq to create the session. This is guarenteed to be
# uniq in the server.
if($HTTP_COOKIE_VARS["PHPSESSID"])
$sid = $HTTP_COOKIE_VARS["PHPSESSID"];
}
else
$sid = msession_uniq(32, "phpsession");
setcookie ("PHPSESSID", $sid);
session_id($sid);
$HTTP_COOKIE_VARS["PHPSESSID"] = $sid;
# New session, set some variables
echo "Set Variable: " . msession_set($sid, 'time',time()) ."<p>\n";
echo "Set Variable: " . msession_set($sid, 'name1','test1') ."<p>\n";
echo "Set Variable: " . msession_set($sid, 'name2','test2') ."<p>\n";
echo "Set Variable: " . msession_set($sid, 'name3','test3') ."<p>\n";
echo "Set Variable: " . msession_set($sid, 'name4','test4') ."<p>\n";
echo "Set Variable: " . msession_set($sid, 'name5','test5') ."<p>\n";
echo "Set Variable: " . msession_set($sid, 'name6','test6') ."<p>\n";
echo "Set Variable: " . msession_set($sid, 'name7','test7') ."<p>\n";
$setarray = array();
$setarray['time']=time();
$setarray['name1'] = 'test1';
$setarray['name2'] = 'test2';
$setarray['name3'] = 'test3';
$setarray['name4'] = 'test4';
$setarray['name5'] = 'test5';
$setarray['name6'] = 'test6';
$setarray['name7'] = 'test7';
msession_set_array($sid, $setarray);
#This makes a link between the variable $count and the
# session variable "count"
session_register("count");
$count ++;
@ -77,6 +72,12 @@ echo "Random String: " . msession_randstr(32) . "<br>\n";
# newer versions create it by default)
echo "Global Count: " . msession_inc(globals, "counter") . "<br>\n";
# Thread safe functions
echo "Local Count: " . msession_add($sid, "counter", 2) . "<br>\n";
echo "Muldiv counter * 2 / 3 :" . msession_muldiv($sid, "counter", "2", "3") . "<br>\n";
# This gets a count of active sessions.
echo "Total active sessions: " . msession_count() . "<br>\n";

File diff suppressed because it is too large Load Diff

366
ext/msession/msessionapi.h Normal file
View File

@ -0,0 +1,366 @@
/*
Mohawk Software Framework by Mohawk Software
Copyright (C) 1998-2001 Mark L. Woodward
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public
License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Library General Public License for more details.
You should have received a copy of the GNU Library General Public
License along with this library; if not, write to the Free
Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
MA 02111-1307, USA
If you want support or to professionally license this library, the author
can be reached at info@mohawksoft.com
When used as part of the PHP project this file, as present in the PHP
CVS and original source distribution is to be considered part of the
PHP package and subject to version 2.02 of the PHP license.
http://www.php.net/license/2_02.txt.
*/
#ifndef _MCACHE_API_
#define _MCACHE_API_
#include "reqclient.h"
#ifdef timeout
#undef timeout
#endif
#ifdef __cplusplus
class MCache_conn : public MSock
{
protected:
REQB m_reqb;
int m_errno;
public:
MCache_conn(char *host, int port);
~MCache_conn();
int Request(int stat, char *session, char *name, char *value, int param=0);
int RequestData(int stat, char *session, char *name, char *value, char *data, int cbdata, int param=0);
int RequestStrings(int stat, char *session, char *name, char *value, int n, char **strings);
char * ResultToStr();
char ** ResultToArray();
char ** ResultToPairs();
Boolean create(char *session, char *classname, char *strdata);
Boolean create_pairs(char *session, char *classname, char *strdata, int count, char **pairs);
Boolean destroy(char *session);
int set(char *session, char *name, char *value);
int set_array(char *session, int count, char **pairs);
int get_int(char *session, char *name, int def);
char *get_text(char *session, char *name, char *def);
char * uniq(int cbstr, char *classname, char *strdata);
char * randstr(int cbstr);
char **get_array(char *session);
char **get_object(char *session);
char **listvar(char *name);
char **find(char *name, char *value);
char **list();
int count();
int lock(char *session);
int unlock(char *session, int key);
Boolean timeout(char *session, int timeout);
int inc_int(char *session, char *name);
char *inc_text(char *session, char *name);
int setdata(char *session, char *data);
char *getdata(char *session);
int setdata(char *session, char *data, int len);
char *getdata(char *session, int *len);
int call_int(char *fn,int n, char **strings);
char *call_text(char *fn,int n, char **strings);
int ctl(char *session,int type);
int plugin_int(char *session, char *nparam, char *sparam, int param);
char *plugin_text(char *session, char *nparam, char *sparam, int param);
int Sessionflush(int seconds);
int admin(int fn, char *opt);
int serialize(char *szparam);
char *exec(char *szcommand);
char *add(char *session, char *name, char *value);
char *muldiv(char *session, char *name, char *mul, char *div);
int ping(void);
static void free(void *result);
static int ctl_type(char *str);
inline Boolean StatOK(void) {return (m_reqb.req->stat == REQ_OK) ? TRUE : FALSE; }
inline int param() {return m_reqb.param; }
inline int merrno() {return m_errno;}
inline char *errtext() {return ReqbErr(&m_reqb); }
inline char *reqtext(void) {return (m_reqb.req->len > 1) ? m_reqb.req->datum : NULL; }
};
typedef MCache_conn *MSCONN;
extern "C" {
#else
typedef void * MSCONN;
#endif
#define MAX_REQ_STRINGS 1024
#define MREQ_TOPHANDLER -42
enum REQ_TYPES
{
MREQ_CTL=2,
MREQ_SETVAL,
MREQ_GETVAL,
MREQ_CREATE,
MREQ_DROP,
MREQ_GETALL,
MREQ_FIND,
MREQ_COUNT,
MREQ_FLUSH,
MREQ_SLOCK,
MREQ_SUNLOCK,
MREQ_TIMEOUT,
MREQ_ADD,
MREQ_DATAGET,
MREQ_DATASET,
MREQ_LIST,
MREQ_LISTVAR,
MREQ_UNIQ,
MREQ_RANDSTR,
MREQ_PLUGIN,
MREQ_CALL,
MREQ_SERIALIZE,
MREQ_RESTORE,
MREQ_EXEC,
MREQ_FIND_CACHEID,
MREQ_SET_CACHEID,
MREQ_RENAME,
MREQ_GETOBJECT,
MREQ_MULDIV,
MREQ_LAST,
// These are handled by the server, not the library.
MREQ_ADMIN = REQ_INTERNAL_END,
};
#define MREQ_OK REQ_OK
#define MREQ_ERR REQ_ERR
#ifdef PLUGMSG_START
enum MREQ_PLUGIN_MSG
{
MREQ_PLUGIN_PULSE=PLUGMSG_START,
MREQ_PLUGIN_SESSION_FLUSH,
MREQ_PLUGIN_SESSION_CREATE,
MREQ_PLUGIN_SESSION_INIT,
MREQ_PLUGIN_SESSION_DESTROY,
MREQ_PLUGIN_ESCAPE,
MREQ_PLUGIN_RELAY,
MREQ_PLUGIN_SERIALIZE,
MREQ_PLUGIN_RESTORE,
MREQ_PLUGIN_SESSION_SETDATA,
MREQ_PLUGIN_SESSION_SETVALUE,
MREQ_PLUGIN_SESSION_SETBLOB,
MREQ_PLUGIN_SESSION_RENAME,
MREQ_PLUGIN_LAST
};
#define MREQ_SESSION_INIT_FAILED -1
#define MREQ_SESSION_INIT_HANDLED 1
#define MREQ_SESSION_INIT_NOTHANDLED 0
#endif
enum MCACHE_ADMIN
{
MADMIN_SHUTDOWN,
MADMIN_LOADDLL,
MADMIN_SETSERIAL,
MADMIN_SETVERBOSE,
MADMIN_SETMAXTHREADS,
MADMIN_SERIALIZE,
MADMIN_AUTOSAVE,
MADMIN_AUTODROP,
MADMIN_STATUS
};
#define MREQ_CTL_EXIST 0
#define MREQ_CTL_TTL 1
#define MREQ_CTL_AGE 2
#define MREQ_CTL_TLA 3
#define MREQ_CTL_CTIM 4
#define MREQ_CTL_TOUCH 5
#define MREQ_CTL_NOW 6
#define MPARM_TIME_MASK 0x0000FFFF
#define MPARM_TIME_FIXED 0x0000FFFF
#define MPARAM_CBSTR_MASK 0x00FF0000
#define MPARM_TIME(parm) ((parm)&MPARM_TIME_MASK)
#define MPARM_CBSTR(parm) (((parm) >> 16) & 0xFF)
#define MPARM_CREATE(TIME, CBSTR) \
((TIME) | (CBSTR << 16))
/* Added for MCache objects */
#define MSTAT_TYPE_SESSION 0x00000000
#define MSTAT_TYPE_OBJECT 0x00100000
#define MSTAT_TYPE_FAST 0x00200000
#define MSTAT_TYPE_FIXED 0x00400000
#define MSTAT_TYPE_MASK 0x00F00000
#define MSTAT_CMD_MASK 0x000FFFFF
#define MSTAT_TYPE_TEST(stat, TY) (((stat) & MSTAT_TYPE_MASK)==(TY))
#define MSTAT_CMD(stat) ((stat) & (MSTAT_CMD_MASK))
// Private session flags
#define MCACHE_FIXED MSTAT_TYPE_FIXED // Fixed object, no expiration
#define MCACHE_NORMAL MSTAT_TYPE_SESSION
#define MCACHE_OBJECT MSTAT_TYPE_OBJECT
#define MCACHE_FAST MSTAT_TYPE_FAST
#ifdef WIN32
#define EXTAPI extern "C" __declspec(dllexport)
#else
#define EXTAPI extern "C"
#endif
#ifndef MCACHEAPI_INTERNAL
MSCONN mcache_connect(char *host, int port);
/** Disconnects a session from an mcache daemon. Returns nothing. */
void mcache_disconnect(MSCONN conn);
/** Creates a new session. Returns TRUE is successful, or FALSE if an error occured.
An error occurs if the session already exists. mcache_errno() will return REQE_DUPSESSION
if the session exists */
Boolean mcache_create(MSCONN conn, char *session, char *classname, char *strdata);
/** Behaves exactly like mcache_create, except that initial data is passed to populate the session.
"pairs" is an array of string data. pairs[0] contains a name, pairs[1] contains its value, pairs[2]
contains the second name, pairs[3] containes the second value, and so on. */
Boolean mcache_create_pairs(MSCONN conn, char *session, char *classname, char *strdata, int count, char **pairs);
/** Destroys a session. Returns TRUE on success, FALSE on error. */
Boolean mcache_destroy(MSCONN conn, char *session);
/** Sets a value in a session. Returns TRUE on success, FALSE on error */
int mcache_set(MSCONN conn, char *session, char *name, char *value);
int mcache_set_array(MSCONN conn, char *session, int count, char **pairs);
/** Gets a value in a session as an integer. Returns the value from the session, or
def from the argument list, if no value was found. */
int mcache_get_int(MSCONN conn, char *session, char *name, int def);
/** Gets a value in a session as a string. Returns the value from the session, or
def from the argument list, if no value was found. */
char * mcache_get_text(MSCONN conn, char *session, char *name, char *def);
/** Create a new session with a guaranteed unique name. Returns a string of the name */
char * mcache_uniq(MSCONN conn, int cbstr, char *classname, char *strdata);
/** Create a pseudo-random alphanumeric string */
char * mcache_randstr(MSCONN conn, int cbstr);
/** Gets all the name/value pairs from a session into a character array. The array is
formatted as an array of string data. array[0] contains a name, array[1] contains its
value, array[2] contains the second name, array[3] containes the second value, and so on.
Returns NULL on error. */
char ** mcache_get_array(MSCONN conn, char *session);
char **mcache_get_object(MSCONN conn, char *session);
/** Finds sessions which have the particular name/value property. Returns NULL
if none were found. */
char ** mcache_find(MSCONN conn, char *name, char *value);
int mcache_inc_int(MSCONN conn, char *session, char *name);
char *mcache_inc_text(MSCONN conn, char *session, char *name);
char *mcache_add(MSCONN conn, char *session, char *name, char *value);
char *mcache_muldiv(MSCONN conn,char *sn,char *nam,char *mul,char *div);
/** Lists all active sessions. */
char ** mcache_list(MSCONN conn);
/** Returns an associative array of session:var for all sessions which have
a setting of name. */
char **mcache_listvar(MSCONN conn, char *name);
/** Returns the number of active sessions */
int mcache_count(MSCONN conn);
/** Locks a session. Returns an integer key for the lock.
Locks remain exclusive for 30 seconds, after which someone else
can lock the session. Locks are coperative, no enforcement
is attempted. */
int mcache_lock(MSCONN conn, char *session);
/** Unlocks a session */
int mcache_unlock(MSCONN conn, char *session, int key);
/** Sets the timeout of a session.
* timeout = 0 means never timeout
* timeout > 0 sets session expire time in seconds
* timeout < 0 resets to default timeout
*/
Boolean mcache_timeout(MSCONN conn, char *session, int timeout);
/** Sets the session data string, creates a session if one does not exist */
int mcache_setdata(MSCONN conn, char *session, char *data);
/** Gets the session data string from a session */
char *mcache_getdata(MSCONN conn, char *session);
/** Returns the last error. */
int mcache_errno(MSCONN conn);
/** Call a function plugin, return an int */
int mcache_call_int(MSCONN conn, char *fn,int n, char **strings);
/** Call a function plugin, return a char */
char *mcache_call_text(MSCONN conn, char *fn,int n, char **strings);
/** Call to get info about a session */
int mcache_ctl(MSCONN conn, char *session, int type);
/** Convert a string ctl to a type for mcache_ctl */
int mcache_ctl_type(char *str);
/** Call the personality plugin escape function, return int */
int mcache_plugin_int(MSCONN conn, char *session, char *nparam, char *sparam, int param);
/** Call the personality plugin escape function, return char * */
char *mcache_plugin_text(MSCONN conn, char *session, char *nparam, char *sparam, int param);
int mcache_flush(MSCONN conn, int seconds);
int mcache_loaddll(MSCONN conn, char *dll);
int mcache_shutdown(MSCONN conn, char *timeout);
int mcache_serialize(MSCONN conn, char *szparam);
char * mcache_exec(MSCONN conn, char *szparam);
int mcache_setserial(MSCONN conn, char *serialize);
int mcache_setmaxthreads(MSCONN conn, char *maxt);
int mcache_setverbose(MSCONN conn, char *verbose);
int mcache_setautosave(MSCONN conn, char *autsave);
int mcache_setautodrop(MSCONN conn, char *autdrop);
int mcache_ping(MSCONN conn);
int mcache_status(MSCONN conn);
Boolean mcache_statok(MSCONN conn);
char *mcache_text(MSCONN conn);
/** Returns a static character string describing an error. */
char * mcache_errtext(MSCONN conn);
#endif
#ifdef __cplusplus
}
#endif
#endif

View File

@ -2,12 +2,12 @@
+----------------------------------------------------------------------+
| msession 1.0 |
+----------------------------------------------------------------------+
| Copyright (c) 1997-2006 The PHP Group |
| Copyright (c) 1997-2003 The PHP Group |
+----------------------------------------------------------------------+
| This source file is subject to version 3.01 of the PHP license, |
| This source file is subject to version 2.02 of the PHP license, |
| that is bundled with this package in the file LICENSE, and is |
| available through the world-wide-web at the following url: |
| http://www.php.net/license/3_01.txt |
| available at through the world-wide-web at |
| http://www.php.net/license/2_02.txt. |
| If you did not receive a copy of the PHP license and are unable to |
| obtain it through the world-wide-web, please send a note to |
| license@php.net so we can mail you a copy immediately. |
@ -35,6 +35,9 @@ extern zend_module_entry msession_module_entry;
#endif
PHP_MINIT_FUNCTION(msession);
PHP_MSHUTDOWN_FUNCTION(msession);
PHP_RINIT_FUNCTION(msession);
PHP_RSHUTDOWN_FUNCTION(msession);
PHP_MINFO_FUNCTION(msession);
PHP_FUNCTION(msession_connect);
@ -45,50 +48,41 @@ PHP_FUNCTION(msession_ctl);
PHP_FUNCTION(msession_count);
PHP_FUNCTION(msession_create);
PHP_FUNCTION(msession_destroy);
PHP_FUNCTION(msession_exec);
PHP_FUNCTION(msession_set);
PHP_FUNCTION(msession_get);
PHP_FUNCTION(msession_find);
PHP_FUNCTION(msession_get_array);
PHP_FUNCTION(msession_save_object);
PHP_FUNCTION(msession_get_object);
PHP_FUNCTION(msession_set_array);
PHP_FUNCTION(msession_timeout);
PHP_FUNCTION(msession_inc);
PHP_FUNCTION(msession_set_data);
PHP_FUNCTION(msession_get_data);
PHP_FUNCTION(msession_add);
PHP_FUNCTION(msession_muldiv);
PHP_FUNCTION(msession_setdata);
PHP_FUNCTION(msession_getdata);
PHP_FUNCTION(msession_listvar);
PHP_FUNCTION(msession_list);
PHP_FUNCTION(msession_uniq);
PHP_FUNCTION(msession_randstr);
PHP_FUNCTION(msession_plugin);
PHP_FUNCTION(msession_call);
PHP_FUNCTION(msession_ping);
/*
Declare any global variables you may need between the BEGIN
and END macros here:
PHP_FUNCTION(msession_find_cacheid);
PHP_FUNCTION(msession_set_cacheid);
ZEND_BEGIN_MODULE_GLOBALS(msession)
int global_variable;
void *conn;
int port;
char *host;
void *conn;
void *reqb;
ZEND_END_MODULE_GLOBALS(msession)
*/
/* In every function that needs to use variables in php_msession_globals,
do call MSESSIONLS_FETCH(); after declaring other variables used by
that function, and always refer to them as MSESSIONG(variable).
You are encouraged to rename these macros something shorter, see
examples in any other php module directory.
*/
#ifdef ZTS
#define MSESSIONG(v) (msession_globals->v)
#define MSESSIONLS_FETCH() php_msession_globals *msession_globals = ts_resource(msession_globals_id)
#define MSESSION_G(v) TSRMG(msession_globals_id, zend_msession_globals *, v)
#else
#define MSESSIONG(v) (msession_globals.v)
#define MSESSIONLS_FETCH()
#define MSESSION_G(v) (msession_globals.v)
#endif
#else
#define phpext_msession_ptr NULL

View File

@ -1,76 +1,45 @@
/*
+----------------------------------------------------------------------+
| msession 1.0 |
+----------------------------------------------------------------------+
| Copyright (c) 1997-2006 The PHP Group |
+----------------------------------------------------------------------+
| This source file is subject to version 3.01 of the PHP license, |
| that is bundled with this package in the file LICENSE, and is |
| available through the world-wide-web at the following url: |
| http://www.php.net/license/3_01.txt |
| If you did not receive a copy of the PHP license and are unable to |
| obtain it through the world-wide-web, please send a note to |
| license@php.net so we can mail you a copy immediately. |
+----------------------------------------------------------------------+
| Authors: Mark Woodward <markw@mohawksoft.com> |
| Portions copyright the PHP group. |
+----------------------------------------------------------------------+
*/
/*
--------------------------------------------------------------------------
These are definitions are pulled from Phoenix.
It would probably be easier to maintain one file,
but some phoenix header files conflict with other
project header files common to PHP. Besides, this
allows this header to be PHP license and Phoenix to
be LGPL with no conflicts.
MAKE NO CHANGES TO THIS FILE IT. MUST REMAIN CONSTANT
WITH PHOENIX OR IT WILL NOT WORK.
--------------------------------------------------------------------------
*/
/* Required to use the new version of Phoenix 1.2 */
Mohawk Software Framework by Mohawk Software
Copyright (C) 1998-2001 Mark L. Woodward
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public
License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Library General Public License for more details.
You should have received a copy of the GNU Library General Public
License along with this library; if not, write to the Free
Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
MA 02111-1307, USA
If you want support or to professionally license this library, the author
can be reached at info@mohawksoft.com
*/
#ifndef _REQCLIENT_
#define _REQCLIENT_
#ifndef REQCLIENT_VER
#define REQCLIENT_VER 030113
#define REQCLIENT_VER 051222
#endif
enum REQ_TYPES
{
REQ_ERR,
REQ_OK,
REQ_CTL,
REQ_SETVAL,
REQ_GETVAL,
REQ_CREATE,
REQ_DROP,
REQ_GETALL,
REQ_FIND,
REQ_COUNT,
REQ_FLUSH,
REQ_SLOCK,
REQ_SUNLOCK,
REQ_TIMEOUT,
REQ_INC,
REQ_DATAGET,
REQ_DATASET,
REQ_LIST,
REQ_LISTVAR,
REQ_UNIQ,
REQ_RANDSTR,
REQ_PLUGIN,
REQ_CALL,
REQ_SERIALIZE,
REQ_RESTORE,
REQ_EXEC,
REQ_LAST,
REQ_INTERNAL_BEGIN=1023,
REQ_INTERNALLAST,
};
#ifndef _PHOENIX_
typedef unsigned char Boolean;
#endif
#if defined (__cplusplus)
extern "C" {
#endif
// Standard request transport errors.
enum REQ_ERRORS
{
REQE_NOERROR=0,
REQE_ERR,
REQE_NOSESSION,
REQE_DUPSESSION,
REQE_NOWLOCK,
@ -85,14 +54,47 @@ enum REQ_ERRORS
REQE_BUFFER,
REQE_DENIED,
REQE_NOFN,
REQE_UNKNOWN
REQE_UNKNOWN,
REQE_PLUGIN1,
REQE_PLUGIN2,
REQE_PLUGIN3,
REQE_PLUGIN4
};
#define REQ_OK 0
#define REQ_ERR 1
#define REQ_UNHANDLED 3
#define REQ_POPEN 1024
#define REQ_PCLOSE 1025
#define REQ_PING 1026
#define REQ_INTERNAL_BEGIN 1024
#define REQ_POPEN 1024
#define REQ_PCLOSE 1025
#define REQ_PING 1026
#define REQ_INTERNAL_END 2048
/** Packet version */
#define REQV_VERSION 0x42
#define REQV_VERMASK 0xFF
/** Packet has array of strings */
#define REQVF_STRINGS 0x0200
/** Packet has binary information */
#define REQVF_BVAL 0x0400
#define REQV_ISVER(V) (((V) & REQV_VERMASK) == REQV_VERSION)
#define REQVF_TEST(V,F) (((V) & (F)) == (F))
#define REQVSZ_VERSION "MV42"
#define REQVSZ_STRINGS "MS42"
#define REQVSZ_BVAL "MB42"
#define REQFMT_BINARY 0
#define REQFMT_ASCII 1
// An application can use this to change the default
extern unsigned int g_defReqFmt;
/** Binary request packet */
/** If sizeof(int) > 4 this will need to be fixed. */
typedef struct _requestPacket
{
int version;
@ -102,70 +104,106 @@ typedef struct _requestPacket
int name;
int value;
int param;
int data;
char datum[0];
}REQ;
/*** Ascii string version of request packet */
typedef struct _REQSZ
{
char version[4];
char stat[4];
char len[4];
char session[4];
char name[4];
char value[4];
char param[4];
char misc[4];
char datum[0];
}REQSZ;
typedef struct _requestBuf
{
unsigned int type; /* Type of packet, dynamic/static */
unsigned int size; /* size of memory block */
unsigned int type; // Type of packet, dynamic/static
unsigned int size; // size of memory block
#if (REQCLIENT_VER >= 030113)
unsigned int fmt; /* format, binary/ascii */
unsigned int reserved; /* Just in case */
unsigned int fmt; // format, binary/ascii
unsigned int reserved; // Just in case
#else
#warning You are using an old Phoenix definition, this will have problems with a newer version
#warning You are using an old Phoenix definition (pre 030113), this will have problems with a newer version
#endif
REQ req;
#if (REQCLIENT_VER >= 051222)
int cmd;
int param;
char * session;
char * name;
char * value;
char * data;
int cbdata;
// To be used by library layer for bookeeping
int cb;
int count;
#endif
REQ *req;
}REQB;
typedef struct ListWalkDataStruct
{
REQB *reqb;
char *Key;
char *Data;
int cb;
int count;
}ListWalkData;
#define MAX_REQ 16384
#define REQB_STATIC 1
#define REQB_DYNAMIC 2
#define REQ_STAT_EXIST 0
#define REQ_STAT_TTL 1
#define REQ_STAT_AGE 2
#define REQ_STAT_TLA 3
#define REQ_STAT_CTIM 4
#define REQ_STAT_TOUCH 5
#define REQ_STAT_NOW 6
#define STATIC_REQB( len ) \
char buffer [ len ]; \
REQB *preq = StaticRequestBuffer(buffer, len);
#if defined (__cplusplus)
extern "C" {
#endif
#define ASCII_REQB(reqb) (REQB)->fmt = REQFMT_ASCII
#define BINARY_REQB(reqb) (REQB)->fmt = REQFMT_BINARY
#define SIZEREQB(REQB,SIZE) (((REQB)->size >= (unsigned int) SIZE) ? REQB : SizeRequestBuffer(REQB,SIZE))
REQB *AllocateRequestBuffer(unsigned size);
REQB *AllocateRequestBuffer(REQB *reqb, unsigned size);
void FreeRequestBuffer(REQB *req);
REQB *SizeRequestBuffer(REQB *req, unsigned int size);
REQB *StaticRequestBuffer(char *buffer, unsigned int cb);
int FormatRequest(REQB **buffer, int stat, const char *session, const char *name, const char *value, int param);
int FormatRequestMulti(REQB **buffer, int stat, char *session, int n, char **pairs, int param);
int FormatRequestStrings(REQB **ppreq, int stat, char *session, int n, char **strings);
int DoSingleRequest(char *hostname, int port, REQB **preq);
int FilterRequest(REQB *preq);
int FormatRequest(REQB *preq, int stat, char *session, char *name, char *value, int param);
int FormatRequestData(REQB *preq, int stat, char *session, char *name, char *value, void *data, int cbdata, int param);
int FormatRequestStrings(REQB *preq, int stat, char *session, char *name, char *value, int n, char **strings);
int FormatRequestf(REQB *preq, int stat, char *session, char *name, char *value, int param, char *fmt, ...);
void *GetReqbDatumPtr(REQB *preq);
int DoSingleRequest(char *hostname, int port, REQB *preq);
void *OpenReqConn(char *hostname, int port);
Boolean ReopenReqConn(void *conn);
void CloseReqConn(void *conn);
void DeleteReqConn(void *conn);
unsigned char ReopenReqConn(void *conn);
int DoRequest(void *conn, REQB **preq);
int DoRequest(void *conn, REQB *preq);
char *ReqbErr(REQB *reqb);
int LWDaddNameVal(ListWalkData *plw, char *name, char *value);
int LWDaddValue(ListWalkData *plw, char *value);
#define ASSERT_STAT(PREQ) if(PREQ->stat != REQ_OK) \
{fprintf(stderr, "Error in Request %s %d %s\n", \
__FILE__,__LINE__, ReqErr(PREQ->param)); exit(-1); }
#if defined (__cplusplus)
/* C API but with class definitions */
int ReadRequestTimeout(REQB **ppreq, MSock *sock, int timeout);
int ReadRequest(REQB **preq, MSock *sock);
int WriteRequest(REQB *preq, MSock *sock);
// C API but with class definitions
int ReadRequestTimeout(REQB *preq, MSock *sock, int timeout);
int ReadRequest(REQB *reqb, MSock *sock);
int WriteRequest(REQB *reqb, MSock *sock);
Boolean OpenReqSock(REQB *reqb, MSock *sock, char *hostname);
void CloseReqSock(REQB *reqb, MSock *sock);
}
#endif
#endif
#endif