Jake Farrell | 2727422 | 2011-11-10 20:32:44 +0000 | [diff] [blame] | 1 | (* |
| 2 | * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | * or more contributor license agreements. See the NOTICE file |
| 4 | * distributed with this work for additional information |
| 5 | * regarding copyright ownership. The ASF licenses this file |
| 6 | * to you under the Apache License, Version 2.0 (the |
| 7 | * "License"); you may not use this file except in compliance |
| 8 | * with the License. You may obtain a copy of the License at |
| 9 | * |
| 10 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | * |
| 12 | * Unless required by applicable law or agreed to in writing, |
| 13 | * software distributed under the License is distributed on an |
| 14 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | * KIND, either express or implied. See the License for the |
| 16 | * specific language governing permissions and limitations |
| 17 | * under the License. |
| 18 | *) |
| 19 | |
| 20 | unit Thrift.Server; |
| 21 | |
Jens Geyer | 26ef743 | 2013-09-23 22:01:20 +0200 | [diff] [blame] | 22 | {$I-} // prevent annoying errors with default log delegate and no console |
| 23 | |
Jake Farrell | 2727422 | 2011-11-10 20:32:44 +0000 | [diff] [blame] | 24 | interface |
| 25 | |
| 26 | uses |
Jens Geyer | 26ef743 | 2013-09-23 22:01:20 +0200 | [diff] [blame] | 27 | Windows, SysUtils, |
Jake Farrell | 2727422 | 2011-11-10 20:32:44 +0000 | [diff] [blame] | 28 | Thrift, |
| 29 | Thrift.Protocol, |
| 30 | Thrift.Transport; |
| 31 | |
| 32 | type |
Jens Geyer | 0164040 | 2013-09-25 21:12:21 +0200 | [diff] [blame] | 33 | IServerEvents = interface |
| 34 | ['{9E2A99C5-EE85-40B2-9A52-2D1722B18176}'] |
| 35 | // Called before the server begins. |
| 36 | procedure PreServe; |
| 37 | // Called when the server transport is ready to accept requests |
| 38 | procedure PreAccept; |
| 39 | // Called when a new client has connected and the server is about to being processing. |
| 40 | function CreateProcessingContext( const input, output : IProtocol) : IProcessorEvents; |
| 41 | end; |
| 42 | |
| 43 | |
Jake Farrell | 2727422 | 2011-11-10 20:32:44 +0000 | [diff] [blame] | 44 | IServer = interface |
Jens Geyer | 0164040 | 2013-09-25 21:12:21 +0200 | [diff] [blame] | 45 | ['{ADC46F2D-8199-4D1C-96D2-87FD54351723}'] |
Jake Farrell | 2727422 | 2011-11-10 20:32:44 +0000 | [diff] [blame] | 46 | procedure Serve; |
| 47 | procedure Stop; |
Jens Geyer | 0164040 | 2013-09-25 21:12:21 +0200 | [diff] [blame] | 48 | |
| 49 | function GetServerEvents : IServerEvents; |
| 50 | procedure SetServerEvents( const value : IServerEvents); |
| 51 | |
| 52 | property ServerEvents : IServerEvents read GetServerEvents write SetServerEvents; |
Jake Farrell | 2727422 | 2011-11-10 20:32:44 +0000 | [diff] [blame] | 53 | end; |
| 54 | |
| 55 | TServerImpl = class abstract( TInterfacedObject, IServer ) |
| 56 | public |
| 57 | type |
Roger Meier | 333bbf3 | 2012-01-08 21:51:08 +0000 | [diff] [blame] | 58 | TLogDelegate = reference to procedure( const str: string); |
Jake Farrell | 2727422 | 2011-11-10 20:32:44 +0000 | [diff] [blame] | 59 | protected |
| 60 | FProcessor : IProcessor; |
| 61 | FServerTransport : IServerTransport; |
| 62 | FInputTransportFactory : ITransportFactory; |
| 63 | FOutputTransportFactory : ITransportFactory; |
| 64 | FInputProtocolFactory : IProtocolFactory; |
| 65 | FOutputProtocolFactory : IProtocolFactory; |
| 66 | FLogDelegate : TLogDelegate; |
Jens Geyer | 0164040 | 2013-09-25 21:12:21 +0200 | [diff] [blame] | 67 | FServerEvents : IServerEvents; |
Jake Farrell | 2727422 | 2011-11-10 20:32:44 +0000 | [diff] [blame] | 68 | |
Roger Meier | 333bbf3 | 2012-01-08 21:51:08 +0000 | [diff] [blame] | 69 | class procedure DefaultLogDelegate( const str: string); |
Jake Farrell | 2727422 | 2011-11-10 20:32:44 +0000 | [diff] [blame] | 70 | |
Jens Geyer | 0164040 | 2013-09-25 21:12:21 +0200 | [diff] [blame] | 71 | function GetServerEvents : IServerEvents; |
| 72 | procedure SetServerEvents( const value : IServerEvents); |
| 73 | |
Jake Farrell | 2727422 | 2011-11-10 20:32:44 +0000 | [diff] [blame] | 74 | procedure Serve; virtual; abstract; |
| 75 | procedure Stop; virtual; abstract; |
| 76 | public |
| 77 | constructor Create( |
Roger Meier | 333bbf3 | 2012-01-08 21:51:08 +0000 | [diff] [blame] | 78 | const AProcessor :IProcessor; |
| 79 | const AServerTransport: IServerTransport; |
| 80 | const AInputTransportFactory : ITransportFactory; |
| 81 | const AOutputTransportFactory : ITransportFactory; |
| 82 | const AInputProtocolFactory : IProtocolFactory; |
| 83 | const AOutputProtocolFactory : IProtocolFactory; |
| 84 | const ALogDelegate : TLogDelegate |
Jake Farrell | 2727422 | 2011-11-10 20:32:44 +0000 | [diff] [blame] | 85 | ); overload; |
| 86 | |
Jens Geyer | 0164040 | 2013-09-25 21:12:21 +0200 | [diff] [blame] | 87 | constructor Create( |
Roger Meier | 333bbf3 | 2012-01-08 21:51:08 +0000 | [diff] [blame] | 88 | const AProcessor :IProcessor; |
| 89 | const AServerTransport: IServerTransport |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 90 | ); overload; |
Jake Farrell | 2727422 | 2011-11-10 20:32:44 +0000 | [diff] [blame] | 91 | |
| 92 | constructor Create( |
Roger Meier | 333bbf3 | 2012-01-08 21:51:08 +0000 | [diff] [blame] | 93 | const AProcessor :IProcessor; |
| 94 | const AServerTransport: IServerTransport; |
| 95 | const ALogDelegate: TLogDelegate |
Jake Farrell | 2727422 | 2011-11-10 20:32:44 +0000 | [diff] [blame] | 96 | ); overload; |
| 97 | |
| 98 | constructor Create( |
Roger Meier | 333bbf3 | 2012-01-08 21:51:08 +0000 | [diff] [blame] | 99 | const AProcessor :IProcessor; |
| 100 | const AServerTransport: IServerTransport; |
| 101 | const ATransportFactory : ITransportFactory |
Jake Farrell | 2727422 | 2011-11-10 20:32:44 +0000 | [diff] [blame] | 102 | ); overload; |
| 103 | |
| 104 | constructor Create( |
Roger Meier | 333bbf3 | 2012-01-08 21:51:08 +0000 | [diff] [blame] | 105 | const AProcessor :IProcessor; |
| 106 | const AServerTransport: IServerTransport; |
| 107 | const ATransportFactory : ITransportFactory; |
| 108 | const AProtocolFactory : IProtocolFactory |
Jake Farrell | 2727422 | 2011-11-10 20:32:44 +0000 | [diff] [blame] | 109 | ); overload; |
| 110 | end; |
| 111 | |
| 112 | TSimpleServer = class( TServerImpl) |
| 113 | private |
| 114 | FStop : Boolean; |
| 115 | public |
Roger Meier | 333bbf3 | 2012-01-08 21:51:08 +0000 | [diff] [blame] | 116 | constructor Create( const AProcessor: IProcessor; const AServerTransport: IServerTransport); overload; |
| 117 | constructor Create( const AProcessor: IProcessor; const AServerTransport: IServerTransport; |
Jake Farrell | 2727422 | 2011-11-10 20:32:44 +0000 | [diff] [blame] | 118 | ALogDel: TServerImpl.TLogDelegate); overload; |
Roger Meier | 333bbf3 | 2012-01-08 21:51:08 +0000 | [diff] [blame] | 119 | constructor Create( const AProcessor: IProcessor; const AServerTransport: IServerTransport; |
| 120 | const ATransportFactory: ITransportFactory); overload; |
| 121 | constructor Create( const AProcessor: IProcessor; const AServerTransport: IServerTransport; |
| 122 | const ATransportFactory: ITransportFactory; const AProtocolFactory: IProtocolFactory); overload; |
Jake Farrell | 2727422 | 2011-11-10 20:32:44 +0000 | [diff] [blame] | 123 | |
| 124 | procedure Serve; override; |
| 125 | procedure Stop; override; |
| 126 | end; |
| 127 | |
| 128 | |
| 129 | implementation |
| 130 | |
| 131 | { TServerImpl } |
| 132 | |
Roger Meier | 333bbf3 | 2012-01-08 21:51:08 +0000 | [diff] [blame] | 133 | constructor TServerImpl.Create( const AProcessor: IProcessor; |
| 134 | const AServerTransport: IServerTransport; const ALogDelegate: TLogDelegate); |
Jake Farrell | 2727422 | 2011-11-10 20:32:44 +0000 | [diff] [blame] | 135 | var |
| 136 | InputFactory, OutputFactory : IProtocolFactory; |
| 137 | InputTransFactory, OutputTransFactory : ITransportFactory; |
| 138 | |
| 139 | begin |
| 140 | InputFactory := TBinaryProtocolImpl.TFactory.Create; |
| 141 | OutputFactory := TBinaryProtocolImpl.TFactory.Create; |
| 142 | InputTransFactory := TTransportFactoryImpl.Create; |
| 143 | OutputTransFactory := TTransportFactoryImpl.Create; |
| 144 | |
Jens Geyer | 0164040 | 2013-09-25 21:12:21 +0200 | [diff] [blame] | 145 | //no inherited; |
Jake Farrell | 2727422 | 2011-11-10 20:32:44 +0000 | [diff] [blame] | 146 | Create( |
| 147 | AProcessor, |
| 148 | AServerTransport, |
| 149 | InputTransFactory, |
| 150 | OutputTransFactory, |
| 151 | InputFactory, |
| 152 | OutputFactory, |
| 153 | ALogDelegate |
| 154 | ); |
| 155 | end; |
| 156 | |
Roger Meier | 333bbf3 | 2012-01-08 21:51:08 +0000 | [diff] [blame] | 157 | constructor TServerImpl.Create(const AProcessor: IProcessor; |
| 158 | const AServerTransport: IServerTransport); |
Jake Farrell | 2727422 | 2011-11-10 20:32:44 +0000 | [diff] [blame] | 159 | var |
| 160 | InputFactory, OutputFactory : IProtocolFactory; |
| 161 | InputTransFactory, OutputTransFactory : ITransportFactory; |
| 162 | |
| 163 | begin |
| 164 | InputFactory := TBinaryProtocolImpl.TFactory.Create; |
| 165 | OutputFactory := TBinaryProtocolImpl.TFactory.Create; |
| 166 | InputTransFactory := TTransportFactoryImpl.Create; |
| 167 | OutputTransFactory := TTransportFactoryImpl.Create; |
| 168 | |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 169 | //no inherited; |
Jake Farrell | 2727422 | 2011-11-10 20:32:44 +0000 | [diff] [blame] | 170 | Create( |
| 171 | AProcessor, |
| 172 | AServerTransport, |
| 173 | InputTransFactory, |
| 174 | OutputTransFactory, |
| 175 | InputFactory, |
| 176 | OutputFactory, |
| 177 | DefaultLogDelegate |
| 178 | ); |
| 179 | end; |
| 180 | |
Roger Meier | 333bbf3 | 2012-01-08 21:51:08 +0000 | [diff] [blame] | 181 | constructor TServerImpl.Create(const AProcessor: IProcessor; |
| 182 | const AServerTransport: IServerTransport; const ATransportFactory: ITransportFactory); |
Jake Farrell | 2727422 | 2011-11-10 20:32:44 +0000 | [diff] [blame] | 183 | var |
| 184 | InputProtocolFactory : IProtocolFactory; |
| 185 | OutputProtocolFactory : IProtocolFactory; |
| 186 | begin |
| 187 | InputProtocolFactory := TBinaryProtocolImpl.TFactory.Create; |
| 188 | OutputProtocolFactory := TBinaryProtocolImpl.TFactory.Create; |
| 189 | |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 190 | //no inherited; |
Jake Farrell | 2727422 | 2011-11-10 20:32:44 +0000 | [diff] [blame] | 191 | Create( AProcessor, AServerTransport, ATransportFactory, ATransportFactory, |
| 192 | InputProtocolFactory, OutputProtocolFactory, DefaultLogDelegate); |
| 193 | end; |
| 194 | |
Roger Meier | 333bbf3 | 2012-01-08 21:51:08 +0000 | [diff] [blame] | 195 | constructor TServerImpl.Create(const AProcessor: IProcessor; |
| 196 | const AServerTransport: IServerTransport; |
| 197 | const AInputTransportFactory, AOutputTransportFactory: ITransportFactory; |
| 198 | const AInputProtocolFactory, AOutputProtocolFactory: IProtocolFactory; |
| 199 | const ALogDelegate : TLogDelegate); |
Jake Farrell | 2727422 | 2011-11-10 20:32:44 +0000 | [diff] [blame] | 200 | begin |
Jens Geyer | 718f6ee | 2013-09-06 21:02:34 +0200 | [diff] [blame] | 201 | inherited Create; |
Jake Farrell | 2727422 | 2011-11-10 20:32:44 +0000 | [diff] [blame] | 202 | FProcessor := AProcessor; |
| 203 | FServerTransport := AServerTransport; |
| 204 | FInputTransportFactory := AInputTransportFactory; |
| 205 | FOutputTransportFactory := AOutputTransportFactory; |
| 206 | FInputProtocolFactory := AInputProtocolFactory; |
| 207 | FOutputProtocolFactory := AOutputProtocolFactory; |
| 208 | FLogDelegate := ALogDelegate; |
| 209 | end; |
| 210 | |
Roger Meier | 333bbf3 | 2012-01-08 21:51:08 +0000 | [diff] [blame] | 211 | class procedure TServerImpl.DefaultLogDelegate( const str: string); |
Jake Farrell | 2727422 | 2011-11-10 20:32:44 +0000 | [diff] [blame] | 212 | begin |
Jens Geyer | 26ef743 | 2013-09-23 22:01:20 +0200 | [diff] [blame] | 213 | try |
| 214 | Writeln( str); |
| 215 | if IoResult <> 0 then OutputDebugString(PChar(str)); |
| 216 | except |
| 217 | OutputDebugString(PChar(str)); |
| 218 | end; |
Jake Farrell | 2727422 | 2011-11-10 20:32:44 +0000 | [diff] [blame] | 219 | end; |
| 220 | |
Roger Meier | 333bbf3 | 2012-01-08 21:51:08 +0000 | [diff] [blame] | 221 | constructor TServerImpl.Create( const AProcessor: IProcessor; |
| 222 | const AServerTransport: IServerTransport; const ATransportFactory: ITransportFactory; |
| 223 | const AProtocolFactory: IProtocolFactory); |
Jake Farrell | 806d298 | 2011-10-26 02:33:31 +0000 | [diff] [blame] | 224 | begin |
Jens Geyer | 0164040 | 2013-09-25 21:12:21 +0200 | [diff] [blame] | 225 | //no inherited; |
Jake Farrell | 806d298 | 2011-10-26 02:33:31 +0000 | [diff] [blame] | 226 | Create( AProcessor, AServerTransport, |
| 227 | ATransportFactory, ATransportFactory, |
| 228 | AProtocolFactory, AProtocolFactory, |
| 229 | DefaultLogDelegate); |
| 230 | end; |
| 231 | |
Jens Geyer | 0164040 | 2013-09-25 21:12:21 +0200 | [diff] [blame] | 232 | |
| 233 | function TServerImpl.GetServerEvents : IServerEvents; |
| 234 | begin |
| 235 | result := FServerEvents; |
| 236 | end; |
| 237 | |
| 238 | |
| 239 | procedure TServerImpl.SetServerEvents( const value : IServerEvents); |
| 240 | begin |
| 241 | // if you need more than one, provide a specialized IServerEvents implementation |
| 242 | FServerEvents := value; |
| 243 | end; |
| 244 | |
| 245 | |
Jake Farrell | 806d298 | 2011-10-26 02:33:31 +0000 | [diff] [blame] | 246 | { TSimpleServer } |
Jake Farrell | 2727422 | 2011-11-10 20:32:44 +0000 | [diff] [blame] | 247 | |
Roger Meier | 333bbf3 | 2012-01-08 21:51:08 +0000 | [diff] [blame] | 248 | constructor TSimpleServer.Create( const AProcessor: IProcessor; |
| 249 | const AServerTransport: IServerTransport); |
Jake Farrell | 2727422 | 2011-11-10 20:32:44 +0000 | [diff] [blame] | 250 | var |
| 251 | InputProtocolFactory : IProtocolFactory; |
| 252 | OutputProtocolFactory : IProtocolFactory; |
| 253 | InputTransportFactory : ITransportFactory; |
| 254 | OutputTransportFactory : ITransportFactory; |
| 255 | begin |
| 256 | InputProtocolFactory := TBinaryProtocolImpl.TFactory.Create; |
| 257 | OutputProtocolFactory := TBinaryProtocolImpl.TFactory.Create; |
| 258 | InputTransportFactory := TTransportFactoryImpl.Create; |
| 259 | OutputTransportFactory := TTransportFactoryImpl.Create; |
| 260 | |
| 261 | inherited Create( AProcessor, AServerTransport, InputTransportFactory, |
| 262 | OutputTransportFactory, InputProtocolFactory, OutputProtocolFactory, DefaultLogDelegate); |
| 263 | end; |
| 264 | |
Roger Meier | 333bbf3 | 2012-01-08 21:51:08 +0000 | [diff] [blame] | 265 | constructor TSimpleServer.Create( const AProcessor: IProcessor; |
| 266 | const AServerTransport: IServerTransport; ALogDel: TServerImpl.TLogDelegate); |
Jake Farrell | 2727422 | 2011-11-10 20:32:44 +0000 | [diff] [blame] | 267 | var |
| 268 | InputProtocolFactory : IProtocolFactory; |
| 269 | OutputProtocolFactory : IProtocolFactory; |
| 270 | InputTransportFactory : ITransportFactory; |
| 271 | OutputTransportFactory : ITransportFactory; |
| 272 | begin |
| 273 | InputProtocolFactory := TBinaryProtocolImpl.TFactory.Create; |
| 274 | OutputProtocolFactory := TBinaryProtocolImpl.TFactory.Create; |
| 275 | InputTransportFactory := TTransportFactoryImpl.Create; |
| 276 | OutputTransportFactory := TTransportFactoryImpl.Create; |
| 277 | |
| 278 | inherited Create( AProcessor, AServerTransport, InputTransportFactory, |
| 279 | OutputTransportFactory, InputProtocolFactory, OutputProtocolFactory, ALogDel); |
| 280 | end; |
| 281 | |
Roger Meier | 333bbf3 | 2012-01-08 21:51:08 +0000 | [diff] [blame] | 282 | constructor TSimpleServer.Create( const AProcessor: IProcessor; |
| 283 | const AServerTransport: IServerTransport; const ATransportFactory: ITransportFactory); |
Jake Farrell | 2727422 | 2011-11-10 20:32:44 +0000 | [diff] [blame] | 284 | begin |
| 285 | inherited Create( AProcessor, AServerTransport, ATransportFactory, |
| 286 | ATransportFactory, TBinaryProtocolImpl.TFactory.Create, TBinaryProtocolImpl.TFactory.Create, DefaultLogDelegate); |
| 287 | end; |
| 288 | |
Roger Meier | 333bbf3 | 2012-01-08 21:51:08 +0000 | [diff] [blame] | 289 | constructor TSimpleServer.Create( const AProcessor: IProcessor; |
| 290 | const AServerTransport: IServerTransport; const ATransportFactory: ITransportFactory; |
| 291 | const AProtocolFactory: IProtocolFactory); |
Jake Farrell | 2727422 | 2011-11-10 20:32:44 +0000 | [diff] [blame] | 292 | begin |
| 293 | inherited Create( AProcessor, AServerTransport, ATransportFactory, |
| 294 | ATransportFactory, AProtocolFactory, AProtocolFactory, DefaultLogDelegate); |
| 295 | end; |
| 296 | |
| 297 | procedure TSimpleServer.Serve; |
| 298 | var |
| 299 | client : ITransport; |
| 300 | InputTransport : ITransport; |
| 301 | OutputTransport : ITransport; |
| 302 | InputProtocol : IProtocol; |
| 303 | OutputProtocol : IProtocol; |
Jens Geyer | 0164040 | 2013-09-25 21:12:21 +0200 | [diff] [blame] | 304 | context : IProcessorEvents; |
Jake Farrell | 2727422 | 2011-11-10 20:32:44 +0000 | [diff] [blame] | 305 | begin |
| 306 | try |
| 307 | FServerTransport.Listen; |
| 308 | except |
| 309 | on E: Exception do |
| 310 | begin |
| 311 | FLogDelegate( E.ToString); |
| 312 | end; |
| 313 | end; |
| 314 | |
Jens Geyer | 0164040 | 2013-09-25 21:12:21 +0200 | [diff] [blame] | 315 | if FServerEvents <> nil |
| 316 | then FServerEvents.PreServe; |
| 317 | |
Jake Farrell | 2727422 | 2011-11-10 20:32:44 +0000 | [diff] [blame] | 318 | client := nil; |
Jake Farrell | 2727422 | 2011-11-10 20:32:44 +0000 | [diff] [blame] | 319 | while (not FStop) do |
| 320 | begin |
| 321 | try |
Jens Geyer | 06045cf | 2013-03-27 20:26:25 +0200 | [diff] [blame] | 322 | // clean up any old instances before waiting for clients |
| 323 | InputTransport := nil; |
| 324 | OutputTransport := nil; |
| 325 | InputProtocol := nil; |
| 326 | OutputProtocol := nil; |
| 327 | |
Jens Geyer | 3e8d927 | 2014-09-14 20:10:40 +0200 | [diff] [blame] | 328 | // close any old connections before before waiting for new clients |
| 329 | if client <> nil then try |
| 330 | try |
| 331 | client.Close; |
| 332 | finally |
| 333 | client := nil; |
| 334 | end; |
| 335 | except |
| 336 | // catch all, we can't do much about it at this point |
| 337 | end; |
| 338 | |
Jens Geyer | 0164040 | 2013-09-25 21:12:21 +0200 | [diff] [blame] | 339 | client := FServerTransport.Accept( procedure |
| 340 | begin |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 341 | if FServerEvents <> nil |
Jens Geyer | 0164040 | 2013-09-25 21:12:21 +0200 | [diff] [blame] | 342 | then FServerEvents.PreAccept; |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 343 | end); |
| 344 | |
| 345 | if client = nil then begin |
| 346 | if FStop |
| 347 | then Abort // silent exception |
| 348 | else raise TTransportException.Create( 'ServerTransport.Accept() may not return NULL' ); |
| 349 | end; |
| 350 | |
Jake Farrell | 2727422 | 2011-11-10 20:32:44 +0000 | [diff] [blame] | 351 | FLogDelegate( 'Client Connected!'); |
Jens Geyer | 06045cf | 2013-03-27 20:26:25 +0200 | [diff] [blame] | 352 | |
Jake Farrell | 2727422 | 2011-11-10 20:32:44 +0000 | [diff] [blame] | 353 | InputTransport := FInputTransportFactory.GetTransport( client ); |
| 354 | OutputTransport := FOutputTransportFactory.GetTransport( client ); |
| 355 | InputProtocol := FInputProtocolFactory.GetProtocol( InputTransport ); |
| 356 | OutputProtocol := FOutputProtocolFactory.GetProtocol( OutputTransport ); |
Jens Geyer | 0164040 | 2013-09-25 21:12:21 +0200 | [diff] [blame] | 357 | |
| 358 | if FServerEvents <> nil |
| 359 | then context := FServerEvents.CreateProcessingContext( InputProtocol, OutputProtocol) |
| 360 | else context := nil; |
| 361 | |
| 362 | while not FStop do begin |
| 363 | if context <> nil |
| 364 | then context.Processing( client); |
| 365 | if not FProcessor.Process( InputProtocol, OutputProtocol, context) |
| 366 | then Break; |
Jake Farrell | 2727422 | 2011-11-10 20:32:44 +0000 | [diff] [blame] | 367 | end; |
Jens Geyer | 06045cf | 2013-03-27 20:26:25 +0200 | [diff] [blame] | 368 | |
Jake Farrell | 2727422 | 2011-11-10 20:32:44 +0000 | [diff] [blame] | 369 | except |
| 370 | on E: TTransportException do |
| 371 | begin |
Roger Meier | 79655fb | 2012-10-20 20:59:41 +0000 | [diff] [blame] | 372 | if FStop |
| 373 | then FLogDelegate('TSimpleServer was shutting down, caught ' + E.ToString) |
| 374 | else FLogDelegate( E.ToString); |
Jake Farrell | 2727422 | 2011-11-10 20:32:44 +0000 | [diff] [blame] | 375 | end; |
| 376 | on E: Exception do |
| 377 | begin |
Roger Meier | 79655fb | 2012-10-20 20:59:41 +0000 | [diff] [blame] | 378 | FLogDelegate( E.ToString); |
Jake Farrell | 2727422 | 2011-11-10 20:32:44 +0000 | [diff] [blame] | 379 | end; |
| 380 | end; |
Jens Geyer | 0164040 | 2013-09-25 21:12:21 +0200 | [diff] [blame] | 381 | |
| 382 | if context <> nil |
| 383 | then begin |
| 384 | context.CleanupContext; |
| 385 | context := nil; |
| 386 | end; |
| 387 | |
Jake Farrell | 2727422 | 2011-11-10 20:32:44 +0000 | [diff] [blame] | 388 | if InputTransport <> nil then |
| 389 | begin |
| 390 | InputTransport.Close; |
| 391 | end; |
| 392 | if OutputTransport <> nil then |
| 393 | begin |
| 394 | OutputTransport.Close; |
| 395 | end; |
| 396 | end; |
| 397 | |
| 398 | if FStop then |
| 399 | begin |
| 400 | try |
| 401 | FServerTransport.Close; |
| 402 | except |
| 403 | on E: TTransportException do |
| 404 | begin |
| 405 | FLogDelegate('TServerTranport failed on close: ' + E.Message); |
| 406 | end; |
| 407 | end; |
| 408 | FStop := False; |
| 409 | end; |
| 410 | end; |
| 411 | |
| 412 | procedure TSimpleServer.Stop; |
| 413 | begin |
| 414 | FStop := True; |
| 415 | FServerTransport.Close; |
| 416 | end; |
| 417 | |
| 418 | end. |