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