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