Pipe template classes基于模板实现的管道类.docx
- 文档编号:10756162
- 上传时间:2023-05-27
- 格式:DOCX
- 页数:19
- 大小:111.07KB
Pipe template classes基于模板实现的管道类.docx
《Pipe template classes基于模板实现的管道类.docx》由会员分享,可在线阅读,更多相关《Pipe template classes基于模板实现的管道类.docx(19页珍藏版)》请在冰点文库上搜索。
Pipetemplateclasses基于模板实现的管道类
Pipetemplateclasses
Byklizardin,2Nov2010
4.69(9votes)
Youaresignedupforournewslettersbutyouremailaddressiseitherunconfirmed,orhasnotbeenreconfirmedinalongtime.Pleaseclickheretohaveaconfirmationemailsentsowecanconfirmyouremailaddressandstartsendingyounewslettersagain.Alternatively,youcanupdateyoursubscriptions.
窗体顶端
∙Downloaddemoproject-58.1KB
∙Downloadsource-441KB
1.Introduction
Thispipetemplatelibrarywaswrittentominimizetheprogrammer'seffortincreatingpipeserversandclients.Withthislibrary,theonlythingsyoushouldneeddotogetaworkingpipeclientandserveristocreatethedataclassesandimplementtheserversidefunctionforthecorrespondingdataclasses.Asecondprioritytaskinthisprojectwastogatheralltheinformationonpipeerrorsandcriticalsituations.Andofcourse,youcanusethepipelibrarycodeastechnicaldocumentationonpipesinWindows.
ThislibrarywaswrittenforMSVC5.0,butitalsoworkswithlaterversionsofMSVC.IfyourunthislibraryforlaterversionsofMSVC,itisbesttousetheATLatlsecurity.hfileinsteadoftheonethatcomeswiththislibrary,becausethesecurity.hfromthispackagewasrewrittentoworkwiththeMSVC5.0compiler.ThissuggestiononlyappliestoservercodewhereDACLisusedtosetpipesecurity.AnyimplementationofDACLwrappersmaybeusedforthatpurpose.
2.Background
ThislibraryusestypelistsasintroducedbyAlexandrescu(seehisbook"ModernC++Design"formoreinformation,orjustlookatthefilesources\utils\typelist.h).Typelisttypedefsareusedinthedeclarationoftheserverbaseclass.
3.UsingtheLibrary
3.1.QuickStart
3.1.1.Functionality
Thelibrarysupportstransmittingthefollowingmessages:
Ontheclientside
1.Createthepipeclient;
2.Openit;
3.Testit,determineifitisconnectedtoaserverside;
4.Createthedataobjectsandsendthemtoaserver;
5.Testtheconnectiontoaserverand,ifneeded,reconnect;
6.Closetheclientpipe.
Ontheserverside
1.Createtheserverobject;
2.Startserverobjects(whencreatingaworkerthread,waitfortheregistrationofapipeclientthatinturncreatesaworkerthreadperclientcommunicationchannel);
3.Whenacommunicationchanneliscreated,thenbegintransmittingdatafromtheclientside;
4.Datafromeveryclientiscomposedintothemessages;
5.Serverdecideswhatmessagecamefromtheclientandcallsthecorrespondingfunctionoftheserver;
6.Communicationscontinuetilltheserverstops.
3.1.2.Messages
Theunitofdatatransmissionisamessage.
Amessageis:
∙anobjectofaclassthatwasderivedfromtheCPipeDataBaseImplclasstemplate;
∙withadeclaredGUIDthatidentifiesthisclassfromallothermessagesprocessedbyaserver;
∙inwhichvirtualfunctionsareimplementedtosave/loaddata.
Let'slookattheclassdeclarationofamessage.Take,forexample,thedeclarationfromthetestserver(test_server\Messages.hfile)
Collapse|CopyCode
//firstofalldeclareGUID
//{E67A8D9E-EEB7-42d5-A1F9-25BDEE214A08}
staticconstGUIDPipeMsgGUID_State=
{0xe67a8d9e,0xeeb7,0x42d5,{0xa1,0xf9,0x25,0xbd,0xee,0x21,0x4a,0x8}};
/*
stateinformationtoregistry/unregistryclientsonserverandverifythereactivity
*/
structCPipeMsg_State:
publicCPipeDataBaseImpl
{
enumStateEn{S_Null=0,S_Started,S_Running,S_Stoped};
CPipeMsg_State(constCString&_sName=_T(""),StateEn_state=S_Null)
:
m_sName(_sName),m_state(_state)
{
}
DECLARE_PIPEDATAGUID(PipeMsgGUID_State);
virtualboolsave(IPipeCommunicator*_ppipe)const
{
VERIFY_EXIT1(NOT_NULL(_ppipe),false);
if(!
_ppipe->write(m_sName))returnfalse;
if(!
_ppipe->write((long)m_state))returnfalse;
returntrue;
}
virtualboolload(constIPipeCommunicator*_ppipe)
{
VERIFY_EXIT1(NOT_NULL(_ppipe),false);
longnstate=0;
if(!
_ppipe->read(m_sName))returnfalse;
if(!
_ppipe->read(nstate))returnfalse;
m_state=(StateEn)nstate;
returntrue;
}
constCString&get_ClientName()const{returnm_sName;}
StateEnget_state()const{returnm_state;}
protected:
CStringm_sName;//nameoftheclient
StateEnm_state;//stateinforamtion
};//structCPipeMsg_State
Everymessagetypeisdeclaredinitsownclass.Inthiscase,youcanseeaclassfortransmittingstateinformation(CPipeMsg_State).Asyoucanseeinthecodefragment,thedataclassisderivedfromtheCPipeDataBaseImpltemplate,thatreceivesthecreatedclassasatemplateparameter.Also,youshouldusethemacroDECLARE_PIPEDATAGUIDtosetaGUIDforthemessageclass.Inaddition,thecodefragmentshowshowtoimplementthevirtualfunctionssave()andload(),tosaveandloadelementsofthedeclaredclassintoorfromthepipe.
Itisimportanttoimplementexactlythesamesequenceofcallsinthesave()andload()functionsasshownheretowriteandreaddatatoandfromapipe.Itisalsoimportantthattheorderbethesameinbothfunctions.
TheinterfaceIPipeCommunicatorgivestheimplementationofsomebasedatatypeswhichareusedforwritingandreadingclasselementstoandfromthepipe.Youshouldusethefunctionsofthisinterface(baseimplementations)tosaveandloadyourmessageclassdata.
Fromanarchitectpointofview,themessageclassdescribeswhichdataissavedandloadedtoapipeandhowthedataissavedandloadedwhileworkingwiththepipe.Themessageclassesareusedinboththeclientandserversidesofthepipechannelinthislibrary.Forthatreason,themessagedeclarationfilesshouldbeincludedinbothpartsofthepipeimplementation.
3.1.3.Clientside
Let'stakealookattheclientsideofthislibrary(filetest_client\test_client.cppoftheprojecttest_client).
TheclientsidesimplycreatesanobjectofclassCClientPipeCommandusesittotransmitdatatoaserver.Ifweignorethedetailsofimplementingthecommunicationsinterface,theessenceofapipecanbeexpressedinafewsimplelines:
Collapse|CopyCode
CPipeMsg_Texttxt(sName,str);
pipe.save(&txt);
bFail=pipe.nead_toReconnect();
and:
Collapse|CopyCode
CPipeMsg_Statesi(_sName,_state);
_pipe.save(&si);
//...
bFail=pipe.nead_toReconnect();
Asyoucansee,tosendsomedatatotheserverside,youjustneedtocreateamessageclassobjectandthencallthesave()methodoftheCClientPipeCommclassobject.Thisfunctionwillreturntrueorfalsetoindicatethesuccessoftheoperation.Inaddition,ifthestateofthepipechanges(i.e.,disconnectsfromtheserver),thefunctionnead_toReconnect()willreturntruetosaythatyouneedtoreconnecttosenddata.
ClassCClientPipeCommdescription
Collapse|CopyCode
boolopen(constCString&_sServerName
constCString&_sPipeName
DWORD_dwDesiredAccess=GENERIC_WRITE
DWORD_nWaitTime=NMPWAIT_USE_DEFAULT_WAIT
bool_bWriteThroughMode=false
DWORD_dwPipeMode=PIPE_READMODE_BYTE
LPSECURITY_ATTRIBUTES_psa=NULL
);
Thisfunctionopensapipeontheclientside.
Parameters:
∙_sServerName-computernamewhereaserverisstartedwhichtheclientisconnectedto(ifitisthelocalmachine,youshouldsetthisparametertoa"."stringvalue).
∙_sPipeName--thepipechannelnametoopen.
∙_dwDesiredAccess-forthisreleaseofthelibrary,thisshouldalwaysbesettoGENERIC_WRITE.
∙_nWaitTime-setsthemaxpipeopentime(ifserverexists,butcan'trespondrightnow).
∙_bWriteThroughMode-setstheflagtouse"writethrough"modetoprocessthewriteoperationandwaituntiltheserverreturnsthereadstatustoguaranteethetransmittingofdata(ifsettotrue),ortousedatabufferingwherethewritefunctionreturnswithoutwaitingfordatatobetransmitted).
∙_dwPipeMode-setsthemodeofpipe(besttousePIPE_READMODE_BYTE).
∙_psa-setsthesecurityattributesforthispipe.
Seethefilepipe\namepipebase.hforadetaileddescriptionanderrorinformation.There,theCNamedPipeWrapclassisdefined,whichimplementsthelowlevelpipeoperations(seethefunctionopen()fordetailedinformation).
Collapse|CopyCode
voidclose()
Thisfunctionclosesthepipeontheclientside.
Collapse|CopyCode
operatorbool()const
booloperator!
()const
Aboveareoperatorstodetermineifthepipeisvalidornot.Apipeisvalidifaconnectionwascreatedsuccessfully.
Collapse|CopyCode
boolsave(IPipeDataBase*_data)
Sendsthedatatotheserver.Iftheoperationfails,theclientwilltrytoreconnectandthentrytoresenddataonetime.Ifreconnectionfails,itreturnsfalse.Ifdatawassuccessfullysent,thenitreturnstrue.
Collapse|CopyCode
boolnead_toReconnect()const
Thisfunctionreturnstrueif,afterasaveoperation,itneedstoreconnecttosendorresenddata.
Collapse|CopyCode
boolreconnect_client(DWORD_nWaitTime=NMPWAIT_USE_DEFAULT_WAIT)
Reconnectstheclientsideofthepipe.Ifaftercallingthisfunction,theconnectionisrestored,thenthepipeobjectwillbecamevalid;otherwise,itwillbeinvalid.(Youcantestthepipewithoperatorbool()orbooloperator!
().)
Parameter:
∙_nWaitTime-setsthemaxtimeforreconnecting.
Collapse|CopyCode
voidasync_reconnect_client(DWORD_nWaitTime=NMPWAIT_USE_DEFAULT_WAIT)
Reconnectstheclientsideofthepipeasynchronously.Createstheworkerthreadwhichtriestoreconnect.
Collapse|CopyCode
boolreconnecting()const
Returnstrueifasynchronouslyreconnectingnow.Ifthefunctionreturnsfalsebeforethefunctionvoidasync_reconnect_client()wascalled,youcantestifthepipeisnowconnected.
Collapse|CopyCode
voidflush()
Flushesbuffersoftheclientpipe.
Ane
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- Pipe template classes基于模板实现的管道类 classes 基于 模板 实现 管道
![提示](https://static.bingdoc.com/images/bang_tan.gif)