TheArkive/Socket_ahk2

sock.Connect return code

Closed this issue · 3 comments

Hey! Thanks for this library.

I wanted to know what does the return code for sock.Connect depict?

Suppose I do sock.connect("localhost", 5700) and no service is listening on 5700, then I would expect the return code to be 0 since it couldn't connect, but it seems the return code is always 1.

I tried to "ahk-ify" the return codes in this lib. I will probably have to revert that to simply mirror the actual API return codes. I'll take a look at it.

EDIT: So basically return 1 should mean success, and 0 to mean failure.

Ah i see. I think since this lib is written to always be async, even a "failed" connect would return WOULDBLOCK. I'm trying to confirm this now.

EDIT: This is the case. It seems there needs to be a user-defined timeout or something. I'll check the ms api docs again and see how this should be handled.

@dufferzafar
I'm open to suggestions on this. Basically it needs to be handled in the callback. Doing this automatically with a non-blocking socket (in a blocking manner?) would be kind of tricky.

EDIT: My latest commit fixes extracting error and event code from lParam. Now it should be easy to determine success of socket connection in an non-blocking manner.

Not as easy as if doing this in a blocking manner, but I would prefer to keep the approach to this class (and any classes built on it) as non-blocking.

I added an F5 example to make a bad connection and show the resulting error code. It works as non-blocking should. Unfortunately that means that sock.connect(...) will always return 1, but this is expected when using non-blocking.

I might add a 3rd param to sock.connect(host, port, blocking:=false), just for convenience. In this case it wouldn't be truly blocking, but merely the script blocking further action until the callback returned.