程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 更多編程語言 >> Delphi >> 讀DataSnap源代碼(二),讀datasnap源代碼

讀DataSnap源代碼(二),讀datasnap源代碼

編輯:Delphi

讀DataSnap源代碼(二),讀datasnap源代碼


program Project1;
{$APPTYPE GUI}

{$R *.dres}

uses
  Vcl.Forms,
  Web.WebReq,
  IdHTTPWebBrokerBridge,
  FormUnit1 in 'FormUnit1.pas' {Form1},
  ServerMethodsUnit1 in 'ServerMethodsUnit1.pas',
  WebModuleUnit1 in 'WebModuleUnit1.pas' {WebModule1: TWebModule};

{$R *.res}

begin
{ 返回是什麼,見下面的【綠色代碼】 } if WebRequestHandler <> nil then WebRequestHandler.WebModuleClass := WebModuleClass; Application.Initialize; Application.CreateForm(TForm1, Form1); Application.Run; end.

 IdHTTPWebBrokerBridge單元中:

 1 initialization
{** 掛鉤 **}
2 WebReq.WebRequestHandlerProc := IdHTTPWebBrokerBridgeRequestHandler; 3 {$IFDEF HAS_CLASSVARS} 4 {$IFNDEF HAS_CLASSDESTRUCTOR} 5 finalization 6 FreeAndNil(TIdHTTPWebBrokerBridgeRequestHandler.FWebRequestHandler); 7 {$ENDIF} 8 {$ELSE} 9 finalization 10 FreeAndNil(IndyWebRequestHandler); 11 {$ENDIF}

WebRequestHandler是Web.WebReq中的一個方法:

1 function WebRequestHandler: TWebRequestHandler;
2 begin
3   if Assigned(WebRequestHandlerProc) then
4     Result := WebRequestHandlerProc     /** 指向 IdHTTPWebBrokerBridgeRequestHandler 函數了 **/
5   else
6     Result := nil;
7 end;

IdHTTPWebBrokerBridgeRequestHandler的定義:

 1 function IdHTTPWebBrokerBridgeRequestHandler: TWebRequestHandler;
 2 begin
 3   {$IFDEF HAS_CLASSVARS}
 4   if not Assigned(TIdHTTPWebBrokerBridgeRequestHandler.FWebRequestHandler) then
 5     TIdHTTPWebBrokerBridgeRequestHandler.FWebRequestHandler := TIdHTTPWebBrokerBridgeRequestHandler.Create(nil);
 6   Result := TIdHTTPWebBrokerBridgeRequestHandler.FWebRequestHandler;
 7   {$ELSE}
 8   if not Assigned(IndyWebRequestHandler) then
 9     IndyWebRequestHandler := TIdHTTPWebBrokerBridgeRequestHandler.Create(nil);
10   Result := IndyWebRequestHandler;
11   {$ENDIF}
12 end;

 還記得(一)中的那個靜態成員嗎? 在第4行初始化了。

 

繼續(一)中 Run 函數中的代碼:

 1 procedure TIdHTTPWebBrokerBridgeRequestHandler.Run(AThread: TIdContext; ARequestInfo: TIdHTTPRequestInfo; AResponseInfo: TIdHTTPResponseInfo);
 2 var
 3   LRequest: TIdHTTPAppRequest;
 4   LResponse: TIdHTTPAppResponse;
 5 begin
 6   try
 7     LRequest := TIdHTTPAppRequest.Create(AThread, ARequestInfo, AResponseInfo);
 8     try
 9       LResponse := TIdHTTPAppResponse.Create(LRequest, AThread, ARequestInfo, AResponseInfo);
10       try
11         // WebBroker will free it and we cannot change this behaviour
12         AResponseInfo.FreeContentStream := False;
13         HandleRequest(LRequest, LResponse);
14       finally
15         FreeAndNil(LResponse);
16       end;
17     finally
18       FreeAndNil(LRequest);
19     end;
20   except
21     // Let Indy handle this exception
22     raise;
23   end;
24 end;

 

TIdHTTPWebBrokerBridgeRequestHandler並沒有 override 父類(TWebRequestHandler)的HandleRequest方法. 看TWebRequestHandler.HandleRequest代碼

 1 function TWebRequestHandler.HandleRequest(Request: TWebRequest;
 2   Response: TWebResponse): Boolean;
 3 var
 4   I: Integer;
 5   LWebModule: TComponent;
 6   LWebAppServices: IWebAppServices;
 7   LGetWebAppServices: IGetWebAppServices;
 8   LComponent: TComponent;
 9 begin
10   Result := False;
11   LWebModule := ActivateWebModules;
12   if Assigned(LWebModule) then
13   try
14     try
15       if Supports(IInterface(LWebModule), IGetWebAppServices, LGetWebAppServices) then
16         LWebAppServices := LGetWebAppServices.GetWebAppServices;
17       if LWebAppServices = nil then
18         for I := 0 to LWebModule.ComponentCount - 1 do
19         begin
20           LComponent := LWebModule.Components[I];
21           if Supports(LComponent, IWebAppServices, LWebAppServices) then
22             if LWebAppServices.Active then
23               break
24             else
25               LWebAppServices := nil;
26         end;
27       if LWebAppServices = nil then
28         LWebAppServices := TDefaultWebAppServices.Create;
29       LWebAppServices.InitContext(LWebModule, Request, Response);
30       try
31         try
32           Result := LWebAppServices.HandleRequest;
33         except
34           ApplicationHandleException(LWebAppServices.ExceptionHandler);
35         end;
36       finally
37         LWebAppServices.FinishContext;
38       end;
39       if Result and not Response.Sent then
40         Response.SendResponse;
41     except
42       ApplicationHandleException(LWebAppServices.ExceptionHandler);
43     end;
44   finally
45     DeactivateWebModules(LWebModule);
46   end;
47 end;

 

在上面代碼中,可以看到 WebModule 字眼了, :)

 

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved