Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [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 | |
Nick | 4f5229e | 2016-04-14 16:43:22 +0300 | [diff] [blame^] | 20 | {$SCOPEDENUMS ON} |
| 21 | {$IF CompilerVersion >= 23.0} |
| 22 | {$LEGACYIFEND ON} |
| 23 | {$IFEND} |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 24 | |
Jens Geyer | 23d6746 | 2015-12-19 11:44:57 +0100 | [diff] [blame] | 25 | {$IF CompilerVersion < 28.0} |
| 26 | {$DEFINE OLD_SOCKETS} // TODO: add socket support for CompilerVersion >= 28.0 |
| 27 | {$IFEND} |
| 28 | |
| 29 | |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 30 | unit Thrift.Transport; |
| 31 | |
| 32 | interface |
| 33 | |
| 34 | uses |
| 35 | Classes, |
| 36 | SysUtils, |
| 37 | Math, |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 38 | Generics.Collections, |
Nick | 4f5229e | 2016-04-14 16:43:22 +0300 | [diff] [blame^] | 39 | {$IF CompilerVersion < 23.0} |
| 40 | ActiveX, msxml, WinSock, Sockets, |
| 41 | {$ELSE} |
| 42 | Winapi.ActiveX, Winapi.msxml, Winapi.WinSock, |
| 43 | {$IF CompilerVersion < 28.0} |
| 44 | Web.Win.Sockets, |
| 45 | {$ELSE} |
| 46 | System.Win.ScktComp, |
| 47 | {$IFEND} |
| 48 | {$IFEND} |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 49 | Thrift.Collections, |
| 50 | Thrift.Utils, |
Nick | 4f5229e | 2016-04-14 16:43:22 +0300 | [diff] [blame^] | 51 | Thrift.Stream; |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 52 | |
| 53 | type |
| 54 | ITransport = interface |
| 55 | ['{A4A9FC37-D620-44DC-AD21-662D16364CE4}'] |
| 56 | function GetIsOpen: Boolean; |
| 57 | property IsOpen: Boolean read GetIsOpen; |
| 58 | function Peek: Boolean; |
| 59 | procedure Open; |
| 60 | procedure Close; |
| 61 | function Read(var buf: TBytes; off: Integer; len: Integer): Integer; |
| 62 | function ReadAll(var buf: TBytes; off: Integer; len: Integer): Integer; |
| 63 | procedure Write( const buf: TBytes); overload; |
| 64 | procedure Write( const buf: TBytes; off: Integer; len: Integer); overload; |
| 65 | procedure Flush; |
| 66 | end; |
| 67 | |
| 68 | TTransportImpl = class( TInterfacedObject, ITransport) |
| 69 | protected |
| 70 | function GetIsOpen: Boolean; virtual; abstract; |
| 71 | property IsOpen: Boolean read GetIsOpen; |
| 72 | function Peek: Boolean; virtual; |
| 73 | procedure Open(); virtual; abstract; |
| 74 | procedure Close(); virtual; abstract; |
| 75 | function Read(var buf: TBytes; off: Integer; len: Integer): Integer; virtual; abstract; |
| 76 | function ReadAll(var buf: TBytes; off: Integer; len: Integer): Integer; virtual; |
| 77 | procedure Write( const buf: TBytes); overload; virtual; |
| 78 | procedure Write( const buf: TBytes; off: Integer; len: Integer); overload; virtual; abstract; |
| 79 | procedure Flush; virtual; |
| 80 | end; |
| 81 | |
| 82 | TTransportException = class( Exception ) |
| 83 | public |
| 84 | type |
| 85 | TExceptionType = ( |
| 86 | Unknown, |
| 87 | NotOpen, |
| 88 | AlreadyOpen, |
| 89 | TimedOut, |
| 90 | EndOfFile |
| 91 | ); |
| 92 | private |
| 93 | FType : TExceptionType; |
| 94 | public |
| 95 | constructor Create( AType: TExceptionType); overload; |
| 96 | constructor Create( const msg: string); overload; |
| 97 | constructor Create( AType: TExceptionType; const msg: string); overload; |
| 98 | property Type_: TExceptionType read FType; |
| 99 | end; |
| 100 | |
| 101 | IHTTPClient = interface( ITransport ) |
| 102 | ['{0F5DB8AB-710D-4338-AAC9-46B5734C5057}'] |
| 103 | procedure SetConnectionTimeout(const Value: Integer); |
| 104 | function GetConnectionTimeout: Integer; |
| 105 | procedure SetReadTimeout(const Value: Integer); |
| 106 | function GetReadTimeout: Integer; |
| 107 | function GetCustomHeaders: IThriftDictionary<string,string>; |
| 108 | procedure SendRequest; |
| 109 | property ConnectionTimeout: Integer read GetConnectionTimeout write SetConnectionTimeout; |
| 110 | property ReadTimeout: Integer read GetReadTimeout write SetReadTimeout; |
| 111 | property CustomHeaders: IThriftDictionary<string,string> read GetCustomHeaders; |
| 112 | end; |
| 113 | |
| 114 | THTTPClientImpl = class( TTransportImpl, IHTTPClient) |
| 115 | private |
| 116 | FUri : string; |
| 117 | FInputStream : IThriftStream; |
| 118 | FOutputStream : IThriftStream; |
| 119 | FConnectionTimeout : Integer; |
| 120 | FReadTimeout : Integer; |
| 121 | FCustomHeaders : IThriftDictionary<string,string>; |
| 122 | |
| 123 | function CreateRequest: IXMLHTTPRequest; |
| 124 | protected |
| 125 | function GetIsOpen: Boolean; override; |
| 126 | procedure Open(); override; |
| 127 | procedure Close(); override; |
| 128 | function Read( var buf: TBytes; off: Integer; len: Integer): Integer; override; |
| 129 | procedure Write( const buf: TBytes; off: Integer; len: Integer); override; |
| 130 | procedure Flush; override; |
| 131 | |
| 132 | procedure SetConnectionTimeout(const Value: Integer); |
| 133 | function GetConnectionTimeout: Integer; |
| 134 | procedure SetReadTimeout(const Value: Integer); |
| 135 | function GetReadTimeout: Integer; |
| 136 | function GetCustomHeaders: IThriftDictionary<string,string>; |
| 137 | procedure SendRequest; |
| 138 | property ConnectionTimeout: Integer read GetConnectionTimeout write SetConnectionTimeout; |
| 139 | property ReadTimeout: Integer read GetReadTimeout write SetReadTimeout; |
| 140 | property CustomHeaders: IThriftDictionary<string,string> read GetCustomHeaders; |
| 141 | public |
| 142 | constructor Create( const AUri: string); |
| 143 | destructor Destroy; override; |
| 144 | end; |
| 145 | |
| 146 | IServerTransport = interface |
| 147 | ['{C43B87ED-69EA-47C4-B77C-15E288252900}'] |
| 148 | procedure Listen; |
| 149 | procedure Close; |
| 150 | function Accept( const fnAccepting: TProc): ITransport; |
| 151 | end; |
| 152 | |
| 153 | TServerTransportImpl = class( TInterfacedObject, IServerTransport) |
| 154 | protected |
| 155 | procedure Listen; virtual; abstract; |
| 156 | procedure Close; virtual; abstract; |
| 157 | function Accept( const fnAccepting: TProc): ITransport; virtual; abstract; |
| 158 | end; |
| 159 | |
| 160 | ITransportFactory = interface |
| 161 | ['{DD809446-000F-49E1-9BFF-E0D0DC76A9D7}'] |
| 162 | function GetTransport( const ATrans: ITransport): ITransport; |
| 163 | end; |
| 164 | |
| 165 | TTransportFactoryImpl = class( TInterfacedObject, ITransportFactory) |
| 166 | function GetTransport( const ATrans: ITransport): ITransport; virtual; |
| 167 | end; |
| 168 | |
Jens Geyer | 23d6746 | 2015-12-19 11:44:57 +0100 | [diff] [blame] | 169 | {$IFDEF OLD_SOCKETS} |
Jens Geyer | 6f6aa8a | 2016-03-10 19:47:12 +0100 | [diff] [blame] | 170 | TThriftCustomIpClient = TCustomIpClient; |
| 171 | TThriftTcpServer = TTcpServer; |
| 172 | TThriftTcpClient = TTcpClient; |
| 173 | {$ELSE} |
| 174 | // TODO |
| 175 | {$ENDIF} |
| 176 | |
| 177 | {$IFDEF OLD_SOCKETS} |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 178 | TTcpSocketStreamImpl = class( TThriftStreamImpl ) |
| 179 | private type |
| 180 | TWaitForData = ( wfd_HaveData, wfd_Timeout, wfd_Error); |
| 181 | private |
Jens Geyer | 6f6aa8a | 2016-03-10 19:47:12 +0100 | [diff] [blame] | 182 | FTcpClient : TThriftCustomIpClient; |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 183 | FTimeout : Integer; |
| 184 | function Select( ReadReady, WriteReady, ExceptFlag: PBoolean; |
| 185 | TimeOut: Integer; var wsaError : Integer): Integer; |
| 186 | function WaitForData( TimeOut : Integer; pBuf : Pointer; DesiredBytes: Integer; |
Jens Geyer | bcb17bc | 2015-07-17 23:11:14 +0200 | [diff] [blame] | 187 | var wsaError, bytesReady : Integer): TWaitForData; |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 188 | protected |
| 189 | procedure Write( const buffer: TBytes; offset: Integer; count: Integer); override; |
| 190 | function Read( var buffer: TBytes; offset: Integer; count: Integer): Integer; override; |
| 191 | procedure Open; override; |
| 192 | procedure Close; override; |
| 193 | procedure Flush; override; |
| 194 | |
| 195 | function IsOpen: Boolean; override; |
| 196 | function ToArray: TBytes; override; |
| 197 | public |
Jens Geyer | 6f6aa8a | 2016-03-10 19:47:12 +0100 | [diff] [blame] | 198 | constructor Create( const ATcpClient: TThriftCustomIpClient; const aTimeout : Integer = 0); |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 199 | end; |
Jens Geyer | 23d6746 | 2015-12-19 11:44:57 +0100 | [diff] [blame] | 200 | {$ENDIF} |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 201 | |
| 202 | IStreamTransport = interface( ITransport ) |
| 203 | ['{A8479B47-2A3E-4421-A9A0-D5A9EDCC634A}'] |
| 204 | function GetInputStream: IThriftStream; |
| 205 | function GetOutputStream: IThriftStream; |
| 206 | property InputStream : IThriftStream read GetInputStream; |
| 207 | property OutputStream : IThriftStream read GetOutputStream; |
| 208 | end; |
| 209 | |
| 210 | TStreamTransportImpl = class( TTransportImpl, IStreamTransport) |
| 211 | protected |
| 212 | FInputStream : IThriftStream; |
| 213 | FOutputStream : IThriftStream; |
| 214 | protected |
| 215 | function GetIsOpen: Boolean; override; |
| 216 | |
| 217 | function GetInputStream: IThriftStream; |
| 218 | function GetOutputStream: IThriftStream; |
| 219 | public |
| 220 | property InputStream : IThriftStream read GetInputStream; |
| 221 | property OutputStream : IThriftStream read GetOutputStream; |
| 222 | |
| 223 | procedure Open; override; |
| 224 | procedure Close; override; |
| 225 | procedure Flush; override; |
| 226 | function Read(var buf: TBytes; off: Integer; len: Integer): Integer; override; |
| 227 | procedure Write( const buf: TBytes; off: Integer; len: Integer); override; |
| 228 | constructor Create( const AInputStream : IThriftStream; const AOutputStream : IThriftStream); |
| 229 | destructor Destroy; override; |
| 230 | end; |
| 231 | |
| 232 | TBufferedStreamImpl = class( TThriftStreamImpl) |
| 233 | private |
| 234 | FStream : IThriftStream; |
| 235 | FBufSize : Integer; |
| 236 | FReadBuffer : TMemoryStream; |
| 237 | FWriteBuffer : TMemoryStream; |
| 238 | protected |
| 239 | procedure Write( const buffer: TBytes; offset: Integer; count: Integer); override; |
| 240 | function Read( var buffer: TBytes; offset: Integer; count: Integer): Integer; override; |
| 241 | procedure Open; override; |
| 242 | procedure Close; override; |
| 243 | procedure Flush; override; |
| 244 | function IsOpen: Boolean; override; |
| 245 | function ToArray: TBytes; override; |
| 246 | public |
| 247 | constructor Create( const AStream: IThriftStream; ABufSize: Integer); |
| 248 | destructor Destroy; override; |
| 249 | end; |
| 250 | |
Jens Geyer | 23d6746 | 2015-12-19 11:44:57 +0100 | [diff] [blame] | 251 | {$IFDEF OLD_SOCKETS} |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 252 | TServerSocketImpl = class( TServerTransportImpl) |
| 253 | private |
Jens Geyer | 6f6aa8a | 2016-03-10 19:47:12 +0100 | [diff] [blame] | 254 | FServer : TThriftTcpServer; |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 255 | FPort : Integer; |
| 256 | FClientTimeout : Integer; |
| 257 | FUseBufferedSocket : Boolean; |
| 258 | FOwnsServer : Boolean; |
| 259 | protected |
| 260 | function Accept( const fnAccepting: TProc) : ITransport; override; |
| 261 | public |
Jens Geyer | 6f6aa8a | 2016-03-10 19:47:12 +0100 | [diff] [blame] | 262 | constructor Create( const AServer: TThriftTcpServer; AClientTimeout: Integer = 0); overload; |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 263 | constructor Create( APort: Integer; AClientTimeout: Integer = 0; AUseBufferedSockets: Boolean = FALSE); overload; |
| 264 | destructor Destroy; override; |
| 265 | procedure Listen; override; |
| 266 | procedure Close; override; |
| 267 | end; |
Jens Geyer | 23d6746 | 2015-12-19 11:44:57 +0100 | [diff] [blame] | 268 | {$ENDIF} |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 269 | |
| 270 | TBufferedTransportImpl = class( TTransportImpl ) |
| 271 | private |
| 272 | FInputBuffer : IThriftStream; |
| 273 | FOutputBuffer : IThriftStream; |
| 274 | FTransport : IStreamTransport; |
| 275 | FBufSize : Integer; |
| 276 | |
| 277 | procedure InitBuffers; |
| 278 | function GetUnderlyingTransport: ITransport; |
| 279 | protected |
| 280 | function GetIsOpen: Boolean; override; |
| 281 | procedure Flush; override; |
| 282 | public |
| 283 | procedure Open(); override; |
| 284 | procedure Close(); override; |
| 285 | function Read(var buf: TBytes; off: Integer; len: Integer): Integer; override; |
| 286 | procedure Write( const buf: TBytes; off: Integer; len: Integer); override; |
| 287 | constructor Create( const ATransport : IStreamTransport ); overload; |
| 288 | constructor Create( const ATransport : IStreamTransport; ABufSize: Integer); overload; |
| 289 | property UnderlyingTransport: ITransport read GetUnderlyingTransport; |
| 290 | property IsOpen: Boolean read GetIsOpen; |
| 291 | end; |
| 292 | |
Jens Geyer | 23d6746 | 2015-12-19 11:44:57 +0100 | [diff] [blame] | 293 | {$IFDEF OLD_SOCKETS} |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 294 | TSocketImpl = class(TStreamTransportImpl) |
| 295 | private |
Jens Geyer | 6f6aa8a | 2016-03-10 19:47:12 +0100 | [diff] [blame] | 296 | FClient : TThriftCustomIpClient; |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 297 | FOwnsClient : Boolean; |
| 298 | FHost : string; |
| 299 | FPort : Integer; |
| 300 | FTimeout : Integer; |
| 301 | |
| 302 | procedure InitSocket; |
| 303 | protected |
| 304 | function GetIsOpen: Boolean; override; |
| 305 | public |
| 306 | procedure Open; override; |
Jens Geyer | 6f6aa8a | 2016-03-10 19:47:12 +0100 | [diff] [blame] | 307 | constructor Create( const AClient : TThriftCustomIpClient; aOwnsClient : Boolean; ATimeout: Integer = 0); overload; |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 308 | constructor Create( const AHost: string; APort: Integer; ATimeout: Integer = 0); overload; |
| 309 | destructor Destroy; override; |
| 310 | procedure Close; override; |
Jens Geyer | 6f6aa8a | 2016-03-10 19:47:12 +0100 | [diff] [blame] | 311 | property TcpClient: TThriftCustomIpClient read FClient; |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 312 | property Host : string read FHost; |
| 313 | property Port: Integer read FPort; |
| 314 | end; |
Jens Geyer | 23d6746 | 2015-12-19 11:44:57 +0100 | [diff] [blame] | 315 | {$ENDIF} |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 316 | |
| 317 | TFramedTransportImpl = class( TTransportImpl) |
| 318 | private const |
| 319 | FHeaderSize : Integer = 4; |
| 320 | private class var |
| 321 | FHeader_Dummy : array of Byte; |
| 322 | protected |
| 323 | FTransport : ITransport; |
| 324 | FWriteBuffer : TMemoryStream; |
| 325 | FReadBuffer : TMemoryStream; |
| 326 | |
| 327 | procedure InitWriteBuffer; |
| 328 | procedure ReadFrame; |
| 329 | public |
| 330 | type |
| 331 | TFactory = class( TTransportFactoryImpl ) |
| 332 | public |
| 333 | function GetTransport( const ATrans: ITransport): ITransport; override; |
| 334 | end; |
| 335 | |
| 336 | {$IF CompilerVersion >= 21.0} |
| 337 | class constructor Create; |
| 338 | {$IFEND} |
| 339 | constructor Create; overload; |
| 340 | constructor Create( const ATrans: ITransport); overload; |
| 341 | destructor Destroy; override; |
| 342 | |
| 343 | procedure Open(); override; |
| 344 | function GetIsOpen: Boolean; override; |
| 345 | |
| 346 | procedure Close(); override; |
| 347 | function Read(var buf: TBytes; off: Integer; len: Integer): Integer; override; |
| 348 | procedure Write( const buf: TBytes; off: Integer; len: Integer); override; |
| 349 | procedure Flush; override; |
| 350 | end; |
| 351 | |
| 352 | {$IF CompilerVersion < 21.0} |
| 353 | procedure TFramedTransportImpl_Initialize; |
| 354 | {$IFEND} |
| 355 | |
| 356 | const |
| 357 | DEFAULT_THRIFT_TIMEOUT = 5 * 1000; // ms |
| 358 | |
| 359 | |
| 360 | implementation |
| 361 | |
| 362 | { TTransportImpl } |
| 363 | |
| 364 | procedure TTransportImpl.Flush; |
| 365 | begin |
| 366 | |
| 367 | end; |
| 368 | |
| 369 | function TTransportImpl.Peek: Boolean; |
| 370 | begin |
| 371 | Result := IsOpen; |
| 372 | end; |
| 373 | |
| 374 | function TTransportImpl.ReadAll( var buf: TBytes; off, len: Integer): Integer; |
| 375 | var |
| 376 | got : Integer; |
| 377 | ret : Integer; |
| 378 | begin |
| 379 | got := 0; |
| 380 | while ( got < len) do |
| 381 | begin |
| 382 | ret := Read( buf, off + got, len - got); |
| 383 | if ( ret <= 0 ) then |
| 384 | begin |
| 385 | raise TTransportException.Create( 'Cannot read, Remote side has closed' ); |
| 386 | end; |
| 387 | got := got + ret; |
| 388 | end; |
| 389 | Result := got; |
| 390 | end; |
| 391 | |
| 392 | procedure TTransportImpl.Write( const buf: TBytes); |
| 393 | begin |
| 394 | Self.Write( buf, 0, Length(buf) ); |
| 395 | end; |
| 396 | |
| 397 | { THTTPClientImpl } |
| 398 | |
| 399 | procedure THTTPClientImpl.Close; |
| 400 | begin |
| 401 | FInputStream := nil; |
| 402 | FOutputStream := nil; |
| 403 | end; |
| 404 | |
| 405 | constructor THTTPClientImpl.Create(const AUri: string); |
| 406 | begin |
| 407 | inherited Create; |
| 408 | FUri := AUri; |
| 409 | FCustomHeaders := TThriftDictionaryImpl<string,string>.Create; |
| 410 | FOutputStream := TThriftStreamAdapterDelphi.Create( TMemoryStream.Create, True); |
| 411 | end; |
| 412 | |
| 413 | function THTTPClientImpl.CreateRequest: IXMLHTTPRequest; |
| 414 | var |
| 415 | pair : TPair<string,string>; |
| 416 | begin |
| 417 | {$IF CompilerVersion >= 21.0} |
| 418 | Result := CoXMLHTTP.Create; |
| 419 | {$ELSE} |
| 420 | Result := CoXMLHTTPRequest.Create; |
| 421 | {$IFEND} |
| 422 | |
| 423 | Result.open('POST', FUri, False, '', ''); |
| 424 | Result.setRequestHeader( 'Content-Type', 'application/x-thrift'); |
| 425 | Result.setRequestHeader( 'Accept', 'application/x-thrift'); |
| 426 | Result.setRequestHeader( 'User-Agent', 'Delphi/IHTTPClient'); |
| 427 | |
| 428 | for pair in FCustomHeaders do |
| 429 | begin |
| 430 | Result.setRequestHeader( pair.Key, pair.Value ); |
| 431 | end; |
| 432 | end; |
| 433 | |
| 434 | destructor THTTPClientImpl.Destroy; |
| 435 | begin |
| 436 | Close; |
| 437 | inherited; |
| 438 | end; |
| 439 | |
| 440 | procedure THTTPClientImpl.Flush; |
| 441 | begin |
| 442 | try |
| 443 | SendRequest; |
| 444 | finally |
| 445 | FOutputStream := nil; |
| 446 | FOutputStream := TThriftStreamAdapterDelphi.Create( TMemoryStream.Create, True); |
| 447 | end; |
| 448 | end; |
| 449 | |
| 450 | function THTTPClientImpl.GetConnectionTimeout: Integer; |
| 451 | begin |
| 452 | Result := FConnectionTimeout; |
| 453 | end; |
| 454 | |
| 455 | function THTTPClientImpl.GetCustomHeaders: IThriftDictionary<string,string>; |
| 456 | begin |
| 457 | Result := FCustomHeaders; |
| 458 | end; |
| 459 | |
| 460 | function THTTPClientImpl.GetIsOpen: Boolean; |
| 461 | begin |
| 462 | Result := True; |
| 463 | end; |
| 464 | |
| 465 | function THTTPClientImpl.GetReadTimeout: Integer; |
| 466 | begin |
| 467 | Result := FReadTimeout; |
| 468 | end; |
| 469 | |
| 470 | procedure THTTPClientImpl.Open; |
| 471 | begin |
| 472 | |
| 473 | end; |
| 474 | |
| 475 | function THTTPClientImpl.Read( var buf: TBytes; off, len: Integer): Integer; |
| 476 | begin |
Jens Geyer | 6f6aa8a | 2016-03-10 19:47:12 +0100 | [diff] [blame] | 477 | if FInputStream = nil then begin |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 478 | raise TTransportException.Create( TTransportException.TExceptionType.NotOpen, |
Jens Geyer | 6f6aa8a | 2016-03-10 19:47:12 +0100 | [diff] [blame] | 479 | 'No request has been sent'); |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 480 | end; |
Jens Geyer | 6f6aa8a | 2016-03-10 19:47:12 +0100 | [diff] [blame] | 481 | |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 482 | try |
| 483 | Result := FInputStream.Read( buf, off, len ) |
| 484 | except |
Jens Geyer | 6f6aa8a | 2016-03-10 19:47:12 +0100 | [diff] [blame] | 485 | on E: Exception |
| 486 | do raise TTransportException.Create( TTransportException.TExceptionType.Unknown, E.Message); |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 487 | end; |
| 488 | end; |
| 489 | |
| 490 | procedure THTTPClientImpl.SendRequest; |
| 491 | var |
| 492 | xmlhttp : IXMLHTTPRequest; |
| 493 | ms : TMemoryStream; |
| 494 | a : TBytes; |
| 495 | len : Integer; |
| 496 | begin |
| 497 | xmlhttp := CreateRequest; |
| 498 | |
| 499 | ms := TMemoryStream.Create; |
| 500 | try |
| 501 | a := FOutputStream.ToArray; |
| 502 | len := Length(a); |
| 503 | if len > 0 then |
| 504 | begin |
| 505 | ms.WriteBuffer( Pointer(@a[0])^, len); |
| 506 | end; |
| 507 | ms.Position := 0; |
| 508 | xmlhttp.send( IUnknown( TStreamAdapter.Create( ms, soReference ))); |
| 509 | FInputStream := nil; |
| 510 | FInputStream := TThriftStreamAdapterCOM.Create( IUnknown( xmlhttp.responseStream) as IStream); |
| 511 | finally |
| 512 | ms.Free; |
| 513 | end; |
| 514 | end; |
| 515 | |
| 516 | procedure THTTPClientImpl.SetConnectionTimeout(const Value: Integer); |
| 517 | begin |
| 518 | FConnectionTimeout := Value; |
| 519 | end; |
| 520 | |
| 521 | procedure THTTPClientImpl.SetReadTimeout(const Value: Integer); |
| 522 | begin |
| 523 | FReadTimeout := Value |
| 524 | end; |
| 525 | |
| 526 | procedure THTTPClientImpl.Write( const buf: TBytes; off, len: Integer); |
| 527 | begin |
| 528 | FOutputStream.Write( buf, off, len); |
| 529 | end; |
| 530 | |
| 531 | { TTransportException } |
| 532 | |
| 533 | constructor TTransportException.Create(AType: TExceptionType); |
| 534 | begin |
| 535 | //no inherited; |
| 536 | Create( AType, '' ) |
| 537 | end; |
| 538 | |
| 539 | constructor TTransportException.Create(AType: TExceptionType; |
| 540 | const msg: string); |
| 541 | begin |
| 542 | inherited Create(msg); |
| 543 | FType := AType; |
| 544 | end; |
| 545 | |
| 546 | constructor TTransportException.Create(const msg: string); |
| 547 | begin |
| 548 | inherited Create(msg); |
| 549 | end; |
| 550 | |
| 551 | { TTransportFactoryImpl } |
| 552 | |
| 553 | function TTransportFactoryImpl.GetTransport( const ATrans: ITransport): ITransport; |
| 554 | begin |
| 555 | Result := ATrans; |
| 556 | end; |
| 557 | |
| 558 | { TServerSocket } |
| 559 | |
Jens Geyer | 23d6746 | 2015-12-19 11:44:57 +0100 | [diff] [blame] | 560 | {$IFDEF OLD_SOCKETS} |
Jens Geyer | 6f6aa8a | 2016-03-10 19:47:12 +0100 | [diff] [blame] | 561 | constructor TServerSocketImpl.Create( const AServer: TThriftTcpServer; AClientTimeout: Integer); |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 562 | begin |
| 563 | inherited Create; |
| 564 | FServer := AServer; |
| 565 | FClientTimeout := AClientTimeout; |
| 566 | end; |
| 567 | |
| 568 | constructor TServerSocketImpl.Create(APort, AClientTimeout: Integer; AUseBufferedSockets: Boolean); |
| 569 | begin |
| 570 | inherited Create; |
| 571 | FPort := APort; |
| 572 | FClientTimeout := AClientTimeout; |
| 573 | FUseBufferedSocket := AUseBufferedSockets; |
| 574 | FOwnsServer := True; |
Jens Geyer | 6f6aa8a | 2016-03-10 19:47:12 +0100 | [diff] [blame] | 575 | FServer := TThriftTcpServer.Create( nil ); |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 576 | FServer.BlockMode := bmBlocking; |
| 577 | {$IF CompilerVersion >= 21.0} |
| 578 | FServer.LocalPort := AnsiString( IntToStr( FPort)); |
| 579 | {$ELSE} |
| 580 | FServer.LocalPort := IntToStr( FPort); |
| 581 | {$IFEND} |
| 582 | end; |
| 583 | |
| 584 | destructor TServerSocketImpl.Destroy; |
| 585 | begin |
| 586 | if FOwnsServer then begin |
| 587 | FServer.Free; |
| 588 | FServer := nil; |
| 589 | end; |
| 590 | inherited; |
| 591 | end; |
| 592 | |
| 593 | function TServerSocketImpl.Accept( const fnAccepting: TProc): ITransport; |
| 594 | var |
Jens Geyer | 6f6aa8a | 2016-03-10 19:47:12 +0100 | [diff] [blame] | 595 | client : TThriftCustomIpClient; |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 596 | trans : IStreamTransport; |
| 597 | begin |
Jens Geyer | 6f6aa8a | 2016-03-10 19:47:12 +0100 | [diff] [blame] | 598 | if FServer = nil then begin |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 599 | raise TTransportException.Create( TTransportException.TExceptionType.NotOpen, |
Jens Geyer | 6f6aa8a | 2016-03-10 19:47:12 +0100 | [diff] [blame] | 600 | 'No underlying server socket.'); |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 601 | end; |
| 602 | |
| 603 | client := nil; |
| 604 | try |
Jens Geyer | 6f6aa8a | 2016-03-10 19:47:12 +0100 | [diff] [blame] | 605 | client := TThriftCustomIpClient.Create(nil); |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 606 | |
| 607 | if Assigned(fnAccepting) |
| 608 | then fnAccepting(); |
| 609 | |
Jens Geyer | 6f6aa8a | 2016-03-10 19:47:12 +0100 | [diff] [blame] | 610 | if not FServer.Accept( client) then begin |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 611 | client.Free; |
| 612 | Result := nil; |
| 613 | Exit; |
| 614 | end; |
| 615 | |
Jens Geyer | 6f6aa8a | 2016-03-10 19:47:12 +0100 | [diff] [blame] | 616 | if client = nil then begin |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 617 | Result := nil; |
| 618 | Exit; |
| 619 | end; |
| 620 | |
| 621 | trans := TSocketImpl.Create( client, TRUE, FClientTimeout); |
| 622 | client := nil; // trans owns it now |
| 623 | |
| 624 | if FUseBufferedSocket |
| 625 | then result := TBufferedTransportImpl.Create( trans) |
| 626 | else result := trans; |
| 627 | |
| 628 | except |
| 629 | on E: Exception do begin |
| 630 | client.Free; |
| 631 | raise TTransportException.Create( E.ToString ); |
| 632 | end; |
| 633 | end; |
| 634 | end; |
| 635 | |
| 636 | procedure TServerSocketImpl.Listen; |
| 637 | begin |
| 638 | if FServer <> nil then |
| 639 | begin |
| 640 | try |
| 641 | FServer.Active := True; |
| 642 | except |
| 643 | on E: Exception do |
| 644 | begin |
| 645 | raise TTransportException.Create('Could not accept on listening socket: ' + E.Message); |
| 646 | end; |
| 647 | end; |
| 648 | end; |
| 649 | end; |
| 650 | |
| 651 | procedure TServerSocketImpl.Close; |
| 652 | begin |
Jens Geyer | 6f6aa8a | 2016-03-10 19:47:12 +0100 | [diff] [blame] | 653 | if FServer <> nil |
| 654 | then try |
| 655 | FServer.Active := False; |
| 656 | except |
| 657 | on E: Exception |
| 658 | do raise TTransportException.Create('Error on closing socket : ' + E.Message); |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 659 | end; |
| 660 | end; |
| 661 | |
| 662 | { TSocket } |
| 663 | |
Jens Geyer | 6f6aa8a | 2016-03-10 19:47:12 +0100 | [diff] [blame] | 664 | constructor TSocketImpl.Create( const AClient : TThriftCustomIpClient; aOwnsClient : Boolean; ATimeout: Integer = 0); |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 665 | var stream : IThriftStream; |
| 666 | begin |
| 667 | FClient := AClient; |
| 668 | FTimeout := ATimeout; |
| 669 | FOwnsClient := aOwnsClient; |
| 670 | stream := TTcpSocketStreamImpl.Create( FClient, FTimeout); |
| 671 | inherited Create( stream, stream); |
| 672 | end; |
| 673 | |
| 674 | constructor TSocketImpl.Create(const AHost: string; APort, ATimeout: Integer); |
| 675 | begin |
| 676 | inherited Create(nil,nil); |
| 677 | FHost := AHost; |
| 678 | FPort := APort; |
| 679 | FTimeout := ATimeout; |
| 680 | InitSocket; |
| 681 | end; |
| 682 | |
| 683 | destructor TSocketImpl.Destroy; |
| 684 | begin |
| 685 | if FOwnsClient |
| 686 | then FreeAndNil( FClient); |
| 687 | inherited; |
| 688 | end; |
| 689 | |
| 690 | procedure TSocketImpl.Close; |
| 691 | begin |
| 692 | inherited Close; |
| 693 | if FOwnsClient |
| 694 | then FreeAndNil( FClient); |
| 695 | end; |
| 696 | |
| 697 | function TSocketImpl.GetIsOpen: Boolean; |
| 698 | begin |
| 699 | Result := (FClient <> nil) and FClient.Connected; |
| 700 | end; |
| 701 | |
| 702 | procedure TSocketImpl.InitSocket; |
| 703 | var |
| 704 | stream : IThriftStream; |
| 705 | begin |
| 706 | if FOwnsClient |
| 707 | then FreeAndNil( FClient) |
| 708 | else FClient := nil; |
| 709 | |
Jens Geyer | 6f6aa8a | 2016-03-10 19:47:12 +0100 | [diff] [blame] | 710 | FClient := TThriftTcpClient.Create( nil); |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 711 | FOwnsClient := True; |
| 712 | |
| 713 | stream := TTcpSocketStreamImpl.Create( FClient, FTimeout); |
| 714 | FInputStream := stream; |
| 715 | FOutputStream := stream; |
| 716 | end; |
| 717 | |
| 718 | procedure TSocketImpl.Open; |
| 719 | begin |
Jens Geyer | 6f6aa8a | 2016-03-10 19:47:12 +0100 | [diff] [blame] | 720 | if IsOpen then begin |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 721 | raise TTransportException.Create( TTransportException.TExceptionType.AlreadyOpen, |
Jens Geyer | 6f6aa8a | 2016-03-10 19:47:12 +0100 | [diff] [blame] | 722 | 'Socket already connected'); |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 723 | end; |
| 724 | |
Jens Geyer | 6f6aa8a | 2016-03-10 19:47:12 +0100 | [diff] [blame] | 725 | if FHost = '' then begin |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 726 | raise TTransportException.Create( TTransportException.TExceptionType.NotOpen, |
Jens Geyer | 6f6aa8a | 2016-03-10 19:47:12 +0100 | [diff] [blame] | 727 | 'Cannot open null host'); |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 728 | end; |
| 729 | |
Jens Geyer | 6f6aa8a | 2016-03-10 19:47:12 +0100 | [diff] [blame] | 730 | if Port <= 0 then begin |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 731 | raise TTransportException.Create( TTransportException.TExceptionType.NotOpen, |
Jens Geyer | 6f6aa8a | 2016-03-10 19:47:12 +0100 | [diff] [blame] | 732 | 'Cannot open without port'); |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 733 | end; |
| 734 | |
Jens Geyer | 6f6aa8a | 2016-03-10 19:47:12 +0100 | [diff] [blame] | 735 | if FClient = nil |
| 736 | then InitSocket; |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 737 | |
| 738 | FClient.RemoteHost := TSocketHost( Host); |
| 739 | FClient.RemotePort := TSocketPort( IntToStr( Port)); |
| 740 | FClient.Connect; |
| 741 | |
| 742 | FInputStream := TTcpSocketStreamImpl.Create( FClient, FTimeout); |
| 743 | FOutputStream := FInputStream; |
| 744 | end; |
Jens Geyer | 23d6746 | 2015-12-19 11:44:57 +0100 | [diff] [blame] | 745 | {$ENDIF} |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 746 | |
| 747 | { TBufferedStream } |
| 748 | |
| 749 | procedure TBufferedStreamImpl.Close; |
| 750 | begin |
| 751 | Flush; |
| 752 | FStream := nil; |
| 753 | |
| 754 | FReadBuffer.Free; |
| 755 | FReadBuffer := nil; |
| 756 | |
| 757 | FWriteBuffer.Free; |
| 758 | FWriteBuffer := nil; |
| 759 | end; |
| 760 | |
| 761 | constructor TBufferedStreamImpl.Create( const AStream: IThriftStream; ABufSize: Integer); |
| 762 | begin |
| 763 | inherited Create; |
| 764 | FStream := AStream; |
| 765 | FBufSize := ABufSize; |
| 766 | FReadBuffer := TMemoryStream.Create; |
| 767 | FWriteBuffer := TMemoryStream.Create; |
| 768 | end; |
| 769 | |
| 770 | destructor TBufferedStreamImpl.Destroy; |
| 771 | begin |
| 772 | Close; |
| 773 | inherited; |
| 774 | end; |
| 775 | |
| 776 | procedure TBufferedStreamImpl.Flush; |
| 777 | var |
| 778 | buf : TBytes; |
| 779 | len : Integer; |
| 780 | begin |
| 781 | if IsOpen then |
| 782 | begin |
| 783 | len := FWriteBuffer.Size; |
| 784 | if len > 0 then |
| 785 | begin |
| 786 | SetLength( buf, len ); |
| 787 | FWriteBuffer.Position := 0; |
| 788 | FWriteBuffer.Read( Pointer(@buf[0])^, len ); |
| 789 | FStream.Write( buf, 0, len ); |
| 790 | end; |
| 791 | FWriteBuffer.Clear; |
| 792 | end; |
| 793 | end; |
| 794 | |
| 795 | function TBufferedStreamImpl.IsOpen: Boolean; |
| 796 | begin |
| 797 | Result := (FWriteBuffer <> nil) |
| 798 | and (FReadBuffer <> nil) |
| 799 | and (FStream <> nil); |
| 800 | end; |
| 801 | |
| 802 | procedure TBufferedStreamImpl.Open; |
| 803 | begin |
| 804 | |
| 805 | end; |
| 806 | |
| 807 | function TBufferedStreamImpl.Read( var buffer: TBytes; offset: Integer; count: Integer): Integer; |
| 808 | var |
| 809 | nRead : Integer; |
| 810 | tempbuf : TBytes; |
| 811 | begin |
| 812 | inherited; |
| 813 | Result := 0; |
| 814 | if IsOpen then |
| 815 | begin |
| 816 | while count > 0 do begin |
| 817 | |
| 818 | if FReadBuffer.Position >= FReadBuffer.Size then |
| 819 | begin |
| 820 | FReadBuffer.Clear; |
| 821 | SetLength( tempbuf, FBufSize); |
| 822 | nRead := FStream.Read( tempbuf, 0, FBufSize ); |
| 823 | if nRead = 0 then Break; // avoid infinite loop |
| 824 | |
| 825 | FReadBuffer.WriteBuffer( Pointer(@tempbuf[0])^, nRead ); |
| 826 | FReadBuffer.Position := 0; |
| 827 | end; |
| 828 | |
| 829 | if FReadBuffer.Position < FReadBuffer.Size then |
| 830 | begin |
| 831 | nRead := Min( FReadBuffer.Size - FReadBuffer.Position, count); |
| 832 | Inc( Result, FReadBuffer.Read( Pointer(@buffer[offset])^, nRead)); |
| 833 | Dec( count, nRead); |
| 834 | Inc( offset, nRead); |
| 835 | end; |
| 836 | end; |
| 837 | end; |
| 838 | end; |
| 839 | |
| 840 | function TBufferedStreamImpl.ToArray: TBytes; |
| 841 | var |
| 842 | len : Integer; |
| 843 | begin |
| 844 | len := 0; |
| 845 | |
| 846 | if IsOpen then |
| 847 | begin |
| 848 | len := FReadBuffer.Size; |
| 849 | end; |
| 850 | |
| 851 | SetLength( Result, len); |
| 852 | |
| 853 | if len > 0 then |
| 854 | begin |
| 855 | FReadBuffer.Position := 0; |
| 856 | FReadBuffer.Read( Pointer(@Result[0])^, len ); |
| 857 | end; |
| 858 | end; |
| 859 | |
| 860 | procedure TBufferedStreamImpl.Write( const buffer: TBytes; offset: Integer; count: Integer); |
| 861 | begin |
| 862 | inherited; |
| 863 | if count > 0 then |
| 864 | begin |
| 865 | if IsOpen then |
| 866 | begin |
| 867 | FWriteBuffer.Write( Pointer(@buffer[offset])^, count ); |
| 868 | if FWriteBuffer.Size > FBufSize then |
| 869 | begin |
| 870 | Flush; |
| 871 | end; |
| 872 | end; |
| 873 | end; |
| 874 | end; |
| 875 | |
| 876 | { TStreamTransportImpl } |
| 877 | |
| 878 | procedure TStreamTransportImpl.Close; |
| 879 | begin |
Jens Geyer | 6f6aa8a | 2016-03-10 19:47:12 +0100 | [diff] [blame] | 880 | FInputStream := nil; |
| 881 | FOutputStream := nil; |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 882 | end; |
| 883 | |
| 884 | constructor TStreamTransportImpl.Create( const AInputStream : IThriftStream; const AOutputStream : IThriftStream); |
| 885 | begin |
| 886 | inherited Create; |
| 887 | FInputStream := AInputStream; |
| 888 | FOutputStream := AOutputStream; |
| 889 | end; |
| 890 | |
| 891 | destructor TStreamTransportImpl.Destroy; |
| 892 | begin |
| 893 | FInputStream := nil; |
| 894 | FOutputStream := nil; |
| 895 | inherited; |
| 896 | end; |
| 897 | |
| 898 | procedure TStreamTransportImpl.Flush; |
| 899 | begin |
Jens Geyer | 6f6aa8a | 2016-03-10 19:47:12 +0100 | [diff] [blame] | 900 | if FOutputStream = nil then begin |
| 901 | raise TTransportException.Create( TTransportException.TExceptionType.NotOpen, |
| 902 | 'Cannot flush null outputstream' ); |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 903 | end; |
| 904 | |
| 905 | FOutputStream.Flush; |
| 906 | end; |
| 907 | |
| 908 | function TStreamTransportImpl.GetInputStream: IThriftStream; |
| 909 | begin |
| 910 | Result := FInputStream; |
| 911 | end; |
| 912 | |
| 913 | function TStreamTransportImpl.GetIsOpen: Boolean; |
| 914 | begin |
| 915 | Result := True; |
| 916 | end; |
| 917 | |
| 918 | function TStreamTransportImpl.GetOutputStream: IThriftStream; |
| 919 | begin |
| 920 | Result := FInputStream; |
| 921 | end; |
| 922 | |
| 923 | procedure TStreamTransportImpl.Open; |
| 924 | begin |
| 925 | |
| 926 | end; |
| 927 | |
| 928 | function TStreamTransportImpl.Read(var buf: TBytes; off, len: Integer): Integer; |
| 929 | begin |
Jens Geyer | 6f6aa8a | 2016-03-10 19:47:12 +0100 | [diff] [blame] | 930 | if FInputStream = nil then begin |
| 931 | raise TTransportException.Create( TTransportException.TExceptionType.NotOpen, |
| 932 | 'Cannot read from null inputstream' ); |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 933 | end; |
Jens Geyer | 6f6aa8a | 2016-03-10 19:47:12 +0100 | [diff] [blame] | 934 | |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 935 | Result := FInputStream.Read( buf, off, len ); |
| 936 | end; |
| 937 | |
| 938 | procedure TStreamTransportImpl.Write(const buf: TBytes; off, len: Integer); |
| 939 | begin |
Jens Geyer | 6f6aa8a | 2016-03-10 19:47:12 +0100 | [diff] [blame] | 940 | if FOutputStream = nil then begin |
| 941 | raise TTransportException.Create( TTransportException.TExceptionType.NotOpen, |
| 942 | 'Cannot write to null outputstream' ); |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 943 | end; |
| 944 | |
| 945 | FOutputStream.Write( buf, off, len ); |
| 946 | end; |
| 947 | |
| 948 | { TBufferedTransportImpl } |
| 949 | |
| 950 | constructor TBufferedTransportImpl.Create( const ATransport: IStreamTransport); |
| 951 | begin |
| 952 | //no inherited; |
| 953 | Create( ATransport, 1024 ); |
| 954 | end; |
| 955 | |
| 956 | procedure TBufferedTransportImpl.Close; |
| 957 | begin |
| 958 | FTransport.Close; |
| 959 | end; |
| 960 | |
| 961 | constructor TBufferedTransportImpl.Create( const ATransport: IStreamTransport; |
| 962 | ABufSize: Integer); |
| 963 | begin |
| 964 | inherited Create; |
| 965 | FTransport := ATransport; |
| 966 | FBufSize := ABufSize; |
| 967 | InitBuffers; |
| 968 | end; |
| 969 | |
| 970 | procedure TBufferedTransportImpl.Flush; |
| 971 | begin |
| 972 | if FOutputBuffer <> nil then |
| 973 | begin |
| 974 | FOutputBuffer.Flush; |
| 975 | end; |
| 976 | end; |
| 977 | |
| 978 | function TBufferedTransportImpl.GetIsOpen: Boolean; |
| 979 | begin |
| 980 | Result := FTransport.IsOpen; |
| 981 | end; |
| 982 | |
| 983 | function TBufferedTransportImpl.GetUnderlyingTransport: ITransport; |
| 984 | begin |
| 985 | Result := FTransport; |
| 986 | end; |
| 987 | |
| 988 | procedure TBufferedTransportImpl.InitBuffers; |
| 989 | begin |
| 990 | if FTransport.InputStream <> nil then |
| 991 | begin |
| 992 | FInputBuffer := TBufferedStreamImpl.Create( FTransport.InputStream, FBufSize ); |
| 993 | end; |
| 994 | if FTransport.OutputStream <> nil then |
| 995 | begin |
| 996 | FOutputBuffer := TBufferedStreamImpl.Create( FTransport.OutputStream, FBufSize ); |
| 997 | end; |
| 998 | end; |
| 999 | |
| 1000 | procedure TBufferedTransportImpl.Open; |
| 1001 | begin |
| 1002 | FTransport.Open |
| 1003 | end; |
| 1004 | |
| 1005 | function TBufferedTransportImpl.Read(var buf: TBytes; off, len: Integer): Integer; |
| 1006 | begin |
| 1007 | Result := 0; |
| 1008 | if FInputBuffer <> nil then |
| 1009 | begin |
| 1010 | Result := FInputBuffer.Read( buf, off, len ); |
| 1011 | end; |
| 1012 | end; |
| 1013 | |
| 1014 | procedure TBufferedTransportImpl.Write(const buf: TBytes; off, len: Integer); |
| 1015 | begin |
| 1016 | if FOutputBuffer <> nil then |
| 1017 | begin |
| 1018 | FOutputBuffer.Write( buf, off, len ); |
| 1019 | end; |
| 1020 | end; |
| 1021 | |
| 1022 | { TFramedTransportImpl } |
| 1023 | |
| 1024 | {$IF CompilerVersion < 21.0} |
| 1025 | procedure TFramedTransportImpl_Initialize; |
| 1026 | begin |
| 1027 | SetLength( TFramedTransportImpl.FHeader_Dummy, TFramedTransportImpl.FHeaderSize); |
| 1028 | FillChar( TFramedTransportImpl.FHeader_Dummy[0], |
| 1029 | Length( TFramedTransportImpl.FHeader_Dummy) * SizeOf( Byte ), 0); |
| 1030 | end; |
| 1031 | {$ELSE} |
| 1032 | class constructor TFramedTransportImpl.Create; |
| 1033 | begin |
| 1034 | SetLength( FHeader_Dummy, FHeaderSize); |
| 1035 | FillChar( FHeader_Dummy[0], Length( FHeader_Dummy) * SizeOf( Byte ), 0); |
| 1036 | end; |
| 1037 | {$IFEND} |
| 1038 | |
| 1039 | constructor TFramedTransportImpl.Create; |
| 1040 | begin |
| 1041 | inherited Create; |
| 1042 | InitWriteBuffer; |
| 1043 | end; |
| 1044 | |
| 1045 | procedure TFramedTransportImpl.Close; |
| 1046 | begin |
| 1047 | FTransport.Close; |
| 1048 | end; |
| 1049 | |
| 1050 | constructor TFramedTransportImpl.Create( const ATrans: ITransport); |
| 1051 | begin |
| 1052 | inherited Create; |
| 1053 | InitWriteBuffer; |
| 1054 | FTransport := ATrans; |
| 1055 | end; |
| 1056 | |
| 1057 | destructor TFramedTransportImpl.Destroy; |
| 1058 | begin |
| 1059 | FWriteBuffer.Free; |
| 1060 | FReadBuffer.Free; |
| 1061 | inherited; |
| 1062 | end; |
| 1063 | |
| 1064 | procedure TFramedTransportImpl.Flush; |
| 1065 | var |
| 1066 | buf : TBytes; |
| 1067 | len : Integer; |
| 1068 | data_len : Integer; |
| 1069 | |
| 1070 | begin |
| 1071 | len := FWriteBuffer.Size; |
| 1072 | SetLength( buf, len); |
| 1073 | if len > 0 then |
| 1074 | begin |
| 1075 | System.Move( FWriteBuffer.Memory^, buf[0], len ); |
| 1076 | end; |
| 1077 | |
| 1078 | data_len := len - FHeaderSize; |
Jens Geyer | 30ed90e | 2016-03-10 20:12:49 +0100 | [diff] [blame] | 1079 | if (data_len < 0) then begin |
| 1080 | raise TTransportException.Create( TTransportException.TExceptionType.Unknown, |
| 1081 | 'TFramedTransport.Flush: data_len < 0' ); |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 1082 | end; |
| 1083 | |
| 1084 | InitWriteBuffer; |
| 1085 | |
| 1086 | buf[0] := Byte($FF and (data_len shr 24)); |
| 1087 | buf[1] := Byte($FF and (data_len shr 16)); |
| 1088 | buf[2] := Byte($FF and (data_len shr 8)); |
| 1089 | buf[3] := Byte($FF and data_len); |
| 1090 | |
| 1091 | FTransport.Write( buf, 0, len ); |
| 1092 | FTransport.Flush; |
| 1093 | end; |
| 1094 | |
| 1095 | function TFramedTransportImpl.GetIsOpen: Boolean; |
| 1096 | begin |
| 1097 | Result := FTransport.IsOpen; |
| 1098 | end; |
| 1099 | |
| 1100 | type |
| 1101 | TAccessMemoryStream = class(TMemoryStream) |
| 1102 | end; |
| 1103 | |
| 1104 | procedure TFramedTransportImpl.InitWriteBuffer; |
| 1105 | begin |
| 1106 | FWriteBuffer.Free; |
| 1107 | FWriteBuffer := TMemoryStream.Create; |
| 1108 | TAccessMemoryStream(FWriteBuffer).Capacity := 1024; |
| 1109 | FWriteBuffer.Write( Pointer(@FHeader_Dummy[0])^, FHeaderSize); |
| 1110 | end; |
| 1111 | |
| 1112 | procedure TFramedTransportImpl.Open; |
| 1113 | begin |
| 1114 | FTransport.Open; |
| 1115 | end; |
| 1116 | |
| 1117 | function TFramedTransportImpl.Read(var buf: TBytes; off, len: Integer): Integer; |
| 1118 | var |
| 1119 | got : Integer; |
| 1120 | begin |
| 1121 | if FReadBuffer <> nil then |
| 1122 | begin |
| 1123 | if len > 0 |
| 1124 | then got := FReadBuffer.Read( Pointer(@buf[off])^, len ) |
| 1125 | else got := 0; |
| 1126 | if got > 0 then |
| 1127 | begin |
| 1128 | Result := got; |
| 1129 | Exit; |
| 1130 | end; |
| 1131 | end; |
| 1132 | |
| 1133 | ReadFrame; |
| 1134 | if len > 0 |
| 1135 | then Result := FReadBuffer.Read( Pointer(@buf[off])^, len) |
| 1136 | else Result := 0; |
| 1137 | end; |
| 1138 | |
| 1139 | procedure TFramedTransportImpl.ReadFrame; |
| 1140 | var |
| 1141 | i32rd : TBytes; |
| 1142 | size : Integer; |
| 1143 | buff : TBytes; |
| 1144 | begin |
| 1145 | SetLength( i32rd, FHeaderSize ); |
| 1146 | FTransport.ReadAll( i32rd, 0, FHeaderSize); |
| 1147 | size := |
| 1148 | ((i32rd[0] and $FF) shl 24) or |
| 1149 | ((i32rd[1] and $FF) shl 16) or |
| 1150 | ((i32rd[2] and $FF) shl 8) or |
| 1151 | (i32rd[3] and $FF); |
| 1152 | SetLength( buff, size ); |
| 1153 | FTransport.ReadAll( buff, 0, size ); |
| 1154 | FReadBuffer.Free; |
| 1155 | FReadBuffer := TMemoryStream.Create; |
| 1156 | FReadBuffer.Write( Pointer(@buff[0])^, size ); |
| 1157 | FReadBuffer.Position := 0; |
| 1158 | end; |
| 1159 | |
| 1160 | procedure TFramedTransportImpl.Write(const buf: TBytes; off, len: Integer); |
| 1161 | begin |
| 1162 | if len > 0 |
| 1163 | then FWriteBuffer.Write( Pointer(@buf[off])^, len ); |
| 1164 | end; |
| 1165 | |
| 1166 | { TFramedTransport.TFactory } |
| 1167 | |
| 1168 | function TFramedTransportImpl.TFactory.GetTransport( const ATrans: ITransport): ITransport; |
| 1169 | begin |
| 1170 | Result := TFramedTransportImpl.Create( ATrans ); |
| 1171 | end; |
| 1172 | |
| 1173 | { TTcpSocketStreamImpl } |
| 1174 | |
Jens Geyer | 23d6746 | 2015-12-19 11:44:57 +0100 | [diff] [blame] | 1175 | {$IFDEF OLD_SOCKETS} |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 1176 | procedure TTcpSocketStreamImpl.Close; |
| 1177 | begin |
| 1178 | FTcpClient.Close; |
| 1179 | end; |
| 1180 | |
Jens Geyer | 6f6aa8a | 2016-03-10 19:47:12 +0100 | [diff] [blame] | 1181 | constructor TTcpSocketStreamImpl.Create( const ATcpClient: TThriftCustomIpClient; const aTimeout : Integer); |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 1182 | begin |
| 1183 | inherited Create; |
| 1184 | FTcpClient := ATcpClient; |
| 1185 | FTimeout := aTimeout; |
| 1186 | end; |
| 1187 | |
| 1188 | procedure TTcpSocketStreamImpl.Flush; |
| 1189 | begin |
| 1190 | |
| 1191 | end; |
| 1192 | |
| 1193 | function TTcpSocketStreamImpl.IsOpen: Boolean; |
| 1194 | begin |
| 1195 | Result := FTcpClient.Active; |
| 1196 | end; |
| 1197 | |
| 1198 | procedure TTcpSocketStreamImpl.Open; |
| 1199 | begin |
| 1200 | FTcpClient.Open; |
| 1201 | end; |
| 1202 | |
| 1203 | |
| 1204 | function TTcpSocketStreamImpl.Select( ReadReady, WriteReady, ExceptFlag: PBoolean; |
| 1205 | TimeOut: Integer; var wsaError : Integer): Integer; |
| 1206 | var |
| 1207 | ReadFds: TFDset; |
| 1208 | ReadFdsptr: PFDset; |
| 1209 | WriteFds: TFDset; |
| 1210 | WriteFdsptr: PFDset; |
| 1211 | ExceptFds: TFDset; |
| 1212 | ExceptFdsptr: PFDset; |
| 1213 | tv: timeval; |
| 1214 | Timeptr: PTimeval; |
| 1215 | socket : TSocket; |
| 1216 | begin |
| 1217 | if not FTcpClient.Active then begin |
| 1218 | wsaError := WSAEINVAL; |
| 1219 | Exit( SOCKET_ERROR); |
| 1220 | end; |
| 1221 | |
| 1222 | socket := FTcpClient.Handle; |
| 1223 | |
| 1224 | if Assigned(ReadReady) then |
| 1225 | begin |
| 1226 | ReadFdsptr := @ReadFds; |
| 1227 | FD_ZERO(ReadFds); |
| 1228 | FD_SET(socket, ReadFds); |
| 1229 | end |
| 1230 | else |
| 1231 | ReadFdsptr := nil; |
| 1232 | |
| 1233 | if Assigned(WriteReady) then |
| 1234 | begin |
| 1235 | WriteFdsptr := @WriteFds; |
| 1236 | FD_ZERO(WriteFds); |
| 1237 | FD_SET(socket, WriteFds); |
| 1238 | end |
| 1239 | else |
| 1240 | WriteFdsptr := nil; |
| 1241 | |
| 1242 | if Assigned(ExceptFlag) then |
| 1243 | begin |
| 1244 | ExceptFdsptr := @ExceptFds; |
| 1245 | FD_ZERO(ExceptFds); |
| 1246 | FD_SET(socket, ExceptFds); |
| 1247 | end |
| 1248 | else |
| 1249 | ExceptFdsptr := nil; |
| 1250 | |
| 1251 | if TimeOut >= 0 then |
| 1252 | begin |
| 1253 | tv.tv_sec := TimeOut div 1000; |
| 1254 | tv.tv_usec := 1000 * (TimeOut mod 1000); |
| 1255 | Timeptr := @tv; |
| 1256 | end |
| 1257 | else |
| 1258 | Timeptr := nil; // wait forever |
| 1259 | |
| 1260 | wsaError := 0; |
| 1261 | try |
| 1262 | {$IFDEF MSWINDOWS} |
Nick | 4f5229e | 2016-04-14 16:43:22 +0300 | [diff] [blame^] | 1263 | {$IF CompilerVersion < 23.0} |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 1264 | result := WinSock.select(socket + 1, ReadFdsptr, WriteFdsptr, ExceptFdsptr, Timeptr); |
Nick | 4f5229e | 2016-04-14 16:43:22 +0300 | [diff] [blame^] | 1265 | {$ELSE} |
| 1266 | result := Winapi.WinSock.select(socket + 1, ReadFdsptr, WriteFdsptr, ExceptFdsptr, Timeptr); |
| 1267 | {$IFEND} |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 1268 | {$ENDIF} |
| 1269 | {$IFDEF LINUX} |
| 1270 | result := Libc.select(socket + 1, ReadFdsptr, WriteFdsptr, ExceptFdsptr, Timeptr); |
| 1271 | {$ENDIF} |
| 1272 | if result = SOCKET_ERROR |
| 1273 | then wsaError := WSAGetLastError; |
| 1274 | |
| 1275 | except |
| 1276 | result := SOCKET_ERROR; |
| 1277 | end; |
| 1278 | |
| 1279 | if Assigned(ReadReady) then |
| 1280 | ReadReady^ := FD_ISSET(socket, ReadFds); |
| 1281 | if Assigned(WriteReady) then |
| 1282 | WriteReady^ := FD_ISSET(socket, WriteFds); |
| 1283 | if Assigned(ExceptFlag) then |
| 1284 | ExceptFlag^ := FD_ISSET(socket, ExceptFds); |
| 1285 | end; |
| 1286 | |
| 1287 | function TTcpSocketStreamImpl.WaitForData( TimeOut : Integer; pBuf : Pointer; |
| 1288 | DesiredBytes : Integer; |
Jens Geyer | bcb17bc | 2015-07-17 23:11:14 +0200 | [diff] [blame] | 1289 | var wsaError, bytesReady : Integer): TWaitForData; |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 1290 | var bCanRead, bError : Boolean; |
| 1291 | retval : Integer; |
| 1292 | begin |
Jens Geyer | bcb17bc | 2015-07-17 23:11:14 +0200 | [diff] [blame] | 1293 | bytesReady := 0; |
| 1294 | |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 1295 | // The select function returns the total number of socket handles that are ready |
| 1296 | // and contained in the fd_set structures, zero if the time limit expired, |
| 1297 | // or SOCKET_ERROR if an error occurred. If the return value is SOCKET_ERROR, |
| 1298 | // WSAGetLastError can be used to retrieve a specific error code. |
| 1299 | retval := Self.Select( @bCanRead, nil, @bError, TimeOut, wsaError); |
| 1300 | if retval = SOCKET_ERROR |
| 1301 | then Exit( TWaitForData.wfd_Error); |
| 1302 | if (retval = 0) or not bCanRead |
| 1303 | then Exit( TWaitForData.wfd_Timeout); |
| 1304 | |
| 1305 | // recv() returns the number of bytes received, or -1 if an error occurred. |
| 1306 | // The return value will be 0 when the peer has performed an orderly shutdown. |
Nick | 4f5229e | 2016-04-14 16:43:22 +0300 | [diff] [blame^] | 1307 | {$IF CompilerVersion < 23.0} |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 1308 | retval := recv( FTcpClient.Handle, pBuf^, DesiredBytes, WinSock.MSG_PEEK); |
Nick | 4f5229e | 2016-04-14 16:43:22 +0300 | [diff] [blame^] | 1309 | {$ELSE} |
| 1310 | retval := recv( FTcpClient.Handle, pBuf^, DesiredBytes, Winapi.WinSock.MSG_PEEK); |
| 1311 | {$IFEND} |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 1312 | if retval <= 0 |
| 1313 | then Exit( TWaitForData.wfd_Error); |
| 1314 | |
Jens Geyer | bcb17bc | 2015-07-17 23:11:14 +0200 | [diff] [blame] | 1315 | // at least we have some data |
| 1316 | bytesReady := Min( retval, DesiredBytes); |
| 1317 | result := TWaitForData.wfd_HaveData; |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 1318 | end; |
| 1319 | |
| 1320 | function TTcpSocketStreamImpl.Read(var buffer: TBytes; offset, count: Integer): Integer; |
| 1321 | var wfd : TWaitForData; |
Jens Geyer | bcb17bc | 2015-07-17 23:11:14 +0200 | [diff] [blame] | 1322 | wsaError, nBytes : Integer; |
| 1323 | pDest : PByte; |
Jens Geyer | 6f6aa8a | 2016-03-10 19:47:12 +0100 | [diff] [blame] | 1324 | msecs : Integer; |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 1325 | begin |
| 1326 | inherited; |
| 1327 | |
Jens Geyer | 6f6aa8a | 2016-03-10 19:47:12 +0100 | [diff] [blame] | 1328 | if FTimeout > 0 |
| 1329 | then msecs := FTimeout |
| 1330 | else msecs := DEFAULT_THRIFT_TIMEOUT; |
| 1331 | |
Jens Geyer | bcb17bc | 2015-07-17 23:11:14 +0200 | [diff] [blame] | 1332 | result := 0; |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 1333 | pDest := Pointer(@buffer[offset]); |
Jens Geyer | bcb17bc | 2015-07-17 23:11:14 +0200 | [diff] [blame] | 1334 | while count > 0 do begin |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 1335 | |
Jens Geyer | bcb17bc | 2015-07-17 23:11:14 +0200 | [diff] [blame] | 1336 | while TRUE do begin |
Jens Geyer | 6f6aa8a | 2016-03-10 19:47:12 +0100 | [diff] [blame] | 1337 | wfd := WaitForData( msecs, pDest, count, wsaError, nBytes); |
Jens Geyer | bcb17bc | 2015-07-17 23:11:14 +0200 | [diff] [blame] | 1338 | case wfd of |
Jens Geyer | 65b1746 | 2016-03-09 00:07:46 +0100 | [diff] [blame] | 1339 | TWaitForData.wfd_Error : Exit; |
Jens Geyer | bcb17bc | 2015-07-17 23:11:14 +0200 | [diff] [blame] | 1340 | TWaitForData.wfd_HaveData : Break; |
| 1341 | TWaitForData.wfd_Timeout : begin |
Jens Geyer | 6f6aa8a | 2016-03-10 19:47:12 +0100 | [diff] [blame] | 1342 | if (FTimeout = 0) |
| 1343 | then Exit |
| 1344 | else begin |
| 1345 | raise TTransportException.Create( TTransportException.TExceptionType.TimedOut, |
| 1346 | SysErrorMessage(Cardinal(wsaError))); |
| 1347 | |
| 1348 | end; |
Jens Geyer | bcb17bc | 2015-07-17 23:11:14 +0200 | [diff] [blame] | 1349 | end; |
| 1350 | else |
| 1351 | ASSERT( FALSE); |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 1352 | end; |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 1353 | end; |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 1354 | |
Jens Geyer | 6f6aa8a | 2016-03-10 19:47:12 +0100 | [diff] [blame] | 1355 | // reduce the timeout once we got data |
| 1356 | if FTimeout > 0 |
| 1357 | then msecs := FTimeout div 10 |
| 1358 | else msecs := DEFAULT_THRIFT_TIMEOUT div 10; |
| 1359 | msecs := Max( msecs, 200); |
| 1360 | |
Jens Geyer | bcb17bc | 2015-07-17 23:11:14 +0200 | [diff] [blame] | 1361 | ASSERT( nBytes <= count); |
| 1362 | nBytes := FTcpClient.ReceiveBuf( pDest^, nBytes); |
| 1363 | Inc( pDest, nBytes); |
| 1364 | Dec( count, nBytes); |
| 1365 | Inc( result, nBytes); |
| 1366 | end; |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 1367 | end; |
| 1368 | |
| 1369 | function TTcpSocketStreamImpl.ToArray: TBytes; |
| 1370 | var |
| 1371 | len : Integer; |
| 1372 | begin |
| 1373 | len := 0; |
| 1374 | if IsOpen then |
| 1375 | begin |
| 1376 | len := FTcpClient.BytesReceived; |
| 1377 | end; |
| 1378 | |
| 1379 | SetLength( Result, len ); |
| 1380 | |
| 1381 | if len > 0 then |
| 1382 | begin |
| 1383 | FTcpClient.ReceiveBuf( Pointer(@Result[0])^, len); |
| 1384 | end; |
| 1385 | end; |
| 1386 | |
| 1387 | procedure TTcpSocketStreamImpl.Write(const buffer: TBytes; offset, count: Integer); |
| 1388 | var bCanWrite, bError : Boolean; |
| 1389 | retval, wsaError : Integer; |
| 1390 | begin |
| 1391 | inherited; |
| 1392 | |
| 1393 | if not FTcpClient.Active |
| 1394 | then raise TTransportException.Create( TTransportException.TExceptionType.NotOpen); |
| 1395 | |
| 1396 | // The select function returns the total number of socket handles that are ready |
| 1397 | // and contained in the fd_set structures, zero if the time limit expired, |
| 1398 | // or SOCKET_ERROR if an error occurred. If the return value is SOCKET_ERROR, |
| 1399 | // WSAGetLastError can be used to retrieve a specific error code. |
| 1400 | retval := Self.Select( nil, @bCanWrite, @bError, FTimeOut, wsaError); |
| 1401 | if retval = SOCKET_ERROR |
| 1402 | then raise TTransportException.Create( TTransportException.TExceptionType.Unknown, |
| 1403 | SysErrorMessage(Cardinal(wsaError))); |
| 1404 | if (retval = 0) |
| 1405 | then raise TTransportException.Create( TTransportException.TExceptionType.TimedOut); |
| 1406 | if bError or not bCanWrite |
| 1407 | then raise TTransportException.Create( TTransportException.TExceptionType.Unknown); |
| 1408 | |
| 1409 | FTcpClient.SendBuf( Pointer(@buffer[offset])^, count); |
| 1410 | end; |
Jens Geyer | 23d6746 | 2015-12-19 11:44:57 +0100 | [diff] [blame] | 1411 | {$ENDIF} |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 1412 | |
| 1413 | {$IF CompilerVersion < 21.0} |
| 1414 | initialization |
| 1415 | begin |
| 1416 | TFramedTransportImpl_Initialize; |
| 1417 | end; |
| 1418 | {$IFEND} |
| 1419 | |
| 1420 | |
| 1421 | end. |