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