DataSnap REST Application
http://edn.embarcadero.com/article/41305
2017.1.19 官方例子
https://community.embarcadero.com/blogs/entry/in-the-c-builder-of-rad-server-firedac-sqlserver-connection
官方创建datasnapServer的步骤说明
http://docwiki.embarcadero.com/RADStudio/Tokyo/en/DataSnap_Server_Wizard_for_Windows
这个是创建 Datasnap Rest 服务的步骤。
http://docwiki.embarcadero.com/RADStudio/Tokyo/en/DataSnap_REST_Application_Wizard_for_Windows
http://docwiki.embarcadero.com/RADStudio/Tokyo/en/Tutorial:_Using_a_REST_DataSnap_Server_with_an_Application
http://docwiki.embarcadero.com/RADStudio/Tokyo/en/Tutorial:_Using_a_REST_DataSnap_Server_with_an_Application_and_FireDAC
建立DataSnap/REST服务器,请点选Files|New菜单,在DataSnap Server选项中选择DataSnap Server图像,如下图所示:3个选项都可以选择,创建后都是DataSnap服务器
官方例子参考D:\Users\Public\Documents\Embarcadero\Studio\14.0\Samples\CPP\DataSnap\SimpleDataSnapDemo\CppDataSnapServer\CppDataSnapServerApp.cbproj
注意:TDSTCPServerTransport.poolsize 线程池
TDSTCPServerTransport.maxThreads 并发连接数量
TDSServerClass.LIFECycle, SESSION/SERVER/INVOCATION
Representational State Transfer (REST) is a new architecture for Web Services that is having a significant impact on the industry
ServerMethodsUnit1.cpp
DSServerModuleCreate是每个客户端连接的时候触发,两个客户端连接就触发两次,所以用到全局指针的话,第二次的触发就可能影响了第一次的指针,必须注意特别小心。
关于数据库操作必须写到ServerMethodsUnit1,包括数据库连接,ServerMethodsUnit1具有线程功能, 一个客户端连接一个线程实例,一个实例就是new一个ServerMethodsUnit1类,各个互不影响。
普通的datamodule无线程功能。
视频教程
http://www.embarcadero.com/rad-in-action/datasnap
服务器可以创建exe,app,也可以是服务;可以是windows 平台,也可以是firemonkey跨平台。
一路完成后会生成控件
TDSServer,TDSTCPServerTransport以及TDSServerClass、TDSHTTPService
USEFORM("MainForm.cpp", Form1); USEFORM("ServerContainerUnit1.cpp", ServerContainer1); /* TDataModule: File Type */ //--------------------------------------------------------------------------- int WINAPI _tWinMain(HINSTANCE, HINSTANCE, LPTSTR, int) { try { Application->Initialize(); Application->MainFormOnTaskBar = true; Application->CreateForm(__classid(TForm1), &Form1); Application->CreateForm(__classid(TServerContainer1), &ServerContainer1); Application->Run(); } } //---------------------------------------------------------------------------- #ifndef ServerMethodsUnit1H #define ServerMethodsUnit1H //---------------------------------------------------------------------------- #include <Classes.hpp> #include <DSServer.hpp> //---------------------------------------------------------------------------- class DECLSPEC_DRTTI TServerMethods1 : public TComponent { private: // User declarations public: // User declarations System::UnicodeString EchoString(System::UnicodeString value); System::UnicodeString ReverseString(System::UnicodeString value); }; #endif //--------------------------------------------------------------------------- // This software is Copyright (c) 2014 Embarcadero Technologies, Inc. // You may only use this software if you are an authorized licensee // of an Embarcadero developer tools product. // This software is considered a Redistributable as defined under // the software license agreement that comes with the Embarcadero Products // and is subject to that software license agreement. //--------------------------------------------------------------------------- //---------------------------------------------------------------------------- #include <SysUtils.hpp> #pragma hdrstop #include "ServerMethodsUnit1.h" #include <Datasnap.DSProviderDataModuleAdapter.hpp> //---------------------------------------------------------------------------- #pragma package(smart_init) //---------------------------------------------------------------------------- System::UnicodeString TServerMethods1::EchoString(System::UnicodeString value) { return value; } //---------------------------------------------------------------------------- System::UnicodeString TServerMethods1::ReverseString(System::UnicodeString value) { return ::ReverseString(value); } //----------------------------------------------------------------------------
//---------------------------------------------------------------------------- #ifndef ServerContainerUnit1H #define ServerContainerUnit1H //--------------------------------------------------------------------------- #include <Classes.hpp> #include <DSCommonServer.hpp> #include <DSServer.hpp> #include <DSTCPServerTransport.hpp> #include <DSAuth.hpp> #include <IPPeerServer.hpp> //---------------------------------------------------------------------------- class TServerContainer1 : public TDataModule { __published: // IDE-managed Components TDSServer *DSServer1; TDSTCPServerTransport *DSTCPServerTransport1; TDSServerClass *DSServerClass1; void __fastcall DSServerClass1GetClass(TDSServerClass *DSServerClass, TPersistentClass &PersistentClass); private: // User declarations public: // User declarations __fastcall TServerContainer1(TComponent* Owner); }; //---------------------------------------------------------------------------- extern PACKAGE TServerContainer1 *ServerContainer1; //---------------------------------------------------------------------------- #endif //--------------------------------------------------------------------------- // This software is Copyright (c) 2014 Embarcadero Technologies, Inc. // You may only use this software if you are an authorized licensee // of an Embarcadero developer tools product. // This software is considered a Redistributable as defined under // the software license agreement that comes with the Embarcadero Products // and is subject to that software license agreement. //--------------------------------------------------------------------------- //---------------------------------------------------------------------------- #include <vcl.h> #pragma hdrstop #include <tchar.h> #include <stdio.h> #include <memory> #include "ServerMethodsUnit1.h" #include "ServerContainerUnit1.h" //--------------------------------------------------------------------------- #pragma package(smart_init) #pragma resource "*.dfm" TServerContainer1 *ServerContainer1; //--------------------------------------------------------------------------- __fastcall TServerContainer1::TServerContainer1(TComponent* Owner) : TDataModule(Owner) { } //---------------------------------------------------------------------------- void __fastcall TServerContainer1::DSServerClass1GetClass(TDSServerClass *DSServerClass, TPersistentClass &PersistentClass) { PersistentClass = __classid(TServerMethods1); } //---------------------------------------------------------------------------- //---------------------------------------------------------------------------
多个类文件
ServerMethodsUnit1.cpp文件中就是类 class TServerMethods1 : public TDSServerModule,如果想多个类,例如分模块分业务多个类,File>new>Other>DataSnap Server》选择 Server Module新添加一个类。没有打开任何功能时Server Module是不可见的。只有打开了某个应用服务器的工程,再new的时候才能看见!
https://newsgroups.embarcadero.com/thread.jspa?threadID=229169
https://forums.embarcadero.com/thread.jspa?messageID=833882
ServerMethodsUnit1、ServerMethodsUnit2
DSServerClass1、DSServerClass2
procedure TWebModule1.DSServerClass2GetClass(DSServerClass: TDSServerClass; var PersistentClass: TPersistentClass); begin PersistentClass := ServerMethodsUnit2.TDSServerModule2; end;
这样就注册上第二个单位类,多模块,多个类、多个业务、多个接口、分离