27 Star 37 Fork 27

calvinwilliams/DirectStruct

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
README-CN 88.19 KB
一键复制 编辑 原始数据 按行查看 历史
calvinwilliams 提交于 2015-03-14 11:16 . UPDATE TO V1.3.0
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407
DirectStruct - C结构体工具
1.概述
2.编译安装
3.基本使用
3.1.编写IDL定义文件,用自动化代码工具dsc处理之
3.2.编写应用代码,调用网络/主机字节序化函数
3.3.编写应用代码,调用二进制序列化/反序列化函数
3.4.编写应用代码,调用XML序列化/反序列化函数
3.5.编写应用代码,调用JSON序列化/反序列化函数
3.6.编写应用代码,使用ESQL代码开发数据库应用
N-2.DirectStruct报文定义文件语法
N-1.性能压测
N.最后
------------------------------------------------------------
1.概述
DirectStruct是一个强有力的代码自动生成工具,它可以自动生成C结构体与XML、JSON、二进制(带压缩)等报文之间的转换代码,还能自动生成ESQL代码片段,在应用中直接使用自动化代码以提高开发效率,避免开发冗余和手工编码风险。
DirectStruct生成了哪些自动化代码?
* C结构体定义
* 批量初始化缺省值函数
* 网络/主机字节序化函数
* 二进制序列化/反序列化函数(可带压缩)
* XML序列化/反序列化函数(展开风格和紧缩风格)(依赖开源库fasterxml[url]http://git.oschina.net/calvinwilliams/fasterxml[/url])
* JSON序列化/反序列化函数(展开风格和紧缩风格)(依赖开源库fasterjson[url]http://git.oschina.net/calvinwilliams/fasterjson[/url])
* 嵌入式SQL代码片段以及一些嵌入式SQL函数
* 打印结构体内所有字段值函数
用DirectStruct自动生成的代码能干什么?
* 直接C结构体通讯交换 : 在特定平台之间直接把C结构体变量通过TCP扔给对方是一个很诱人的想法,阻碍我们的主要是整型字段的字节序问题,手工编码是一件冗余的事情,DirectStruct可以帮助你自动化这一过程。解决了字节序问题后就可以直接交换报文,避免了通讯发送前的打包和通讯接收后的解包,所以速度非常快,是某些追求极端性能的场景中的首选报文格式。
* 结构体序列化/反序列化 : 使用DirectStruct自动生成的序列化/反序列化函数,可以将C结构体打包成二进制报文或XML报文或JSON报文等,以及反向解包,是的,打包解包函数自动生成,省却了你不少麻烦吧。二进制报文如果启用了压缩选项,压缩后的二进制数据块大幅缩小,更便于通讯高效交换。
* 基于嵌入式SQL的数据库映射接口的代码自动化生成
* 快速打印结构体内所有字段值 : 对于一个大结构,编写他的打印所有字段值的函数是一件很无聊的事情,你可以用DirectStruct帮助你自动生成日志函数,输出到屏幕,或你自己的日志流中。
* ( 新主意增加中 ... 把你的需求告诉我吧 )
为什么使用DirectStruct?
* 灵活的报文定义语法 : 命令行工具dsc读入定义文件,生成自动化代码,支持子结构嵌套、子结构数组、定义文件包含等灵活配置方式。
* 跨平台:DirectStruct支持WINDOWS、Linux、AIX等主流操作系统,尤其是多平台中不同的表达方法,如64位整型,相同的IDL定义文件将生成不同平台的标准。
2.编译安装
for Linux
$ tar xvzf dsc-1.1.0.tar.gz
$ cd dsc-1.1.0/src
$ su
# make -f makefile.Linux clean
rm -f util.o
rm -f main.o
rm -f dsc.o
rm -f ReadDscFile.o
rm -f GenerateCCode.o
rm -f dsc
# make -f makefile.Linux install
gcc -g -fPIC -O2 -Wall -Werror -fno-strict-aliasing -I. -c util.c
gcc -g -fPIC -O2 -Wall -Werror -fno-strict-aliasing -I. -c main.c
gcc -g -fPIC -O2 -Wall -Werror -fno-strict-aliasing -I. -c dsc.c
gcc -g -fPIC -O2 -Wall -Werror -fno-strict-aliasing -I. -c ReadDscFile.c
gcc -g -fPIC -O2 -Wall -Werror -fno-strict-aliasing -I. -c GenerateCCode.c
gcc -g -fPIC -O2 -Wall -Werror -fno-strict-aliasing -o dsc util.o main.o dsc.o ReadDscFile.o GenerateCCode.o -L.
cp -f dsc /usr/bin/
3.基本使用
本章示例都取自于test目录中的测试程序。
3.1.编写IDL定义文件,用自动化代码工具dsc处理之
[code]
$ cat IDL_AllTypes.dsc
STRUCT AllTypes
{
# 这是注释
INT 1 n1 # 这也是注释
INT 2 n2
INT 4 n4
INT 8 n8
UINT 1 u1
UINT 2 u2
UINT 4 u4
UINT 8 u8
FLOAT 4 f4
FLOAT 8 f8
CHAR 1 ch
UCHAR 1 uch
STRING 32 str32
STRING 1024 str1024
}
$ dsc -f IDL_AllTypes.dsc -c-ALL
STRUCT AllTypes
INT 1 n1
INT 2 n2
INT 4 n4
INT 8 n8 # longlong or _int64 for c
UINT 1 u1
UINT 2 u2
UINT 4 u4
UINT 8 u8
FLOAT 4 f4
FLOAT 8 f8
CHAR 1 ch
UCHAR 1 uch
STRING 32 str32
STRING 1024 str1024
ok!
$ ls
IDL_AllTypes.dsc
IDL_AllTypes.dsc.h
IDL_AllTypes.dsc.c
IDL_AllTypes.dsc.LOG.c
[/code]
IDL_test.dsc.h包含了自动生成的一个C结构体定义。
IDL_test.dsc.c包含了自动生成的初始化缺省函数、网络/主机字节序化、序列化/反序列化等代码。
IDL_test.dsc.LOG.c包含了自动生成的一个日志函数用于快速打印报文所有字段到标准输出或你自己的日志流上便于调试。
注意:IDL_test.dsc.h、IDL_test.dsc.c实际内容取决于参数"-c-*"
[code]
$ dsc
dsc v1.1.0 - DirectSrtuct Compiler
Copyright by calvin<calvinwilliams.c@gmail.com> 2014
USAGE : dsc -f .dsc [ -c ] [ -c-order ] [ -c-compact ] [ -c-compress ] [ -c-xml ] [ -c-LOG ] [ -c-ALL ]
[/code]
-c 基本的c结构体定义
-c-order 还包含主机/网络字节序化函数
-c-compact 还包含二进制紧凑序列化/反序列化函数
-c-compress 还包含二进制压缩序列化/反序列化函数
-c-xml 还包含XML序列化/反序列化函数(依赖于开源库fastxml)
-c-LOG 生成IDL_test.dsc.LOG.c
-c-ALL 生成以上所有
注意:test/makefile未包含处理.dsc定义文件的动作,如修改.dsc文件后需要手工处理之。
3.2.编写应用代码,调用网络/主机字节序化函数
[code]
$ cat test_netorder.c
int test_netorder()
{
AllTypes st ;
int nret = 0 ;
/* client code */
memset( & st , 0x00 , sizeof(AllTypes) );
st.n1 = -0x11 ;
st.n2 = -0x1122 ;
st.n4 = -0x11223344 ;
st.n8 = LL(-0x1122334455667788) ;
st.u1 = 0x11 ;
st.u2 = 0x1122 ;
st.u4 = 0x11223344 ;
st.u8 = ULL(0x1122334455667788) ;
st.f4 = 100.00 ;
st.f8 = 10000.00 ;
st.ch = 'A' ;
st.uch = 'B' ;
strcpy( st.str32 , "calvin" );
strcpy( st.str1024 , "XXXXXXXXXXXXXXXX" );
DSCLOG_AllTypes( & st );
nret = DSCNETORDER_AllTypes( & st ) ;
if( nret )
{
printf( "DSCNETORDER_AllTypes failed[%d]\n" , nret );
return nret;
}
else
{
printf( "DSCNETORDER_AllTypes ok\n" );
}
/* ... client send communication struct to server ... */
/* server code */
nret = DSCHOSTORDER_AllTypes( & st ) ;
if( nret )
{
printf( "DSCHOSTORDER_AllTypes failed[%d]\n" , nret );
return nret;
}
else
{
printf( "DSCHOSTORDER_AllTypes ok\n" );
}
DSCLOG_AllTypes( & st );
return 0;
}
$ make -f makefile.Linux
gcc -g -fPIC -O2 -Wall -Werror -fno-strict-aliasing -I. -c test_netorder.c
gcc -g -fPIC -O2 -Wall -Werror -fno-strict-aliasing -I. -c IDL_AllTypes.dsc.c
gcc -g -fPIC -O2 -Wall -Werror -fno-strict-aliasing -o test_netorder test_netorder.o IDL_AllTypes.dsc.o -L.
$ ./test_netorder
AllTypes.n1[-17]
AllTypes.n2[-4386]
AllTypes.n4[-287454020]
AllTypes.n8[-1234605616436508552]
AllTypes.u1[17]
AllTypes.u2[4386]
AllTypes.u4[287454020]
AllTypes.u8[1234605616436508552]
AllTypes.f4[100.000000]
AllTypes.f8[10000.000000]
AllTypes.ch[A]
AllTypes.uch[B]
AllTypes.str32[calvin]
AllTypes.str1024[XXXXXXXXXXXXXXXX]
DSCNETORDER_AllTypes ok
0x0000000000 ef 00 ee de ee dd cc bc ee dd cc bb aa 99 88 78 ...............x
0x0000000001 11 00 11 22 11 22 33 44 11 22 33 44 55 66 77 88 ..."."3D."3DUfw.
0x0000000002 00 00 c8 42 00 00 00 00 00 88 c3 40 41 42 63 61 ...B.......@ABca
0x0000000003 6c 76 69 6e 00 00 00 00 00 00 00 00 00 00 00 00 lvin............
0x0000000004 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 58 ...............X
0x0000000005 58 58 58 58 58 58 58 58 58 58 58 58 58 58 58 00 XXXXXXXXXXXXXXX.
...
DSCHOSTORDER_AllTypes ok
AllTypes.n1[-17]
AllTypes.n2[-4386]
AllTypes.n4[-287454020]
AllTypes.n8[-1234605616436508552]
AllTypes.u1[17]
AllTypes.u2[4386]
AllTypes.u4[287454020]
AllTypes.u8[1234605616436508552]
AllTypes.f4[100.000000]
AllTypes.f8[10000.000000]
AllTypes.ch[A]
AllTypes.uch[B]
AllTypes.str32[calvin]
AllTypes.str1024[XXXXXXXXXXXXXXXX]
[/code]
3.3.编写应用代码,调用二进制序列化/反序列化函数
[code]
$ cat test_serialize_compact.c
int test_serialize_compact()
{
AllTypes st ;
char buf[ DSCSERIALIZE_COMPACT_BUFSIZE_AllTypes + 1 ] ;
int len ;
int nret = 0 ;
/* client code */
memset( & st , 0x00 , sizeof(AllTypes) );
st.n1 = -0x11 ;
st.n2 = -0x1122 ;
st.n4 = -0x11223344 ;
st.n8 = LL(-0x1122334455667788) ;
st.u1 = 0x11 ;
st.u2 = 0x1122 ;
st.u4 = 0x11223344 ;
st.u8 = ULL(0x1122334455667788) ;
st.f4 = 100.00 ;
st.f8 = 10000.00 ;
st.ch = 'A' ;
st.uch = 'B' ;
strcpy( st.str32 , "calvin" );
strcpy( st.str1024 , "XXXXXXXXXXXXXXXX" );
memset( buf , 0x00 , sizeof(buf) );
nret = DSCSERIALIZE_COMPACT_AllTypes( & st , buf , & len ) ;
if( nret )
{
printf( "DSCSERIALIZE_COMPACT_AllTypes failed[%d]\n" , nret );
return nret;
}
else
{
printf( "DSCSERIALIZE_COMPACT_AllTypes ok , len[%d]\n" , len );
}
dump( buf , len );
/* ... client send communication struct to server ... */
/* server code */
memset( & st , 0x00 , sizeof(AllTypes) );
nret = DSCDESERIALIZE_COMPACT_AllTypes( buf , & len , & st ) ;
if( nret )
{
printf( "DSCDESERIALIZE_COMPACT_AllTypes failed[%d]\n" , nret );
return nret;
}
else
{
printf( "DSCDESERIALIZE_COMPACT_AllTypes ok\n" );
}
DSCLOG_AllTypes( & st );
return 0;
}
$ make -f makefile.Linux
gcc -g -fPIC -O2 -Wall -Werror -fno-strict-aliasing -I. -c test_netorder.c
gcc -g -fPIC -O2 -Wall -Werror -fno-strict-aliasing -I. -c test_serialize_compact.c
gcc -g -fPIC -O2 -Wall -Werror -fno-strict-aliasing -o test_serialize_compact test_serialize_compact.o IDL_AllTypes.dsc.o -L.
$ ./test_serialize_compact
DSCSERIALIZE_COMPACT_AllTypes ok , len[1100]
0x0000000000 ef ee de ee dd cc bc ee dd cc bb aa 99 88 78 11 ..............x.
0x0000000001 11 22 11 22 33 44 11 22 33 44 55 66 77 88 00 00 ."."3D."3DUfw...
0x0000000002 c8 42 00 00 00 00 00 88 c3 40 41 42 63 61 6c 76 .B.......@ABcalv
0x0000000003 69 6e 00 00 00 00 00 00 00 00 00 00 00 00 00 00 in..............
0x0000000004 00 00 00 00 00 00 00 00 00 00 00 00 58 58 58 58 ............XXXX
0x0000000005 58 58 58 58 58 58 58 58 58 58 58 58 00 00 00 00 XXXXXXXXXXXX....
...
DSCDESERIALIZE_COMPACT_AllTypes ok
AllTypes.n1[-17]
AllTypes.n2[-4386]
AllTypes.n4[-287454020]
AllTypes.n8[-1234605616436508552]
AllTypes.u1[17]
AllTypes.u2[4386]
AllTypes.u4[287454020]
AllTypes.u8[1234605616436508552]
AllTypes.f4[100.000000]
AllTypes.f8[10000.000000]
AllTypes.ch[A]
AllTypes.uch[B]
AllTypes.str32[calvin]
AllTypes.str1024[XXXXXXXXXXXXXXXX]
[/code]
紧凑(COMPACT)序列化的内部原理是把所有字段值在内存中的映像平铺在一个大数据块中,首尾相接,无填充空间,且整型值做字节序处理。如
STRUCT struct_name
{
INT 4 n4
STRING 256 str256
}
序列化后的布局为
n4(4字节)+str256(256字节)
总共260字节
使用带压缩的序列化函数可以使得最终二进制数据块更小
[code]
$ cat test_serialize_compress.c
...
int test_serialize_compress()
{
AllTypes st ;
char buf[ DSCSERIALIZE_COMPRESS_BUFSIZE_AllTypes + 1 ] ;
int len ;
int nret = 0 ;
/* client code */
memset( & st , 0x00 , sizeof(AllTypes) );
st.n1 = -0x11 ;
st.n2 = -0x1122 ;
st.n4 = -0x11223344 ;
st.n8 = LL(-0x1122334455667788) ;
st.u1 = 0x11 ;
st.u2 = 0x1122 ;
st.u4 = 0x11223344 ;
st.u8 = ULL(0x1122334455667788) ;
st.f4 = 100.00 ;
st.f8 = 10000.00 ;
st.ch = 'A' ;
st.uch = 'B' ;
strcpy( st.str32 , "calvin" );
strcpy( st.str1024 , "XXXXXXXXXXXXXXXX" );
DSCLOG_AllTypes( & st );
memset( buf , 0x00 , sizeof(buf) );
nret = DSCSERIALIZE_COMPRESS_AllTypes( & st , buf , & len ) ;
if( nret )
{
printf( "DSCSERIALIZE_COMPRESS_AllTypes failed[%d]\n" , nret );
return nret;
}
else
{
printf( "DSCSERIALIZE_COMPRESS_AllTypes ok , len[%d]\n" , len );
}
dump( buf , len );
/* ... client send communication struct to server ... */
/* server code */
memset( & st , 0x00 , sizeof(AllTypes) );
nret = DSCDESERIALIZE_COMPRESS_AllTypes( buf , & len , & st ) ;
if( nret )
{
printf( "DSCDESERIALIZE_COMPRESS_AllTypes failed[%d]\n" , nret );
return nret;
}
else
{
printf( "DSCDESERIALIZE_COMPRESS_AllTypes ok\n" );
}
DSCLOG_AllTypes( & st );
return 0;
}
...
$ make -f makefile.Linux
gcc -g -fPIC -O2 -Wall -Werror -fno-strict-aliasing -I. -c test_netorder.c
gcc -g -fPIC -O2 -Wall -Werror -fno-strict-aliasing -I. -c test_serialize_compress.c
gcc -g -fPIC -O2 -Wall -Werror -fno-strict-aliasing -o test_serialize_compress test_serialize_compress.o IDL_AllTypes.dsc.o -L.
$ ./test_serialize_compress
AllTypes.n1[-17]
AllTypes.n2[-4386]
AllTypes.n4[-287454020]
AllTypes.n8[-1234605616436508552]
AllTypes.u1[17]
AllTypes.u2[4386]
AllTypes.u4[287454020]
AllTypes.u8[1234605616436508552]
AllTypes.f4[100.000000]
AllTypes.f8[10000.000000]
AllTypes.ch[A]
AllTypes.uch[B]
AllTypes.str32[calvin]
AllTypes.str1024[XXXXXXXXXXXXXXXX]
DSCSERIALIZE_COMPRESS_AllTypes ok , len[74]
0x0000000000 ef ee de 14 11 22 33 44 18 11 22 33 44 55 66 77 ....."3D.."3DUfw
0x0000000001 88 11 11 22 04 11 22 33 44 08 11 22 33 44 55 66 ...".."3D.."3DUf
0x0000000002 77 88 00 00 c8 42 00 00 00 00 00 88 c3 40 41 42 w....B.......@AB
0x0000000003 01 06 63 61 6c 76 69 6e 01 10 58 58 58 58 58 58 ..calvin..XXXXXX
0x0000000004 58 58 58 58 58 58 58 58 58 58 XXXXXXXXXX
DSCDESERIALIZE_COMPRESS_AllTypes ok
AllTypes.n1[-17]
AllTypes.n2[-4386]
AllTypes.n4[-287454020]
AllTypes.n8[-1234605616436508552]
AllTypes.u1[17]
AllTypes.u2[4386]
AllTypes.u4[287454020]
AllTypes.u8[1234605616436508552]
AllTypes.f4[100.000000]
AllTypes.f8[10000.000000]
AllTypes.ch[A]
AllTypes.uch[B]
AllTypes.str32[calvin]
AllTypes.str1024[XXXXXXXXXXXXXXXX]
[/code]
这个示例中不压缩的二进制数据块有1100字节,压缩后只有74字节,适用于网络数据交换。
压缩(COMPRESS)序列化的内部原理是在紧凑序列化的基础上对某些类型的字段做压缩转换处理。
INT 4类型放置前置头字节,左四位为选项,目前只用到了正负数标志(正数为0,负数为1),右四位为实际存储长度,如某值的网络字节序为0x00001234,实际存储为0x1234,加上前置头,总共用3字节表达
0x02 0x12 0x34
值-0x1234也是用3字节表达
0x12 0x12 0x34
INT 8类同
UINT类型INT
STRING n类型放置前置UINT 4字段记录长度。如"abc",实际长度为3字节,前置UINT 4字段0x01 0x03,总共用5字节表达
0x01 0x03 0x61 0x62 0x63
其它字段类型不压缩转换
STRUCT struct_name
{
INT 4 n4
STRING 256 str256
}
n4=0x3456
str256="abc"
压缩序列化后的数据为
0x02 0x34 0x56 0x01 0x05 0x61 0x62 0x63
总共8字节,看,是不是比紧凑序列化要短的多呢,这在通讯数据交换中可以节省更多的流量。
3.4.编写应用代码,调用XML序列化/反序列化函数
XML序列化/反序列化函数依赖于开源库fasterxml。
[code]
$ cat test_serialize_xml.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include "IDL_BankXmlTransaction.dsc.h"
#include "IDL_BankXmlTransaction.dsc.LOG.c"
int test_serialize_xml()
{
BankXmlTransaction trans ;
char buf[ 40960 + 1 ] ;
int len ;
int nret = 0 ;
/* client code */
DSCINIT_BankXmlTransaction( & trans );
strcpy( trans.ResponseHeader.transaction_code , "P0101" );
trans.ResponseHeader.trans_jnlsno = 12345678 ;
trans.ResponseHeader.response_code = 0 ;
strcpy( trans.ResponseHeader.response_desc , "OK" );
strcpy( trans.QueryTransactionDetails.AddonMessages.AddonMessage[1].message_text , "Check channel passed" );
strcpy( trans.QueryTransactionDetails.AddonMessages.AddonMessage[0].message_text , "Check account passed" );
strcpy( trans.QueryTransactionDetails.TransactionDetailTitle.title_text , "DETAIL TITLE" );
trans.QueryTransactionDetails.TransactionDetailTitle.page_no = 1 ;
trans.QueryTransactionDetails.TransactionDetailTitle.page_size = 2 ;
strcpy( trans.QueryTransactionDetails.TransactionDetails.TransactionDetail[0].trans_date , "2014-01-01" );
strcpy( trans.QueryTransactionDetails.TransactionDetails.TransactionDetail[0].trans_time , "08:01:01" );
strcpy( trans.QueryTransactionDetails.TransactionDetails.TransactionDetail[0].outlet_id , "1001" );
strcpy( trans.QueryTransactionDetails.TransactionDetails.TransactionDetail[0].card_no , "603367123412341234" );
trans.QueryTransactionDetails.TransactionDetails.TransactionDetail[0].trans_amount = 100.00 ;
strcpy( trans.QueryTransactionDetails.TransactionDetails.TransactionDetail[1].trans_date , "2014-02-02" );
strcpy( trans.QueryTransactionDetails.TransactionDetails.TransactionDetail[1].trans_time , "08:02:02" );
strcpy( trans.QueryTransactionDetails.TransactionDetails.TransactionDetail[1].outlet_id , "2002" );
strcpy( trans.QueryTransactionDetails.TransactionDetails.TransactionDetail[1].card_no , "603367123412341234" );
trans.QueryTransactionDetails.TransactionDetails.TransactionDetail[1].trans_amount = 200.00 ;
strcpy( trans.QueryTransactionDetails.TransactionDetails.TransactionDetail[2].card_no , "<" );
strcpy( trans.QueryTransactionDetails.TransactionDetails.TransactionDetail[3].card_no , "&" );
strcpy( trans.QueryTransactionDetails.TransactionDetails.TransactionDetail[4].card_no , ">" );
strcpy( trans.QueryTransactionDetails.TransactionDetails.TransactionDetail[5].card_no , "<&>" );
strcpy( trans.QueryTransactionDetails.TransactionDetails.TransactionDetail[6].card_no , "<你&他>" );
strcpy( trans.QueryTransactionDetails.TransactionDetails.TransactionDetail[7].card_no , "我<你&他>她" );
DSCLOG_BankXmlTransaction( & trans );
memset( buf , 0x00 , sizeof(buf) );
len = sizeof(buf)-1 ;
nret = DSCSERIALIZE_XML_BankXmlTransaction( & trans , "GBK" , buf , & len ) ;
if( nret )
{
printf( "DSCSERIALIZE_XML_BankXmlTransaction failed[%d]\n" , nret );
return nret;
}
else
{
printf( "DSCSERIALIZE_XML_BankXmlTransaction ok , len[%d]\n" , len );
}
printf( "[%s]\n" , buf );
/* ... client send communication struct to server ... */
/* server code */
memset( & trans , 0x00 , sizeof(BankXmlTransaction) );
nret = DSCDESERIALIZE_XML_BankXmlTransaction( NULL , buf , & len , & trans ) ;
if( nret )
{
printf( "DSCDESERIALIZE_XML_BankXmlTransaction failed[%d]\n" , nret );
return nret;
}
else
{
printf( "DSCDESERIALIZE_XML_BankXmlTransaction ok\n" );
}
DSCLOG_BankXmlTransaction( & trans );
return 0;
}
int main()
{
return -test_serialize_xml();
}
$ make
gcc -g -fPIC -O2 -Wall -Werror -fno-strict-aliasing -I. -c test_serialize_xml.c
gcc -g -fPIC -O2 -Wall -Werror -fno-strict-aliasing -I/home/calvin/exinc/fastxml -I. -c IDL_BankXmlTransaction.dsc.c
gcc -g -fPIC -O2 -Wall -Werror -fno-strict-aliasing -o test_serialize_xml test_serialize_xml.o IDL_BankXmlTransaction.dsc.o -L. -L/home/calvin/exlib -lfastxl
$ ./test_serialize_xml
BankXmlTransaction.version[1]
BankXmlTransaction.ResponseHeader.transaction_code[P0101]
BankXmlTransaction.ResponseHeader.trans_jnlsno[12345678]
BankXmlTransaction.ResponseHeader.response_code[0]
BankXmlTransaction.ResponseHeader.response_desc[OK]
BankXmlTransaction.QueryTransactionDetails.AddonMessages.AddonMessage[index[3]].message_text[Check account passed]
BankXmlTransaction.QueryTransactionDetails.AddonMessages.AddonMessage[index[3]].message_text[Check channel passed]
BankXmlTransaction.QueryTransactionDetails.AddonMessages.AddonMessage[index[3]].message_text[]
BankXmlTransaction.QueryTransactionDetails.TransactionDetailTitle.title_text[DETAIL TITLE]
BankXmlTransaction.QueryTransactionDetails.TransactionDetailTitle.page_no[1]
BankXmlTransaction.QueryTransactionDetails.TransactionDetailTitle.page_size[2]
BankXmlTransaction.QueryTransactionDetails.TransactionDetails.TransactionDetail[index[3]].trans_date[2014-01-01]
BankXmlTransaction.QueryTransactionDetails.TransactionDetails.TransactionDetail[index[3]].trans_time[08:01:01]
BankXmlTransaction.QueryTransactionDetails.TransactionDetails.TransactionDetail[index[3]].outlet_id[1001]
BankXmlTransaction.QueryTransactionDetails.TransactionDetails.TransactionDetail[index[3]].card_no[603367123412341234]
BankXmlTransaction.QueryTransactionDetails.TransactionDetails.TransactionDetail[index[3]].trans_amount[100.000000]
BankXmlTransaction.QueryTransactionDetails.TransactionDetails.TransactionDetail[index[3]].trans_date[2014-02-02]
BankXmlTransaction.QueryTransactionDetails.TransactionDetails.TransactionDetail[index[3]].trans_time[08:02:02]
BankXmlTransaction.QueryTransactionDetails.TransactionDetails.TransactionDetail[index[3]].outlet_id[2002]
BankXmlTransaction.QueryTransactionDetails.TransactionDetails.TransactionDetail[index[3]].card_no[603367123412341234]
BankXmlTransaction.QueryTransactionDetails.TransactionDetails.TransactionDetail[index[3]].trans_amount[200.000000]
BankXmlTransaction.QueryTransactionDetails.TransactionDetails.TransactionDetail[index[3]].trans_date[]
BankXmlTransaction.QueryTransactionDetails.TransactionDetails.TransactionDetail[index[3]].trans_time[]
BankXmlTransaction.QueryTransactionDetails.TransactionDetails.TransactionDetail[index[3]].outlet_id[]
BankXmlTransaction.QueryTransactionDetails.TransactionDetails.TransactionDetail[index[3]].card_no[<]
BankXmlTransaction.QueryTransactionDetails.TransactionDetails.TransactionDetail[index[3]].trans_amount[0.000000]
BankXmlTransaction.QueryTransactionDetails.TransactionDetails.TransactionDetail[index[3]].trans_date[]
BankXmlTransaction.QueryTransactionDetails.TransactionDetails.TransactionDetail[index[3]].trans_time[]
BankXmlTransaction.QueryTransactionDetails.TransactionDetails.TransactionDetail[index[3]].outlet_id[]
BankXmlTransaction.QueryTransactionDetails.TransactionDetails.TransactionDetail[index[3]].card_no[&]
BankXmlTransaction.QueryTransactionDetails.TransactionDetails.TransactionDetail[index[3]].trans_amount[0.000000]
BankXmlTransaction.QueryTransactionDetails.TransactionDetails.TransactionDetail[index[3]].trans_date[]
BankXmlTransaction.QueryTransactionDetails.TransactionDetails.TransactionDetail[index[3]].trans_time[]
BankXmlTransaction.QueryTransactionDetails.TransactionDetails.TransactionDetail[index[3]].outlet_id[]
BankXmlTransaction.QueryTransactionDetails.TransactionDetails.TransactionDetail[index[3]].card_no[>]
BankXmlTransaction.QueryTransactionDetails.TransactionDetails.TransactionDetail[index[3]].trans_amount[0.000000]
BankXmlTransaction.QueryTransactionDetails.TransactionDetails.TransactionDetail[index[3]].trans_date[]
BankXmlTransaction.QueryTransactionDetails.TransactionDetails.TransactionDetail[index[3]].trans_time[]
BankXmlTransaction.QueryTransactionDetails.TransactionDetails.TransactionDetail[index[3]].outlet_id[]
BankXmlTransaction.QueryTransactionDetails.TransactionDetails.TransactionDetail[index[3]].card_no[<&>]
BankXmlTransaction.QueryTransactionDetails.TransactionDetails.TransactionDetail[index[3]].trans_amount[0.000000]
BankXmlTransaction.QueryTransactionDetails.TransactionDetails.TransactionDetail[index[3]].trans_date[]
BankXmlTransaction.QueryTransactionDetails.TransactionDetails.TransactionDetail[index[3]].trans_time[]
BankXmlTransaction.QueryTransactionDetails.TransactionDetails.TransactionDetail[index[3]].outlet_id[]
BankXmlTransaction.QueryTransactionDetails.TransactionDetails.TransactionDetail[index[3]].card_no[<你&他>]
BankXmlTransaction.QueryTransactionDetails.TransactionDetails.TransactionDetail[index[3]].trans_amount[0.000000]
BankXmlTransaction.QueryTransactionDetails.TransactionDetails.TransactionDetail[index[3]].trans_date[]
BankXmlTransaction.QueryTransactionDetails.TransactionDetails.TransactionDetail[index[3]].trans_time[]
BankXmlTransaction.QueryTransactionDetails.TransactionDetails.TransactionDetail[index[3]].outlet_id[]
BankXmlTransaction.QueryTransactionDetails.TransactionDetails.TransactionDetail[index[3]].card_no[我<你&他>她]
BankXmlTransaction.QueryTransactionDetails.TransactionDetails.TransactionDetail[index[3]].trans_amount[0.000000]
BankXmlTransaction.QueryTransactionDetails.TransactionDetails.TransactionDetail[index[3]].trans_date[]
BankXmlTransaction.QueryTransactionDetails.TransactionDetails.TransactionDetail[index[3]].trans_time[]
BankXmlTransaction.QueryTransactionDetails.TransactionDetails.TransactionDetail[index[3]].outlet_id[]
BankXmlTransaction.QueryTransactionDetails.TransactionDetails.TransactionDetail[index[3]].card_no[]
BankXmlTransaction.QueryTransactionDetails.TransactionDetails.TransactionDetail[index[3]].trans_amount[0.000000]
BankXmlTransaction.QueryTransactionDetails.TransactionDetails.TransactionDetail[index[3]].trans_date[]
BankXmlTransaction.QueryTransactionDetails.TransactionDetails.TransactionDetail[index[3]].trans_time[]
BankXmlTransaction.QueryTransactionDetails.TransactionDetails.TransactionDetail[index[3]].outlet_id[]
BankXmlTransaction.QueryTransactionDetails.TransactionDetails.TransactionDetail[index[3]].card_no[]
BankXmlTransaction.QueryTransactionDetails.TransactionDetails.TransactionDetail[index[3]].trans_amount[0.000000]
DSCSERIALIZE_XML_BankXmlTransaction ok , len[2734]
[<?xml version="1.0" encoding="GBK"?>
<BankXmlTransaction>
<version>1</version>
<ResponseHeader>
<transaction_code>P0101</transaction_code>
<trans_jnlsno>12345678</trans_jnlsno>
<response_code>0</response_code>
<response_desc>OK</response_desc>
</ResponseHeader>
<QueryTransactionDetails>
<AddonMessages>
<AddonMessage>
<message_text>Check account passed</message_text>
</AddonMessage>
<AddonMessage>
<message_text>Check channel passed</message_text>
</AddonMessage>
<AddonMessage>
<message_text></message_text>
</AddonMessage>
</AddonMessages>
<TransactionDetailTitle>
<title_text>DETAIL TITLE</title_text>
<page_no>1</page_no>
<page_size>2</page_size>
</TransactionDetailTitle>
<TransactionDetails>
<TransactionDetail>
<trans_date>2014-01-01</trans_date>
<trans_time>08:01:01</trans_time>
<outlet_id>1001</outlet_id>
<card_no>603367123412341234</card_no>
<trans_amount>100.000000</trans_amount>
</TransactionDetail>
<TransactionDetail>
<trans_date>2014-02-02</trans_date>
<trans_time>08:02:02</trans_time>
<outlet_id>2002</outlet_id>
<card_no>603367123412341234</card_no>
<trans_amount>200.000000</trans_amount>
</TransactionDetail>
<TransactionDetail>
<trans_date></trans_date>
<trans_time></trans_time>
<outlet_id></outlet_id>
<card_no>&lt;</card_no>
<trans_amount>0.000000</trans_amount>
</TransactionDetail>
<TransactionDetail>
<trans_date></trans_date>
<trans_time></trans_time>
<outlet_id></outlet_id>
<card_no>&amp;</card_no>
<trans_amount>0.000000</trans_amount>
</TransactionDetail>
<TransactionDetail>
<trans_date></trans_date>
<trans_time></trans_time>
<outlet_id></outlet_id>
<card_no>&gt;</card_no>
<trans_amount>0.000000</trans_amount>
</TransactionDetail>
<TransactionDetail>
<trans_date></trans_date>
<trans_time></trans_time>
<outlet_id></outlet_id>
<card_no>&lt;&amp;&gt;</card_no>
<trans_amount>0.000000</trans_amount>
</TransactionDetail>
<TransactionDetail>
<trans_date></trans_date>
<trans_time></trans_time>
<outlet_id></outlet_id>
<card_no>&lt;你&amp;他&gt;</card_no>
<trans_amount>0.000000</trans_amount>
</TransactionDetail>
<TransactionDetail>
<trans_date></trans_date>
<trans_time></trans_time>
<outlet_id></outlet_id>
<card_no>我&lt;你&amp;他&gt;她</card_no>
<trans_amount>0.000000</trans_amount>
</TransactionDetail>
<TransactionDetail>
<trans_date></trans_date>
<trans_time></trans_time>
<outlet_id></outlet_id>
<card_no></card_no>
<trans_amount>0.000000</trans_amount>
</TransactionDetail>
<TransactionDetail>
<trans_date></trans_date>
<trans_time></trans_time>
<outlet_id></outlet_id>
<card_no></card_no>
<trans_amount>0.000000</trans_amount>
</TransactionDetail>
</TransactionDetails>
</QueryTransactionDetails>
</BankXmlTransaction>
]
DSCDESERIALIZE_XML_BankXmlTransaction ok
BankXmlTransaction.version[1]
BankXmlTransaction.ResponseHeader.transaction_code[P0101]
BankXmlTransaction.ResponseHeader.trans_jnlsno[12345678]
BankXmlTransaction.ResponseHeader.response_code[0]
BankXmlTransaction.ResponseHeader.response_desc[OK]
BankXmlTransaction.QueryTransactionDetails.AddonMessages.AddonMessage[index[3]].message_text[Check account passed]
BankXmlTransaction.QueryTransactionDetails.AddonMessages.AddonMessage[index[3]].message_text[Check channel passed]
BankXmlTransaction.QueryTransactionDetails.AddonMessages.AddonMessage[index[3]].message_text[]
BankXmlTransaction.QueryTransactionDetails.TransactionDetailTitle.title_text[DETAIL TITLE]
BankXmlTransaction.QueryTransactionDetails.TransactionDetailTitle.page_no[1]
BankXmlTransaction.QueryTransactionDetails.TransactionDetailTitle.page_size[2]
BankXmlTransaction.QueryTransactionDetails.TransactionDetails.TransactionDetail[index[3]].trans_date[2014-01-01]
BankXmlTransaction.QueryTransactionDetails.TransactionDetails.TransactionDetail[index[3]].trans_time[08:01:01]
BankXmlTransaction.QueryTransactionDetails.TransactionDetails.TransactionDetail[index[3]].outlet_id[1001]
BankXmlTransaction.QueryTransactionDetails.TransactionDetails.TransactionDetail[index[3]].card_no[603367123412341234]
BankXmlTransaction.QueryTransactionDetails.TransactionDetails.TransactionDetail[index[3]].trans_amount[100.000000]
BankXmlTransaction.QueryTransactionDetails.TransactionDetails.TransactionDetail[index[3]].trans_date[2014-02-02]
BankXmlTransaction.QueryTransactionDetails.TransactionDetails.TransactionDetail[index[3]].trans_time[08:02:02]
BankXmlTransaction.QueryTransactionDetails.TransactionDetails.TransactionDetail[index[3]].outlet_id[2002]
BankXmlTransaction.QueryTransactionDetails.TransactionDetails.TransactionDetail[index[3]].card_no[603367123412341234]
BankXmlTransaction.QueryTransactionDetails.TransactionDetails.TransactionDetail[index[3]].trans_amount[200.000000]
BankXmlTransaction.QueryTransactionDetails.TransactionDetails.TransactionDetail[index[3]].trans_date[]
BankXmlTransaction.QueryTransactionDetails.TransactionDetails.TransactionDetail[index[3]].trans_time[]
BankXmlTransaction.QueryTransactionDetails.TransactionDetails.TransactionDetail[index[3]].outlet_id[]
BankXmlTransaction.QueryTransactionDetails.TransactionDetails.TransactionDetail[index[3]].card_no[<]
BankXmlTransaction.QueryTransactionDetails.TransactionDetails.TransactionDetail[index[3]].trans_amount[0.000000]
BankXmlTransaction.QueryTransactionDetails.TransactionDetails.TransactionDetail[index[3]].trans_date[]
BankXmlTransaction.QueryTransactionDetails.TransactionDetails.TransactionDetail[index[3]].trans_time[]
BankXmlTransaction.QueryTransactionDetails.TransactionDetails.TransactionDetail[index[3]].outlet_id[]
BankXmlTransaction.QueryTransactionDetails.TransactionDetails.TransactionDetail[index[3]].card_no[&]
BankXmlTransaction.QueryTransactionDetails.TransactionDetails.TransactionDetail[index[3]].trans_amount[0.000000]
BankXmlTransaction.QueryTransactionDetails.TransactionDetails.TransactionDetail[index[3]].trans_date[]
BankXmlTransaction.QueryTransactionDetails.TransactionDetails.TransactionDetail[index[3]].trans_time[]
BankXmlTransaction.QueryTransactionDetails.TransactionDetails.TransactionDetail[index[3]].outlet_id[]
BankXmlTransaction.QueryTransactionDetails.TransactionDetails.TransactionDetail[index[3]].card_no[>]
BankXmlTransaction.QueryTransactionDetails.TransactionDetails.TransactionDetail[index[3]].trans_amount[0.000000]
BankXmlTransaction.QueryTransactionDetails.TransactionDetails.TransactionDetail[index[3]].trans_date[]
BankXmlTransaction.QueryTransactionDetails.TransactionDetails.TransactionDetail[index[3]].trans_time[]
BankXmlTransaction.QueryTransactionDetails.TransactionDetails.TransactionDetail[index[3]].outlet_id[]
BankXmlTransaction.QueryTransactionDetails.TransactionDetails.TransactionDetail[index[3]].card_no[<&>]
BankXmlTransaction.QueryTransactionDetails.TransactionDetails.TransactionDetail[index[3]].trans_amount[0.000000]
BankXmlTransaction.QueryTransactionDetails.TransactionDetails.TransactionDetail[index[3]].trans_date[]
BankXmlTransaction.QueryTransactionDetails.TransactionDetails.TransactionDetail[index[3]].trans_time[]
BankXmlTransaction.QueryTransactionDetails.TransactionDetails.TransactionDetail[index[3]].outlet_id[]
BankXmlTransaction.QueryTransactionDetails.TransactionDetails.TransactionDetail[index[3]].card_no[<你&他>]
BankXmlTransaction.QueryTransactionDetails.TransactionDetails.TransactionDetail[index[3]].trans_amount[0.000000]
BankXmlTransaction.QueryTransactionDetails.TransactionDetails.TransactionDetail[index[3]].trans_date[]
BankXmlTransaction.QueryTransactionDetails.TransactionDetails.TransactionDetail[index[3]].trans_time[]
BankXmlTransaction.QueryTransactionDetails.TransactionDetails.TransactionDetail[index[3]].outlet_id[]
BankXmlTransaction.QueryTransactionDetails.TransactionDetails.TransactionDetail[index[3]].card_no[我<你&他>她]
BankXmlTransaction.QueryTransactionDetails.TransactionDetails.TransactionDetail[index[3]].trans_amount[0.000000]
BankXmlTransaction.QueryTransactionDetails.TransactionDetails.TransactionDetail[index[3]].trans_date[]
BankXmlTransaction.QueryTransactionDetails.TransactionDetails.TransactionDetail[index[3]].trans_time[]
BankXmlTransaction.QueryTransactionDetails.TransactionDetails.TransactionDetail[index[3]].outlet_id[]
BankXmlTransaction.QueryTransactionDetails.TransactionDetails.TransactionDetail[index[3]].card_no[]
BankXmlTransaction.QueryTransactionDetails.TransactionDetails.TransactionDetail[index[3]].trans_amount[0.000000]
BankXmlTransaction.QueryTransactionDetails.TransactionDetails.TransactionDetail[index[3]].trans_date[]
BankXmlTransaction.QueryTransactionDetails.TransactionDetails.TransactionDetail[index[3]].trans_time[]
BankXmlTransaction.QueryTransactionDetails.TransactionDetails.TransactionDetail[index[3]].outlet_id[]
BankXmlTransaction.QueryTransactionDetails.TransactionDetails.TransactionDetail[index[3]].card_no[]
BankXmlTransaction.QueryTransactionDetails.TransactionDetails.TransactionDetail[index[3]].trans_amount[0.000000]
[/code]
3.5.编写应用代码,调用JSON序列化/反序列化函数
JSON序列化/反序列化函数依赖于开源库fasterjson。
[code]
$ cat test_serialize_json.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include "IDL_BankJsonTransaction.dsc.h"
#include "IDL_BankJsonTransaction.dsc.LOG.c"
int test_serialize_json()
{
BankJsonTransaction trans ;
char buf[ 40960 + 1 ] ;
int len ;
int nret = 0 ;
/* client code */
DSCINIT_BankJsonTransaction( & trans );
strcpy( trans.ResponseHeader.transaction_code , "P0101" );
trans.ResponseHeader.trans_jnlsno = 12345678 ;
trans.ResponseHeader.response_code = 0 ;
strcpy( trans.ResponseHeader.response_desc , "OK" );
strcpy( trans.QueryTransactionDetails.AddonMessages.AddonMessage[1].message_text , "Check channel passed" );
strcpy( trans.QueryTransactionDetails.AddonMessages.AddonMessage[0].message_text , "Check account passed" );
strcpy( trans.QueryTransactionDetails.TransactionDetailTitle.title_text , "DETAIL TITLE" );
trans.QueryTransactionDetails.TransactionDetailTitle.page_no = 1 ;
trans.QueryTransactionDetails.TransactionDetailTitle.page_size = 2 ;
strcpy( trans.QueryTransactionDetails.TransactionDetails.TransactionDetail[0].trans_date , "2014-01-01" );
strcpy( trans.QueryTransactionDetails.TransactionDetails.TransactionDetail[0].trans_time , "08:01:01" );
strcpy( trans.QueryTransactionDetails.TransactionDetails.TransactionDetail[0].outlet_id , "1001" );
strcpy( trans.QueryTransactionDetails.TransactionDetails.TransactionDetail[0].card_no , "603367123412341234" );
trans.QueryTransactionDetails.TransactionDetails.TransactionDetail[0].trans_amount = 100.00 ;
strcpy( trans.QueryTransactionDetails.TransactionDetails.TransactionDetail[1].trans_date , "2014-02-02" );
strcpy( trans.QueryTransactionDetails.TransactionDetails.TransactionDetail[1].trans_time , "08:02:02" );
strcpy( trans.QueryTransactionDetails.TransactionDetails.TransactionDetail[1].outlet_id , "2002" );
strcpy( trans.QueryTransactionDetails.TransactionDetails.TransactionDetail[1].card_no , "603367123412341234" );
trans.QueryTransactionDetails.TransactionDetails.TransactionDetail[1].trans_amount = 200.00 ;
strcpy( trans.QueryTransactionDetails.TransactionDetails.TransactionDetail[2].card_no , "\"" );
strcpy( trans.QueryTransactionDetails.TransactionDetails.TransactionDetail[3].card_no , "\"你\"他\"" );
strcpy( trans.QueryTransactionDetails.TransactionDetails.TransactionDetail[4].card_no , "我\"你\"他\"她" );
DSCLOG_BankJsonTransaction( & trans );
memset( buf , 0x00 , sizeof(buf) );
len = sizeof(buf)-1 ;
nret = DSCSERIALIZE_JSON_BankJsonTransaction( & trans , "GBK" , buf , & len ) ;
if( nret )
{
printf( "DSCSERIALIZE_JSON_BankJsonTransaction failed[%d]\n" , nret );
return nret;
}
else
{
printf( "DSCSERIALIZE_JSON_BankJsonTransaction ok , len[%d]\n" , len );
}
printf( "[%s]\n" , buf );
/* ... client send communication struct to server ... */
/* server code */
memset( & trans , 0x00 , sizeof(BankJsonTransaction) );
nret = DSCDESERIALIZE_JSON_BankJsonTransaction( NULL , buf , & len , & trans ) ;
if( nret )
{
printf( "DSCDESERIALIZE_JSON_BankJsonTransaction failed[%d]\n" , nret );
return nret;
}
else
{
printf( "DSCDESERIALIZE_JSON_BankJsonTransaction ok\n" );
}
DSCLOG_BankJsonTransaction( & trans );
return 0;
}
int main()
{
return -test_serialize_json();
}
$ make
gcc -g -fPIC -O2 -Wall -Werror -fno-strict-aliasing -I. -I/home/calvin/exinc/fasterxml -I/home/calvin/exinc/fasterjson -c test_serialize_json.c
gcc -g -fPIC -O2 -Wall -Werror -fno-strict-aliasing -o test_serialize_json test_serialize_json.o IDL_BankJsonTransaction.dsc.o -L. -L/home/calvin/exlib -lfasterjson
$ ./test_serialize_json
BankJsonTransaction.version[1]
BankJsonTransaction.ResponseHeader.transaction_code[P0101]
BankJsonTransaction.ResponseHeader.trans_jnlsno[12345678]
BankJsonTransaction.ResponseHeader.response_code[0]
BankJsonTransaction.ResponseHeader.response_desc[OK]
BankJsonTransaction.QueryTransactionDetails.AddonMessages.AddonMessage[index[3]].message_text[Check account passed]
BankJsonTransaction.QueryTransactionDetails.AddonMessages.AddonMessage[index[3]].message_text[Check channel passed]
BankJsonTransaction.QueryTransactionDetails.AddonMessages.AddonMessage[index[3]].message_text[]
BankJsonTransaction.QueryTransactionDetails.TransactionDetailTitle.title_text[DETAIL TITLE]
BankJsonTransaction.QueryTransactionDetails.TransactionDetailTitle.page_no[1]
BankJsonTransaction.QueryTransactionDetails.TransactionDetailTitle.page_size[2]
BankJsonTransaction.QueryTransactionDetails.TransactionDetails.TransactionDetail[index[3]].trans_date[2014-01-01]
BankJsonTransaction.QueryTransactionDetails.TransactionDetails.TransactionDetail[index[3]].trans_time[08:01:01]
BankJsonTransaction.QueryTransactionDetails.TransactionDetails.TransactionDetail[index[3]].outlet_id[1001]
BankJsonTransaction.QueryTransactionDetails.TransactionDetails.TransactionDetail[index[3]].card_no[603367123412341234]
BankJsonTransaction.QueryTransactionDetails.TransactionDetails.TransactionDetail[index[3]].trans_amount[100.000000]
BankJsonTransaction.QueryTransactionDetails.TransactionDetails.TransactionDetail[index[3]].trans_date[2014-02-02]
BankJsonTransaction.QueryTransactionDetails.TransactionDetails.TransactionDetail[index[3]].trans_time[08:02:02]
BankJsonTransaction.QueryTransactionDetails.TransactionDetails.TransactionDetail[index[3]].outlet_id[2002]
BankJsonTransaction.QueryTransactionDetails.TransactionDetails.TransactionDetail[index[3]].card_no[603367123412341234]
BankJsonTransaction.QueryTransactionDetails.TransactionDetails.TransactionDetail[index[3]].trans_amount[200.000000]
BankJsonTransaction.QueryTransactionDetails.TransactionDetails.TransactionDetail[index[3]].trans_date[]
BankJsonTransaction.QueryTransactionDetails.TransactionDetails.TransactionDetail[index[3]].trans_time[]
BankJsonTransaction.QueryTransactionDetails.TransactionDetails.TransactionDetail[index[3]].outlet_id[]
BankJsonTransaction.QueryTransactionDetails.TransactionDetails.TransactionDetail[index[3]].card_no["]
BankJsonTransaction.QueryTransactionDetails.TransactionDetails.TransactionDetail[index[3]].trans_amount[0.000000]
BankJsonTransaction.QueryTransactionDetails.TransactionDetails.TransactionDetail[index[3]].trans_date[]
BankJsonTransaction.QueryTransactionDetails.TransactionDetails.TransactionDetail[index[3]].trans_time[]
BankJsonTransaction.QueryTransactionDetails.TransactionDetails.TransactionDetail[index[3]].outlet_id[]
BankJsonTransaction.QueryTransactionDetails.TransactionDetails.TransactionDetail[index[3]].card_no["你"他"]
BankJsonTransaction.QueryTransactionDetails.TransactionDetails.TransactionDetail[index[3]].trans_amount[0.000000]
BankJsonTransaction.QueryTransactionDetails.TransactionDetails.TransactionDetail[index[3]].trans_date[]
BankJsonTransaction.QueryTransactionDetails.TransactionDetails.TransactionDetail[index[3]].trans_time[]
BankJsonTransaction.QueryTransactionDetails.TransactionDetails.TransactionDetail[index[3]].outlet_id[]
BankJsonTransaction.QueryTransactionDetails.TransactionDetails.TransactionDetail[index[3]].card_no[我"你"他"她]
BankJsonTransaction.QueryTransactionDetails.TransactionDetails.TransactionDetail[index[3]].trans_amount[0.000000]
BankJsonTransaction.QueryTransactionDetails.TransactionDetails.TransactionDetail[index[3]].trans_date[]
BankJsonTransaction.QueryTransactionDetails.TransactionDetails.TransactionDetail[index[3]].trans_time[]
BankJsonTransaction.QueryTransactionDetails.TransactionDetails.TransactionDetail[index[3]].outlet_id[]
BankJsonTransaction.QueryTransactionDetails.TransactionDetails.TransactionDetail[index[3]].card_no[]
BankJsonTransaction.QueryTransactionDetails.TransactionDetails.TransactionDetail[index[3]].trans_amount[0.000000]
BankJsonTransaction.QueryTransactionDetails.TransactionDetails.TransactionDetail[index[3]].trans_date[]
BankJsonTransaction.QueryTransactionDetails.TransactionDetails.TransactionDetail[index[3]].trans_time[]
BankJsonTransaction.QueryTransactionDetails.TransactionDetails.TransactionDetail[index[3]].outlet_id[]
BankJsonTransaction.QueryTransactionDetails.TransactionDetails.TransactionDetail[index[3]].card_no[]
BankJsonTransaction.QueryTransactionDetails.TransactionDetails.TransactionDetail[index[3]].trans_amount[0.000000]
BankJsonTransaction.QueryTransactionDetails.TransactionDetails.TransactionDetail[index[3]].trans_date[]
BankJsonTransaction.QueryTransactionDetails.TransactionDetails.TransactionDetail[index[3]].trans_time[]
BankJsonTransaction.QueryTransactionDetails.TransactionDetails.TransactionDetail[index[3]].outlet_id[]
BankJsonTransaction.QueryTransactionDetails.TransactionDetails.TransactionDetail[index[3]].card_no[]
BankJsonTransaction.QueryTransactionDetails.TransactionDetails.TransactionDetail[index[3]].trans_amount[0.000000]
BankJsonTransaction.QueryTransactionDetails.TransactionDetails.TransactionDetail[index[3]].trans_date[]
BankJsonTransaction.QueryTransactionDetails.TransactionDetails.TransactionDetail[index[3]].trans_time[]
BankJsonTransaction.QueryTransactionDetails.TransactionDetails.TransactionDetail[index[3]].outlet_id[]
BankJsonTransaction.QueryTransactionDetails.TransactionDetails.TransactionDetail[index[3]].card_no[]
BankJsonTransaction.QueryTransactionDetails.TransactionDetails.TransactionDetail[index[3]].trans_amount[0.000000]
BankJsonTransaction.QueryTransactionDetails.TransactionDetails.TransactionDetail[index[3]].trans_date[]
BankJsonTransaction.QueryTransactionDetails.TransactionDetails.TransactionDetail[index[3]].trans_time[]
BankJsonTransaction.QueryTransactionDetails.TransactionDetails.TransactionDetail[index[3]].outlet_id[]
BankJsonTransaction.QueryTransactionDetails.TransactionDetails.TransactionDetail[index[3]].card_no[]
BankJsonTransaction.QueryTransactionDetails.TransactionDetails.TransactionDetail[index[3]].trans_amount[0.000000]
BankJsonTransaction.QueryTransactionDetails.TransactionDetails.TransactionTailDetail[index[3]].message_text[]
BankJsonTransaction.QueryTransactionDetails.TransactionDetails.TransactionTailDetail[index[3]].message_text[]
BankJsonTransaction.QueryTransactionDetails.TransactionDetails.TransactionTailDetail[index[3]].message_text[]
DSCSERIALIZE_JSON_BankJsonTransaction ok , len[2330]
[{
"BankJsonTransaction" :
{
"version" : 1 ,
"ResponseHeader" :
{
"transaction_code" : "P0101" ,
"trans_jnlsno" : 12345678 ,
"response_code" : 0 ,
"response_desc" : "OK"
} ,
"QueryTransactionDetails" :
{
"AddonMessages" :
{
"AddonMessage" :
[
{
"message_text" : "Check account passed"
} ,
{
"message_text" : "Check channel passed"
} ,
{
"message_text" : ""
}
]
} ,
"TransactionDetailTitle" :
{
"title_text" : "DETAIL TITLE" ,
"page_no" : 1 ,
"page_size" : 2
} ,
"TransactionDetails" :
{
"TransactionDetail" :
[
{
"trans_date" : "2014-01-01" ,
"trans_time" : "08:01:01" ,
"outlet_id" : "1001" ,
"card_no" : "603367123412341234" ,
"trans_amount" : 100.000000
} ,
{
"trans_date" : "2014-02-02" ,
"trans_time" : "08:02:02" ,
"outlet_id" : "2002" ,
"card_no" : "603367123412341234" ,
"trans_amount" : 200.000000
} ,
{
"trans_date" : "" ,
"trans_time" : "" ,
"outlet_id" : "" ,
"card_no" : "\"" ,
"trans_amount" : 0.000000
} ,
{
"trans_date" : "" ,
"trans_time" : "" ,
"outlet_id" : "" ,
"card_no" : "\"你\"他\"" ,
"trans_amount" : 0.000000
} ,
{
"trans_date" : "" ,
"trans_time" : "" ,
"outlet_id" : "" ,
"card_no" : "我\"你\"他\"她" ,
"trans_amount" : 0.000000
} ,
{
"trans_date" : "" ,
"trans_time" : "" ,
"outlet_id" : "" ,
"card_no" : "" ,
"trans_amount" : 0.000000
} ,
{
"trans_date" : "" ,
"trans_time" : "" ,
"outlet_id" : "" ,
"card_no" : "" ,
"trans_amount" : 0.000000
} ,
{
"trans_date" : "" ,
"trans_time" : "" ,
"outlet_id" : "" ,
"card_no" : "" ,
"trans_amount" : 0.000000
} ,
{
"trans_date" : "" ,
"trans_time" : "" ,
"outlet_id" : "" ,
"card_no" : "" ,
"trans_amount" : 0.000000
} ,
{
"trans_date" : "" ,
"trans_time" : "" ,
"outlet_id" : "" ,
"card_no" : "" ,
"trans_amount" : 0.000000
}
] ,
"TransactionTailDetail" :
[
{
"message_text" : ""
} ,
{
"message_text" : ""
} ,
{
"message_text" : ""
}
]
}
}
}
}
]
DSCDESERIALIZE_JSON_BankJsonTransaction ok
BankJsonTransaction.version[1]
BankJsonTransaction.ResponseHeader.transaction_code[P0101]
BankJsonTransaction.ResponseHeader.trans_jnlsno[12345678]
BankJsonTransaction.ResponseHeader.response_code[0]
BankJsonTransaction.ResponseHeader.response_desc[OK]
BankJsonTransaction.QueryTransactionDetails.AddonMessages.AddonMessage[index[3]].message_text[Check account passed]
BankJsonTransaction.QueryTransactionDetails.AddonMessages.AddonMessage[index[3]].message_text[Check channel passed]
BankJsonTransaction.QueryTransactionDetails.AddonMessages.AddonMessage[index[3]].message_text[]
BankJsonTransaction.QueryTransactionDetails.TransactionDetailTitle.title_text[DETAIL TITLE]
BankJsonTransaction.QueryTransactionDetails.TransactionDetailTitle.page_no[1]
BankJsonTransaction.QueryTransactionDetails.TransactionDetailTitle.page_size[2]
BankJsonTransaction.QueryTransactionDetails.TransactionDetails.TransactionDetail[index[3]].trans_date[2014-01-01]
BankJsonTransaction.QueryTransactionDetails.TransactionDetails.TransactionDetail[index[3]].trans_time[08:01:01]
BankJsonTransaction.QueryTransactionDetails.TransactionDetails.TransactionDetail[index[3]].outlet_id[1001]
BankJsonTransaction.QueryTransactionDetails.TransactionDetails.TransactionDetail[index[3]].card_no[603367123412341234]
BankJsonTransaction.QueryTransactionDetails.TransactionDetails.TransactionDetail[index[3]].trans_amount[100.000000]
BankJsonTransaction.QueryTransactionDetails.TransactionDetails.TransactionDetail[index[3]].trans_date[2014-02-02]
BankJsonTransaction.QueryTransactionDetails.TransactionDetails.TransactionDetail[index[3]].trans_time[08:02:02]
BankJsonTransaction.QueryTransactionDetails.TransactionDetails.TransactionDetail[index[3]].outlet_id[2002]
BankJsonTransaction.QueryTransactionDetails.TransactionDetails.TransactionDetail[index[3]].card_no[603367123412341234]
BankJsonTransaction.QueryTransactionDetails.TransactionDetails.TransactionDetail[index[3]].trans_amount[200.000000]
BankJsonTransaction.QueryTransactionDetails.TransactionDetails.TransactionDetail[index[3]].trans_date[]
BankJsonTransaction.QueryTransactionDetails.TransactionDetails.TransactionDetail[index[3]].trans_time[]
BankJsonTransaction.QueryTransactionDetails.TransactionDetails.TransactionDetail[index[3]].outlet_id[]
BankJsonTransaction.QueryTransactionDetails.TransactionDetails.TransactionDetail[index[3]].card_no["]
BankJsonTransaction.QueryTransactionDetails.TransactionDetails.TransactionDetail[index[3]].trans_amount[0.000000]
BankJsonTransaction.QueryTransactionDetails.TransactionDetails.TransactionDetail[index[3]].trans_date[]
BankJsonTransaction.QueryTransactionDetails.TransactionDetails.TransactionDetail[index[3]].trans_time[]
BankJsonTransaction.QueryTransactionDetails.TransactionDetails.TransactionDetail[index[3]].outlet_id[]
BankJsonTransaction.QueryTransactionDetails.TransactionDetails.TransactionDetail[index[3]].card_no["你"他"]
BankJsonTransaction.QueryTransactionDetails.TransactionDetails.TransactionDetail[index[3]].trans_amount[0.000000]
BankJsonTransaction.QueryTransactionDetails.TransactionDetails.TransactionDetail[index[3]].trans_date[]
BankJsonTransaction.QueryTransactionDetails.TransactionDetails.TransactionDetail[index[3]].trans_time[]
BankJsonTransaction.QueryTransactionDetails.TransactionDetails.TransactionDetail[index[3]].outlet_id[]
BankJsonTransaction.QueryTransactionDetails.TransactionDetails.TransactionDetail[index[3]].card_no[我"你"他"她]
BankJsonTransaction.QueryTransactionDetails.TransactionDetails.TransactionDetail[index[3]].trans_amount[0.000000]
BankJsonTransaction.QueryTransactionDetails.TransactionDetails.TransactionDetail[index[3]].trans_date[]
BankJsonTransaction.QueryTransactionDetails.TransactionDetails.TransactionDetail[index[3]].trans_time[]
BankJsonTransaction.QueryTransactionDetails.TransactionDetails.TransactionDetail[index[3]].outlet_id[]
BankJsonTransaction.QueryTransactionDetails.TransactionDetails.TransactionDetail[index[3]].card_no[]
BankJsonTransaction.QueryTransactionDetails.TransactionDetails.TransactionDetail[index[3]].trans_amount[0.000000]
BankJsonTransaction.QueryTransactionDetails.TransactionDetails.TransactionDetail[index[3]].trans_date[]
BankJsonTransaction.QueryTransactionDetails.TransactionDetails.TransactionDetail[index[3]].trans_time[]
BankJsonTransaction.QueryTransactionDetails.TransactionDetails.TransactionDetail[index[3]].outlet_id[]
BankJsonTransaction.QueryTransactionDetails.TransactionDetails.TransactionDetail[index[3]].card_no[]
BankJsonTransaction.QueryTransactionDetails.TransactionDetails.TransactionDetail[index[3]].trans_amount[0.000000]
BankJsonTransaction.QueryTransactionDetails.TransactionDetails.TransactionDetail[index[3]].trans_date[]
BankJsonTransaction.QueryTransactionDetails.TransactionDetails.TransactionDetail[index[3]].trans_time[]
BankJsonTransaction.QueryTransactionDetails.TransactionDetails.TransactionDetail[index[3]].outlet_id[]
BankJsonTransaction.QueryTransactionDetails.TransactionDetails.TransactionDetail[index[3]].card_no[]
BankJsonTransaction.QueryTransactionDetails.TransactionDetails.TransactionDetail[index[3]].trans_amount[0.000000]
BankJsonTransaction.QueryTransactionDetails.TransactionDetails.TransactionDetail[index[3]].trans_date[]
BankJsonTransaction.QueryTransactionDetails.TransactionDetails.TransactionDetail[index[3]].trans_time[]
BankJsonTransaction.QueryTransactionDetails.TransactionDetails.TransactionDetail[index[3]].outlet_id[]
BankJsonTransaction.QueryTransactionDetails.TransactionDetails.TransactionDetail[index[3]].card_no[]
BankJsonTransaction.QueryTransactionDetails.TransactionDetails.TransactionDetail[index[3]].trans_amount[0.000000]
BankJsonTransaction.QueryTransactionDetails.TransactionDetails.TransactionDetail[index[3]].trans_date[]
BankJsonTransaction.QueryTransactionDetails.TransactionDetails.TransactionDetail[index[3]].trans_time[]
BankJsonTransaction.QueryTransactionDetails.TransactionDetails.TransactionDetail[index[3]].outlet_id[]
BankJsonTransaction.QueryTransactionDetails.TransactionDetails.TransactionDetail[index[3]].card_no[]
BankJsonTransaction.QueryTransactionDetails.TransactionDetails.TransactionDetail[index[3]].trans_amount[0.000000]
BankJsonTransaction.QueryTransactionDetails.TransactionDetails.TransactionTailDetail[index[3]].message_text[]
BankJsonTransaction.QueryTransactionDetails.TransactionDetails.TransactionTailDetail[index[3]].message_text[]
BankJsonTransaction.QueryTransactionDetails.TransactionDetails.TransactionTailDetail[index[3]].message_text[]
[/code]
3.6.编写应用代码,使用ESQL代码开发数据库应用
[code]
$ cat IDL_testable.dsc
STRUCT testable
{
INT 2 n2
INT 4 n4
FLOAT 4 f4
FLOAT 8 f8
STRING 32 str32
STRING 1024 str1024
CREATE_SQL "CREATE INDEX testable_idx1 ON testable ( n2 ) ;"
CREATE_SQL "CREATE UNIQUE INDEX testable_idx2 ON testable ( str32 , str1024 ) ;"
DROP_SQL "DROP INDEX testable_idx1 ;"
DROP_SQL "DROP INDEX testable_idx2 ;"
}
$ dsc -f IDL_testable.dsc -sql -ec-pqsql -c-LOG
STRUCT testable
INT 2 n2
INT 4 n4
FLOAT 4 f4
FLOAT 8 f8
STRING 32 str32
STRING 1024 str1024
ok!
$ ls IDL_testable.dsc*
IDL_testable.dsc IDL_testable.dsc.ESQL.ec IDL_testable.dsc.ESQL.eh IDL_testable.dsc.LOG.c IDL_testable.dsc.create.sql IDL_testable.dsc.h
IDL_testable.dsc.ESQL.c IDL_testable.dsc.ESQL.ecE IDL_testable.dsc.ESQL.o IDL_testable.dsc.c IDL_testable.dsc.drop.sql
$ cat IDL_testable.dsc.create.sql
-- It had generated by DirectStruct v1.1.2
CREATE TABLE testable
(
n2 SMALLINT ,
n4 INTEGER ,
f4 REAL ,
f8 DOUBLE PRECISION ,
str32 CHARACTER VARYING(32) ,
str1024 CHARACTER VARYING(1024)
) ;
CREATE INDEX testable_idx1 ON testable ( n2 ) ;
CREATE UNIQUE INDEX testable_idx2 ON testable ( str32 , str1024 ) ;
$ ... run IDL_testable.dsc.create.sql in your database ...
$ cat test_testable.ec
#include "IDL_testable.dsc.ESQL.eh"
#include "IDL_testable.dsc.LOG.c"
int test_delete()
{
EXEC SQL
DELETE
FROM testable
WHERE n2 IN ( 100 , 101 , 102 ) ;
if( SQLCODE == 100 )
{
printf( "DELETE ok\n" );
}
else if( SQLCODE )
{
printf( "DELETE failed[%ld][%s]\n" , SQLCODE , SQLSTATE );
return SQLCODE;
}
else
{
printf( "DELETE ok\n" );
}
return 0;
}
int test_insert()
{
testable t ;
memset( & t , 0x00 , sizeof(testable) );
t.n2 = 100 ;
t.n4 = 56789 ;
t.f4 = 1.2 ;
t.f8 = 456.789 ;
strcpy( t.str32 , "hi" );
strcpy( t.str1024 , "hello world" );
DSCINITV_testable();
DSCSTOV_testable( & t );
EXEC SQL
INSERT
INTO testable
VALUES ( DBVLIST_testable ) ;
if( SQLCODE )
{
printf( "INSERT failed[%ld][%s]\n" , SQLCODE , SQLSTATE );
return SQLCODE;
}
else
{
printf( "INSERT ok\n" );
}
memset( & t , 0x00 , sizeof(testable) );
t.n2 = 101 ;
t.n4 = 56789 ;
t.f4 = 1.2 ;
t.f8 = 456.789 ;
strcpy( t.str32 , "hi" );
strcpy( t.str1024 , "hello earth" );
DSCINITV_testable();
DSCSTOV_testable( & t );
EXEC SQL
INSERT
INTO testable
VALUES ( DBVLIST_testable ) ;
if( SQLCODE )
{
printf( "INSERT failed[%ld][%s]\n" , SQLCODE , SQLSTATE );
return SQLCODE;
}
else
{
printf( "INSERT ok\n" );
}
memset( & t , 0x00 , sizeof(testable) );
t.n2 = 102 ;
t.n4 = 56789 ;
t.f4 = 1.2 ;
t.f8 = 456.789 ;
strcpy( t.str32 , "hi" );
strcpy( t.str1024 , "hello march" );
DSCINITV_testable();
DSCSTOV_testable( & t );
EXEC SQL
INSERT
INTO testable
VALUES ( DBVLIST_testable ) ;
if( SQLCODE )
{
printf( "INSERT failed[%ld][%s]\n" , SQLCODE , SQLSTATE );
return SQLCODE;
}
else
{
printf( "INSERT ok\n" );
}
return 0;
}
int test_update()
{
testable t ;
memset( & t , 0x00 , sizeof(testable) );
t.n2 = 101 ;
DSCINITV_testable();
DSCSTOV_testable( & t );
EXEC SQL
SELECT TFLIST_testable
INTO DBVLLIST_testable
FROM testable
WHERE n2 = :testable_n2 ;
if( SQLCODE )
{
printf( "SELECT failed[%ld][%s]\n" , SQLCODE , SQLSTATE );
return SQLCODE;
}
else
{
printf( "SELECT ok\n" );
}
DSCVTOS_testable( & t );
strcpy( t.str1024 , "hello sun" );
DSCINITV_testable();
DSCSTOV_testable( & t );
EXEC SQL
UPDATE testable
SET str1024 = :testable_str1024
WHERE n2 = :testable_n2 ;
if( SQLCODE )
{
printf( "UPDATE failed[%ld][%s]\n" , SQLCODE , SQLSTATE );
return SQLCODE;
}
else
{
printf( "UPDATE ok\n" );
}
return 0;
}
int test_cursor()
{
testable t ;
int nret = 0 ;
EXEC SQL
DECLARE testable_cursor CURSOR FOR
SELECT TFLIST_testable
FROM testable
ORDER BY n2 ASC ;
if( SQLCODE )
{
printf( "DECLARE failed[%ld][%s]\n" , SQLCODE , SQLSTATE );
return SQLCODE;
}
else
{
printf( "DECLARE ok\n" );
}
EXEC SQL
OPEN testable_cursor ;
if( SQLCODE )
{
printf( "OPEN failed[%ld][%s]\n" , SQLCODE , SQLSTATE );
return SQLCODE;
}
else
{
printf( "OPEN ok\n" );
}
while(1)
{
EXEC SQL
FETCH testable_cursor
INTO DBVLLIST_testable ;
if( SQLCODE == 100 )
{
break;
}
else if( SQLCODE )
{
printf( "FETCH failed[%ld][%s]\n" , SQLCODE , SQLSTATE );
nret = SQLCODE ;
}
else
{
printf( "FETCH ok\n" );
}
memset( & t , 0x00 , sizeof(testable) );
DSCVTOS_testable( & t );
DSCLOG_testable( & t );
}
EXEC SQL
CLOSE testable_cursor ;
if( SQLCODE )
{
printf( "CLOSE failed[%ld][%s]\n" , SQLCODE , SQLSTATE );
return SQLCODE;
}
else
{
printf( "CLOSE ok\n" );
}
if( nret )
return nret;
return 0;
}
EXEC SQL BEGIN DECLARE SECTION ;
char sql[] = "DELETE FROM testable WHERE n2 = 102 ;" ;
EXEC SQL END DECLARE SECTION ;
int test_immediate()
{
EXEC SQL
EXECUTE IMMEDIATE :sql ;
if( SQLCODE )
{
printf( "EXECUTE failed[%ld][%s]\n" , SQLCODE , SQLSTATE );
return SQLCODE;
}
else
{
printf( "EXECUTE ok\n" );
}
return 0;
}
EXEC SQL BEGIN DECLARE SECTION ;
char sql2[] = "SELECT * FROM testable WHERE n2 >= ? ;" ;
EXEC SQL END DECLARE SECTION ;
int test_prepare()
{
testable t ;
int nret = 0 ;
EXEC SQL
PREPARE prepare FROM :sql2 ;
if( SQLCODE )
{
printf( "PREPARE failed[%ld][%s]\n" , SQLCODE , SQLSTATE );
return SQLCODE;
}
else
{
printf( "PREPARE ok\n" );
}
testable_n2 = 101 ;
EXEC SQL
EXECUTE prepare
INTO DBVLLIST_testable
USING :testable_n2 ;
if( SQLCODE )
{
printf( "EXECUTE failed[%ld][%s]\n" , SQLCODE , SQLSTATE );
return SQLCODE;
}
else
{
printf( "EXECUTE ok\n" );
}
memset( & t , 0x00 , sizeof(testable) );
DSCVTOS_testable( & t );
DSCLOG_testable( & t );
testable_n2 = 100 ;
EXEC SQL
DECLARE testable_cursor2 CURSOR FOR prepare ;
if( SQLCODE )
{
printf( "DECLARE failed[%ld][%s]\n" , SQLCODE , SQLSTATE );
return SQLCODE;
}
else
{
printf( "DECLARE ok\n" );
}
EXEC SQL
OPEN testable_cursor2
USING :testable_n2 ;
if( SQLCODE )
{
printf( "OPEN failed[%ld][%s]\n" , SQLCODE , SQLSTATE );
return SQLCODE;
}
else
{
printf( "OPEN ok\n" );
}
while(1)
{
EXEC SQL
FETCH testable_cursor2
INTO DBVLLIST_testable ;
if( SQLCODE == 100 )
{
break;
}
else if( SQLCODE )
{
printf( "FETCH failed[%ld][%s]\n" , SQLCODE , SQLSTATE );
nret = SQLCODE ;
}
else
{
printf( "FETCH ok\n" );
}
memset( & t , 0x00 , sizeof(testable) );
DSCVTOS_testable( & t );
DSCLOG_testable( & t );
}
EXEC SQL
CLOSE testable_cursor2 ;
if( SQLCODE )
{
printf( "CLOSE failed[%ld][%s]\n" , SQLCODE , SQLSTATE );
return SQLCODE;
}
else
{
printf( "CLOSE ok\n" );
}
if( nret )
return nret;
EXEC SQL
DEALLOCATE PREPARE prepare ;
return 0;
}
int main()
{
int nret = 0 ;
EXEC SQL
CONNECT TO 'calvin@127.0.0.1:18432'
USER 'calvin'
IDENTIFIED BY 'calvin' ;
if( SQLCODE )
{
printf( "CONNECT failed[%ld][%s]\n" , SQLCODE , SQLSTATE );
return 1;
}
else
{
printf( "CONNECT ok\n" );
}
EXEC SQL
BEGIN WORK ;
if( SQLCODE )
{
printf( "BEGIN WORK failed[%ld][%s]\n" , SQLCODE , SQLSTATE );
return 1;
}
else
{
printf( "BEGIN WORK ok\n" );
}
do
{
printf( "--- delete ---------\n" );
nret = test_delete() ;
if( nret )
break;
printf( "--- insert ---------\n" );
nret = test_insert() ;
if( nret )
break;
printf( "--- update ---------\n" );
nret = test_update() ;
if( nret )
break;
printf( "--- cursor ---------\n" );
nret = test_cursor() ;
if( nret )
break;
printf( "--- immediate ---------\n" );
nret = test_immediate() ;
if( nret )
break;
printf( "--- prepare ---------\n" );
nret = test_prepare() ;
if( nret )
break;
}
while(0);
if( nret )
{
EXEC SQL
ROLLBACK WORK ;
if( SQLCODE )
{
printf( "ROLLBACK WORK failed[%ld][%s]\n" , SQLCODE , SQLSTATE );
return 1;
}
else
{
printf( "ROLLBACK WORK ok\n" );
}
}
else
{
EXEC SQL
COMMIT WORK ;
if( SQLCODE )
{
printf( "COMMIT WORK failed[%ld][%s]\n" , SQLCODE , SQLSTATE );
return 1;
}
else
{
printf( "COMMIT WORK ok\n" );
}
}
EXEC SQL
DISCONNECT ;
if( SQLCODE )
{
printf( "DISCONNECT failed[%ld][%s]\n" , SQLCODE , SQLSTATE );
return 1;
}
else
{
printf( "DISCONNECT ok\n" );
}
return 0;
}
$ make
gcc -g -fPIC -O2 -Wall -Werror -fno-strict-aliasing -I/root/local/postgresql/include -E -x c IDL_testable.dsc.ESQL.ec > IDL_testable.dsc.ESQL.ecE # c预处理
ecpg -h IDL_testable.dsc.ESQL.ecE -o IDL_testable.dsc.ESQL.c # postgresql esql预处理器处理之
sed -i 's/NULL/0/g' IDL_testable.dsc.ESQL.c # 去掉ecpg加上的pqsql头文件,防止重复包含错误
gcc -g -fPIC -O2 -Wall -Werror -fno-strict-aliasing -I/root/local/postgresql/include -c IDL_testable.dsc.ESQL.c # 最后的编译
gcc -g -fPIC -O2 -Wall -Werror -fno-strict-aliasing -I/root/local/postgresql/include -E -x c test_testable.ec > test_testable.ecE
ecpg -h test_testable.ecE -o test_testable.c
sed -i 's/NULL/0/g' test_testable.c
gcc -g -fPIC -O2 -Wall -Werror -fno-strict-aliasing -I/root/local/postgresql/include -c test_testable.c
gcc -g -fPIC -O2 -Wall -Werror -fno-strict-aliasing -o test_testable IDL_testable.dsc.ESQL.o test_testable.o -L. -L/root/local/postgresql/lib -lecpg
$ ./test_testable
CONNECT ok
BEGIN WORK ok
--- delete ---------
DELETE ok
--- insert ---------
INSERT ok
INSERT ok
INSERT ok
--- update ---------
SELECT ok
UPDATE ok
--- cursor ---------
DECLARE ok
OPEN ok
FETCH ok
testable.n2[100]
testable.n4[56789]
testable.f4[1.200000]
testable.f8[456.789000]
testable.str32[hi]
testable.str1024[hello world]
FETCH ok
testable.n2[101]
testable.n4[56789]
testable.f4[1.200000]
testable.f8[456.789000]
testable.str32[hi]
testable.str1024[hello sun]
FETCH ok
testable.n2[102]
testable.n4[56789]
testable.f4[1.200000]
testable.f8[456.789000]
testable.str32[hi]
testable.str1024[hello march]
CLOSE ok
--- immediate ---------
EXECUTE ok
--- prepare ---------
PREPARE ok
EXECUTE ok
testable.n2[101]
testable.n4[56789]
testable.f4[1.200000]
testable.f8[456.789000]
testable.str32[hi]
testable.str1024[hello sun]
DECLARE ok
OPEN ok
FETCH ok
testable.n2[100]
testable.n4[56789]
testable.f4[1.200000]
testable.f8[456.789000]
testable.str32[hi]
testable.str1024[hello world]
FETCH ok
testable.n2[101]
testable.n4[56789]
testable.f4[1.200000]
testable.f8[456.789000]
testable.str32[hi]
testable.str1024[hello sun]
CLOSE ok
COMMIT WORK ok
DISCONNECT ok
[/code]
在dsc文件中还可以使用SQLACTION指令自动生成一些ESQL函数,语法如下:
SQLACTION "SELECT ... FROM 表名 [ WHERE ... ]"
SQLACTION "INSERT INTO 表名"
SQLACTION "UPDATE 表名 SET ... [ WHERE ... ]"
SQLACTION "DELETE FROM 表名 [ WHERE ... ]"
SQLACTION "CURSOR 游标名 SELECT ... FROM 表名 [ WHERE ... ] [ ORDER BY ... [DESC|ASC] ]"
示例如下:
[code]
$ cat IDL_userinfo.dsc
STRUCT userinfo
{
INT 4 user_id DEFAULT 1 NOTNULL
STRING 16 user_name DEFAULT '0000'
STRING 128 email NULL
CREATE_SQL "CREATE UNIQUE INDEX userinfo_idx1 ON userinfo ( user_id ) ;"
DROP_SQL "DROP INDEX testable_idx1 ;"
SQLACTION "SELECT user_name,email FROM userinfo WHERE user_id ="
SQLACTION "SELECT * FROM userinfo WHERE user_id = AND user_name ="
SQLACTION "INSERT INTO userinfo"
SQLACTION "UPDATE userinfo SET * WHERE user_id ="
SQLACTION "UPDATE userinfo SET email"
SQLACTION "DELETE FROM userinfo WHERE user_id >="
SQLACTION "DELETE FROM userinfo"
SQLACTION "CURSOR mycursor SELECT * FROM userinfo"
SQLACTION "CURSOR mycursor1 SELECT user_name FROM userinfo WHERE user_id >="
SQLACTION "CURSOR mycursor2 SELECT user_name,email FROM userinfo ORDER BY user_id ASC"
SQLACTION "CURSOR mycursor3 SELECT * FROM userinfo WHERE user_id >= ORDER BY user_id ASC"
}
$ dsc -f IDL_userinfo.dsc -sql -ec-pqsql
...
$ cat IDL_userinfo.dsc.ESQL.ec
...
void DSCSQLACTION_SELECT_user_name_email_FROM_userinfo_WHERE_user_id_E( userinfo *pst )
{
DSCSTOV_userinfo( pst );
EXEC SQL
SELECT user_name,email
INTO :userinfo_user_name :userinfo_user_name_id , :userinfo_email :userinfo_email_id
FROM userinfo
WHERE user_id = :userinfo_user_id
;
if( SQLCODE )
return;
DSCVTOS_userinfo( pst );
return;
}
void DSCSQLACTION_SELECT_A_FROM_userinfo_WHERE_user_id_E_AND_user_name_E( userinfo *pst )
{
DSCSTOV_userinfo( pst );
EXEC SQL
SELECT *
INTO DBVLLIST_userinfo
FROM userinfo
WHERE user_id = :userinfo_user_id AND user_name = :userinfo_user_name
;
if( SQLCODE )
return;
DSCVTOS_userinfo( pst );
return;
}
void DSCSQLACTION_INSERT_INTO_userinfo( userinfo *pst )
{
DSCSTOV_userinfo( pst );
EXEC SQL
INSERT INTO userinfo
VALUES ( DBVLLIST_userinfo )
;
return;
}
void DSCSQLACTION_UPDATE_userinfo_SET_A_WHERE_user_id_E( userinfo *pst )
{
DSCSTOV_userinfo( pst );
EXEC SQL
UPDATE userinfo
SET ( TFLIST_userinfo ) = ( DBVLIST_userinfo )
WHERE user_id = :userinfo_user_id
;
return;
}
void DSCSQLACTION_UPDATE_userinfo_SET_email( userinfo *pst )
{
DSCSTOV_userinfo( pst );
EXEC SQL
UPDATE userinfo
SET email = :userinfo_email
;
return;
}
void DSCSQLACTION_DELETE_FROM_userinfo_WHERE_user_id_GE( userinfo *pst )
{
DSCSTOV_userinfo( pst );
EXEC SQL
DELETE
FROM userinfo
WHERE user_id >= :userinfo_user_id
;
return;
}
void DSCSQLACTION_DELETE_FROM_userinfo( userinfo *pst )
{
DSCSTOV_userinfo( pst );
EXEC SQL
DELETE
FROM userinfo
;
return;
}
void DSCSQLACTION_OPEN_CURSOR_mycursor_SELECT_A_FROM_userinfo( userinfo *pst )
{
EXEC SQL
DECLARE userinfo_mycursor CURSOR FOR
SELECT *
FROM userinfo
;
if( SQLCODE )
return;
EXEC SQL
OPEN userinfo_mycursor
; if( SQLCODE )
return;
return;
}
void DSCSQLACTION_FETCH_CURSOR_mycursor( userinfo *pst )
{
EXEC SQL
FETCH userinfo_mycursor
INTO DBVLLIST_userinfo
;
if( SQLCODE )
return;
DSCVTOS_userinfo( pst );
return;
}
void DSCSQLACTION_CLOSE_CURSOR_mycursor()
{
EXEC SQL
CLOSE userinfo_mycursor
;
return;
}
void DSCSQLACTION_OPEN_CURSOR_mycursor1_SELECT_user_name_FROM_userinfo_WHERE_user_id_GE( userinfo *pst )
{
EXEC SQL
DECLARE userinfo_mycursor1 CURSOR FOR
SELECT user_name
FROM userinfo
WHERE user_id >= :userinfo_user_id
;
if( SQLCODE )
return;
EXEC SQL
OPEN userinfo_mycursor1
; if( SQLCODE )
return;
return;
}
void DSCSQLACTION_FETCH_CURSOR_mycursor1( userinfo *pst )
{
EXEC SQL
FETCH userinfo_mycursor1
INTO :userinfo_user_name :userinfo_user_name_id
;
if( SQLCODE )
return;
DSCVTOS_userinfo( pst );
return;
}
void DSCSQLACTION_CLOSE_CURSOR_mycursor1()
{
EXEC SQL
CLOSE userinfo_mycursor1
;
return;
}
void DSCSQLACTION_OPEN_CURSOR_mycursor2_SELECT_user_name_email_FROM_userinfo_ORDER_BY_user_id_ASC( userinfo *pst )
{
EXEC SQL
DECLARE userinfo_mycursor2 CURSOR FOR
SELECT user_name,email
FROM userinfo
ORDER BY user_id ASC
;
if( SQLCODE )
return;
EXEC SQL
OPEN userinfo_mycursor2
; if( SQLCODE )
return;
return;
}
void DSCSQLACTION_FETCH_CURSOR_mycursor2( userinfo *pst )
{
EXEC SQL
FETCH userinfo_mycursor2
INTO :userinfo_user_name :userinfo_user_name_id, :userinfo_email :userinfo_email_id
;
if( SQLCODE )
return;
DSCVTOS_userinfo( pst );
return;
}
void DSCSQLACTION_CLOSE_CURSOR_mycursor2()
{
EXEC SQL
CLOSE userinfo_mycursor2
;
return;
}
void DSCSQLACTION_OPEN_CURSOR_mycursor3_SELECT_A_FROM_userinfo_WHERE_user_id_GE_ORDER_BY_user_id_ASC( userinfo *pst )
{
EXEC SQL
DECLARE userinfo_mycursor3 CURSOR FOR
SELECT *
FROM userinfo
WHERE user_id >= :userinfo_user_id
ORDER BY user_id ASC
;
if( SQLCODE )
return;
EXEC SQL
OPEN userinfo_mycursor3
; if( SQLCODE )
return;
return;
}
void DSCSQLACTION_FETCH_CURSOR_mycursor3( userinfo *pst )
{
EXEC SQL
FETCH userinfo_mycursor3
INTO DBVLLIST_userinfo
;
if( SQLCODE )
return;
DSCVTOS_userinfo( pst );
return;
}
void DSCSQLACTION_CLOSE_CURSOR_mycursor3()
{
EXEC SQL
CLOSE userinfo_mycursor3
;
return;
}
...
[/code]
可以在你的代码中直接调用这些函数,而不用手工编码了。
还能根据不同数据库自动生成各自的连接数据库、断开数据库、事务开始、事务提交、事务回滚等函数,只要加上SQLCONN指令即可
N-2.DirectStruct报文定义文件语法
每个结构或内嵌子结构定义格式都以
[code]
STRUCT 结构名 ARRAY 数组数量
{
...
}
[/code]
囊括,其中首行的ARRAY及后是可选的
每个字段定义行格式为
[code]
字段类型 字段长度 字段名 DEFAULT 缺省值
[/code]
其中字段类型及长度目前支持
INT 1,2,4,8 ( 8位至64位整型 )
UINT 1,2,4,8
FLOAT 4,8
CHAR 1
UCHAR 1
STRING (N)
DEFAULT及后是可选的
任何行位置可以用
[code]
INCLUDE 文件名
[/code]
引用其它.dsc文件
下面是一个综合的例子:
[code]
$ cat IDL_BankTransaction.dsc
STRUCT BankTransaction
{
INT 1 version DEFAULT 1
INCLUDE IDL_ResponseHeader.dsc
STRUCT QueryTransactionDetails
{
STRUCT AddonMessages ARRAY 3
{
STRING 64 message_text
}
STRUCT TransactionDetailTitle
{
STRING 64 title_text
INT 2 page_no
INT 2 page_size
}
STRUCT TransactionDetails ARRAY 10
{
STRING 10 trans_date
STRING 10 trans_time
STRING 6 outlet_id
STRING 20 card_no
FLOAT 4 trans_amount
}
}
}
$ cat IDL_ResponseHeader.dsc
STRUCT ResponseHeader
{
STRING 32 transaction_code
INT 4 trans_jnlsno
INT 4 response_code DEFAULT 0
STRING 1024 response_desc DEFAULT "OK"
}
[/code]
附加说明:DEFAULT后面的缺省值,由自动化生成的函数DSCINIT_BankTransaction批量初始化。
N-1.性能压测
在高性能场合中,数据序列化和反序列化效率很关键,其中主要考察最终报文大小和序列化速度。
同时,我们还拿了Google公司大名鼎鼎的Protocol Buffer来做对比实验。
压测环境:
CPU : Intel(R) Core(TM) i3-3240 CPU 3.4GHz/3.4GHz
内存 : 2GB
操作系统 : WINDOWS XP SP3 ( VMWare 6.0.1 ( Red Hat Enterprise Linux Server release 5.4 ) )
压测版本:
DirectStruct-1.0.3
protobuf-2.3.0
压测报文结构:
DirectStruct
[code]
$ cat IDL_AllTypes2.dsc
STRUCT AllTypes2
{
INT 4 n4
INT 8 n8
UINT 4 u4
UINT 8 u8
FLOAT 4 f4
FLOAT 8 f8
STRING 32 str32
STRING 1024 str1024
}
$ dsc -f IDL_AllTypes2.dsc -c
$ ls
IDL_AllTypes2.dsc.c IDL_AllTypes2.dsc.h
[/code]
Google Protocol Buffer
[code]
$ cat PressProtobuf.proto
message PressProtobuf
{
required sint32 n4 = 2 ;
required sint64 n8 =3 ;
required int32 un4 = 5 ;
required int64 un8 = 6 ;
required float f4 = 7 ;
required double f8 = 8 ;
required string str32 = 9 ;
required string str1024 = 10 ;
}
$ protoc -I=. --cpp_out=. PressProtobuf.proto
$ ls
PressProtobuf.pb.cc PressProtobuf.pb.h
[/code]
压测代码:
DirectStruct
[code]
$ cat test/test_serialize_compress_press.c
...
int test_serialize_compress_press( long count )
{
AllTypes2 st ;
char buf[ DSCSERIALIZE_COMPRESS_BUFSIZE_AllTypes2 + 1 ] ;
int len ;
long c ;
long t1 , t2 , dt ;
int nret = 0 ;
memset( & st , 0x00 , sizeof(AllTypes2) );
st.n4 = -0x11223344 ;
st.n8 = LL(-0x1122334455667788) ;
st.u4 = 0x11223344 ;
st.u8 = ULL(0x1122334455667788) ;
st.f4 = 100.00 ;
st.f8 = 10000.00 ;
strcpy( st.str32 , "calvin" );
strcpy( st.str1024 , "XXXXXXXXXXXXXXXX" );
DSCLOG_AllTypes2( & st );
printf( "Press begin\n" );
time( & t1 );
for( c = 0 ; c < count ; c++ )
{
memset( buf , 0x00 , sizeof(buf) );
nret = DSCSERIALIZE_COMPRESS_AllTypes2( & st , buf , & len ) ;
if( nret )
{
printf( "DSCSERIALIZE_COMPRESS_AllTypes2 failed[%d]\n" , nret );
return nret;
}
memset( & st , 0x00 , sizeof(AllTypes2) );
nret = DSCDESERIALIZE_COMPRESS_AllTypes2( buf , & len , & st ) ;
if( nret )
{
printf( "DSCDESERIALIZE_COMPRESS_AllTypes2 failed[%d]\n" , nret );
return nret;
}
}
printf( "Press end\n" );
time( & t2 );
dt = t2 - t1 ;
printf( "data compress size[%d] - compact size[%d]\n" , len , DSCSERIALIZE_COMPACT_BUFSIZE_AllTypes2 );
printf( "Elapse %ld seconds\n" , dt );
dump( buf , len );
DSCLOG_AllTypes2( & st );
return 0;
}
...
$ make -f makefile
gcc -g -fPIC -O2 -Wall -Werror -fno-strict-aliasing -I. -c IDL_AllTypes2.dsc.c
gcc -g -fPIC -O2 -Wall -Werror -fno-strict-aliasing -I. -c test_serialize_compress_press.c
gcc -o test_serialize_compress_press test_serialize_compress_press.o IDL_AllTypes2.dsc.o -L.
[/code]
Google Protocol Buffer
[code]
$ cat test_protobuf/test_protobuf_press.cc
...
int test_protobuf_press( int count )
{
PressProtobuf press ;
PressProtobuf press2 ;
string str ;
int len ;
int c ;
press.set_n4( -0x11223344 );
press.set_n8( -0x1122334455667788LL );
press.set_un4( 0x11223344 );
press.set_un8( 0x1122334455667788LL );
press.set_f4( 100.00 );
press.set_f8( 10000.00 );
press.set_str32( "calvin" );
press.set_str1024( "XXXXXXXXXXXXXXXX" );
printf( "[%d]\n" , press.n4() );
printf( "[%lld]\n" , press.n8() );
printf( "[%d]\n" , press.un4() );
printf( "[%lld]\n" , press.un8() );
printf( "[%f]\n" , press.f4() );
printf( "[%lf]\n" , press.f8() );
printf( "[%s]\n" , press.str32().c_str() );
printf( "[%s]\n" , press.str1024().c_str() );
for( c = 0 ; c < count ; c++ )
{
str = press.SerializeAsString() ;
press2.ParseFromString( str );
}
len = str.length() ;
printf( "[%d]\n" , len );
dump( (char*)str.c_str() , len );
printf( "[%d]\n" , press2.n4() );
printf( "[%lld]\n" , press2.n8() );
printf( "[%d]\n" , press2.un4() );
printf( "[%lld]\n" , press2.un8() );
printf( "[%f]\n" , press2.f4() );
printf( "[%lf]\n" , press2.f8() );
printf( "[%s]\n" , press2.str32().c_str() );
printf( "[%s]\n" , press2.str1024().c_str() );
return 0;
}
...
make
g++ -I. -c test_protobuf_press.cc
g++ -o test_protobuf_press test_protobuf_press.o PressProtobuf.pb.cc -L/usr/local/lib -lprotobuf -lpthread
[/code]
压测输出:
DirectStruct
[code]
time ./test_serialize_compress_press 10000000
AllTypes2.n4[-287454020]
AllTypes2.n8[-1234605616436508552]
AllTypes2.u4[287454020]
AllTypes2.u8[1234605616436508552]
AllTypes2.f4[100.000000]
AllTypes2.f8[10000.000000]
AllTypes2.str32[calvin]
AllTypes2.str1024[XXXXXXXXXXXXXXXX]
Press begin
Press end
data compress size[66] - compact size[1092]
Elapse 1 seconds
0x0000000000 04 11 22 33 44 08 11 22 33 44 55 66 77 88 04 11 .."3D.."3DUfw...
0x0000000001 22 33 44 08 11 22 33 44 55 66 77 88 00 00 c8 42 "3D.."3DUfw....B
0x0000000002 00 00 00 00 00 88 c3 40 01 06 63 61 6c 76 69 6e .......@..calvin
0x0000000003 01 10 58 58 58 58 58 58 58 58 58 58 58 58 58 58 ..XXXXXXXXXXXXXX
0x0000000004 58 58 XX
AllTypes2.n4[-287454020]
AllTypes2.n8[-1234605616436508552]
AllTypes2.u4[287454020]
AllTypes2.u8[1234605616436508552]
AllTypes2.f4[100.000000]
AllTypes2.f8[10000.000000]
AllTypes2.str32[calvin]
AllTypes2.str1024[XXXXXXXXXXXXXXXX]
real 0m1.080s
user 0m1.074s
sys 0m0.007s
[/code]
Google Protocol Buffer
[code]
$ time ./test_protobuf_press 10000000
[-287454020]
[-1234605616436508552]
[287454020]
[1234605616436508552]
[100.000000]
[10000.000000]
[calvin]
[XXXXXXXXXXXXXXXX]
[72]
0x0000000000 10 87 cd 91 92 02 18 8f de b3 d6 8a d1 99 a2 22 ..............."
0x0000000001 28 c4 e6 88 89 01 30 88 ef 99 ab c5 e8 8c 91 11 (.....0.........
0x0000000002 3d 00 00 c8 42 41 00 00 00 00 00 88 c3 40 4a 06 =...BA.......@J.
0x0000000003 63 61 6c 76 69 6e 52 10 58 58 58 58 58 58 58 58 calvinR.XXXXXXXX
0x0000000004 58 58 58 58 58 58 58 58 XXXXXXXX
[-287454020]
[-1234605616436508552]
[287454020]
[1234605616436508552]
[100.000000]
[10000.000000]
[calvin]
[XXXXXXXXXXXXXXXX]
real 0m4.248s
user 0m4.235s
sys 0m0.011s
[/code]
压测结论:
* DirectStruct序列化(带压缩)后的报文大小66字节,而Google Protocol Buffer为72字节,DirectStruct在序列化和压缩报文大小上险胜。
* DirectStruct反复序列化/反序列化(带压缩)1000万次总耗时1.080s,而Google Protocol Buffer总耗时4.248s,DirectStruct在序列化性能上完胜。
* DirectStruct编译客户应用代码时不需要额外的头文件和库文件,只需要工具dsc生成的IDL_AllTypes2.dsc.c、IDL_AllTypes2.dsc.h即可,而Google Protocol Buffer不仅需要PressProtobuf.pb.cc、PressProtobuf.pb.h,还需要/usr/local/include/google/protobuf下的一大堆头文件以及/usr/local/lib/libprotobuf.a(9MB),还依赖于-lpthread,编译环境复杂。
是不是越看越心动了?那就赶紧下载来玩玩吧
N.最后
最近我正在开发一个应用平台,利用tcpdaemon做TCP服务器,利用DirectStruct+fasterxml自动生成的代码轻松解包XML到C结构体,再调用DirectStruct自动生成的ESQL代码INSERT到数据库表里,十几行代码搞定,看,自由组合DirectStruct自动生成的代码能给你带来巨大的便捷性。有了DirectStruct,以后谁还会老老实实的写大量XML转换和ESQL代码呢?
欢迎使用DirectStruct,如果你使用中碰到了问题或者有更酷的想法请告诉我,谢谢 ^_^
首页传送门 : [url]http://git.oschina.net/calvinwilliams/DirectStruct[/url]
作者邮箱 : calvinwilliams.c@gmail.com
C结构体工具DirectStruct(综合示例一)
[url]http://my.oschina.net/u/988092/blog/325072[/url]
C结构体工具DirectStruct(综合示例二)
[url]http://my.oschina.net/u/988092/blog/325073[/url]
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
C
1
https://gitee.com/calvinwilliams/DirectStruct.git
git@gitee.com:calvinwilliams/DirectStruct.git
calvinwilliams
DirectStruct
DirectStruct
release

搜索帮助