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