• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>
            穩(wěn)定盈利的期貨交易方法-量化趨勢(shì)交易

            alantop -專業(yè)量化投資者

            愛好:量化投資,逆向工程,滲透
            隨筆 - 595, 文章 - 0, 評(píng)論 - 921, 引用 - 0
            數(shù)據(jù)加載中……

            由淺入深,舉例講解RPC(一)

             

            關(guān)于RPC的文章很多,但是系統(tǒng)講解的很少。下面我將寫一個(gè)系列報(bào)道。用代碼和論述來把rpc來講講清楚。

            這篇就是開始第一篇了。

             

            由于工作比較忙。我們抽出一個(gè)星期的時(shí)間,有時(shí)間會(huì)寫一點(diǎn)。把這個(gè)系列寫完。所以,有可能每個(gè)系列都比較短些。

            從最基本的講起,讓大家徹底明白RPC.

             

            好了廢話不多說了。正是開始。

             

             

            首先,你要用RPC,必須先搞清楚什么是IDL.

             

            Rpc是什么?

            http://www.shnenglu.com/alantop/archive/2007/07/09/27717.html

            IDL是什么?

            http://www.shnenglu.com/alantop/archive/2007/07/09/27725.html

             

            下來,舉個(gè)例子。怎么樣把一個(gè)標(biāo)準(zhǔn)程序改成用IDL語言寫的程序。

             

            這是一個(gè)標(biāo)準(zhǔn)程序。

            // File Standalone.cpp

            #include <iostream>

             

            // Future server function.

            void Output(const char* szOutput)

            {

               std::cout << szOutput << std::endl;

            }

             

            int main()

            {

               // Future client call.

               Output("Hello Lonely World!");

            }

             

            下來看我們?cè)趺窗阉臑橐粋€(gè)標(biāo)準(zhǔn)IDL語言的程序

            IDL語言定義接口:

            // File Example1.idl

            [

               // A unique identifier that distinguishes this

               // interface from other interfaces.

               uuid(00000001-EAF3-4A7A-A0F2-BCE4C30DA77E),

             

               // This is version 1.0 of this interface.

               version(1.0),

             

               // This interface will use an implicit binding

               // handle named hExample1Binding.

               implicit_handle(handle_t hExample1Binding)

            ]

            interface Example1 // The interface is named Example1

            {

               // A function that takes a zero-terminated string.

               void Output(

                  [in, string] const char* szOutput);

            }

            上面這個(gè)文件是我們用idl語言定義的,我們定義了一個(gè)接口Example1, 它帶有uuidversion. 這個(gè)接口里定義了一個(gè)函數(shù)Output.

             

            UUID是什么?

            http://www.shnenglu.com/alantop/archive/2007/07/09/27726.html

             

             

            接口的implicit_handle屬性,我們后面再討論。

             

            接下來干什么呢?

            我們?yōu)榱嗽诔绦蛑惺褂?/span>idl,必須通過通過編譯器(midl.exe)把它翻譯成客戶代理和服務(wù)器存根, 代理和存根將在后面被我們的編譯器(windows平臺(tái)下的cl.exe)所使用。

             

             

            改好的服務(wù)器端程序:

            // File Example1Server.cpp

            #include <iostream>

            #include "Example1.h"

             

            // Server function.

            void Output(const char* szOutput)

            {

               std::cout << szOutput << std::endl;

            }

             

            int main()

            {

               RPC_STATUS status;

             

               // Uses the protocol combined with the endpoint for receiving

               // remote procedure calls.

               status = RpcServerUseProtseqEp(

                  reinterpret_cast<unsigned char*>("ncacn_ip_tcp"), // Use TCP/IP

                                                                    // protocol.

                  RPC_C_PROTSEQ_MAX_REQS_DEFAULT, // Backlog queue length for TCP/IP.

                  reinterpret_cast<unsigned char*>("4747"), // TCP/IP port to use.

                  NULL); // No security.

             

               if (status)

                  exit(status);

             

               // Registers the Example1 interface.

               status = RpcServerRegisterIf(

                  Example1_v1_0_s_ifspec, // Interface to register.

                  NULL, // Use the MIDL generated entry-point vector.

                  NULL); // Use the MIDL generated entry-point vector.

             

               if (status)

                  exit(status);

             

               // Start to listen for remote procedure

               // calls for all registered interfaces.

               // This call will not return until

               // RpcMgmtStopServerListening is called.

               status = RpcServerListen(

                 1, // Recommended minimum number of threads.

                 RPC_C_LISTEN_MAX_CALLS_DEFAULT, // Recommended

                                        //maximum number of threads.

                 FALSE); // Start listening now.

             

               if (status)

                  exit(status);

            }

             

            // Memory allocation function for RPC.

            // The runtime uses these two functions for allocating/deallocating

            // enough memory to pass the string to the server.

            void* __RPC_USER midl_user_allocate(size_t size)

            {

                return malloc(size);

            }

             

            // Memory deallocation function for RPC.

            void __RPC_USER midl_user_free(void* p)

            {

                free(p);

            }

             

            這是初始化,和注冊(cè)接口的代碼。

             

            現(xiàn)在看看怎么寫客戶端

            // File Example1Client.cpp

            #include <iostream>

            #include "Example1.h"

             

            int main()

            {

               RPC_STATUS status;

               unsigned char* szStringBinding = NULL;

             

               // Creates a string binding handle.

               // This function is nothing more than a printf.

               // Connection is not done here.

               status = RpcStringBindingCompose(

                  NULL, // UUID to bind to.

                  reinterpret_cast<unsigned char*>("ncacn_ip_tcp"), // Use TCP/IP

                                                                    // protocol.

                  reinterpret_cast<unsigned char*>("localhost"), // TCP/IP network

                                                                 // address to use.

                  reinterpret_cast<unsigned char*>("4747"), // TCP/IP port to use.

                  NULL, // Protocol dependent network options to use.

                  &szStringBinding); // String binding output.

             

               if (status)

                  exit(status);

             

               // Validates the format of the string binding handle and converts

               // it to a binding handle.

               // Connection is not done here either.

               status = RpcBindingFromStringBinding(

                  szStringBinding, // The string binding to validate.

                  &hExample1Binding); // Put the result in the implicit binding

                                      // handle defined in the IDL file.

             

               if (status)

                  exit(status);

             

               RpcTryExcept

               {

                  // Calls the RPC function. The hExample1Binding binding handle

                  // is used implicitly.

                  // Connection is done here.

                  Output("Hello RPC World!");

               }

               RpcExcept(1)

               {

                  std::cerr << "Runtime reported exception " << RpcExceptionCode()

                            << std::endl;

               }

               RpcEndExcept

             

               // Free the memory allocated by a string.

               status = RpcStringFree(

                  &szStringBinding); // String to be freed.

             

               if (status)

                  exit(status);

             

               // Releases binding handle resources and disconnects from the server.

               status = RpcBindingFree(

                  &hExample1Binding); // Frees the implicit binding handle defined in

                                      // the IDL file.

             

               if (status)

                  exit(status);

            }

             

            // Memory allocation function for RPC.

            // The runtime uses these two functions for allocating/deallocating

            // enough memory to pass the string to the server.

            void* __RPC_USER midl_user_allocate(size_t size)

            {

                return malloc(size);

            }

             

            // Memory deallocation function for RPC.

            void __RPC_USER midl_user_free(void* p)

            {

                free(p);

            }

             

            posted on 2007-07-09 12:41 AlanTop 閱讀(4639) 評(píng)論(3)  編輯 收藏 引用 所屬分類: COMC++

            評(píng)論

            # re: 由淺入深,舉例講解RPC(一)[未登錄]  回復(fù)  更多評(píng)論   

            好文章!
            2007-07-09 17:13 | 111

            # re: 由淺入深,舉例講解RPC(一)  回復(fù)  更多評(píng)論   

            好,頂!!
            2007-07-13 17:07 | 黃大仙

            # re: 由淺入深,舉例講解RPC(一)  回復(fù)  更多評(píng)論   

            后面還有嗎?謝謝
            2009-04-05 23:54 | 螢火蟲
            久久久这里有精品| 欧洲精品久久久av无码电影 | 久久这里只有精品久久| 日韩影院久久| 久久99精品久久久久久秒播| 午夜欧美精品久久久久久久| 日本欧美国产精品第一页久久| 欧洲国产伦久久久久久久| 国内高清久久久久久| 怡红院日本一道日本久久| 久久久中文字幕日本| 91久久香蕉国产熟女线看| 精品免费久久久久久久| 亚洲va国产va天堂va久久| 91精品国产综合久久久久久| 俺来也俺去啦久久综合网| 国产精品成人久久久久久久| 久久不射电影网| 亚洲综合婷婷久久| 99久久国产亚洲综合精品| 久久精品免费全国观看国产| av无码久久久久不卡免费网站 | 久久精品这里只有精99品| 久久久久九九精品影院| 亚洲AV无码一区东京热久久| 青青青伊人色综合久久| 中文无码久久精品| 色偷偷91久久综合噜噜噜噜| 97久久精品无码一区二区 | 波多野结衣久久| 精品国产热久久久福利| 久久亚洲AV成人无码电影| 免费精品久久久久久中文字幕| 久久91综合国产91久久精品| 久久精品一区二区三区AV| 久久亚洲私人国产精品vA| 亚洲国产精品一区二区三区久久| 国产69精品久久久久观看软件 | 国产偷久久久精品专区| 久久人人超碰精品CAOPOREN| 久久青草国产精品一区|