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