Indy10 控件的使用(2)TidTCpServer組件學習

Indy10 控件的使用(2)TidTCpServer組件學習

(2012-05-18 15:16:53)
轉載
標籤:

indy10

lazarus

idtcpserver

分類: Indy10

如下來自英文原版幫助文件,文桓英語很差,翻譯了老半天。有錯誤的地方見諒,別罵我。
html

TIdTCPServer = class(TIdComponent)windows



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).less

 TIdTCPServer 容許多個監聽線程來監聽客戶端對服務器的連接,接受新的客服連接,處理客戶的任務。默認狀況下,至少有一個監聽線程在服務器中被建立。在多網卡服務器中,附加的socket Bindings被建立,他們用來監聽來自特定網卡IP地址的鏈接請求。socket

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.tcp

 若是服務器啓用了IPv6,一個額外的監聽線程將被建立,用來爲沒一個網卡的使用IPv6地址族的連接服務。ide

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.

相關文章
相關標籤/搜索