MORMOT通訊類說明
MORMOT在
SynCrtSock.pas單元實現通訊類。
MORMOT實現
TCP/UDP/HTTP/WEBSOCKET客戶端和服務端的協議的單元文件。可以看出MORMOT實現的通訊協議是很全面的。
MORMOT 支持跨操作系統平台(WINDOWS 和 LINUX)。
MORMOT 支持多種開發工具(DELPHI和LAZARUS)。
但我們使用MORMOT是因為它 支持HTTP.SYS,這是我們使用它的最主要的原因(WINDOWS下最有效率的HTTP通訊協議,沒有之一)。
MORMOT的跨操作系統是通過LAZARUS的 FPC。這點是有別於DELPHI10.2及以上版本的支持跨操作系統的策略。
MORMOT 支持跨操作系統平台(WINDOWS 和 LINUX)。
MORMOT 支持多種開發工具(DELPHI和LAZARUS)。
但我們使用MORMOT是因為它 支持HTTP.SYS,這是我們使用它的最主要的原因(WINDOWS下最有效率的HTTP通訊協議,沒有之一)。
MORMOT的跨操作系統是通過LAZARUS的 FPC。這點是有別於DELPHI10.2及以上版本的支持跨操作系統的策略。
由於MORMOT的通訊支持跨操作系統,所以接下來,詠南將分成二部分介紹。
第一部分:MORMOT專門用於WINDOWS的通訊類說明
第一部分:MORMOT專門用於WINDOWS的通訊類說明
MORMOT WINDOWS通訊類繼承圖譜
TSynThread = class(TThread)
TServerGeneric = class(TSynThread)
THttpServerGeneric = class(TServerGeneric)
THttpApiServer = class(THttpServerGeneric)
THttpApiWebSocketServer = class(THttpApiServer)
TSynThread = class(TThread)
TServerGeneric = class(TSynThread)
THttpServerGeneric = class(TServerGeneric)
THttpApiServer = class(THttpServerGeneric)
THttpApiWebSocketServer = class(THttpApiServer)
MORMOT線程和線程池類說明
MORMOT線程類繼承圖譜
TSynThread = class(TThread)
MORMOT線程池類繼承圖譜
TSynThreadPool = class
TSynThreadPoolTHttpServer = class(TSynThreadPool)
MORMOT線程類繼承圖譜
TSynThread = class(TThread)
MORMOT線程池類繼承圖譜
TSynThreadPool = class
TSynThreadPoolTHttpServer = class(TSynThreadPool)
TSynThread = class(TThread)
///在線程上下文中運行帶有“Terminate”事件的簡單TThread
//-TThread.OnTerminate事件在Synchronize()中運行,因此沒有
//符合我們的期望,能夠釋放線程中的資源
//創建它們的上下文(例如,用於COM對象或某些DB驅動程序)
//-由THttpServerGeneric.NotifyThreadStart()在內部使用-您應該
//不必使用受保護的fOnTerminate事件處理程序
//-還定義了與舊版本Delphi兼容的Start方法
///在線程上下文中運行帶有“Terminate”事件的簡單TThread
//-TThread.OnTerminate事件在Synchronize()中運行,因此沒有
//符合我們的期望,能夠釋放線程中的資源
//創建它們的上下文(例如,用於COM對象或某些DB驅動程序)
//-由THttpServerGeneric.NotifyThreadStart()在內部使用-您應該
//不必使用受保護的fOnTerminate事件處理程序
//-還定義了與舊版本Delphi兼容的Start方法
TSynThreadPool = class
///一個簡單的線程池,用於快速處理HTTP請求
//-在Windows下通過I/O完成端口實現,或
//Linux/POSIX下的事件驅動方法
///一個簡單的線程池,用於快速處理HTTP請求
//-在Windows下通過I/O完成端口實現,或
//Linux/POSIX下的事件驅動方法
TSynThreadPoolTHttpServer = class(TSynThreadPool)
///一個簡單的線程池,用於快速處理THttpServer的HTTP請求
//-將以比創建線程更少的開銷處理多個連接
//對於每個傳入的請求
//-如果傳入的請求是
//標識為HTTP/1.1 keep alive,或HTTP正文長度大於1 MB
///一個簡單的線程池,用於快速處理THttpServer的HTTP請求
//-將以比創建線程更少的開銷處理多個連接
//對於每個傳入的請求
//-如果傳入的請求是
//標識為HTTP/1.1 keep alive,或HTTP正文長度大於1 MB
THttpApiServer = class(THttpServerGeneric)
WINDOWS HTTP.SYS的相關說明,在此略過,有興趣的朋友,可以自己百度。
使用快速http.sys內核模式服務器的HTTP服務器。 允許應用程序通過HTTP進行通信,而不需要使用Microsoft Internet信息服務器(IIS)。應用程序可以注冊接收特定url的HTTP請求、接收HTTP請求和發送HTTP響應。
HTTP服務器API包括SSL支持,以便應用程序可以通過不帶IIS的安全HTTP連接交換數據。是這樣的設計用於I/O完成端口。
Windows Server 2003操作系統支持HTTP服務器API,以及帶有Service Pack 2(SP2)的Windows XP。請注意,Microsoft IIS 5 在帶有SP2的Windows XP上運行無法與其他HTTP共享端口80同時運行的應用程序。
使用快速http.sys內核模式服務器的HTTP服務器。 允許應用程序通過HTTP進行通信,而不需要使用Microsoft Internet信息服務器(IIS)。應用程序可以注冊接收特定url的HTTP請求、接收HTTP請求和發送HTTP響應。
HTTP服務器API包括SSL支持,以便應用程序可以通過不帶IIS的安全HTTP連接交換數據。是這樣的設計用於I/O完成端口。
Windows Server 2003操作系統支持HTTP服務器API,以及帶有Service Pack 2(SP2)的Windows XP。請注意,Microsoft IIS 5 在帶有SP2的Windows XP上運行無法與其他HTTP共享端口80同時運行的應用程序。
THttpApiWebSocketServer = class(THttpApiServer)
使用快速HTTP.sys內核模式服務器的HTTP& WebSocket服務器 。可以像簡單的THttpApiServer一樣使用。當AddUrlWebSocket被調用時,將添加支持WebSocket。 在這種情況下,WebSocket將以異步方式接收幀。
使用快速HTTP.sys內核模式服務器的HTTP& WebSocket服務器 。可以像簡單的THttpApiServer一樣使用。當AddUrlWebSocket被調用時,將添加支持WebSocket。 在這種情況下,WebSocket將以異步方式接收幀。
第二部分:MORMOT跨操作系統的通訊類說明
MORMOT通訊類繼承圖譜
TCrtSocket = class
THttpSocket = class(TCrtSocket)
THttpServerSocket = class(THttpSocket)
TCrtSocket = class
THttpSocket = class(TCrtSocket)
THttpServerSocket = class(THttpSocket)
TCrtSocket = class
/// Fast low-level Socket implementation
// - direct access to the OS (Windows, Linux) network layer API
// - use Open constructor to create a client to be connected to a server
// - use Bind constructor to initialize a server
// - use SockIn and SockOut (after CreateSock*) to read/readln or write/writeln
// as with standard Delphi text files (see SendEmail implementation)
// - even if you do not use read(SockIn^), you may call CreateSockIn then
// read the (binary) content via SockInRead/SockInPending methods, which would
// benefit of the SockIn^ input buffer to maximize reading speed
// - to write data, CreateSockOut and write(SockOut^) is not mandatory: you
// rather may use SockSend() overloaded methods, followed by a SockFlush call
// - in fact, you can decide whatever to use none, one or both SockIn/SockOut
// - since this class rely on its internal optimized buffering system,
// TCP_NODELAY is set to disable the Nagle algorithm
// - our classes are (much) faster than the Indy or Synapse implementation
/// Fast low-level Socket implementation
// - direct access to the OS (Windows, Linux) network layer API
// - use Open constructor to create a client to be connected to a server
// - use Bind constructor to initialize a server
// - use SockIn and SockOut (after CreateSock*) to read/readln or write/writeln
// as with standard Delphi text files (see SendEmail implementation)
// - even if you do not use read(SockIn^), you may call CreateSockIn then
// read the (binary) content via SockInRead/SockInPending methods, which would
// benefit of the SockIn^ input buffer to maximize reading speed
// - to write data, CreateSockOut and write(SockOut^) is not mandatory: you
// rather may use SockSend() overloaded methods, followed by a SockFlush call
// - in fact, you can decide whatever to use none, one or both SockIn/SockOut
// - since this class rely on its internal optimized buffering system,
// TCP_NODELAY is set to disable the Nagle algorithm
// - our classes are (much) faster than the Indy or Synapse implementation
///快速底層套接字實現
//-直接訪問OS(Windows、Linux)網絡層API
//-使用Open構造函數創建要連接到服務器的客戶端
//-使用綁定構造函數初始化服務器
//-使用SockIn和SockOut(在CreateSock*之后)讀取/讀取ln或寫入/寫入ln
//與標准的Delphi文本文件一樣(參見sendmail實現)
//-即使不使用read(SockIn^),也可以調用CreateSockIn
//通過SockInRead/SockInPending方法讀取(二進制)內容,這將
//SockIn輸入緩沖區最大化讀取速度的好處
//-要寫入數據,CreateSockOut和write(SockOut^)不是必需的:您
//而是可以使用SockSend()重載方法,然后是SockFlush調用
//-事實上,你可以決定什么都不用,一個或兩個SockIn/SockOut
//-由於該類依賴於其內部優化的緩沖系統,
//TCP_NODELAY設置為禁用Nagle算法
//-我們的類比Indy或Synapse實現快得多
//-直接訪問OS(Windows、Linux)網絡層API
//-使用Open構造函數創建要連接到服務器的客戶端
//-使用綁定構造函數初始化服務器
//-使用SockIn和SockOut(在CreateSock*之后)讀取/讀取ln或寫入/寫入ln
//與標准的Delphi文本文件一樣(參見sendmail實現)
//-即使不使用read(SockIn^),也可以調用CreateSockIn
//通過SockInRead/SockInPending方法讀取(二進制)內容,這將
//SockIn輸入緩沖區最大化讀取速度的好處
//-要寫入數據,CreateSockOut和write(SockOut^)不是必需的:您
//而是可以使用SockSend()重載方法,然后是SockFlush調用
//-事實上,你可以決定什么都不用,一個或兩個SockIn/SockOut
//-由於該類依賴於其內部優化的緩沖系統,
//TCP_NODELAY設置為禁用Nagle算法
//-我們的類比Indy或Synapse實現快得多
THttpSocket = class(TCrtSocket)
/// parent of THttpClientSocket and THttpServerSocket classes
// - contain properties for implementing HTTP/1.1 using the Socket API
// - handle chunking of body content
// - can optionaly compress and uncompress on the fly the data, with
// standard gzip/deflate or custom (synlzo/synlz) protocols
///THttpClientSocket和THttpServerSocket類的父級
//-包含用於使用套接字API實現HTTP/1.1的屬性
//-處理正文內容的分塊
//-可以選擇動態壓縮和解壓縮數據,使用
//標准gzip/deflate或自定義(synlzo/synlz)協議
/// parent of THttpClientSocket and THttpServerSocket classes
// - contain properties for implementing HTTP/1.1 using the Socket API
// - handle chunking of body content
// - can optionaly compress and uncompress on the fly the data, with
// standard gzip/deflate or custom (synlzo/synlz) protocols
///THttpClientSocket和THttpServerSocket類的父級
//-包含用於使用套接字API實現HTTP/1.1的屬性
//-處理正文內容的分塊
//-可以選擇動態壓縮和解壓縮數據,使用
//標准gzip/deflate或自定義(synlzo/synlz)協議
THttpServerSocket = class(THttpSocket)
/// Socket API based HTTP/1.1 server class used by THttpServer Threads
/// Socket API based HTTP/1.1 server class used by THttpServer Threads
THttpServer = class(THttpServerGeneric)
THttpServer 支持跨操作系統,至於它的性能如何,詠南沒有測試,所以不得而知。
/// main HTTP server Thread using the standard Sockets API (e.g. WinSock)
// - bind to a port and listen to incoming requests
// - assign this requests to THttpServerResp threads from a ThreadPool
// - it implements a HTTP/1.1 compatible server, according to RFC 2068 specifications
// - if the client is also HTTP/1.1 compatible, KeepAlive connection is handled:
// multiple requests will use the existing connection and thread;
// this is faster and uses less resources, especialy under Windows
// - a Thread Pool is used internaly to speed up HTTP/1.0 connections - a
// typical use, under Linux, is to run this class behind a NGINX frontend,
// configured as https reverse proxy, leaving default "proxy_http_version 1.0"
// and "proxy_request_buffering on" options for best performance, and
// setting KeepAliveTimeOut=0 in the THttpServer.Create constructor
// - under windows, will trigger the firewall UAC popup at first run
// - don't forget to use Free method when you are finished
///使用標准套接字API(例如WinSock)的主HTTP服務器線程
//-綁定到端口並偵聽傳入的請求
//-將此請求分配給線程池中的thttpserver resp線程
//-根據RFC 2068規范,它實現了一個與HTTP/1.1兼容的服務器
//-如果客戶端也兼容HTTP/1.1,則處理KeepAlive連接:
//多個請求將使用現有連接和線程;
//這樣做速度更快,使用的資源更少,特別是在Windows下
//-在內部使用線程池來加速HTTP/1.0連接-a
//在Linux下,典型的用法是在NGINX前端后面運行這個類,
//配置為https反向代理,保留默認的“proxy_http_version 1.0”
//和“proxy_request_buffering on”選項以獲得最佳性能,以及
//在THttpServer.Create構造函數中設置KeepAliveTimeOut=0
//-在windows下,將在第一次運行時觸發防火牆UAC彈出窗口
//-完成后別忘了使用免費方法
THttpServer 支持跨操作系統,至於它的性能如何,詠南沒有測試,所以不得而知。
/// main HTTP server Thread using the standard Sockets API (e.g. WinSock)
// - bind to a port and listen to incoming requests
// - assign this requests to THttpServerResp threads from a ThreadPool
// - it implements a HTTP/1.1 compatible server, according to RFC 2068 specifications
// - if the client is also HTTP/1.1 compatible, KeepAlive connection is handled:
// multiple requests will use the existing connection and thread;
// this is faster and uses less resources, especialy under Windows
// - a Thread Pool is used internaly to speed up HTTP/1.0 connections - a
// typical use, under Linux, is to run this class behind a NGINX frontend,
// configured as https reverse proxy, leaving default "proxy_http_version 1.0"
// and "proxy_request_buffering on" options for best performance, and
// setting KeepAliveTimeOut=0 in the THttpServer.Create constructor
// - under windows, will trigger the firewall UAC popup at first run
// - don't forget to use Free method when you are finished
///使用標准套接字API(例如WinSock)的主HTTP服務器線程
//-綁定到端口並偵聽傳入的請求
//-將此請求分配給線程池中的thttpserver resp線程
//-根據RFC 2068規范,它實現了一個與HTTP/1.1兼容的服務器
//-如果客戶端也兼容HTTP/1.1,則處理KeepAlive連接:
//多個請求將使用現有連接和線程;
//這樣做速度更快,使用的資源更少,特別是在Windows下
//-在內部使用線程池來加速HTTP/1.0連接-a
//在Linux下,典型的用法是在NGINX前端后面運行這個類,
//配置為https反向代理,保留默認的“proxy_http_version 1.0”
//和“proxy_request_buffering on”選項以獲得最佳性能,以及
//在THttpServer.Create構造函數中設置KeepAliveTimeOut=0
//-在windows下,將在第一次運行時觸發防火牆UAC彈出窗口
//-完成后別忘了使用免費方法
THttpClientSocket = class(THttpSocket)
/// Socket API based REST and HTTP/1.1 compatible client class
// - this component is HTTP/1.1 compatible, according to RFC 2068 document
// - the REST commands (GET/POST/PUT/DELETE) are directly available
// - open connection with the server with inherited Open(server,port) function
// - if KeepAlive>0, the connection is not broken: a further request (within
// KeepAlive milliseconds) will use the existing connection if available,
// or recreate a new one if the former is outdated or reset by server
// (will retry only once); this is faster, uses less resources (especialy
// under Windows), and is the recommended way to implement a HTTP/1.1 server
// - on any error (timeout, connection closed) will retry once to get the value
// - don't forget to use Free procedure when you are finished
///基於Socket API的REST和HTTP/1.1兼容的客戶端類
//-根據RFC 2068文件,該組件與HTTP/1.1兼容
//-REST命令(GET/POST/PUT/DELETE)直接可用
//-使用繼承的打開(服務器、端口)功能打開與服務器的連接
//-如果KeepAlive>0,則連接不會斷開:另一個請求(在
//KeepAlive毫秒)將使用現有連接(如果可用),
//或者,如果前者過時或被服務器重置,則重新創建一個新的
//(只重試一次);速度更快,使用的資源更少(特別是
//,是實現HTTP/1.1服務器的推薦方法
//-如果出現任何錯誤(超時,連接關閉),將重試一次以獲取值
//-完成后不要忘記使用免費程序
/// Socket API based REST and HTTP/1.1 compatible client class
// - this component is HTTP/1.1 compatible, according to RFC 2068 document
// - the REST commands (GET/POST/PUT/DELETE) are directly available
// - open connection with the server with inherited Open(server,port) function
// - if KeepAlive>0, the connection is not broken: a further request (within
// KeepAlive milliseconds) will use the existing connection if available,
// or recreate a new one if the former is outdated or reset by server
// (will retry only once); this is faster, uses less resources (especialy
// under Windows), and is the recommended way to implement a HTTP/1.1 server
// - on any error (timeout, connection closed) will retry once to get the value
// - don't forget to use Free procedure when you are finished
///基於Socket API的REST和HTTP/1.1兼容的客戶端類
//-根據RFC 2068文件,該組件與HTTP/1.1兼容
//-REST命令(GET/POST/PUT/DELETE)直接可用
//-使用繼承的打開(服務器、端口)功能打開與服務器的連接
//-如果KeepAlive>0,則連接不會斷開:另一個請求(在
//KeepAlive毫秒)將使用現有連接(如果可用),
//或者,如果前者過時或被服務器重置,則重新創建一個新的
//(只重試一次);速度更快,使用的資源更少(特別是
//,是實現HTTP/1.1服務器的推薦方法
//-如果出現任何錯誤(超時,連接關閉),將重試一次以獲取值
//-完成后不要忘記使用免費程序
還有許多的客戶端類,詠南不再一 一貼出,在此略過,有興趣的可以自己查看源碼。