码迷,mamicode.com
首页 > 其他好文 > 详细

读DataSnap源代码(二)

时间:2015-09-18 12:01:32      阅读:275      评论:0      收藏:0      [点我收藏+]

标签:

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 字眼了, :)

 

读DataSnap源代码(二)

标签:

原文地址:http://www.cnblogs.com/Jiaojiawang/p/4818506.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!