Changeset 88 for branches/DirectWeb/UTCPServer.pas
- Timestamp:
- Dec 18, 2009, 12:21:39 PM (15 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/DirectWeb/UTCPServer.pas
r84 r88 13 13 //LibC, 14 14 {$ENDIF} 15 BlckSock ;15 BlckSock, UPool, UResetableThread; 16 16 17 17 type 18 18 TTCPServer = class; 19 TTCPClientList = class;20 19 21 20 { TTCPClientThread } 22 21 23 TTCPClientThread = class(T Thread)24 Parent: TTCP ClientList;22 TTCPClientThread = class(TResetableThread) 23 Parent: TTCPServer; 25 24 Socket: TTCPBlockSocket; 26 25 procedure Execute; override; 27 constructor Create(CreateSuspended: Boolean; 28 const StackSize: SizeUInt = DefaultStackSize); 26 constructor Create; 29 27 destructor Destroy; override; 28 end; 29 30 { TClientThreadedPool } 31 32 TClientThreadedPool = class(TThreadedPool) 33 private 34 FActive: Boolean; 35 procedure SetActive(const AValue: Boolean); 36 public 37 property Active: Boolean read FActive write SetActive; 30 38 end; 31 39 … … 35 43 Parent: TTCPServer; 36 44 procedure Execute; override; 37 end;38 39 { TTCPClientList }40 41 TTCPClientList = class(TList)42 Parent: TTCPServer;43 procedure Add(ASocket: TSocket);44 procedure Remove(Item: TTCPClientThread);45 procedure Terminate;46 procedure WaitFor;47 constructor Create;48 destructor Destroy; override;49 45 end; 50 46 … … 59 55 procedure SetActive(const AValue: Boolean); 60 56 public 61 Clients: TTCPClientList;57 ThreadPool: TClientThreadedPool; 62 58 Address: string; 63 59 Port: Word; … … 77 73 if AValue and not FActive then begin 78 74 with Socket do begin 75 ThreadPool.Active := True; 79 76 CreateSocket; 80 77 SetLinger(True, 10); … … 96 93 AcceptThread.WaitFor; 97 94 AcceptThread.Destroy; 98 Clients.Terminate; 99 Clients.WaitFor; 95 ThreadPool.Active := False; 100 96 CloseSocket; 101 97 end; … … 105 101 106 102 constructor TTCPServer.Create; 103 var 104 I: Integer; 107 105 begin 108 Clients := TTCPClientList.Create; 109 Clients.Parent := Self; 106 ThreadPool := TClientThreadedPool.Create; 107 ThreadPool.TotalCount := 10; 108 110 109 Socket := TTCPBlockSocket.Create; 111 110 Address := '0.0.0.0'; … … 115 114 destructor TTCPServer.Destroy; 116 115 begin 116 ThreadPool.Destroy; 117 117 Active := False; 118 118 Socket.Destroy; 119 Clients.Destroy;120 119 inherited Destroy; 121 120 end; … … 126 125 var 127 126 NewSocket: TSocket; 127 NewObject: TTCPClientThread; 128 128 begin 129 129 repeat … … 131 131 NewSocket := Parent.Socket.Accept; 132 132 if Parent.Socket.LastError = 0 then begin 133 Parent.Clients.Add(NewSocket); 133 NewObject := TTCPClientThread(Parent.ThreadPool.Acquire); 134 NewObject.Parent := Parent; 135 NewObject.Socket.Socket := NewSocket; 136 NewObject.Start; 134 137 end; 135 138 end; 136 139 until Terminated; 137 end;138 139 { TTCPClientList }140 141 procedure TTCPClientList.Add(ASocket: TSocket);142 begin143 with TTCPClientThread(Items[inherited Add(TTCPClientThread.Create(True))]) do begin144 Socket.Socket := ASocket;145 Parent := Self;146 Resume;147 end;148 end;149 150 procedure TTCPClientList.Remove(Item: TTCPClientThread);151 begin152 inherited Remove(Item);153 Item.Destroy;154 end;155 156 procedure TTCPClientList.Terminate;157 var158 I: Integer;159 begin160 for I := 0 to Count - 1 do161 TTCPClientThread(Items[I]).Terminate;162 end;163 164 procedure TTCPClientList.WaitFor;165 var166 I: Integer;167 begin168 for I := 0 to Count - 1 do169 TTCPClientThread(Items[I]).WaitFor;170 end;171 172 constructor TTCPClientList.Create;173 begin174 inherited;175 end;176 177 destructor TTCPClientList.Destroy;178 var179 I: Integer;180 begin181 for I := 0 to Count - 1 do182 TTCPClientThread(Items[I]).Destroy;183 inherited Destroy;184 140 end; 185 141 … … 188 144 procedure TTCPClientThread.Execute; 189 145 begin 190 if Assigned(Parent.Parent.FOnClientConnect) then 191 Parent.Parent.FOnClientConnect(Self); 192 Parent.Remove(Self); 146 if Assigned(Parent.FOnClientConnect) then 147 Parent.FOnClientConnect(Self); 193 148 end; 194 149 195 constructor TTCPClientThread.Create(CreateSuspended: Boolean; 196 const StackSize: SizeUInt); 150 constructor TTCPClientThread.Create; 197 151 begin 198 inherited Create(CreateSuspended, StackSize);152 inherited; 199 153 Socket := TTCPBlockSocket.Create; 200 154 end; … … 202 156 destructor TTCPClientThread.Destroy; 203 157 begin 204 //Socket.Destroy; 205 inherited Destroy; 158 Socket.Destroy; 159 inherited; 160 end; 161 162 { TClientThreadedPool } 163 164 procedure TClientThreadedPool.SetActive(const AValue: Boolean); 165 var 166 I: Integer; 167 begin 168 if not FActive and Active then begin 169 Allocate; 170 for I := 0 to TotalCount - 1 do begin 171 TThreadedPoolItem(Items[I]).Item := TTCPClientThread.Create; 172 end; 173 end else 174 if FActive and not Active then begin 175 176 end; 177 FActive := Active; 206 178 end; 207 179
Note:
See TracChangeset
for help on using the changeset viewer.