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