读DataSnap源代码(一)
Delphi的DataSnap用了一段时间了,但一直感觉有些地方还不够了解,所以花时间阅读了源代码,特作此烂笔头。
Datasnap是在之前的WebBorker基础上搭建的,DataSnap向导自动生成了基础的代码,所以就以基础代码为起点来看看DataSnap的内部机制。
首选创建一个 Stand-alone 的REST App, 向导至少会为我们生成一个Form1和一个WebModule1,
FormUnit1单元如下:
- unit FormUnit1;
- interface
- uses
- Winapi.Messages, System.SysUtils, System.Variants,
- System.Classes, Vcl.Graphics, Vcl.Controls, Vcl.Forms, Vcl.Dialogs,
- Vcl.AppEvnts, Vcl.StdCtrls, IdHTTPWebBrokerBridge, Web.HTTPApp;
- type
- TForm1 = class(TForm)
- ButtonStart: TButton;
- ButtonStop: TButton;
- EditPort: TEdit;
- Label1: TLabel;
- ApplicationEvents1: TApplicationEvents;
- ButtonOpenBrowser: TButton;
- procedure FormCreate(Sender: TObject);
- procedure ApplicationEvents1Idle(Sender: TObject; var Done: Boolean);
- procedure ButtonStartClick(Sender: TObject);
- procedure ButtonStopClick(Sender: TObject);
- procedure ButtonOpenBrowserClick(Sender: TObject);
- private
- FServer: TIdHTTPWebBrokerBridge;
- procedure StartServer;
- { Private declarations }
- public
- { Public declarations }
- end;
- var
- Form1: TForm1;
- implementation
- {$R *.dfm}
- uses
- WinApi.Windows, Winapi.ShellApi, Datasnap.DSSession;
- procedure TForm1.ApplicationEvents1Idle(Sender: TObject; var Done: Boolean);
- begin
- ButtonStart.Enabled := not FServer.Active;
- ButtonStop.Enabled := FServer.Active;
- EditPort.Enabled := not FServer.Active;
- end;
- procedure TForm1.ButtonOpenBrowserClick(Sender: TObject);
- var
- LURL: string;
- begin
- StartServer;
- LURL := Format('http://localhost:%s', [EditPort.Text]);
- ShellExecute(0,
- nil,
- PChar(LURL), nil, nil, SW_SHOWNOACTIVATE);
- end;
- procedure TForm1.ButtonStartClick(Sender: TObject);
- begin
- StartServer;
- end;
- procedure TerminateThreads;
- begin
- if TDSSessionManager.Instance <> nil then
- TDSSessionManager.Instance.TerminateAllSessions;
- end;
- procedure TForm1.ButtonStopClick(Sender: TObject);
- begin
- TerminateThreads;
- FServer.Active := False;
- FServer.Bindings.Clear;
- end;
- procedure TForm1.FormCreate(Sender: TObject);
- begin
- FServer := TIdHTTPWebBrokerBridge.Create(Self);
- end;
- procedure TForm1.StartServer;
- begin
- if not FServer.Active then
- begin
- FServer.Bindings.Clear;
- FServer.DefaultPort := StrToInt(EditPort.Text);
- FServer.Active := True;
- end;
- end;
- end.
在Form1中,有一个FServer ,ButtonStart.Click事件中有FServer.Active := True;
TIdHTTPWebBrokerBridge 是 TIdHTTPWebBrokerBridge = class(TIdCustomHTTPServer)
到目前上为,至少知道Datasanp是基于Id组件的,那么Id(idHttpServer)和WebModule, DSServer, DSHTTPWebDispatcher1 是如何关连上的,又是如何调用到
DDServerClass实例方法呢?
- TIdHTTPWebBrokerBridge = class(TIdCustomHTTPServer)
- private
- procedure RunWebModuleClass(AThread: TIdContext; ARequestInfo: TIdHTTPRequestInfo;
- AResponseInfo: TIdHTTPResponseInfo);
- protected
- FWebModuleClass: TComponentClass;
- //
- 8 procedure DoCommandGet(AThread: TIdContext; ARequestInfo: TIdHTTPRequestInfo;
- 9 AResponseInfo: TIdHTTPResponseInfo); override;
- 10 procedure DoCommandOther(AThread: TIdContext; ARequestInfo: TIdHTTPRequestInfo;
- 11 AResponseInfo: TIdHTTPResponseInfo); override;
- procedure InitComponent; override;
- public
- procedure RegisterWebModuleClass(AClass: TComponentClass);
- end;
DoCommandGet的内部代码:
- procedure TIdHTTPWebBrokerBridge.DoCommandGet(AThread: TIdContext;
- ARequestInfo: TIdHTTPRequestInfo; AResponseInfo: TIdHTTPResponseInfo);
- begin
- if FWebModuleClass <> nil then begin
- // FWebModuleClass, RegisterWebModuleClass supported for backward compatability
- RunWebModuleClass(AThread, ARequestInfo, AResponseInfo)
- end else
- begin
- {$IFDEF HAS_CLASSVARS}
- TIdHTTPWebBrokerBridgeRequestHandler.FWebRequestHandler.Run(AThread, ARequestInfo, AResponseInfo);
- {$ELSE}
- IndyWebRequestHandler.Run(AThread, ARequestInfo, AResponseInfo);
- {$ENDIF}
- end;
- end;
TIdHTTPWebBrokerBridgeRequestHandler的定义:
- TIdHTTPWebBrokerBridgeRequestHandler = class(TWebRequestHandler)
- {$IFDEF HAS_CLASSVARS}
- private
- 4 class var FWebRequestHandler: TIdHTTPWebBrokerBridgeRequestHandler;
- {$ENDIF}
- public
- constructor Create(AOwner: TComponent); override;
- {$IFDEF HAS_CLASSVARS}
- {$IFDEF HAS_CLASSDESTRUCTOR}
- class destructor Destroy;
- {$ENDIF}
- {$ENDIF}
- destructor Destroy; override;
- 14 procedure Run(AThread: TIdContext; ARequestInfo: TIdHTTPRequestInfo; AResponseInfo: TIdHTTPResponseInfo);
- end;
第4行,静态的,唯一的。
第14行 就是TIdHTTPWebBrokerBridge.DoCommandGet中被调用的。
Run内部代码:
- procedure TIdHTTPWebBrokerBridgeRequestHandler.Run(AThread: TIdContext; ARequestInfo: TIdHTTPRequestInfo; AResponseInfo: TIdHTTPResponseInfo);
- var
- LRequest: TIdHTTPAppRequest;
- LResponse: TIdHTTPAppResponse;
- begin
- try
- LRequest := TIdHTTPAppRequest.Create(AThread, ARequestInfo, AResponseInfo);
- try
- LResponse := TIdHTTPAppResponse.Create(LRequest, AThread, ARequestInfo, AResponseInfo);
- try
- // WebBroker will free it and we cannot change this behaviour
- AResponseInfo.FreeContentStream := False;
- HandleRequest(LRequest, LResponse);
- finally
- FreeAndNil(LResponse);
- end;
- finally
- FreeAndNil(LRequest);
- end;
- except
- // Let Indy handle this exception
- raise;
- end;
- end;
读DataSnap源代码(一)的更多相关文章
- 读DataSnap源代码(五)
function TDSHTTPWebDispatcher.DispatchRequest(Sender: TObject; Request: TWebRequest; Response: TWebR ...
- 读DataSnap源代码(六)
具体分析一下DataSanp App与Rest, WebBroker App的不同,先看TDSHTTPService. **************************************** ...
- 读DataSnap源代码(四)
继续篇中的 function TCustomWebDispatcher.DispatchAction(Request: TWebRequest; Response: TWebResponse): Bo ...
- 读DataSnap源代码(三)
function TWebRequestHandler.HandleRequest(Request: TWebRequest; Response: TWebResponse): Boolean; va ...
- 读DataSnap源代码(二)
program Project1; {$APPTYPE GUI} {$R *.dres} uses Vcl.Forms, Web.WebReq, IdHTTPWebBrokerBridge, Form ...
- 读Flask源代码学习Python--config原理
读Flask源代码学习Python--config原理 个人学习笔记,水平有限.如果理解错误的地方,请大家指出来,谢谢!第一次写文章,发现好累--!. 起因 莫名其妙在第一份工作中使用了从来没有接 ...
- session自己定义存储,怎样更好地进行session共享;读tomcat7源代码,org.apache.catalina.session.FileStore可知
session自己定义存储.怎样更好地进行session共享: 读tomcat源代码,org.apache.catalina.session.FileStore可知 一.详见: 方法1 public ...
- dotnet 读 WPF 源代码笔记 布局时 Arrange 如何影响元素渲染坐标
大家是否好奇,在 WPF 里面,对 UIElement 重写 OnRender 方法进行渲染的内容,是如何受到上层容器控件的布局而进行坐标偏移.如有两个放入到 StackPanel 的自定义 UIEl ...
- dotnet 读 WPF 源代码笔记 渲染收集是如何触发
在 WPF 里面,渲染可以从架构上划分为两层.上层是 WPF 框架的 OnRender 之类的函数,作用是收集应用程序渲染的命令.上层将收集到的应用程序绘制渲染的命令传给下层,下层是 WPF 的 GF ...
随机推荐
- Python 默认值字典
from collections import defaultdict # 默认值字典 dd = defaultdict(lambda: "胡辣汤") # callable 可调用 ...
- 重启uwsgi脚本备份
NAME="identifyImg_uwsgi.init" if [ ! -n "$NAME" ];then echo "no arguments&q ...
- Spring Boot 揭秘与实战(二) 数据缓存篇 - EhCache
文章目录 1. EhCache 集成 2. 源代码 本文,讲解 Spring Boot 如何集成 EhCache,实现缓存. 在阅读「Spring Boot 揭秘与实战(二) 数据缓存篇 - 快速入门 ...
- busybox devmem 直接获取、修改内存信息
/********************************************************************** * busybox devmem 直接获取.修改内存信息 ...
- Linux命令学习之路——档案拷贝:cp
使用权限:所有角色 使用方式:cp [ -arf ] source dest / cp [ -arf ] source... Directory 作用:把一个档案拷贝到另一个档案(档案复制),或将多个 ...
- 利用sql语句进行增删改查
1.查询 函数:raw(sql语句) 语法:Entry.objects.raw(sql) 返回:QuerySet 2.增删改 from django.db import connection def ...
- 对Functional Language的认识
What: A functional language is a programming language built over and around logical functions or pro ...
- 利用Git hub创建博客
1.准备工作 到Git官网 下载Git,并且配置环境变量 2.注册Git Hub账号 到Git Hub官网注册相关账号,比如本文的账号为13627225740L,并至New repository创建仓 ...
- 专题--XOR之线性基
没想到xor居然和线性代数有着那么有趣的联系哎 n个数可以转化为一个上三角矩阵 (线性无关?!) 链接:https://www.nowcoder.com/acm/contest/180/D来源:牛客 ...
- PAT-L2-007 家庭房产 (union-find) 小数据 没有什么是暴力解决不了的!!
题目分析: 典型的union-find 算法 想法: 先不着急 union 因为每一个人的房产信息不知道 所以先输入所有信息 同时保留与自己有关系的每一个人 待初始化每一个人的房产信息后,再union ...