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