Delphi DataSnap 流程分析(二)
Delphi DataSnap 流程分析(一)_看那山瞧那水的博客-CSDN博客
粗略分析了 创建传统DataSnap的流程,现在再分析下创建现在更常用的 方式:
DataSnap REST Application
这种方式只支持HTTP(普通HTTP和REST HTTP)通信,不支持TCP通信。
这种方式包含有WebModule,HTTP服务器也是TIdHTTPWebBrokerBridge
因此其HTTP Server的启动流程和 Delphi Web Server 流程分析_看那山瞧那水的博客-CSDN博客
里分析的一样,只是到了最后,也就是方法TCustomWebDispatcher.DispatchAction(),接着进行后续处理。
代码:
constructor TCustomWebDispatcher.Create(AOwner: TComponent);
varI: Integer;Component: TComponent;SetAppDispatcher: ISetAppDispatcher;
begin
{$IFDEF MSWINDOWS}
{$ENDIF}FDispatchList := TObjectList<TComponent>.Create;//TComponentList.Create;FDispatchList.OwnsObjects := False;FOnException := nil;if AOwner <> nil thenif AOwner is TCustomWebDispatcher thenraise EWebBrokerException.Create(sOnlyOneDispatcher)elsefor I := 0 to AOwner.ComponentCount - 1 doif AOwner.Components[I] is TCustomWebDispatcher thenraise EWebBrokerException.Create(sOnlyOneDispatcher);inherited CreateNew(AOwner, -1);FActions := TWebActionItems.Create(Self, TWebActionItem);if Owner <> nil thenfor I := 0 to Owner.ComponentCount - 1 dobeginComponent := Owner.Components[I];if Supports(IInterface(Component), ISetAppDispatcher, SetAppDispatcher) thenSetAppDispatcher.SetAppDispatcher(Self)else if Supports(IInterface(Component), IWebDispatch) thenFDispatchList.Add(Component);end;
end;
TCustomWebDispatcher类创建的时候,会加载支持接口IWebDispatch的组件,TDSHTTPWebDispatcher是支持IWebDispatch接口的:
TDSHTTPWebDispatcher = class(TDSHTTPServerTransport, IWebDispatch)
所以FDispatchList列表包含了WebModule的组件DSHTTPWebDispatcher1。
function TCustomWebDispatcher.DispatchAction(Request: TWebRequest;Response: TWebResponse): Boolean;
varI: Integer;Action, Default: TWebActionItem;Dispatch: IWebDispatch;
beginFRequest := Request;FResponse := Response;I := 0;Default := nil;if Response.Sent thenbeginResult := True;{ Note that WebSnapSvr enabled apps have no way to mark response as sent }Exit;end;Result := DoBeforeDispatch(Request, Response) or Response.Sent;while not Result and (I < FActions.Count) dobeginAction := FActions[I];Result := Action.DispatchAction(Request, Response, False);if Action.Default then Default := Action;Inc(I);end;// Dispatch to self registering componentsI := 0;while not Result and (I < FDispatchList.Count) dobeginif Supports(IInterface(FDispatchList.Items[I]), IWebDispatch, Dispatch) thenbeginResult := DispatchHandler(Self, Dispatch,Request, Response, False);end;Inc(I);end;if not Result and Assigned(Default) thenResult := Default.DispatchAction(Request, Response, True);if Result and not Response.Sent thenResult := DoAfterDispatch(Request, Response);end;
如果前面没有中断,则执行下面的代码:
// Dispatch to self registering components
I := 0;
while not Result and (I < FDispatchList.Count) do
begin
if Supports(IInterface(FDispatchList.Items[I]), IWebDispatch, Dispatch) then
begin
Result := DispatchHandler(Self, Dispatch,
Request, Response, False);
end;
Inc(I);
end;
因而执行Dispatch := DSHTTPWebDispatcher1(TDSHTTPWebDispatcher类),
DispatchHandler(): 这是一个局部方法
function DispatchHandler(Sender: TObject; Dispatch: IWebDispatch; Request: TWebRequest; Response: TWebResponse;DoDefault: Boolean): Boolean;
beginRes