Recent

Author Topic: Question re. FCL's ssockets unit, "Buffer" type  (Read 1080 times)

Curt Carpenter

  • Hero Member
  • *****
  • Posts: 699
Question re. FCL's ssockets unit, "Buffer" type
« on: December 14, 2025, 10:34:03 pm »
In the FCL ssockets unit I find two methods in the TSocketStream class:

Code: Pascal  [Select][+][-]
  1.      Function Read (Var Buffer; Count : Longint) : longint; Override;
  2.      Function Write (Const Buffer; Count : Longint) :Longint; Override;    
  3.  
     

I'm trying to understand the type of "Var Buffer" and "Const Buffer" here, and have had no luck tracking it down. Can anyone point me in the right direction?  Is it declared explicitly somewhere that I just haven't found?

ojz0r

  • Jr. Member
  • **
  • Posts: 72
Re: Question re. FCL's ssockets unit, "Buffer" type
« Reply #1 on: December 14, 2025, 11:02:28 pm »
I dont know where its defined, and correct me if im wrong but TInetSocket seems to use the function Read/Write from TSocketStream and that Buffer is a Byte array.
I like big endians and i can not lie.

Thausand

  • Sr. Member
  • ****
  • Posts: 457
Re: Question re. FCL's ssockets unit, "Buffer" type
« Reply #2 on: December 14, 2025, 11:09:00 pm »
I'm trying to understand the type of "Var Buffer" and "Const Buffer" here, and have had no luck tracking it down. Can anyone point me in the right direction?  Is it declared explicitly somewhere that I just haven't found?
Type is no type  :D

That mean can be every thing that want have. But, as write ojz0r when send is byte. Then can be important byte-order of type > byte. https://www.freepascal.org/docs-html/ref/refsu70.html

If look documentation https://www.freepascal.org/daily/packages/fcl-net/ssockets/tsocketstream.html then see is parent THandleStream. Handlestream documentation here https://www.freepascal.org/docs-html/rtl/classes/thandlestream.html,.

Read method have:
Read overrides the Read method of TStream. It uses the Handle property to read the Count bytes into Buffer
If no error occurs while reading, the number of bytes actually read will be returned.

Write method have:
Write overrides the Write method of TStream. It uses the Handle property to write the Count bytes from Buffer.
If no error occurs while writing, the number of bytes actually written will be returned.

If socketstream not have same then is mandatory for write documentation (but documentation not say then is assume byte).
« Last Edit: December 14, 2025, 11:20:48 pm by Thausand »

Curt Carpenter

  • Hero Member
  • *****
  • Posts: 699
Re: Question re. FCL's ssockets unit, "Buffer" type
« Reply #3 on: December 15, 2025, 12:22:45 am »
Thank you for the link and the explanation Thausand!  I did not know that.   I will have to do some experimenting to figure out where to go from here. 

jamie

  • Hero Member
  • *****
  • Posts: 7494
Re: Question re. FCL's ssockets unit, "Buffer" type
« Reply #4 on: December 15, 2025, 09:23:35 pm »
That is what you call an Un-typed parameter which means you can pass just about anything to it but, the body of the function really needs to know what's doing because you can access memory out-side the realm of things.

 SO, for example, I could give it a Byte,Word, Dword, QWord Record, an Object, or just about anything of any size of anytype.

 For this to happen, things are normally passed around via a pointer, especially the VAR version you would need some sort of size indicator what is what the second parameter is for.

I suppose you could of just used a (var data:pointer....)
 
but then, each time you called the function you would need to give it an address reference to the item you are entering.

The compiler does this for you if you use the Un-Typed parameter.

The Const version would be the same except it follows the CONST rules.

Jamie.


The only true wisdom is knowing you know nothing

Khrys

  • Sr. Member
  • ****
  • Posts: 383
Re: Question re. FCL's ssockets unit, "Buffer" type
« Reply #5 on: December 16, 2025, 07:00:32 am »
For those coming from C++, untyped parameters are essentially  void&  (for  var)  or  const void&  (for  const)  parameters (which are forbidden in C++); all you get is a non-Nil  address.

Another strange thing about untyped parameters is that if the buffer you're trying to pass comes as an untyped  Pointer,  you'll have to do what looks like dereferencing a generic/void pointer, which is... weird, to say the least:

Code: Pascal  [Select][+][-]
  1. function memmove(Destination, Source: Pointer; Count: SizeUInt): Pointer; cdecl;
  2. begin
  3.   Move(Source^, Destination^, Count); // The only context where it's possible to do `Pointer^`!
  4.   Result := Destination;
  5. end;

tetrastes

  • Hero Member
  • *****
  • Posts: 734
Re: Question re. FCL's ssockets unit, "Buffer" type
« Reply #6 on: December 17, 2025, 10:48:13 am »
Nothing weird, this is Sparta pascal.  ;)
If you don't dereference pointers passed to Move, they will be pointer to pointer.
And they are not necessarily untyped pointers.

PascalDragon

  • Hero Member
  • *****
  • Posts: 6284
  • Compiler Developer
Re: Question re. FCL's ssockets unit, "Buffer" type
« Reply #7 on: December 18, 2025, 09:28:29 pm »
For those coming from C++, untyped parameters are essentially  void&  (for  var)  or  const void&  (for  const)  parameters (which are forbidden in C++); all you get is a non-Nil  address.

Well... it is possible to pass a Nil-value ;)

Code: Pascal  [Select][+][-]
  1. SomeProc(PLongInt(Nil)^)

Yes, that is supported.

440bx

  • Hero Member
  • *****
  • Posts: 6029
Re: Question re. FCL's ssockets unit, "Buffer" type
« Reply #8 on: December 18, 2025, 10:10:38 pm »
For those coming from C++, untyped parameters are essentially  void&  (for  var)  or  const void&  (for  const)  parameters (which are forbidden in C++); all you get is a non-Nil  address.

Well... it is possible to pass a Nil-value ;)

Code: Pascal  [Select][+][-]
  1. SomeProc(PLongInt(Nil)^)

Yes, that is supported.
Which is unfortunate because one of the functional purposes of "var" is to ensure a nil value cannot be passed and the above "trick" violates that.
FPC v3.2.2 and Lazarus v4.0rc3 on Windows 7 SP1 64bit.

creaothceann

  • Sr. Member
  • ****
  • Posts: 251
Re: Question re. FCL's ssockets unit, "Buffer" type
« Reply #9 on: December 19, 2025, 01:08:56 am »
That trick can also be used to suppress compiler warnings, for example when clearing stack variables with FillChar.
« Last Edit: December 19, 2025, 10:09:02 am by creaothceann »

Khrys

  • Sr. Member
  • ****
  • Posts: 383
Re: Question re. FCL's ssockets unit, "Buffer" type
« Reply #10 on: December 19, 2025, 07:00:27 am »
Well... it is possible to pass a Nil-value ;)

Code: Pascal  [Select][+][-]
  1. SomeProc(PLongInt(Nil)^)

Hey, no cheating!  ;D

If you don't dereference pointers passed to Move, they will be pointer to pointer.

Yes, and I consider that a footgun (and an insidious one at that):

Code: Pascal  [Select][+][-]
  1. // I fell for it again just yesterday!
  2. // Also ensure {$H+} for maximum destruction.
  3. FillChar(Indent, SPACE_COUNT, ' ');

 

TinyPortal © 2005-2018