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 | 17c3ad9 | 2017-09-05 20:31:27 +0200 | [diff] [blame^] | 417 | result := Read( @buf[0], Length(buf), off, len); |
| 418 | end; |
| 419 | |
| 420 | function TTransportImpl.ReadAll(var buf: TBytes; off: Integer; len: Integer): Integer; |
| 421 | begin |
| 422 | result := ReadAll( @buf[0], Length(buf), off, len); |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 423 | end; |
| 424 | |
| 425 | procedure TTransportImpl.Write( const buf: TBytes); |
| 426 | begin |
Jens Geyer | 17c3ad9 | 2017-09-05 20:31:27 +0200 | [diff] [blame^] | 427 | Write( @buf[0], 0, Length(buf)); |
| 428 | end; |
| 429 | |
| 430 | procedure TTransportImpl.Write( const buf: TBytes; off: Integer; len: Integer); |
| 431 | begin |
| 432 | Write( @buf[0], off, len); |
| 433 | end; |
| 434 | |
| 435 | function TTransportImpl.ReadAll(const pBuf : Pointer; const buflen : Integer; off: Integer; len: Integer): Integer; |
| 436 | var ret : Integer; |
| 437 | begin |
| 438 | result := 0; |
| 439 | while result < len do begin |
| 440 | ret := Read( pBuf, buflen, off + result, len - result); |
| 441 | if ret > 0 |
| 442 | then Inc( result, ret) |
| 443 | else raise TTransportExceptionNotOpen.Create( 'Cannot read, Remote side has closed' ); |
| 444 | end; |
| 445 | end; |
| 446 | |
| 447 | procedure TTransportImpl.Write( const pBuf : Pointer; len : Integer); |
| 448 | begin |
| 449 | Self.Write( pBuf, 0, len); |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 450 | end; |
| 451 | |
| 452 | { THTTPClientImpl } |
| 453 | |
| 454 | procedure THTTPClientImpl.Close; |
| 455 | begin |
| 456 | FInputStream := nil; |
| 457 | FOutputStream := nil; |
| 458 | end; |
| 459 | |
| 460 | constructor THTTPClientImpl.Create(const AUri: string); |
| 461 | begin |
| 462 | inherited Create; |
| 463 | FUri := AUri; |
| 464 | FCustomHeaders := TThriftDictionaryImpl<string,string>.Create; |
| 465 | FOutputStream := TThriftStreamAdapterDelphi.Create( TMemoryStream.Create, True); |
| 466 | end; |
| 467 | |
| 468 | function THTTPClientImpl.CreateRequest: IXMLHTTPRequest; |
| 469 | var |
| 470 | pair : TPair<string,string>; |
| 471 | begin |
Jens Geyer | 9f7f11e | 2016-04-14 21:37:11 +0200 | [diff] [blame] | 472 | {$IF CompilerVersion >= 21.0} |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 473 | Result := CoXMLHTTP.Create; |
Jens Geyer | 9f7f11e | 2016-04-14 21:37:11 +0200 | [diff] [blame] | 474 | {$ELSE} |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 475 | Result := CoXMLHTTPRequest.Create; |
Jens Geyer | 9f7f11e | 2016-04-14 21:37:11 +0200 | [diff] [blame] | 476 | {$IFEND} |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 477 | |
| 478 | Result.open('POST', FUri, False, '', ''); |
| 479 | Result.setRequestHeader( 'Content-Type', 'application/x-thrift'); |
| 480 | Result.setRequestHeader( 'Accept', 'application/x-thrift'); |
| 481 | Result.setRequestHeader( 'User-Agent', 'Delphi/IHTTPClient'); |
| 482 | |
Jens Geyer | 9f7f11e | 2016-04-14 21:37:11 +0200 | [diff] [blame] | 483 | for pair in FCustomHeaders do begin |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 484 | Result.setRequestHeader( pair.Key, pair.Value ); |
| 485 | end; |
| 486 | end; |
| 487 | |
| 488 | destructor THTTPClientImpl.Destroy; |
| 489 | begin |
| 490 | Close; |
| 491 | inherited; |
| 492 | end; |
| 493 | |
| 494 | procedure THTTPClientImpl.Flush; |
| 495 | begin |
| 496 | try |
| 497 | SendRequest; |
| 498 | finally |
| 499 | FOutputStream := nil; |
| 500 | FOutputStream := TThriftStreamAdapterDelphi.Create( TMemoryStream.Create, True); |
| 501 | end; |
| 502 | end; |
| 503 | |
| 504 | function THTTPClientImpl.GetConnectionTimeout: Integer; |
| 505 | begin |
| 506 | Result := FConnectionTimeout; |
| 507 | end; |
| 508 | |
| 509 | function THTTPClientImpl.GetCustomHeaders: IThriftDictionary<string,string>; |
| 510 | begin |
| 511 | Result := FCustomHeaders; |
| 512 | end; |
| 513 | |
| 514 | function THTTPClientImpl.GetIsOpen: Boolean; |
| 515 | begin |
| 516 | Result := True; |
| 517 | end; |
| 518 | |
| 519 | function THTTPClientImpl.GetReadTimeout: Integer; |
| 520 | begin |
| 521 | Result := FReadTimeout; |
| 522 | end; |
| 523 | |
| 524 | procedure THTTPClientImpl.Open; |
| 525 | begin |
Jens Geyer | 9f7f11e | 2016-04-14 21:37:11 +0200 | [diff] [blame] | 526 | // nothing to do |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 527 | end; |
| 528 | |
Jens Geyer | 17c3ad9 | 2017-09-05 20:31:27 +0200 | [diff] [blame^] | 529 | 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] | 530 | begin |
Jens Geyer | 6f6aa8a | 2016-03-10 19:47:12 +0100 | [diff] [blame] | 531 | if FInputStream = nil then begin |
Jens Geyer | e0e3240 | 2016-04-20 21:50:48 +0200 | [diff] [blame] | 532 | raise TTransportExceptionNotOpen.Create('No request has been sent'); |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 533 | end; |
Jens Geyer | 6f6aa8a | 2016-03-10 19:47:12 +0100 | [diff] [blame] | 534 | |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 535 | try |
Jens Geyer | 17c3ad9 | 2017-09-05 20:31:27 +0200 | [diff] [blame^] | 536 | Result := FInputStream.Read( pBuf, buflen, off, len) |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 537 | except |
Jens Geyer | 6f6aa8a | 2016-03-10 19:47:12 +0100 | [diff] [blame] | 538 | on E: Exception |
Jens Geyer | e0e3240 | 2016-04-20 21:50:48 +0200 | [diff] [blame] | 539 | do raise TTransportExceptionUnknown.Create(E.Message); |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 540 | end; |
| 541 | end; |
| 542 | |
| 543 | procedure THTTPClientImpl.SendRequest; |
| 544 | var |
| 545 | xmlhttp : IXMLHTTPRequest; |
| 546 | ms : TMemoryStream; |
| 547 | a : TBytes; |
| 548 | len : Integer; |
| 549 | begin |
| 550 | xmlhttp := CreateRequest; |
| 551 | |
| 552 | ms := TMemoryStream.Create; |
| 553 | try |
| 554 | a := FOutputStream.ToArray; |
| 555 | len := Length(a); |
Jens Geyer | 9f7f11e | 2016-04-14 21:37:11 +0200 | [diff] [blame] | 556 | if len > 0 then begin |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 557 | ms.WriteBuffer( Pointer(@a[0])^, len); |
| 558 | end; |
| 559 | ms.Position := 0; |
| 560 | xmlhttp.send( IUnknown( TStreamAdapter.Create( ms, soReference ))); |
| 561 | FInputStream := nil; |
| 562 | FInputStream := TThriftStreamAdapterCOM.Create( IUnknown( xmlhttp.responseStream) as IStream); |
| 563 | finally |
| 564 | ms.Free; |
| 565 | end; |
| 566 | end; |
| 567 | |
| 568 | procedure THTTPClientImpl.SetConnectionTimeout(const Value: Integer); |
| 569 | begin |
| 570 | FConnectionTimeout := Value; |
| 571 | end; |
| 572 | |
| 573 | procedure THTTPClientImpl.SetReadTimeout(const Value: Integer); |
| 574 | begin |
| 575 | FReadTimeout := Value |
| 576 | end; |
| 577 | |
Jens Geyer | 17c3ad9 | 2017-09-05 20:31:27 +0200 | [diff] [blame^] | 578 | procedure THTTPClientImpl.Write( const pBuf : Pointer; off, len : Integer); |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 579 | begin |
Jens Geyer | 17c3ad9 | 2017-09-05 20:31:27 +0200 | [diff] [blame^] | 580 | FOutputStream.Write( pBuf, off, len); |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 581 | end; |
| 582 | |
| 583 | { TTransportException } |
| 584 | |
Jens Geyer | e0e3240 | 2016-04-20 21:50:48 +0200 | [diff] [blame] | 585 | function TTransportException.GetType: TExceptionType; |
| 586 | begin |
| 587 | if Self is TTransportExceptionNotOpen then Result := TExceptionType.NotOpen |
| 588 | else if Self is TTransportExceptionAlreadyOpen then Result := TExceptionType.AlreadyOpen |
| 589 | else if Self is TTransportExceptionTimedOut then Result := TExceptionType.TimedOut |
| 590 | else if Self is TTransportExceptionEndOfFile then Result := TExceptionType.EndOfFile |
| 591 | else if Self is TTransportExceptionBadArgs then Result := TExceptionType.BadArgs |
| 592 | else if Self is TTransportExceptionInterrupted then Result := TExceptionType.Interrupted |
| 593 | else Result := TExceptionType.Unknown; |
| 594 | end; |
| 595 | |
| 596 | constructor TTransportException.HiddenCreate(const Msg: string); |
| 597 | begin |
| 598 | inherited Create(Msg); |
| 599 | end; |
| 600 | |
| 601 | class function TTransportException.Create(AType: TExceptionType): TTransportException; |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 602 | begin |
| 603 | //no inherited; |
Jens Geyer | e0e3240 | 2016-04-20 21:50:48 +0200 | [diff] [blame] | 604 | {$WARN SYMBOL_DEPRECATED OFF} |
| 605 | Result := Create(AType, '') |
| 606 | {$WARN SYMBOL_DEPRECATED DEFAULT} |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 607 | end; |
| 608 | |
Jens Geyer | e0e3240 | 2016-04-20 21:50:48 +0200 | [diff] [blame] | 609 | class function TTransportException.Create(AType: TExceptionType; |
| 610 | const msg: string): TTransportException; |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 611 | begin |
Jens Geyer | e0e3240 | 2016-04-20 21:50:48 +0200 | [diff] [blame] | 612 | case AType of |
| 613 | TExceptionType.NotOpen: Result := TTransportExceptionNotOpen.Create(msg); |
| 614 | TExceptionType.AlreadyOpen: Result := TTransportExceptionAlreadyOpen.Create(msg); |
| 615 | TExceptionType.TimedOut: Result := TTransportExceptionTimedOut.Create(msg); |
| 616 | TExceptionType.EndOfFile: Result := TTransportExceptionEndOfFile.Create(msg); |
| 617 | TExceptionType.BadArgs: Result := TTransportExceptionBadArgs.Create(msg); |
| 618 | TExceptionType.Interrupted: Result := TTransportExceptionInterrupted.Create(msg); |
| 619 | else |
| 620 | Result := TTransportExceptionUnknown.Create(msg); |
| 621 | end; |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 622 | end; |
| 623 | |
Jens Geyer | e0e3240 | 2016-04-20 21:50:48 +0200 | [diff] [blame] | 624 | class function TTransportException.Create(const msg: string): TTransportException; |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 625 | begin |
Jens Geyer | e0e3240 | 2016-04-20 21:50:48 +0200 | [diff] [blame] | 626 | Result := TTransportExceptionUnknown.Create(Msg); |
| 627 | end; |
| 628 | |
| 629 | { TTransportExceptionSpecialized } |
| 630 | |
| 631 | constructor TTransportExceptionSpecialized.Create(const Msg: string); |
| 632 | begin |
| 633 | inherited HiddenCreate(Msg); |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 634 | end; |
| 635 | |
| 636 | { TTransportFactoryImpl } |
| 637 | |
| 638 | function TTransportFactoryImpl.GetTransport( const ATrans: ITransport): ITransport; |
| 639 | begin |
| 640 | Result := ATrans; |
| 641 | end; |
| 642 | |
| 643 | { TServerSocket } |
| 644 | |
Jens Geyer | 23d6746 | 2015-12-19 11:44:57 +0100 | [diff] [blame] | 645 | {$IFDEF OLD_SOCKETS} |
Jens Geyer | bea9bbe | 2016-04-20 00:02:40 +0200 | [diff] [blame] | 646 | constructor TServerSocketImpl.Create( const AServer: TTcpServer; AClientTimeout: Integer); |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 647 | begin |
| 648 | inherited Create; |
| 649 | FServer := AServer; |
| 650 | FClientTimeout := AClientTimeout; |
| 651 | end; |
Jens Geyer | bea9bbe | 2016-04-20 00:02:40 +0200 | [diff] [blame] | 652 | {$ELSE} |
| 653 | constructor TServerSocketImpl.Create( const AServer: TServerSocket; AClientTimeout: Longword); |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 654 | begin |
| 655 | inherited Create; |
Jens Geyer | bea9bbe | 2016-04-20 00:02:40 +0200 | [diff] [blame] | 656 | FServer := AServer; |
| 657 | FServer.RecvTimeout := AClientTimeout; |
| 658 | FServer.SendTimeout := AClientTimeout; |
| 659 | end; |
| 660 | {$ENDIF} |
| 661 | |
| 662 | {$IFDEF OLD_SOCKETS} |
| 663 | constructor TServerSocketImpl.Create(APort, AClientTimeout: Integer; AUseBufferedSockets: Boolean); |
| 664 | {$ELSE} |
| 665 | constructor TServerSocketImpl.Create(APort: Integer; AClientTimeout: Longword; AUseBufferedSockets: Boolean); |
| 666 | {$ENDIF} |
| 667 | begin |
| 668 | inherited Create; |
| 669 | {$IFDEF OLD_SOCKETS} |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 670 | FPort := APort; |
| 671 | FClientTimeout := AClientTimeout; |
Jens Geyer | bea9bbe | 2016-04-20 00:02:40 +0200 | [diff] [blame] | 672 | FServer := TTcpServer.Create( nil ); |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 673 | FServer.BlockMode := bmBlocking; |
Jens Geyer | 9f7f11e | 2016-04-14 21:37:11 +0200 | [diff] [blame] | 674 | {$IF CompilerVersion >= 21.0} |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 675 | FServer.LocalPort := AnsiString( IntToStr( FPort)); |
Jens Geyer | 9f7f11e | 2016-04-14 21:37:11 +0200 | [diff] [blame] | 676 | {$ELSE} |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 677 | FServer.LocalPort := IntToStr( FPort); |
Jens Geyer | 9f7f11e | 2016-04-14 21:37:11 +0200 | [diff] [blame] | 678 | {$IFEND} |
Jens Geyer | bea9bbe | 2016-04-20 00:02:40 +0200 | [diff] [blame] | 679 | {$ELSE} |
| 680 | FServer := TServerSocket.Create(APort, AClientTimeout, AClientTimeout); |
| 681 | {$ENDIF} |
| 682 | FUseBufferedSocket := AUseBufferedSockets; |
| 683 | FOwnsServer := True; |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 684 | end; |
| 685 | |
| 686 | destructor TServerSocketImpl.Destroy; |
| 687 | begin |
| 688 | if FOwnsServer then begin |
| 689 | FServer.Free; |
| 690 | FServer := nil; |
| 691 | end; |
| 692 | inherited; |
| 693 | end; |
| 694 | |
| 695 | function TServerSocketImpl.Accept( const fnAccepting: TProc): ITransport; |
| 696 | var |
Jens Geyer | bea9bbe | 2016-04-20 00:02:40 +0200 | [diff] [blame] | 697 | {$IFDEF OLD_SOCKETS} |
| 698 | client : TCustomIpClient; |
| 699 | {$ELSE} |
| 700 | client: TSocket; |
| 701 | {$ENDIF} |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 702 | trans : IStreamTransport; |
| 703 | begin |
Jens Geyer | 6f6aa8a | 2016-03-10 19:47:12 +0100 | [diff] [blame] | 704 | if FServer = nil then begin |
Jens Geyer | e0e3240 | 2016-04-20 21:50:48 +0200 | [diff] [blame] | 705 | raise TTransportExceptionNotOpen.Create('No underlying server socket.'); |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 706 | end; |
| 707 | |
Jens Geyer | bea9bbe | 2016-04-20 00:02:40 +0200 | [diff] [blame] | 708 | {$IFDEF OLD_SOCKETS} |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 709 | client := nil; |
| 710 | try |
Jens Geyer | bea9bbe | 2016-04-20 00:02:40 +0200 | [diff] [blame] | 711 | client := TCustomIpClient.Create(nil); |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 712 | |
| 713 | if Assigned(fnAccepting) |
| 714 | then fnAccepting(); |
| 715 | |
Jens Geyer | 6f6aa8a | 2016-03-10 19:47:12 +0100 | [diff] [blame] | 716 | if not FServer.Accept( client) then begin |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 717 | client.Free; |
| 718 | Result := nil; |
| 719 | Exit; |
| 720 | end; |
| 721 | |
Jens Geyer | 6f6aa8a | 2016-03-10 19:47:12 +0100 | [diff] [blame] | 722 | if client = nil then begin |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 723 | Result := nil; |
| 724 | Exit; |
| 725 | end; |
| 726 | |
| 727 | trans := TSocketImpl.Create( client, TRUE, FClientTimeout); |
| 728 | client := nil; // trans owns it now |
| 729 | |
| 730 | if FUseBufferedSocket |
| 731 | then result := TBufferedTransportImpl.Create( trans) |
| 732 | else result := trans; |
| 733 | |
| 734 | except |
| 735 | on E: Exception do begin |
| 736 | client.Free; |
Jens Geyer | e0e3240 | 2016-04-20 21:50:48 +0200 | [diff] [blame] | 737 | raise TTransportExceptionUnknown.Create(E.ToString); |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 738 | end; |
| 739 | end; |
Jens Geyer | bea9bbe | 2016-04-20 00:02:40 +0200 | [diff] [blame] | 740 | {$ELSE} |
| 741 | if Assigned(fnAccepting) then |
| 742 | fnAccepting(); |
| 743 | |
| 744 | client := FServer.Accept; |
| 745 | try |
| 746 | trans := TSocketImpl.Create(client, True); |
| 747 | client := nil; |
| 748 | |
| 749 | if FUseBufferedSocket then |
| 750 | Result := TBufferedTransportImpl.Create(trans) |
| 751 | else |
| 752 | Result := trans; |
| 753 | except |
| 754 | client.Free; |
| 755 | raise; |
| 756 | end; |
| 757 | {$ENDIF} |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 758 | end; |
| 759 | |
| 760 | procedure TServerSocketImpl.Listen; |
| 761 | begin |
| 762 | if FServer <> nil then |
| 763 | begin |
Jens Geyer | bea9bbe | 2016-04-20 00:02:40 +0200 | [diff] [blame] | 764 | {$IFDEF OLD_SOCKETS} |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 765 | try |
| 766 | FServer.Active := True; |
| 767 | except |
Jens Geyer | bea9bbe | 2016-04-20 00:02:40 +0200 | [diff] [blame] | 768 | on E: Exception |
Jens Geyer | e0e3240 | 2016-04-20 21:50:48 +0200 | [diff] [blame] | 769 | do raise TTransportExceptionUnknown.Create('Could not accept on listening socket: ' + E.Message); |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 770 | end; |
Jens Geyer | bea9bbe | 2016-04-20 00:02:40 +0200 | [diff] [blame] | 771 | {$ELSE} |
| 772 | FServer.Listen; |
| 773 | {$ENDIF} |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 774 | end; |
| 775 | end; |
| 776 | |
| 777 | procedure TServerSocketImpl.Close; |
| 778 | begin |
Jens Geyer | bea9bbe | 2016-04-20 00:02:40 +0200 | [diff] [blame] | 779 | if FServer <> nil then |
| 780 | {$IFDEF OLD_SOCKETS} |
| 781 | try |
| 782 | FServer.Active := False; |
| 783 | except |
| 784 | on E: Exception |
Jens Geyer | e0e3240 | 2016-04-20 21:50:48 +0200 | [diff] [blame] | 785 | do raise TTransportExceptionUnknown.Create('Error on closing socket : ' + E.Message); |
Jens Geyer | bea9bbe | 2016-04-20 00:02:40 +0200 | [diff] [blame] | 786 | end; |
| 787 | {$ELSE} |
| 788 | FServer.Close; |
| 789 | {$ENDIF} |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 790 | end; |
| 791 | |
| 792 | { TSocket } |
| 793 | |
Jens Geyer | bea9bbe | 2016-04-20 00:02:40 +0200 | [diff] [blame] | 794 | {$IFDEF OLD_SOCKETS} |
| 795 | constructor TSocketImpl.Create( const AClient : TCustomIpClient; aOwnsClient : Boolean; ATimeout: Integer = 0); |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 796 | var stream : IThriftStream; |
| 797 | begin |
| 798 | FClient := AClient; |
| 799 | FTimeout := ATimeout; |
| 800 | FOwnsClient := aOwnsClient; |
| 801 | stream := TTcpSocketStreamImpl.Create( FClient, FTimeout); |
| 802 | inherited Create( stream, stream); |
| 803 | end; |
Jens Geyer | bea9bbe | 2016-04-20 00:02:40 +0200 | [diff] [blame] | 804 | {$ELSE} |
| 805 | constructor TSocketImpl.Create(const AClient: TSocket; aOwnsClient: Boolean); |
| 806 | var stream : IThriftStream; |
| 807 | begin |
| 808 | FClient := AClient; |
| 809 | FTimeout := AClient.RecvTimeout; |
| 810 | FOwnsClient := aOwnsClient; |
| 811 | stream := TTcpSocketStreamImpl.Create(FClient, FTimeout); |
| 812 | inherited Create(stream, stream); |
| 813 | end; |
| 814 | {$ENDIF} |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 815 | |
Jens Geyer | bea9bbe | 2016-04-20 00:02:40 +0200 | [diff] [blame] | 816 | {$IFDEF OLD_SOCKETS} |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 817 | constructor TSocketImpl.Create(const AHost: string; APort, ATimeout: Integer); |
Jens Geyer | bea9bbe | 2016-04-20 00:02:40 +0200 | [diff] [blame] | 818 | {$ELSE} |
| 819 | constructor TSocketImpl.Create(const AHost: string; APort: Integer; ATimeout: Longword); |
| 820 | {$ENDIF} |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 821 | begin |
| 822 | inherited Create(nil,nil); |
| 823 | FHost := AHost; |
| 824 | FPort := APort; |
| 825 | FTimeout := ATimeout; |
| 826 | InitSocket; |
| 827 | end; |
| 828 | |
| 829 | destructor TSocketImpl.Destroy; |
| 830 | begin |
| 831 | if FOwnsClient |
| 832 | then FreeAndNil( FClient); |
| 833 | inherited; |
| 834 | end; |
| 835 | |
| 836 | procedure TSocketImpl.Close; |
| 837 | begin |
| 838 | inherited Close; |
| 839 | if FOwnsClient |
| 840 | then FreeAndNil( FClient); |
| 841 | end; |
| 842 | |
| 843 | function TSocketImpl.GetIsOpen: Boolean; |
| 844 | begin |
Jens Geyer | bea9bbe | 2016-04-20 00:02:40 +0200 | [diff] [blame] | 845 | {$IFDEF OLD_SOCKETS} |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 846 | Result := (FClient <> nil) and FClient.Connected; |
Jens Geyer | bea9bbe | 2016-04-20 00:02:40 +0200 | [diff] [blame] | 847 | {$ELSE} |
| 848 | Result := (FClient <> nil) and FClient.IsOpen |
| 849 | {$ENDIF} |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 850 | end; |
| 851 | |
| 852 | procedure TSocketImpl.InitSocket; |
| 853 | var |
| 854 | stream : IThriftStream; |
| 855 | begin |
| 856 | if FOwnsClient |
| 857 | then FreeAndNil( FClient) |
| 858 | else FClient := nil; |
| 859 | |
Jens Geyer | bea9bbe | 2016-04-20 00:02:40 +0200 | [diff] [blame] | 860 | {$IFDEF OLD_SOCKETS} |
| 861 | FClient := TTcpClient.Create( nil); |
| 862 | {$ELSE} |
| 863 | FClient := TSocket.Create(FHost, FPort); |
| 864 | {$ENDIF} |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 865 | FOwnsClient := True; |
| 866 | |
| 867 | stream := TTcpSocketStreamImpl.Create( FClient, FTimeout); |
| 868 | FInputStream := stream; |
| 869 | FOutputStream := stream; |
| 870 | end; |
| 871 | |
| 872 | procedure TSocketImpl.Open; |
| 873 | begin |
Jens Geyer | 6f6aa8a | 2016-03-10 19:47:12 +0100 | [diff] [blame] | 874 | if IsOpen then begin |
Jens Geyer | e0e3240 | 2016-04-20 21:50:48 +0200 | [diff] [blame] | 875 | raise TTransportExceptionAlreadyOpen.Create('Socket already connected'); |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 876 | end; |
| 877 | |
Jens Geyer | 6f6aa8a | 2016-03-10 19:47:12 +0100 | [diff] [blame] | 878 | if FHost = '' then begin |
Jens Geyer | e0e3240 | 2016-04-20 21:50:48 +0200 | [diff] [blame] | 879 | raise TTransportExceptionNotOpen.Create('Cannot open null host'); |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 880 | end; |
| 881 | |
Jens Geyer | 6f6aa8a | 2016-03-10 19:47:12 +0100 | [diff] [blame] | 882 | if Port <= 0 then begin |
Jens Geyer | e0e3240 | 2016-04-20 21:50:48 +0200 | [diff] [blame] | 883 | raise TTransportExceptionNotOpen.Create('Cannot open without port'); |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 884 | end; |
| 885 | |
Jens Geyer | 6f6aa8a | 2016-03-10 19:47:12 +0100 | [diff] [blame] | 886 | if FClient = nil |
| 887 | then InitSocket; |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 888 | |
Jens Geyer | bea9bbe | 2016-04-20 00:02:40 +0200 | [diff] [blame] | 889 | {$IFDEF OLD_SOCKETS} |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 890 | FClient.RemoteHost := TSocketHost( Host); |
| 891 | FClient.RemotePort := TSocketPort( IntToStr( Port)); |
| 892 | FClient.Connect; |
Jens Geyer | bea9bbe | 2016-04-20 00:02:40 +0200 | [diff] [blame] | 893 | {$ELSE} |
| 894 | FClient.Open; |
| 895 | {$ENDIF} |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 896 | |
| 897 | FInputStream := TTcpSocketStreamImpl.Create( FClient, FTimeout); |
| 898 | FOutputStream := FInputStream; |
| 899 | end; |
| 900 | |
| 901 | { TBufferedStream } |
| 902 | |
| 903 | procedure TBufferedStreamImpl.Close; |
| 904 | begin |
| 905 | Flush; |
| 906 | FStream := nil; |
| 907 | |
| 908 | FReadBuffer.Free; |
| 909 | FReadBuffer := nil; |
| 910 | |
| 911 | FWriteBuffer.Free; |
| 912 | FWriteBuffer := nil; |
| 913 | end; |
| 914 | |
| 915 | constructor TBufferedStreamImpl.Create( const AStream: IThriftStream; ABufSize: Integer); |
| 916 | begin |
| 917 | inherited Create; |
| 918 | FStream := AStream; |
| 919 | FBufSize := ABufSize; |
| 920 | FReadBuffer := TMemoryStream.Create; |
| 921 | FWriteBuffer := TMemoryStream.Create; |
| 922 | end; |
| 923 | |
| 924 | destructor TBufferedStreamImpl.Destroy; |
| 925 | begin |
| 926 | Close; |
| 927 | inherited; |
| 928 | end; |
| 929 | |
| 930 | procedure TBufferedStreamImpl.Flush; |
| 931 | var |
| 932 | buf : TBytes; |
| 933 | len : Integer; |
| 934 | begin |
Jens Geyer | 9f7f11e | 2016-04-14 21:37:11 +0200 | [diff] [blame] | 935 | if IsOpen then begin |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 936 | len := FWriteBuffer.Size; |
Jens Geyer | 9f7f11e | 2016-04-14 21:37:11 +0200 | [diff] [blame] | 937 | if len > 0 then begin |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 938 | SetLength( buf, len ); |
| 939 | FWriteBuffer.Position := 0; |
| 940 | FWriteBuffer.Read( Pointer(@buf[0])^, len ); |
| 941 | FStream.Write( buf, 0, len ); |
| 942 | end; |
| 943 | FWriteBuffer.Clear; |
| 944 | end; |
| 945 | end; |
| 946 | |
| 947 | function TBufferedStreamImpl.IsOpen: Boolean; |
| 948 | begin |
| 949 | Result := (FWriteBuffer <> nil) |
| 950 | and (FReadBuffer <> nil) |
| 951 | and (FStream <> nil); |
| 952 | end; |
| 953 | |
| 954 | procedure TBufferedStreamImpl.Open; |
| 955 | begin |
Jens Geyer | 9f7f11e | 2016-04-14 21:37:11 +0200 | [diff] [blame] | 956 | // nothing to do |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 957 | end; |
| 958 | |
Jens Geyer | 17c3ad9 | 2017-09-05 20:31:27 +0200 | [diff] [blame^] | 959 | 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] | 960 | var |
| 961 | nRead : Integer; |
| 962 | tempbuf : TBytes; |
| 963 | begin |
| 964 | inherited; |
| 965 | Result := 0; |
Jens Geyer | 9f7f11e | 2016-04-14 21:37:11 +0200 | [diff] [blame] | 966 | |
| 967 | if IsOpen then begin |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 968 | while count > 0 do begin |
| 969 | |
Jens Geyer | 9f7f11e | 2016-04-14 21:37:11 +0200 | [diff] [blame] | 970 | if FReadBuffer.Position >= FReadBuffer.Size then begin |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 971 | FReadBuffer.Clear; |
| 972 | SetLength( tempbuf, FBufSize); |
| 973 | nRead := FStream.Read( tempbuf, 0, FBufSize ); |
| 974 | if nRead = 0 then Break; // avoid infinite loop |
| 975 | |
| 976 | FReadBuffer.WriteBuffer( Pointer(@tempbuf[0])^, nRead ); |
| 977 | FReadBuffer.Position := 0; |
| 978 | end; |
| 979 | |
Jens Geyer | 9f7f11e | 2016-04-14 21:37:11 +0200 | [diff] [blame] | 980 | if FReadBuffer.Position < FReadBuffer.Size then begin |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 981 | nRead := Min( FReadBuffer.Size - FReadBuffer.Position, count); |
Jens Geyer | 17c3ad9 | 2017-09-05 20:31:27 +0200 | [diff] [blame^] | 982 | Inc( Result, FReadBuffer.Read( PByteArray(pBuf)^[offset], nRead)); |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 983 | Dec( count, nRead); |
| 984 | Inc( offset, nRead); |
| 985 | end; |
| 986 | end; |
| 987 | end; |
| 988 | end; |
| 989 | |
| 990 | function TBufferedStreamImpl.ToArray: TBytes; |
Jens Geyer | 9f7f11e | 2016-04-14 21:37:11 +0200 | [diff] [blame] | 991 | var len : Integer; |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 992 | begin |
| 993 | len := 0; |
| 994 | |
Jens Geyer | 9f7f11e | 2016-04-14 21:37:11 +0200 | [diff] [blame] | 995 | if IsOpen then begin |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 996 | len := FReadBuffer.Size; |
| 997 | end; |
| 998 | |
| 999 | SetLength( Result, len); |
| 1000 | |
Jens Geyer | 9f7f11e | 2016-04-14 21:37:11 +0200 | [diff] [blame] | 1001 | if len > 0 then begin |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 1002 | FReadBuffer.Position := 0; |
| 1003 | FReadBuffer.Read( Pointer(@Result[0])^, len ); |
| 1004 | end; |
| 1005 | end; |
| 1006 | |
Jens Geyer | 17c3ad9 | 2017-09-05 20:31:27 +0200 | [diff] [blame^] | 1007 | procedure TBufferedStreamImpl.Write( const pBuf : Pointer; offset: Integer; count: Integer); |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 1008 | begin |
| 1009 | inherited; |
Jens Geyer | 9f7f11e | 2016-04-14 21:37:11 +0200 | [diff] [blame] | 1010 | if count > 0 then begin |
| 1011 | if IsOpen then begin |
Jens Geyer | 17c3ad9 | 2017-09-05 20:31:27 +0200 | [diff] [blame^] | 1012 | FWriteBuffer.Write( PByteArray(pBuf)^[offset], count ); |
Jens Geyer | 9f7f11e | 2016-04-14 21:37:11 +0200 | [diff] [blame] | 1013 | if FWriteBuffer.Size > FBufSize then begin |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 1014 | Flush; |
| 1015 | end; |
| 1016 | end; |
| 1017 | end; |
| 1018 | end; |
| 1019 | |
| 1020 | { TStreamTransportImpl } |
| 1021 | |
| 1022 | procedure TStreamTransportImpl.Close; |
| 1023 | begin |
Jens Geyer | 6f6aa8a | 2016-03-10 19:47:12 +0100 | [diff] [blame] | 1024 | FInputStream := nil; |
| 1025 | FOutputStream := nil; |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 1026 | end; |
| 1027 | |
| 1028 | constructor TStreamTransportImpl.Create( const AInputStream : IThriftStream; const AOutputStream : IThriftStream); |
| 1029 | begin |
| 1030 | inherited Create; |
| 1031 | FInputStream := AInputStream; |
| 1032 | FOutputStream := AOutputStream; |
| 1033 | end; |
| 1034 | |
| 1035 | destructor TStreamTransportImpl.Destroy; |
| 1036 | begin |
| 1037 | FInputStream := nil; |
| 1038 | FOutputStream := nil; |
| 1039 | inherited; |
| 1040 | end; |
| 1041 | |
| 1042 | procedure TStreamTransportImpl.Flush; |
| 1043 | begin |
Jens Geyer | 6f6aa8a | 2016-03-10 19:47:12 +0100 | [diff] [blame] | 1044 | if FOutputStream = nil then begin |
Jens Geyer | e0e3240 | 2016-04-20 21:50:48 +0200 | [diff] [blame] | 1045 | raise TTransportExceptionNotOpen.Create('Cannot flush null outputstream' ); |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 1046 | end; |
| 1047 | |
| 1048 | FOutputStream.Flush; |
| 1049 | end; |
| 1050 | |
| 1051 | function TStreamTransportImpl.GetInputStream: IThriftStream; |
| 1052 | begin |
| 1053 | Result := FInputStream; |
| 1054 | end; |
| 1055 | |
| 1056 | function TStreamTransportImpl.GetIsOpen: Boolean; |
| 1057 | begin |
| 1058 | Result := True; |
| 1059 | end; |
| 1060 | |
| 1061 | function TStreamTransportImpl.GetOutputStream: IThriftStream; |
| 1062 | begin |
| 1063 | Result := FInputStream; |
| 1064 | end; |
| 1065 | |
| 1066 | procedure TStreamTransportImpl.Open; |
| 1067 | begin |
| 1068 | |
| 1069 | end; |
| 1070 | |
Jens Geyer | 17c3ad9 | 2017-09-05 20:31:27 +0200 | [diff] [blame^] | 1071 | 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] | 1072 | begin |
Jens Geyer | 6f6aa8a | 2016-03-10 19:47:12 +0100 | [diff] [blame] | 1073 | if FInputStream = nil then begin |
Jens Geyer | e0e3240 | 2016-04-20 21:50:48 +0200 | [diff] [blame] | 1074 | raise TTransportExceptionNotOpen.Create('Cannot read from null inputstream' ); |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 1075 | end; |
Jens Geyer | 6f6aa8a | 2016-03-10 19:47:12 +0100 | [diff] [blame] | 1076 | |
Jens Geyer | 17c3ad9 | 2017-09-05 20:31:27 +0200 | [diff] [blame^] | 1077 | Result := FInputStream.Read( pBuf,buflen, off, len ); |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 1078 | end; |
| 1079 | |
Jens Geyer | 17c3ad9 | 2017-09-05 20:31:27 +0200 | [diff] [blame^] | 1080 | procedure TStreamTransportImpl.Write( const pBuf : Pointer; off, len : Integer); |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 1081 | begin |
Jens Geyer | 6f6aa8a | 2016-03-10 19:47:12 +0100 | [diff] [blame] | 1082 | if FOutputStream = nil then begin |
Jens Geyer | e0e3240 | 2016-04-20 21:50:48 +0200 | [diff] [blame] | 1083 | raise TTransportExceptionNotOpen.Create('Cannot write to null outputstream' ); |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 1084 | end; |
| 1085 | |
Jens Geyer | 17c3ad9 | 2017-09-05 20:31:27 +0200 | [diff] [blame^] | 1086 | FOutputStream.Write( pBuf, off, len ); |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 1087 | end; |
| 1088 | |
| 1089 | { TBufferedTransportImpl } |
| 1090 | |
| 1091 | constructor TBufferedTransportImpl.Create( const ATransport: IStreamTransport); |
| 1092 | begin |
| 1093 | //no inherited; |
| 1094 | Create( ATransport, 1024 ); |
| 1095 | end; |
| 1096 | |
| 1097 | procedure TBufferedTransportImpl.Close; |
| 1098 | begin |
| 1099 | FTransport.Close; |
| 1100 | end; |
| 1101 | |
Jens Geyer | 9f7f11e | 2016-04-14 21:37:11 +0200 | [diff] [blame] | 1102 | constructor TBufferedTransportImpl.Create( const ATransport: IStreamTransport; ABufSize: Integer); |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 1103 | begin |
| 1104 | inherited Create; |
| 1105 | FTransport := ATransport; |
| 1106 | FBufSize := ABufSize; |
| 1107 | InitBuffers; |
| 1108 | end; |
| 1109 | |
| 1110 | procedure TBufferedTransportImpl.Flush; |
| 1111 | begin |
Jens Geyer | 9f7f11e | 2016-04-14 21:37:11 +0200 | [diff] [blame] | 1112 | if FOutputBuffer <> nil then begin |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 1113 | FOutputBuffer.Flush; |
| 1114 | end; |
| 1115 | end; |
| 1116 | |
| 1117 | function TBufferedTransportImpl.GetIsOpen: Boolean; |
| 1118 | begin |
| 1119 | Result := FTransport.IsOpen; |
| 1120 | end; |
| 1121 | |
| 1122 | function TBufferedTransportImpl.GetUnderlyingTransport: ITransport; |
| 1123 | begin |
| 1124 | Result := FTransport; |
| 1125 | end; |
| 1126 | |
| 1127 | procedure TBufferedTransportImpl.InitBuffers; |
| 1128 | begin |
Jens Geyer | 9f7f11e | 2016-04-14 21:37:11 +0200 | [diff] [blame] | 1129 | if FTransport.InputStream <> nil then begin |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 1130 | FInputBuffer := TBufferedStreamImpl.Create( FTransport.InputStream, FBufSize ); |
| 1131 | end; |
Jens Geyer | 9f7f11e | 2016-04-14 21:37:11 +0200 | [diff] [blame] | 1132 | if FTransport.OutputStream <> nil then begin |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 1133 | FOutputBuffer := TBufferedStreamImpl.Create( FTransport.OutputStream, FBufSize ); |
| 1134 | end; |
| 1135 | end; |
| 1136 | |
| 1137 | procedure TBufferedTransportImpl.Open; |
| 1138 | begin |
| 1139 | FTransport.Open |
| 1140 | end; |
| 1141 | |
Jens Geyer | 17c3ad9 | 2017-09-05 20:31:27 +0200 | [diff] [blame^] | 1142 | 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] | 1143 | begin |
| 1144 | Result := 0; |
Jens Geyer | 9f7f11e | 2016-04-14 21:37:11 +0200 | [diff] [blame] | 1145 | if FInputBuffer <> nil then begin |
Jens Geyer | 17c3ad9 | 2017-09-05 20:31:27 +0200 | [diff] [blame^] | 1146 | Result := FInputBuffer.Read( pBuf,buflen, off, len ); |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 1147 | end; |
| 1148 | end; |
| 1149 | |
Jens Geyer | 17c3ad9 | 2017-09-05 20:31:27 +0200 | [diff] [blame^] | 1150 | procedure TBufferedTransportImpl.Write( const pBuf : Pointer; off, len : Integer); |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 1151 | begin |
Jens Geyer | 9f7f11e | 2016-04-14 21:37:11 +0200 | [diff] [blame] | 1152 | if FOutputBuffer <> nil then begin |
Jens Geyer | 17c3ad9 | 2017-09-05 20:31:27 +0200 | [diff] [blame^] | 1153 | FOutputBuffer.Write( pBuf, off, len ); |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 1154 | end; |
| 1155 | end; |
| 1156 | |
| 1157 | { TFramedTransportImpl } |
| 1158 | |
Jens Geyer | 9f7f11e | 2016-04-14 21:37:11 +0200 | [diff] [blame] | 1159 | {$IFDEF HAVE_CLASS_CTOR} |
| 1160 | class constructor TFramedTransportImpl.Create; |
| 1161 | begin |
| 1162 | SetLength( FHeader_Dummy, FHeaderSize); |
| 1163 | FillChar( FHeader_Dummy[0], Length( FHeader_Dummy) * SizeOf( Byte ), 0); |
| 1164 | end; |
| 1165 | {$ELSE} |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 1166 | procedure TFramedTransportImpl_Initialize; |
| 1167 | begin |
| 1168 | SetLength( TFramedTransportImpl.FHeader_Dummy, TFramedTransportImpl.FHeaderSize); |
| 1169 | FillChar( TFramedTransportImpl.FHeader_Dummy[0], |
| 1170 | Length( TFramedTransportImpl.FHeader_Dummy) * SizeOf( Byte ), 0); |
| 1171 | end; |
Jens Geyer | 9f7f11e | 2016-04-14 21:37:11 +0200 | [diff] [blame] | 1172 | {$ENDIF} |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 1173 | |
| 1174 | constructor TFramedTransportImpl.Create; |
| 1175 | begin |
| 1176 | inherited Create; |
| 1177 | InitWriteBuffer; |
| 1178 | end; |
| 1179 | |
| 1180 | procedure TFramedTransportImpl.Close; |
| 1181 | begin |
| 1182 | FTransport.Close; |
| 1183 | end; |
| 1184 | |
| 1185 | constructor TFramedTransportImpl.Create( const ATrans: ITransport); |
| 1186 | begin |
| 1187 | inherited Create; |
| 1188 | InitWriteBuffer; |
| 1189 | FTransport := ATrans; |
| 1190 | end; |
| 1191 | |
| 1192 | destructor TFramedTransportImpl.Destroy; |
| 1193 | begin |
| 1194 | FWriteBuffer.Free; |
| 1195 | FReadBuffer.Free; |
| 1196 | inherited; |
| 1197 | end; |
| 1198 | |
| 1199 | procedure TFramedTransportImpl.Flush; |
| 1200 | var |
| 1201 | buf : TBytes; |
| 1202 | len : Integer; |
| 1203 | data_len : Integer; |
| 1204 | |
| 1205 | begin |
| 1206 | len := FWriteBuffer.Size; |
| 1207 | SetLength( buf, len); |
Jens Geyer | 9f7f11e | 2016-04-14 21:37:11 +0200 | [diff] [blame] | 1208 | if len > 0 then begin |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 1209 | System.Move( FWriteBuffer.Memory^, buf[0], len ); |
| 1210 | end; |
| 1211 | |
| 1212 | data_len := len - FHeaderSize; |
Jens Geyer | 30ed90e | 2016-03-10 20:12:49 +0100 | [diff] [blame] | 1213 | if (data_len < 0) then begin |
Jens Geyer | e0e3240 | 2016-04-20 21:50:48 +0200 | [diff] [blame] | 1214 | raise TTransportExceptionUnknown.Create('TFramedTransport.Flush: data_len < 0' ); |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 1215 | end; |
| 1216 | |
| 1217 | InitWriteBuffer; |
| 1218 | |
| 1219 | buf[0] := Byte($FF and (data_len shr 24)); |
| 1220 | buf[1] := Byte($FF and (data_len shr 16)); |
| 1221 | buf[2] := Byte($FF and (data_len shr 8)); |
| 1222 | buf[3] := Byte($FF and data_len); |
| 1223 | |
| 1224 | FTransport.Write( buf, 0, len ); |
| 1225 | FTransport.Flush; |
| 1226 | end; |
| 1227 | |
| 1228 | function TFramedTransportImpl.GetIsOpen: Boolean; |
| 1229 | begin |
| 1230 | Result := FTransport.IsOpen; |
| 1231 | end; |
| 1232 | |
| 1233 | type |
| 1234 | TAccessMemoryStream = class(TMemoryStream) |
| 1235 | end; |
| 1236 | |
| 1237 | procedure TFramedTransportImpl.InitWriteBuffer; |
| 1238 | begin |
| 1239 | FWriteBuffer.Free; |
| 1240 | FWriteBuffer := TMemoryStream.Create; |
| 1241 | TAccessMemoryStream(FWriteBuffer).Capacity := 1024; |
| 1242 | FWriteBuffer.Write( Pointer(@FHeader_Dummy[0])^, FHeaderSize); |
| 1243 | end; |
| 1244 | |
| 1245 | procedure TFramedTransportImpl.Open; |
| 1246 | begin |
| 1247 | FTransport.Open; |
| 1248 | end; |
| 1249 | |
Jens Geyer | 17c3ad9 | 2017-09-05 20:31:27 +0200 | [diff] [blame^] | 1250 | function TFramedTransportImpl.Read( const pBuf : Pointer; const buflen : Integer; off: Integer; len: Integer): Integer; |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 1251 | begin |
Jens Geyer | 17c3ad9 | 2017-09-05 20:31:27 +0200 | [diff] [blame^] | 1252 | if len > (buflen-off) |
| 1253 | then len := buflen-off; |
| 1254 | |
| 1255 | if (FReadBuffer <> nil) and (len > 0) then begin |
| 1256 | result := FReadBuffer.Read( PByteArray(pBuf)^[off], len); |
| 1257 | if result > 0 then begin |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 1258 | Exit; |
| 1259 | end; |
| 1260 | end; |
| 1261 | |
| 1262 | ReadFrame; |
| 1263 | if len > 0 |
Jens Geyer | 17c3ad9 | 2017-09-05 20:31:27 +0200 | [diff] [blame^] | 1264 | then Result := FReadBuffer.Read( PByteArray(pBuf)^[off], len) |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 1265 | else Result := 0; |
| 1266 | end; |
| 1267 | |
| 1268 | procedure TFramedTransportImpl.ReadFrame; |
| 1269 | var |
| 1270 | i32rd : TBytes; |
| 1271 | size : Integer; |
| 1272 | buff : TBytes; |
| 1273 | begin |
| 1274 | SetLength( i32rd, FHeaderSize ); |
| 1275 | FTransport.ReadAll( i32rd, 0, FHeaderSize); |
| 1276 | size := |
| 1277 | ((i32rd[0] and $FF) shl 24) or |
| 1278 | ((i32rd[1] and $FF) shl 16) or |
| 1279 | ((i32rd[2] and $FF) shl 8) or |
| 1280 | (i32rd[3] and $FF); |
| 1281 | SetLength( buff, size ); |
| 1282 | FTransport.ReadAll( buff, 0, size ); |
| 1283 | FReadBuffer.Free; |
| 1284 | FReadBuffer := TMemoryStream.Create; |
| 1285 | FReadBuffer.Write( Pointer(@buff[0])^, size ); |
| 1286 | FReadBuffer.Position := 0; |
| 1287 | end; |
| 1288 | |
Jens Geyer | 17c3ad9 | 2017-09-05 20:31:27 +0200 | [diff] [blame^] | 1289 | procedure TFramedTransportImpl.Write( const pBuf : Pointer; off, len : Integer); |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 1290 | begin |
| 1291 | if len > 0 |
Jens Geyer | 17c3ad9 | 2017-09-05 20:31:27 +0200 | [diff] [blame^] | 1292 | then FWriteBuffer.Write( PByteArray(pBuf)^[off], len ); |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 1293 | end; |
| 1294 | |
| 1295 | { TFramedTransport.TFactory } |
| 1296 | |
| 1297 | function TFramedTransportImpl.TFactory.GetTransport( const ATrans: ITransport): ITransport; |
| 1298 | begin |
| 1299 | Result := TFramedTransportImpl.Create( ATrans ); |
| 1300 | end; |
| 1301 | |
| 1302 | { TTcpSocketStreamImpl } |
| 1303 | |
| 1304 | procedure TTcpSocketStreamImpl.Close; |
| 1305 | begin |
| 1306 | FTcpClient.Close; |
| 1307 | end; |
| 1308 | |
Jens Geyer | bea9bbe | 2016-04-20 00:02:40 +0200 | [diff] [blame] | 1309 | {$IFDEF OLD_SOCKETS} |
| 1310 | constructor TTcpSocketStreamImpl.Create( const ATcpClient: TCustomIpClient; const aTimeout : Integer); |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 1311 | begin |
| 1312 | inherited Create; |
| 1313 | FTcpClient := ATcpClient; |
| 1314 | FTimeout := aTimeout; |
| 1315 | end; |
Jens Geyer | bea9bbe | 2016-04-20 00:02:40 +0200 | [diff] [blame] | 1316 | {$ELSE} |
| 1317 | constructor TTcpSocketStreamImpl.Create( const ATcpClient: TSocket; const aTimeout : Longword); |
| 1318 | begin |
| 1319 | inherited Create; |
| 1320 | FTcpClient := ATcpClient; |
| 1321 | if aTimeout = 0 then |
| 1322 | FTcpClient.RecvTimeout := SLEEP_TIME |
| 1323 | else |
| 1324 | FTcpClient.RecvTimeout := aTimeout; |
| 1325 | FTcpClient.SendTimeout := aTimeout; |
| 1326 | end; |
| 1327 | {$ENDIF} |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 1328 | |
| 1329 | procedure TTcpSocketStreamImpl.Flush; |
| 1330 | begin |
| 1331 | |
| 1332 | end; |
| 1333 | |
| 1334 | function TTcpSocketStreamImpl.IsOpen: Boolean; |
| 1335 | begin |
Jens Geyer | bea9bbe | 2016-04-20 00:02:40 +0200 | [diff] [blame] | 1336 | {$IFDEF OLD_SOCKETS} |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 1337 | Result := FTcpClient.Active; |
Jens Geyer | bea9bbe | 2016-04-20 00:02:40 +0200 | [diff] [blame] | 1338 | {$ELSE} |
| 1339 | Result := FTcpClient.IsOpen; |
| 1340 | {$ENDIF} |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 1341 | end; |
| 1342 | |
| 1343 | procedure TTcpSocketStreamImpl.Open; |
| 1344 | begin |
| 1345 | FTcpClient.Open; |
| 1346 | end; |
| 1347 | |
| 1348 | |
Jens Geyer | bea9bbe | 2016-04-20 00:02:40 +0200 | [diff] [blame] | 1349 | {$IFDEF OLD_SOCKETS} |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 1350 | function TTcpSocketStreamImpl.Select( ReadReady, WriteReady, ExceptFlag: PBoolean; |
| 1351 | TimeOut: Integer; var wsaError : Integer): Integer; |
| 1352 | var |
| 1353 | ReadFds: TFDset; |
| 1354 | ReadFdsptr: PFDset; |
| 1355 | WriteFds: TFDset; |
| 1356 | WriteFdsptr: PFDset; |
| 1357 | ExceptFds: TFDset; |
| 1358 | ExceptFdsptr: PFDset; |
| 1359 | tv: timeval; |
| 1360 | Timeptr: PTimeval; |
| 1361 | socket : TSocket; |
| 1362 | begin |
| 1363 | if not FTcpClient.Active then begin |
| 1364 | wsaError := WSAEINVAL; |
| 1365 | Exit( SOCKET_ERROR); |
| 1366 | end; |
| 1367 | |
| 1368 | socket := FTcpClient.Handle; |
| 1369 | |
Jens Geyer | 9f7f11e | 2016-04-14 21:37:11 +0200 | [diff] [blame] | 1370 | if Assigned(ReadReady) then begin |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 1371 | ReadFdsptr := @ReadFds; |
| 1372 | FD_ZERO(ReadFds); |
| 1373 | FD_SET(socket, ReadFds); |
| 1374 | end |
Jens Geyer | 9f7f11e | 2016-04-14 21:37:11 +0200 | [diff] [blame] | 1375 | else begin |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 1376 | ReadFdsptr := nil; |
Jens Geyer | 9f7f11e | 2016-04-14 21:37:11 +0200 | [diff] [blame] | 1377 | end; |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 1378 | |
Jens Geyer | 9f7f11e | 2016-04-14 21:37:11 +0200 | [diff] [blame] | 1379 | if Assigned(WriteReady) then begin |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 1380 | WriteFdsptr := @WriteFds; |
| 1381 | FD_ZERO(WriteFds); |
| 1382 | FD_SET(socket, WriteFds); |
| 1383 | end |
Jens Geyer | 9f7f11e | 2016-04-14 21:37:11 +0200 | [diff] [blame] | 1384 | else begin |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 1385 | WriteFdsptr := nil; |
Jens Geyer | 9f7f11e | 2016-04-14 21:37:11 +0200 | [diff] [blame] | 1386 | end; |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 1387 | |
Jens Geyer | 9f7f11e | 2016-04-14 21:37:11 +0200 | [diff] [blame] | 1388 | if Assigned(ExceptFlag) then begin |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 1389 | ExceptFdsptr := @ExceptFds; |
| 1390 | FD_ZERO(ExceptFds); |
| 1391 | FD_SET(socket, ExceptFds); |
| 1392 | end |
Jens Geyer | 9f7f11e | 2016-04-14 21:37:11 +0200 | [diff] [blame] | 1393 | else begin |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 1394 | ExceptFdsptr := nil; |
Jens Geyer | 9f7f11e | 2016-04-14 21:37:11 +0200 | [diff] [blame] | 1395 | end; |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 1396 | |
Jens Geyer | 9f7f11e | 2016-04-14 21:37:11 +0200 | [diff] [blame] | 1397 | if TimeOut >= 0 then begin |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 1398 | tv.tv_sec := TimeOut div 1000; |
| 1399 | tv.tv_usec := 1000 * (TimeOut mod 1000); |
| 1400 | Timeptr := @tv; |
| 1401 | end |
Jens Geyer | 9f7f11e | 2016-04-14 21:37:11 +0200 | [diff] [blame] | 1402 | else begin |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 1403 | Timeptr := nil; // wait forever |
Jens Geyer | 9f7f11e | 2016-04-14 21:37:11 +0200 | [diff] [blame] | 1404 | end; |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 1405 | |
| 1406 | wsaError := 0; |
| 1407 | try |
Jens Geyer | 9f7f11e | 2016-04-14 21:37:11 +0200 | [diff] [blame] | 1408 | {$IFDEF MSWINDOWS} |
| 1409 | {$IFDEF OLD_UNIT_NAMES} |
| 1410 | result := WinSock.select( socket + 1, ReadFdsptr, WriteFdsptr, ExceptFdsptr, Timeptr); |
| 1411 | {$ELSE} |
| 1412 | result := Winapi.WinSock.select( socket + 1, ReadFdsptr, WriteFdsptr, ExceptFdsptr, Timeptr); |
| 1413 | {$ENDIF} |
| 1414 | {$ENDIF} |
| 1415 | {$IFDEF LINUX} |
| 1416 | result := Libc.select( socket + 1, ReadFdsptr, WriteFdsptr, ExceptFdsptr, Timeptr); |
| 1417 | {$ENDIF} |
| 1418 | |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 1419 | if result = SOCKET_ERROR |
| 1420 | then wsaError := WSAGetLastError; |
| 1421 | |
| 1422 | except |
| 1423 | result := SOCKET_ERROR; |
| 1424 | end; |
| 1425 | |
| 1426 | if Assigned(ReadReady) then |
Jens Geyer | 9f7f11e | 2016-04-14 21:37:11 +0200 | [diff] [blame] | 1427 | ReadReady^ := FD_ISSET(socket, ReadFds); |
| 1428 | |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 1429 | if Assigned(WriteReady) then |
| 1430 | WriteReady^ := FD_ISSET(socket, WriteFds); |
Jens Geyer | 9f7f11e | 2016-04-14 21:37:11 +0200 | [diff] [blame] | 1431 | |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 1432 | if Assigned(ExceptFlag) then |
| 1433 | ExceptFlag^ := FD_ISSET(socket, ExceptFds); |
| 1434 | end; |
Jens Geyer | bea9bbe | 2016-04-20 00:02:40 +0200 | [diff] [blame] | 1435 | {$ENDIF} |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 1436 | |
Jens Geyer | bea9bbe | 2016-04-20 00:02:40 +0200 | [diff] [blame] | 1437 | {$IFDEF OLD_SOCKETS} |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 1438 | function TTcpSocketStreamImpl.WaitForData( TimeOut : Integer; pBuf : Pointer; |
| 1439 | DesiredBytes : Integer; |
Jens Geyer | bcb17bc | 2015-07-17 23:11:14 +0200 | [diff] [blame] | 1440 | var wsaError, bytesReady : Integer): TWaitForData; |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 1441 | var bCanRead, bError : Boolean; |
| 1442 | retval : Integer; |
Jens Geyer | 9f7f11e | 2016-04-14 21:37:11 +0200 | [diff] [blame] | 1443 | const |
| 1444 | 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] | 1445 | begin |
Jens Geyer | bcb17bc | 2015-07-17 23:11:14 +0200 | [diff] [blame] | 1446 | bytesReady := 0; |
| 1447 | |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 1448 | // The select function returns the total number of socket handles that are ready |
| 1449 | // and contained in the fd_set structures, zero if the time limit expired, |
| 1450 | // or SOCKET_ERROR if an error occurred. If the return value is SOCKET_ERROR, |
| 1451 | // WSAGetLastError can be used to retrieve a specific error code. |
| 1452 | retval := Self.Select( @bCanRead, nil, @bError, TimeOut, wsaError); |
| 1453 | if retval = SOCKET_ERROR |
| 1454 | then Exit( TWaitForData.wfd_Error); |
| 1455 | if (retval = 0) or not bCanRead |
| 1456 | then Exit( TWaitForData.wfd_Timeout); |
| 1457 | |
| 1458 | // recv() returns the number of bytes received, or -1 if an error occurred. |
| 1459 | // 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] | 1460 | |
| 1461 | retval := recv( FTcpClient.Handle, pBuf^, DesiredBytes, MSG_PEEK); |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 1462 | if retval <= 0 |
| 1463 | then Exit( TWaitForData.wfd_Error); |
| 1464 | |
Jens Geyer | bcb17bc | 2015-07-17 23:11:14 +0200 | [diff] [blame] | 1465 | // at least we have some data |
| 1466 | bytesReady := Min( retval, DesiredBytes); |
| 1467 | result := TWaitForData.wfd_HaveData; |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 1468 | end; |
Jens Geyer | bea9bbe | 2016-04-20 00:02:40 +0200 | [diff] [blame] | 1469 | {$ENDIF} |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 1470 | |
Jens Geyer | bea9bbe | 2016-04-20 00:02:40 +0200 | [diff] [blame] | 1471 | {$IFDEF OLD_SOCKETS} |
Jens Geyer | 17c3ad9 | 2017-09-05 20:31:27 +0200 | [diff] [blame^] | 1472 | 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] | 1473 | // old sockets version |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 1474 | var wfd : TWaitForData; |
Jens Geyer | bea9bbe | 2016-04-20 00:02:40 +0200 | [diff] [blame] | 1475 | wsaError, |
Jens Geyer | 6f6aa8a | 2016-03-10 19:47:12 +0100 | [diff] [blame] | 1476 | msecs : Integer; |
Jens Geyer | bea9bbe | 2016-04-20 00:02:40 +0200 | [diff] [blame] | 1477 | nBytes : Integer; |
| 1478 | pDest : PByte; |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 1479 | begin |
| 1480 | inherited; |
| 1481 | |
Jens Geyer | 6f6aa8a | 2016-03-10 19:47:12 +0100 | [diff] [blame] | 1482 | if FTimeout > 0 |
| 1483 | then msecs := FTimeout |
| 1484 | else msecs := DEFAULT_THRIFT_TIMEOUT; |
| 1485 | |
Jens Geyer | bcb17bc | 2015-07-17 23:11:14 +0200 | [diff] [blame] | 1486 | result := 0; |
Jens Geyer | 17c3ad9 | 2017-09-05 20:31:27 +0200 | [diff] [blame^] | 1487 | pDest := @(PByteArray(pBuf)^[offset]); |
Jens Geyer | bcb17bc | 2015-07-17 23:11:14 +0200 | [diff] [blame] | 1488 | while count > 0 do begin |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 1489 | |
Jens Geyer | bcb17bc | 2015-07-17 23:11:14 +0200 | [diff] [blame] | 1490 | while TRUE do begin |
Jens Geyer | 6f6aa8a | 2016-03-10 19:47:12 +0100 | [diff] [blame] | 1491 | wfd := WaitForData( msecs, pDest, count, wsaError, nBytes); |
Jens Geyer | bcb17bc | 2015-07-17 23:11:14 +0200 | [diff] [blame] | 1492 | case wfd of |
Jens Geyer | 65b1746 | 2016-03-09 00:07:46 +0100 | [diff] [blame] | 1493 | TWaitForData.wfd_Error : Exit; |
Jens Geyer | bcb17bc | 2015-07-17 23:11:14 +0200 | [diff] [blame] | 1494 | TWaitForData.wfd_HaveData : Break; |
| 1495 | TWaitForData.wfd_Timeout : begin |
Jens Geyer | 6f6aa8a | 2016-03-10 19:47:12 +0100 | [diff] [blame] | 1496 | if (FTimeout = 0) |
| 1497 | then Exit |
| 1498 | else begin |
Jens Geyer | e0e3240 | 2016-04-20 21:50:48 +0200 | [diff] [blame] | 1499 | raise TTransportExceptionTimedOut.Create(SysErrorMessage(Cardinal(wsaError))); |
Jens Geyer | 6f6aa8a | 2016-03-10 19:47:12 +0100 | [diff] [blame] | 1500 | |
| 1501 | end; |
Jens Geyer | bcb17bc | 2015-07-17 23:11:14 +0200 | [diff] [blame] | 1502 | end; |
| 1503 | else |
| 1504 | ASSERT( FALSE); |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 1505 | end; |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 1506 | end; |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 1507 | |
Jens Geyer | 6f6aa8a | 2016-03-10 19:47:12 +0100 | [diff] [blame] | 1508 | // reduce the timeout once we got data |
| 1509 | if FTimeout > 0 |
| 1510 | then msecs := FTimeout div 10 |
| 1511 | else msecs := DEFAULT_THRIFT_TIMEOUT div 10; |
| 1512 | msecs := Max( msecs, 200); |
| 1513 | |
Jens Geyer | bcb17bc | 2015-07-17 23:11:14 +0200 | [diff] [blame] | 1514 | ASSERT( nBytes <= count); |
| 1515 | nBytes := FTcpClient.ReceiveBuf( pDest^, nBytes); |
| 1516 | Inc( pDest, nBytes); |
| 1517 | Dec( count, nBytes); |
| 1518 | Inc( result, nBytes); |
| 1519 | end; |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 1520 | end; |
| 1521 | |
| 1522 | function TTcpSocketStreamImpl.ToArray: TBytes; |
Jens Geyer | bea9bbe | 2016-04-20 00:02:40 +0200 | [diff] [blame] | 1523 | // old sockets version |
| 1524 | var len : Integer; |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 1525 | begin |
| 1526 | len := 0; |
Jens Geyer | 9f7f11e | 2016-04-14 21:37:11 +0200 | [diff] [blame] | 1527 | if IsOpen then begin |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 1528 | len := FTcpClient.BytesReceived; |
| 1529 | end; |
| 1530 | |
| 1531 | SetLength( Result, len ); |
| 1532 | |
Jens Geyer | 9f7f11e | 2016-04-14 21:37:11 +0200 | [diff] [blame] | 1533 | if len > 0 then begin |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 1534 | FTcpClient.ReceiveBuf( Pointer(@Result[0])^, len); |
| 1535 | end; |
| 1536 | end; |
| 1537 | |
Jens Geyer | 17c3ad9 | 2017-09-05 20:31:27 +0200 | [diff] [blame^] | 1538 | procedure TTcpSocketStreamImpl.Write( const pBuf : Pointer; offset, count: Integer); |
Jens Geyer | bea9bbe | 2016-04-20 00:02:40 +0200 | [diff] [blame] | 1539 | // old sockets version |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 1540 | var bCanWrite, bError : Boolean; |
| 1541 | retval, wsaError : Integer; |
| 1542 | begin |
| 1543 | inherited; |
| 1544 | |
| 1545 | if not FTcpClient.Active |
Jens Geyer | e0e3240 | 2016-04-20 21:50:48 +0200 | [diff] [blame] | 1546 | then raise TTransportExceptionNotOpen.Create('not open'); |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 1547 | |
| 1548 | // The select function returns the total number of socket handles that are ready |
| 1549 | // and contained in the fd_set structures, zero if the time limit expired, |
| 1550 | // or SOCKET_ERROR if an error occurred. If the return value is SOCKET_ERROR, |
| 1551 | // WSAGetLastError can be used to retrieve a specific error code. |
| 1552 | retval := Self.Select( nil, @bCanWrite, @bError, FTimeOut, wsaError); |
| 1553 | if retval = SOCKET_ERROR |
Jens Geyer | e0e3240 | 2016-04-20 21:50:48 +0200 | [diff] [blame] | 1554 | then raise TTransportExceptionUnknown.Create(SysErrorMessage(Cardinal(wsaError))); |
Jens Geyer | 9f7f11e | 2016-04-14 21:37:11 +0200 | [diff] [blame] | 1555 | |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 1556 | if (retval = 0) |
Jens Geyer | e0e3240 | 2016-04-20 21:50:48 +0200 | [diff] [blame] | 1557 | then raise TTransportExceptionTimedOut.Create('timed out'); |
Jens Geyer | 9f7f11e | 2016-04-14 21:37:11 +0200 | [diff] [blame] | 1558 | |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 1559 | if bError or not bCanWrite |
Jens Geyer | e0e3240 | 2016-04-20 21:50:48 +0200 | [diff] [blame] | 1560 | then raise TTransportExceptionUnknown.Create('unknown error'); |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 1561 | |
Jens Geyer | 17c3ad9 | 2017-09-05 20:31:27 +0200 | [diff] [blame^] | 1562 | FTcpClient.SendBuf( PByteArray(pBuf)^[offset], count); |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 1563 | end; |
Jens Geyer | bea9bbe | 2016-04-20 00:02:40 +0200 | [diff] [blame] | 1564 | |
| 1565 | {$ELSE} |
| 1566 | |
Jens Geyer | 17c3ad9 | 2017-09-05 20:31:27 +0200 | [diff] [blame^] | 1567 | 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] | 1568 | // new sockets version |
| 1569 | var nBytes : Integer; |
| 1570 | pDest : PByte; |
| 1571 | begin |
| 1572 | inherited; |
| 1573 | |
| 1574 | result := 0; |
Jens Geyer | 17c3ad9 | 2017-09-05 20:31:27 +0200 | [diff] [blame^] | 1575 | pDest := @(PByteArray(pBuf)^[offset]); |
Jens Geyer | bea9bbe | 2016-04-20 00:02:40 +0200 | [diff] [blame] | 1576 | while count > 0 do begin |
| 1577 | nBytes := FTcpClient.Read(pDest^, count); |
| 1578 | if nBytes = 0 then Exit; |
| 1579 | Inc( pDest, nBytes); |
| 1580 | Dec( count, nBytes); |
| 1581 | Inc( result, nBytes); |
| 1582 | end; |
| 1583 | end; |
| 1584 | |
| 1585 | function TTcpSocketStreamImpl.ToArray: TBytes; |
| 1586 | // new sockets version |
| 1587 | var len : Integer; |
| 1588 | begin |
| 1589 | len := 0; |
| 1590 | try |
| 1591 | if FTcpClient.Peek then |
| 1592 | repeat |
| 1593 | SetLength(Result, Length(Result) + 1024); |
| 1594 | len := FTcpClient.Read(Result[Length(Result) - 1024], 1024); |
| 1595 | until len < 1024; |
| 1596 | except |
| 1597 | on TTransportException do begin { don't allow default exceptions } end; |
| 1598 | else raise; |
| 1599 | end; |
| 1600 | if len > 0 then |
| 1601 | SetLength(Result, Length(Result) - 1024 + len); |
| 1602 | end; |
| 1603 | |
Jens Geyer | 17c3ad9 | 2017-09-05 20:31:27 +0200 | [diff] [blame^] | 1604 | procedure TTcpSocketStreamImpl.Write( const pBuf : Pointer; offset, count: Integer); |
Jens Geyer | bea9bbe | 2016-04-20 00:02:40 +0200 | [diff] [blame] | 1605 | // new sockets version |
| 1606 | begin |
| 1607 | inherited; |
| 1608 | |
| 1609 | if not FTcpClient.IsOpen |
Kyle Johnson | e363a34 | 2016-04-22 19:11:16 -0500 | [diff] [blame] | 1610 | then raise TTransportExceptionNotOpen.Create('not open'); |
Jens Geyer | bea9bbe | 2016-04-20 00:02:40 +0200 | [diff] [blame] | 1611 | |
Jens Geyer | 17c3ad9 | 2017-09-05 20:31:27 +0200 | [diff] [blame^] | 1612 | FTcpClient.Write( PByteArray(pBuf)^[offset], count); |
Jens Geyer | bea9bbe | 2016-04-20 00:02:40 +0200 | [diff] [blame] | 1613 | end; |
| 1614 | |
Jens Geyer | 23d6746 | 2015-12-19 11:44:57 +0100 | [diff] [blame] | 1615 | {$ENDIF} |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 1616 | |
Jens Geyer | bea9bbe | 2016-04-20 00:02:40 +0200 | [diff] [blame] | 1617 | |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 1618 | {$IF CompilerVersion < 21.0} |
| 1619 | initialization |
| 1620 | begin |
| 1621 | TFramedTransportImpl_Initialize; |
| 1622 | end; |
| 1623 | {$IFEND} |
| 1624 | |
| 1625 | |
| 1626 | end. |