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