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