|
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的通讯支持跨操作系统,所以接下来,咏南将分成二部分介绍。 第一部分:MORMOT专门用于WINDOWS的通讯类说明
MORMOT WINDOWS通讯类继承图谱 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)
TSynThread = class(TThread) ///在线程上下文中运行带有“Terminate”事件的简单TThread //-TThread.OnTerminate事件在Synchronize()中运行,因此没有 //符合我们的期望,能够释放线程中的资源 //创建它们的上下文(例如,用于COM对象或某些DB驱动程序) //-由THttpServerGeneric.NotifyThreadStart()在内部使用-您应该 //不必使用受保护的fOnTerminate事件处理程序 //-还定义了与旧版本Delphi兼容的Start方法
TSynThreadPool = class ///一个简单的线程池,用于快速处理HTTP请求 //-在Windows下通过I/O完成端口实现,或 //Linux/POSIX下的事件驱动方法
TSynThreadPoolTHttpServer = class(TSynThreadPool) ///一个简单的线程池,用于快速处理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同时运行的应用程序。
THttpApiWebSocketServer = class(THttpApiServer) 使用快速HTTP.sys内核模式服务器的HTTP&WebSocket服务器 。可以像简单的THttpApiServer一样使用。当AddUrlWebSocket被调用时,将添加支持WebSocket。 在这种情况下,WebSocket将以异步方式接收帧。
第二部分:MORMOT跨操作系统的通讯类说明
MORMOT通讯类继承图谱 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
///快速底层套接字实现 //-直接访问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)协议
THttpServerSocket = class(THttpSocket) /// 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弹出窗口 //-完成后别忘了使用免费方法
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服务器的推荐方法 //-如果出现任何错误(超时,连接关闭),将重试一次以获取值 //-完成后不要忘记使用免费程序
还有许多的客户端类,咏南不再一 一贴出,在此略过,有兴趣的可以自己查看源码。
本文来自博客园,作者:{咏南中间件},转载请注明原文链接:https://www.cnblogs.com/hnxxcxg/p/12454559.html
来源:https://www.cnblogs.com/hnxxcxg/p/12454559.html |