Thread overview
What does dot before method name mean?
May 08, 2021
Stephen Miller
May 08, 2021
Adam D. Ruppe
May 08, 2021
Stephen Miller
May 08, 2021
Adam D. Ruppe
May 08, 2021
Stephen Miller
May 08, 2021

I am writing a tcp proxy server and I noticed that when a socket is in non-blocking mode, it returns -1 if it doesn't receive data, as opposed to 0.
I wanted to track down the documentation or figure out what might be going on, so I went to the dlang documentation page for std.socket.Socket. The code for receive calls .recv. I don't know where that is defined. It calls other methods in the same way (eg .recvFrom).

Here is the actual code:

return buf.length
                   ? .recv(sock, buf.ptr, capToInt(buf.length), cast(int) flags)
                   : 0;

This is the reference.
https://github.com/dlang/phobos/blob/19264401a8873c211b0ee815fef660d6e7eb081d/std/socket.d#L3085

My questions are: What does the dot in front of recv mean? Where is that code?

May 08, 2021

On Saturday, 8 May 2021 at 01:45:49 UTC, Stephen Miller wrote:

>

I am writing a tcp proxy server and I noticed that when a socket is in non-blocking mode, it returns -1 if it doesn't receive data, as opposed to 0.

It sets the wouldHaveBlocked flag in that case returning -1. 0 always means connection closed.

>

My questions are: What does the dot in front of recv mean? Where is that code?

Leading dot means to look it up from top level instead of the local/class member. So then it uses the system function - recv from C - instead of the local ones.

May 08, 2021

On Saturday, 8 May 2021 at 01:48:22 UTC, Adam D. Ruppe wrote:

>

On Saturday, 8 May 2021 at 01:45:49 UTC, Stephen Miller wrote:

>

I am writing a tcp proxy server and I noticed that when a socket is in non-blocking mode, it returns -1 if it doesn't receive data, as opposed to 0.

It sets the wouldHaveBlocked flag in that case returning -1. 0 always means connection closed.

>

My questions are: What does the dot in front of recv mean? Where is that code?

Leading dot means to look it up from top level instead of the local/class member. So then it uses the system function - recv from C - instead of the local ones.

Thanks for the explanation. Is there an easy way to know what the system functions are? Is it defined by the standard library or does it just include all the system libraries (or some subset) for the operating system you are on?

Also, thanks for the wouldHaveBlocked flag information. That makes everything about the socket method return types make sense.

May 08, 2021

On Saturday, 8 May 2021 at 02:29:18 UTC, Stephen Miller wrote:

>

Is there an easy way to know what the system functions are?

they are imported.

Windows uses winsock:
http://phobos.dpldocs.info/source/std.socket.d.html#L50

posix uses their socket thing:
http://phobos.dpldocs.info/source/std.socket.d.html#L80

So all the leading dot does is go back to the top level - the module - and then if there's a local one defined there, it uses it, otherwise it scans the imports like normal to find it.

See my search results:

http://dpldocs.info/recv

and you can see where the bindings come from.

So it binds to these functions ultimately:

https://man7.org/linux/man-pages/man2/recv.2.html

https://docs.microsoft.com/en-us/windows/win32/api/winsock/nf-winsock-recv

May 08, 2021

On Saturday, 8 May 2021 at 02:33:44 UTC, Adam D. Ruppe wrote:

>

On Saturday, 8 May 2021 at 02:29:18 UTC, Stephen Miller wrote:

>

Is there an easy way to know what the system functions are?

they are imported.

Windows uses winsock:
http://phobos.dpldocs.info/source/std.socket.d.html#L50

posix uses their socket thing:
http://phobos.dpldocs.info/source/std.socket.d.html#L80

So all the leading dot does is go back to the top level - the module - and then if there's a local one defined there, it uses it, otherwise it scans the imports like normal to find it.

See my search results:

http://dpldocs.info/recv

and you can see where the bindings come from.

So it binds to these functions ultimately:

https://man7.org/linux/man-pages/man2/recv.2.html

https://docs.microsoft.com/en-us/windows/win32/api/winsock/nf-winsock-recv

Thanks. Your answers are always great!