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