Indy10 控件的使用(2)TidTCpServer组件学习
(2012-05-18 15:16:53)标签:
indy10lazarusidtcpserver |
分类: Indy10 |
以下来自英文原版帮助文件,文桓英语不好,翻译了老半天。有错误的地方见谅,别骂我。
TIdTCPServer = class(TIdComponent)
Description
TIdTCPServer is a TIdComponent descendant that encapsulates a complete, multi-threaded TCP (Transmission Control Protocol) server.
TIdTCPServer 是 TIdComponent 的子类,封装了一个完整的多线程TCP服务。
TIdTCPServer allows multiple listener threads that listen for client connections to the server, accept new client connections, and hand-off execution of the client task to the server. By default, at least one listener thread is created for the server. For servers with multiple network adapters, additional socket Bindings can be created that allow the server to listen for connection requests using the IP address for the selected network adapter(s).
TIdTCPServer 允许多个监听线程来监听客户端对服务器的链接,接受新的客服链接,处理客户的任务。默认情况下,至少有一个监听线程在服务器中被创建。在多网卡服务器中,附加的socket Bindings被创建,他们用来监听来自特定网卡IP地址的连接请求。
If IP version 6 addresses are enabled for the server, an additional listener thread is created for each adapter specifically for connections using the IPv6 address family.
如果服务器启用了IPv6,一个额外的监听线程将被创建,用来为没一个网卡的使用IPv6地址族的链接服务。
TIdTCPServer allows multiple simultaneous(同一时刻) client connections, and allocates a separate unit of execution for each client connecting to the server. Each client connection represents a task that is managed by the Scheduler for the server. Listener threads use the Scheduler to create an executable task for each client connection.
TIdTCPServer允许同一时刻有多个客户端链接,并为没一个客户链接分配一个单独的执行单元 。每一个客户链接代表了一个任务,这些任务被Scheduler管理。监听线程使用Scheduler来为每一个客户链接创建任务。
The Scheduler handles creation, execution, and termination of tasks for client connections found in Contexts. The ContextClass property indicates(指示,表明) the type of executable task created for client connections and added to Contexts.
Scheduler在Contexts里为客户连接处理任务的创建,执行,终止,ContextClass的属性表明了可执行任务的类型。
There are basically two types of Schedulers available for TIdTCPServer: Thread-based and Fiber-based. Each is designed to work with a specific type of executable task that represents(代表) the client connections. There are further(更进一步) Scheduler refinements(改进) that allow a pool of pre-allocated Threads, or Threads which perform scheduling for dependent Fibers.
TIdTCPServer有两种基本的Schedulers:基于线程的和基于纤程的。它们为代表客户连接的特定的可执行任务而设计。
The default Scheduler implementation in TIdTCPServer uses a Thread to represent each client connection. Threads are a common feature found on all platforms and Operating Systems hosting the Indy library.
在TIdTCPServer中默认的Scheduler实现是使用一个线程来代表每一个客户连接的。线程是一个通用的特色,可以工作于不同的操作系统。
But there are performance(性能) and resource(资源) limitations imposed(强加的) by the platform or Operating System on the number of threads that can be created. On Windows, for example, the number of threads is limited by the available virtual memory. By default, every thread gets one megabyte(兆字节) of stack space and implies(意味着) a theoretical(理论上的) limit of 2028 threads. Reducing the default stack size will allow more threads to be created, but will adversely impact system performance. In addition, threads are pre-emptively(抢先的) scheduled by the host operating system and allow no control over execution of the thread process.
但是由于性能和资源的限制,操作系统只能创建有限数量的线程。例如在windows中,线程的数量取决于可用虚拟内存的大小。默认情况下,每个线程的栈需要一兆字节的话,意味着理论上只能有2028个线程。减少默认栈大小可以增加线程数量,但是会影响系统性能。线程是抢占式的被操作系统调度,我们没法控制。
Windows addresses these issues by providing the Fibers API . Essentially, Fibers are a light-weight thread. Fibers require fewer resources that threads. Fibers can be manually scheduled in the server, and run in the context of the thread that schedules them. In other words, Fibers are not pre-emptively scheduled by the Operating System. The Fiber runs when its thread runs. As a result, servers using the Fiber API can be more scalable(可伸缩的) than contemporary(同一时代的) thread-based implementations.
大意是:windows为了解决这个问题,提供了Fiber
API,纤程是轻量级的线程,需要更少的资源。纤程可以在服务中被人工的调用,而且处于线程的上下文中。
The Fiber API is very different from the Thread API (even on the Windows platform). To hide these API differences, and to preserve code portability to non-Windows platforms, the Scheduler in TIdTCPServer provides an abstraction that masks the differences: the Yarn.
大意:Fiber API 与Thread API有很大不同,为了隐藏这些不同,提供了Yarn类。
Q: What do you get when you weave threads and fibers
together? A: Yarn.
While TIdTCPServer does not use Fibers unless a Scheduler supporting Fibers is assigned (TIdSchedulerOfFiber), the Yarn abstraction is an important one that is central to the use of the Scheduler. When the Scheduler supports Threads, it deals only with a Yarn at the Thread level. When the Scheduler supports Fibers, it deals with a Yarn at the Fiber level.
TIdTCPServer在不指定的情况下不使用Fiber。
The ContextClass property for the server is used to create the unit of execution for the client connection, and also uses the Yarn abstraction.
服务的ContextClass属性用来为客户链接创建执行单元,也是用Yarn来抽象的。
TIdTCPServer provides properties and methods that allow configuration of the server and listener threads, including:
TIdTCPServer提供的属性和方法来配置服务和监听线程,包括:
Active
启动
DefaultPort 默认端口
Bindings
ListenQueue
MaxConnections
IOHandler IO处理
Intercept 中断
ReuseSocket
TerminateWaitTime
OnAfterBind
OnBeforeListenerRun
OnListenException
The TIdTCPServer architecture provides properties and methods that allow controlling execution of the client connection tasks for the server, including:
TIdTCPServer架构提供属性和方法允许控制客户链接任务的执行。 包括:
ContextClass
Scheduler
OnBeforeConnect
OnConnect
OnExecute
OnDisconnect
OnException
During initialization of the TIdTCPServer component, the following resources are allocated for properties in the server:
初始化TidTCPServer控件时,服务中以下属性的资源将被分配:
Bindings
Contexts
During initialization of the TIdTCPServer component, the following properties are set to their default values:
初始化阶段的默认值:
Property Value
ContextClass TIdContext class reference
TerminateWaitTime 5000 (5 seconds)
ListenQueue IdListenQueueDefault
At runtime, TIdTCPServer is controlled by changing the value in its Active property. When Active is set to True, the following actions are performed as required to start the server including:
运行时,TIdTCPServer通过改变Active属性值来控制。当Active设置为True时,以下操作将被执行:
Bindings are created and/or initialized for IPv4 and IPv6 addresses.
绑定IP地址
Ensures that an IOHandler is assigned for the server.
确认IO处理器是否已分配。
Ensures that a Scheduler is assigned for the server.
确认Scheduler是否已分配。
Creates and starts listener threads for Bindings using the thread priority tpHighest.
在每一个Bingdings上创建并启动一个监听线程。
When Active is set to False, the following actions are performed as required to stop the server including:
当Active属性设置为False时,执行:
Terminates and closes the socket handle for the any listener threads.
终止一个监听线程,关闭Socket。
Terminates and disconnects any client connections in Contexts, and Yarns in Scheduler.
终止和断开所有客户链接。
Frees an implicitly(隐藏的) created Scheduler for the server.
释放一个被暗中建立的Scheduler。
Changing the value in Active in the IDE (design-time) has no effect other than storing the property value that is used at runtime and during component streaming.
设计阶段改变Active值没有什么作用,只是存储的值在运行时在工作。
While the server is Active, listener thread(s) are used to detect and accept client connection requests. When a connection request is detected, the Scheduler in the server instance is used to acquire the thread or fiber that controls execution of the task for the client connection. The IOHandler in the server is used to get an IOHandler for the client connection.
当服务被激活时,监听线程被用来探测和接受客户请求,当一个链接被探测到时,服务实例中的Scheduler用来获取控制任务执行的线程或者纤程。服务中的IOHandler用来为客户连接获取一个IOHandler。
A client connection request can be refused for a number of reasons. The listener thread may get stopped during processing of the request, or the attempt to create a client IOHandler fails because of an invalid socket handle.
一个客户连接请求可能因为一些原因被拒绝。监听线程可能停止处理请求,或者因为一个无效的socket处理而尝试创建一个客户IOHandler失败。
The most common error occurs when the number of client connections in Contexts exceeds the number allowed in MaxConnections. In this situation, an message is written to the client connection that indicates the condition and the client connection is disconnected.
最常见的错误发生在客户连接超过最大连接数时。在这种情况下,将向客户端发送一条消息,这个链接也将被关闭。
If the connection is refused due to any other exception, the OnListenException is triggered.
如果链接被拒绝是因为其他异常,OnListenException将用来跟踪。
If no error or exception is detected for the connection request, a TIdContext is created for the client connections executable task. Procedures that trigger the OnConnect, OnDisconnect, and OnExecute event handlers are assigned to the context. The context is then given to the Scheduler for execution using its native thread or fiber support.
如果链接请求没有错误,或者异常被检测到,将为这个客户链接任务创建一个TIdContext。处理OnConnect, OnDisconnect, OnExecute这些事件时使用Context。这个Context会交给Scheduler。
At this point, the executable task for the client connection is fully self-contained and controlled using the OnConnect, OnDisconnect, and OnExecute event handlers. OnConnect and OnDisconnect are optional, and should be used only to perform simple tasks that are not time consuming(消费). Using Synchronized method calls that access the main VCL thread are highly discouraged(气馁的,灰心的). Use OnExecute to control the interaction(相互作用) between the client and the server during the lifetime of the client connection.
TIdTCPServer does not implement support for any given protocol used for communication exchanges between clients and the server. TIdTCPServer can be used as a base class to create custom TCP server descendants that support specific protocols.
Use TIdCmdTCPServer for a TCP server that includes TIdCommandHandler and TIdCommand support.
Copyright (c) 1993-2004, Chad Z. Hower (Kudzu) and the Indy Pit Crew. All rights reserved.