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