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