C++ Network Programming Volume 2 The C++ In-Depth Series Bjarne Stroustrup, Editor “I have made this letter longer than usual, because I lack the time to make it short.” —BLAISEPASCAL The advent of the ISO/ANSI C++ standard marked the beginning of a new era for C++ programmers. The standard offers many new facilities and opportunities, but how can a real-world programmer find the time to discover the key nuggets of wisdom within this mass of information? The C++ In-Depth Seriesminimizes learning time and confusion by giving programmers concise, focused guides to specific topics. Each book in this series presents a single topic, at a technical level appropriate to that topic. The Series’ practical approach is designed to lift professionals to their next level of programming skills. Written by experts in the field, these short, in-depth monographs can be read and referenced without the distraction of unrelated material. The books are cross-referenced within the Series, and also reference The C++ Programming Languageby Bjarne Stroustrup. As you develop your skills in C++, it becomes increasingly important to separate essential information from hype and glitz, and to find the in-depth content you need in order to grow. The C++ In-Depth Series provides the tools, concepts, techniques, and new approaches to C++ that will give you a critical edge. Titles in the Series Accelerated C++:Practical Programming by Example, Andrew Koenig and Barbara E. Moo Applied C++: Practical Techniques for Building Better Software, Philip Romanik and Amy Muntz The Boost Graph Library: User Guide and Reference Manual, Jeremy G. Siek, Lie-Quan Lee, and Andrew Lumsdaine C++ Coding Standards: 101 Rules, Guidelines, and Best Practices, Herb Sutter and Andrei Alexandrescu C++ In-Depth Box Set, Bjarne Stroustrup, Andrei Alexandrescu, Andrew Koenig, Barbara E. Moo, Stanley B. Lippman, and Herb Sutter C++ Network Programming, Volume 1: Mastering Complexity with ACE and Patterns, Douglas C. Schmidt and Stephen D. Huston C++ Network Programming, Volume 2: Systematic Reuse with ACE and Frameworks, Douglas C. Schmidt and Stephen D. Huston C++ Template Metaprogramming: Concepts, Tools, and Techniques from Boost and Beyond, David Abrahams and Aleksey Gurtovoy Essential C++, Stanley B. Lippman Exceptional C++: 47 Engineering Puzzles, Programming Problems, and Solutions, Herb Sutter Exceptional C++ Style: 40 New Engineering Puzzles, Programming Problems, and Solutions, Herb Sutter Modern C++ Design: Generic Programming and Design Patterns Applied, Andrei Alexandrescu More Exceptional C++: 40 New Engineering Puzzles, Programming Problems, and Solutions, Herb Sutter For more information, check out the series web site at www.awprofessional.com/series/indepth/ C++ Network Programming Volume 2 Systematic Reuse with ACE and Frameworks Douglas C. Schmidt Stephen D. Huston Boston•SanFrancisco•NewYork•Toronto•Montreal London•Munich•Paris•Madrid Capetown•Sydney•Tokyo•Singapore•MexicoCity Manyofthedesignationsusedbymanufacturersandsellerstodistinguishtheirproductsareclaimedastrademarks.Where thosedesignationsappearinthisbook,andAddison-Wesleywasawareofatrademarkclaim,thedesignationshavebeen printedwithinitialcapitallettersorinallcapitals. Theauthorsandpublisherhavetakencareinthepreparationofthisbook,butmakenoexpressedorimpliedwarrantyofany kindandassumenoresponsibilityforerrorsoromissions.Noliabilityisassumedforincidentalorconsequentialdamages inconnectionwithorarisingoutoftheuseoftheinformationorprogramscontainedherein. Thepublisheroffersdiscountsonthisbookwhenorderedinquantityforbulkpurchasesandspecialsales.Formore information,pleasecontact: U.S.CorporateandGovernmentSales (800)382-3419 [email protected] ForsalesoutsideoftheU.S.,pleasecontact: InternationalSales (317)581-3793 [email protected] VisitAddison-WesleyontheWeb:www.awprofessional.com LibraryofCongressCataloging-in-PublicationData Schmidt,DouglasC. C++networkprogramming/DouglasC.Schmidt,StephenD.Huston. p.cm. Includesbibliographicalreferencesandindex. Contents:Vol.2.SystematicreusewithACEandframeworks. ISBN0-201-79525-6(v.2:pbk.) 1.C++(Computerprogramlanguage)2.Object-orientedprogramming(Computer science)3.Computernetworks.I.Huston,StephenD.II.Title. QA76.73.C153S3682002 005.2’762—dc21 2001053345 Copyright(cid:2)c 2003byPearsonEducation,Inc. Allrightsreserved.Nopartofthispublicationmaybereproduced,storedinaretrievalsystem,ortransmitted,inanyform, orbyanymeans,electronic,mechanical,photocopying,recording,orotherwise,withoutthepriorconsentofthepublisher. PrintedintheUnitedStatesofAmerica.PublishedsimultaneouslyinCanada. Forinformationonobtainingpermissionforuseofmaterialfromthiswork,pleasesubmitawrittenrequestto: PearsonEducation,Inc. RightsandContractsDepartment 75ArlingtonStreet,Suite300 Boston,MA02116 Fax:(617)848-7047 ISBN:0-201-79525-6 2345678910—OPM—06050403 Second printing, February 2005 Contents Foreword vii AboutThisBook xi Chapter1 Object-OrientedFrameworksforNetworkProgramming 1 1.1 AnOverviewofObject-OrientedFrameworks 1 1.2 ComparingSoftwareDevelopmentandReuseTechniques 4 1.3 ApplyingFrameworkstoNetworkProgramming 12 1.4 ATourthroughtheACEFrameworks 14 1.5 Example: ANetworkedLoggingService 19 1.6 Summary 21 Chapter2 ServiceandConfigurationDesignDimensions 23 2.1 ServiceandServerDesignDimensions 24 2.2 ConfigurationDesignDimensions 34 2.3 Summary 38 Chapter3 TheACEReactorFramework 39 3.1 Overview 39 3.2 TheACE Time ValueClass 42 3.3 TheACE Event HandlerClass 46 3.4 TheACETimerQueueClasses 61 3.5 TheACE ReactorClass 70 3.6 Summary 86 Chapter4 ACEReactorImplementations 87 4.1 Overview 87 4.2 TheACE Select ReactorClass 89 4.3 TheACE TP ReactorClass 99 v vi CONTENTS 4.4 TheACE WFMO ReactorClass 103 4.5 Summary 113 Chapter5 TheACEServiceConfiguratorFramework 115 5.1 Overview 115 5.2 TheACE Service ObjectClass 118 5.3 TheACE Service RepositoryClasses 126 5.4 TheACE Service ConfigClass 138 5.5 Summary 154 Chapter6 TheACETaskFramework 155 6.1 Overview 155 6.2 TheACE Message QueueClass 157 6.3 TheACE TaskClass 183 6.4 Summary 202 Chapter7 TheACEAcceptor-ConnectorFramework 203 7.1 Overview 203 7.2 TheACE Svc HandlerClass 206 7.3 TheACE AcceptorClass 216 7.4 TheACE ConnectorClass 229 7.5 Summary 256 Chapter8 TheACEProactorFramework 257 8.1 Overview 257 8.2 TheAsynchronousI/OFactoryClasses 261 8.3 TheACE HandlerClass 270 8.4 TheProactiveAcceptor-ConnectorClasses 278 8.5 TheACE ProactorClass 286 8.6 Summary 296 Chapter9 TheACEStreamsFramework 297 9.1 Overview 297 9.2 TheACE ModuleClass 299 9.3 TheACE StreamClass 314 9.4 Summary 318 Glossary 319 Bibliography 329 Index 337 Foreword TheADAPTIVECommunicationEnvironment(ACE)toolkithasachievedenormoussuc- cess in the area of middleware for networked computing. Due to its flexibility, perfor- mance, platform coverage, and other key properties, ACE enjoys broad acceptance by the networkedapplicationsoftwarecommunity,asevidencedbyitsuseinthousandsofapplica- tions,inscoresofcountries,andindozensofdomains. ACEhasalsoreceivedconsiderable attentionbeyondthemiddlewarecommunitysinceit’sanopen-sourcerolemodelforhigh- qualityandwell-designedpattern-orientedsoftwarearchitectures. ButwhyisACEsosuccessful? Addressingthisquestionproperlytakessomethought. To start off, let’s reconsider the Foreword from C++ Network Programming: Mastering Complexity with ACE and Patterns (C++NPv1) and resume the mass transit analogy pre- sented there by my colleague Steve Vinoski. Steve’s right that a high-quality mass transit system consists of more than just aircraft, airports, trains, train stations, and rails. It also needs less obvious infrastructure, such as scheduling, routing, ticketing, maintenance, and monitoring. But even a complete collection of ingredients is still not sufficient to develop aneffectivemasstransitsystem. Arrangingtheseingredientssotheyseamlesslyfulfilltheir primaryobjective—fastandreliabletransportationofpeople—isequallyimportant. Would you use a mass transit system whose ticketing was located in a train maintenance location oranairporthangar,orwhoseplannedandactualschedulingandroutingweren’tavailable tothepublic? Idoubtit! Thesuccessofmasstransitsystemsdependsonmorethantheknowledgeoftheinfra- structurepartsthatareprovided—itdependsonhowthesedifferentpartsmustbeconnected and integrated with their environment. This knowledge enables architects of mass transit systemstointegrateindividualpartsintohigher-levelbuildingblocksandtoconnectthese buildingblockseffectively. Forexample,ticketing,informationpoints,baggageoffices,and boarding are integrated in train stations located at city centers or major suburban centers. Likewise, airports are often located near large cities and connected by frequent express trains. vii viii Foreword Even mass transit centers themselves are arranged so that activities can be performed effectively. Forexample,whenyouenteratrainstationorairportviathemainentrance,you find ticket agents, information centers, and timetables. You also find shops to satisfy your travelneeds. Asyouenterthemaintrainhallorairportconcourse,youfindotherinforma- tion centers, up-to-date scheduling information, and the platforms and gates for boarding the trains and planes. Mass transit centers thus not only provide all necessary services to begin and end a journey, they also organize their internal “control flows” effectively. While the core structures and control flows in most train stations and airports are similar, their concrete realization can differ widely. Yet we all recognize these mass transit center patterns immediately since they follow key invariants that we’ve learned through years of experience. Sowhat’stheconnectionbetweensuccessfulmasstransitsystemdesignandthesuccess ofACE?Theanswerissimple: Inadditiontothebasicnetworkcomputingingredients(the wrapper facades that Doug and Steve introduced in C++NPv1), ACE also includes use- ful object-oriented frameworks that build upon these wrapper facades and provide useful higher-level communication services, such as event demultiplexing and dispatching, con- nectionmanagement,serviceconfiguration,concurrency,andhierarchicallylayeredstream processing. The ACE framework services satisfy many networked software needs by or- ganizing the structures and internal control flows of your applications effectively via key patternslearnedthroughyearsofexperience. TheACEframeworksofferyouanumberofimportantbenefits: • Youneedn’tdevelopthecapabilitiesprovidedbyACE,whichwillsaveconsiderable time and effort. You can therefore focus on your key responsibility: implementing theapplicationfunctionalityrequiredbyyourcustomersandendusers. • TheACEframeworksreifytheextensivenetworkprogrammingexpertisethatDoug, Steve,andtheircolleagueshavegainedoverseveraldecades. Inparticular,theACE frameworksefficientlyimplementthecanonicalclasses,classrelationships,andcon- trol flows common to networked applications. The ACE frameworks are tested reg- ularly by thousands of users from around the world, which has yielded many useful corrections and improvements. As an ACE user, you can directly leverage the cor- rectness,effectiveness,andefficiencyoftheACEframeworksinyourapplications. • A framework isn’t a framework if it can’t be adapted to specific user needs. This means you can adapt the ACE frameworks at key points of variation in networked applications. For example, the ACE Reactor framework can be adapted to use dif- ferent event demultiplexer functions, such as WaitForMultipleObjects() or select(). Likewise, the ACE Acceptor-Connector framework can be configured with different IPC mechanisms. While this adaptability is beneficial by itself, ACE goes a step further: for many adaptations you can configure the desired strategies fromavailableandinterchangeableimplementations. InadditiontothedifferentRe- Foreword ix actorimplementationsmentionedabove,forinstance,ACEprovideswrapperfacades forvariousIPCmechanisms,suchastheSockets,SSL,TLI,andsharedmemory,that helptoconfiguretheACEAcceptor-Connectorframeworkforspecificplatformsand applications. • Last but not least, the ACE frameworks don’t exist in isolation. You can therefore combinetheminnovelwaystocreatenetworkedapplicationsandentirelynewtypes of middleware. For example, you can integrate the Reactor framework with the Acceptor-Connector framework to separate connection establishment from service processing functionality in event-driven applications. You can likewise introduce variousformsofconcurrencyintoyourapplicationsusingtheACETaskframework. As a result of advising and leading many software projects over the years, I’ve found that ACE greatly simplifies the task of employing reusable middleware that can be cus- tomized readily to meet the needs of networked applications. Not all networked applica- tionsneedheavyweightmiddleware,suchasapplicationservers,webservices,andcomplex componentmodels. Yetmostnetworkedapplicationscanbenefitfromportableandefficient hostinfrastructuremiddlewarelikeACE.ThisflexibilityisthecoreofACE’ssuccesssince you needn’t commit to an entire middleware suite if you don’t use all of it. Instead, you can combine just the essential ACE middleware classes you need to compose applications thataresmall,butaspowerfulasnecessary. Forthisreason,IpredictthatACEwillstillbe widelyusedlongaftertheinfluenceoftoday’sheavyweightmiddlewarehaswaned. ACE’s tremendous flexibility also needn’t lead to a sea of incompatible middleware implementations. For example, if you build an embedded system that speaks the CORBA Internetinter-ORBprotocol(IIOP)totheoutsideworld,youcanuseTheACEORB(TAO), which is a CORBA-compliant, open-source, real-time object request broker (ORB) built usingtheACEwrapperfacadesandframeworks. IfCORBAisoverkillforyourapplication needs,however,youcanbuildcustom,yetinteroperable,middlewareusingtheappropriate ACE classes. Both solutions can be based on the same core structures and protocols, such as the ACE Common Data Representation (CDR) classes and its TCP/IP Socket wrapper facades. They can therefore communicate seamlessly with one another, just as you can take a train from Paris to Istanbul—the famous Orient Express—and travel through many Europeancountrieswithouthavingtochangetrainsduetoincompatiblerailroadnetworks. As Steve Vinoski and I have pointed out, there are many similarities between high- qualitymasstransitsystemsandhigh-qualitynetworkingmiddleware. Tomeandthousands of other C++ developers around the world, ACE is the toolkit for building the latter! Af- ter saying so many good things about ACE, however, let’s return to the main intent of this foreword: introducing the second volume (C++NPv2) of the C++ Network Programming series. As with all software technologies and middleware, the more you understand your tools, the better you’ll be able to apply them. It turns out that using ACE in your appli- cations is just one aspect of improving your networked software. To benefit significantly