Main Page   Class Hierarchy   Compound List   File List   Compound Members  

cliproto.h

00001 //-< CLIPROTO.H >----------------------------------------------------*--------*
00002 // FastDB                    Version 1.0         (c) 1999  GARRET    *     ?  *
00003 // (Main Memory Database Management System)                          *   /\|  *
00004 //                                                                   *  /  \  *
00005 //                          Created:     13-Jan-2000  K.A. Knizhnik  * / [] \ *
00006 //                          Last update: 13-Jan-2000  K.A. Knizhnik  * GARRET *
00007 //-------------------------------------------------------------------*--------*
00008 // Client-server communication protocol
00009 //-------------------------------------------------------------------*--------*
00010 
00011 #ifndef __CLIPROTO_H__
00012 #define __CLIPROTO_H__
00013 
00014 enum cli_commands { 
00015     cli_cmd_close_session, 
00016     cli_cmd_prepare_and_execute,
00017     cli_cmd_execute,
00018     cli_cmd_get_first, 
00019     cli_cmd_get_last, 
00020     cli_cmd_get_next,
00021     cli_cmd_get_prev,
00022     cli_cmd_free_statement,
00023     cli_cmd_abort,
00024     cli_cmd_commit,
00025     cli_cmd_update,
00026     cli_cmd_remove,
00027     cli_cmd_insert,
00028     cli_cmd_prepare_and_insert,
00029     cli_cmd_describe_table,
00030     cli_cmd_show_tables,
00031     cli_cmd_precommit
00032 };
00033 
00034 static const int sizeof_type[] = { 
00035     sizeof(cli_oid_t), 
00036     sizeof(cli_bool_t), 
00037     sizeof(cli_int1_t), 
00038     sizeof(cli_int2_t), 
00039     sizeof(cli_int4_t), 
00040     sizeof(cli_int8_t), 
00041     sizeof(cli_real4_t), 
00042     sizeof(cli_real8_t),
00043     0, // cli_asciiz, 
00044     0, // cli_pasciiz,
00045     0, // cli_array_of_oid,
00046     0, // cli_array_of_bool,
00047     0, // cli_array_of_int1,
00048     0, // cli_array_of_int2,
00049     0, // cli_array_of_int4,
00050     0, // cli_array_of_int8,
00051     0, // cli_array_of_real4,
00052     0, // cli_array_of_real8, 
00053     4  // cli_autoincrement
00054 };
00055 
00056 static const int fd2cli_type_mapping[] = {
00057     cli_bool,
00058     cli_int1,
00059     cli_int2,
00060     cli_int4,
00061     cli_int8,
00062     cli_real4,
00063     cli_real8,
00064     cli_asciiz,
00065     cli_oid
00066 };
00067 
00068 #if defined(__FreeBSD__)
00069 #include <sys/param.h>
00070 #define USE_HTON_NTOH
00071 #elif defined(__linux__)
00072 //
00073 // At Linux inline assembly declarations of ntohl, htonl... are available
00074 //
00075 #include <netinet/in.h>
00076 #define USE_HTON_NTOH
00077 #else
00078 #if defined(_WIN32) && _M_IX86 >= 400
00079 #pragma warning(disable:4035) // disable "no return" warning
00080 inline int swap_bytes_in_dword(int val) {
00081     __asm {
00082           mov eax, val
00083           bswap eax
00084     }
00085 }
00086 inline short swap_bytes_in_word(short val) {
00087     __asm {
00088           mov ax, val
00089           xchg al,ah
00090     }
00091 }
00092 #pragma warning(default:4035)
00093 #define ntohl(w) swap_bytes_in_dword(w)
00094 #define htonl(w) swap_bytes_in_dword(w)
00095 #define ntohs(w) swap_bytes_in_word(w)
00096 #define htons(w) swap_bytes_in_word(w)
00097 
00098 #define USE_HTON_NTOH
00099 #endif
00100 #endif
00101 
00102 
00103 
00104 
00105 inline char* pack2(char* dst, int2 val) { 
00106     *dst++ = char(val >> 8);
00107     *dst++ = char(val);     
00108     return dst;
00109 }
00110 
00111 inline char* pack2(char* dst, char* src) { 
00112     return pack2(dst, *(int2*)src); 
00113 }
00114 
00115 inline void pack2(int2& val) { 
00116 #if BYTE_ORDER != BIG_ENDIAN
00117 #ifdef USE_HTON_NTOH
00118     val = htons(val);
00119 #else
00120     pack2((char*)&val, val); 
00121 #endif
00122 #endif
00123 }
00124 
00125 
00126 inline char* pack4(char* dst, int4 val) { 
00127     *dst++ = char(val >> 24);
00128     *dst++ = char(val >> 16);     
00129     *dst++ = char(val >> 8); 
00130     *dst++ = char(val);
00131     return dst;
00132 }
00133 
00134 inline char* pack4(char* dst, char* src) { 
00135     return pack4(dst, *(int4*)src); 
00136 }
00137 
00138 inline void pack4(int4& val) { 
00139 #if BYTE_ORDER != BIG_ENDIAN
00140 #ifdef USE_HTON_NTOH
00141     val = htonl(val);
00142 #else
00143     pack4((char*)&val, val); 
00144 #endif
00145 #endif
00146 }
00147 
00148 
00149 inline char* pack8(char* dst, char* src) { 
00150 #if BYTE_ORDER == BIG_ENDIAN
00151     return pack4( pack4(dst, src), src + 4);
00152 #else
00153     return pack4( pack4(dst, src + 4), src);
00154 #endif
00155 }
00156 
00157 inline char* pack8(char* dst, db_int8 val) { 
00158     return pack8(dst, (char*)&val);
00159 }
00160 
00161 inline char* pack_oid(char* dst, cli_oid_t oid)
00162 {
00163     return (sizeof(oid) == 4) ? pack4(dst, oid) : pack8(dst, (char*)&oid);
00164 }
00165 
00166 inline int2 unpack2(char* src) { 
00167     nat1* s = (nat1*)src;
00168     return (s[0] << 8) + s[1]; 
00169 }
00170 
00171 inline char* unpack2(char* dst, char* src) { 
00172     *(int2*)dst = unpack2(src);
00173     return src + 2;
00174 }
00175 
00176 inline void  unpack2(int2& val) { 
00177 #if BYTE_ORDER != BIG_ENDIAN
00178 #ifdef USE_HTON_NTOH
00179     val = ntohs(val);
00180 #else
00181     val = unpack2((char*)&val); 
00182 #endif
00183 #endif
00184 }
00185 
00186 
00187 inline int4  unpack4(char* src) { 
00188     nat1* s = (nat1*)src;
00189     return (((((s[0] << 8) + s[1]) << 8) + s[2]) << 8) + s[3];
00190 } 
00191 
00192 inline char* unpack4(char* dst, char* src) { 
00193     *(int4*)dst = unpack4(src);
00194     return src + 4;
00195 }
00196 
00197 inline void unpack4(int4& val) { 
00198 #if BYTE_ORDER != BIG_ENDIAN
00199 #ifdef USE_HTON_NTOH
00200     val = ntohl(val);
00201 #else
00202     val = unpack4((char*)&val); 
00203 #endif
00204 #endif
00205 }
00206 
00207 inline char* unpack8(char* dst, char* src) { 
00208 #if BYTE_ORDER == BIG_ENDIAN
00209     *(int4*)dst = unpack4(src);
00210     *((int4*)dst+1) = unpack4(src+4);
00211 #else
00212     *(int4*)dst = unpack4(src+4);
00213     *((int4*)dst+1) = unpack4(src);
00214 #endif
00215     return src + 8;
00216 }
00217 
00218 inline db_int8 unpack8(char* src) { 
00219     db_int8 val;
00220     unpack8((char*)&val, src);
00221     return val;
00222 }
00223 
00224 inline cli_oid_t unpack_oid(char* src)
00225 {
00226     cli_oid_t oid;
00227     if (sizeof(oid) == 4) {
00228         oid = unpack4(src);
00229     } else { 
00230         unpack8((char*)&oid, src);
00231     }
00232     return oid;
00233 }
00234 
00235 struct cli_request { 
00236     int4 length;
00237     int4 cmd;
00238     int4 stmt_id;
00239     
00240     void pack() { 
00241         pack4(length);
00242         pack4(cmd);
00243         pack4(stmt_id);
00244     }
00245 
00246     void unpack() { 
00247         unpack4(length);
00248         unpack4(cmd);
00249         unpack4(stmt_id);
00250     }
00251 };
00252 
00253 #endif

Generated on Fri Nov 15 21:06:28 2002 for FastDB by doxygen1.2.15