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