Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Connection timeout #65

Closed
mna opened this issue May 15, 2017 · 6 comments
Closed

Connection timeout #65

mna opened this issue May 15, 2017 · 6 comments

Comments

@mna
Copy link
Contributor

mna commented May 15, 2017

Hello,

It looks like there's no way to set a connection timeout right now? There's a read/write timeout that was added (awesome!) but it would be nice to have a connection timeout too. From what I've read here (http://stackoverflow.com/questions/2597608/c-socket-connection-timeout, second answer is more detailed than accepted one), it involves setting the socket in non-blocking mode and using select with a timeout until the socket is connected (the socket can then be put back in blocking mode if that was how it was requested initially).

I believe the BlueSocket package exposes everything needed to implement this, but this is a non-trivial piece of code and it would make for a much nicer API to have e.g. a timeout: TimeInterval (or UInt in milliseconds, as is the case for read/write timeouts) parameter added to the connect variants.

Thoughts? And thanks for the great package!

Martin

@SelvaBalasubramanian
Copy link

@mna have u tried this...can you help me out with is case... i need fix a timeout for connect.
#76
can you pls check the above issue and help me out....

@mna
Copy link
Contributor Author

mna commented Jul 26, 2017

Yeah I tried it out and commented on #76, so it looks like a connection timeout cannot be implemented with the current API (without dropping to lower-level socket calls).

@billabt
Copy link
Collaborator

billabt commented Aug 1, 2017

There's a bit more to it than that. I'm looking at it...

@billabt
Copy link
Collaborator

billabt commented Aug 4, 2017

I think I've found the problem. Hope to have a fix posted over the weekend. I've got the code complete but haven't finished testing. Will advise... With this fix, the new connect API will look like this:

public func connect(to host: String, port: Int32, timeout: UInt = 0) throws

The timeout parameter is ignored if blocking is not turned off for the Socket.

@billabt
Copy link
Collaborator

billabt commented Aug 6, 2017

Decided to enhance the above a bit more. You won't have to set the Socket into non-blocking mode to use the timeout. You just have to supply a timeout value greater than zero. Here's the new documentation for the connect API from the README.md:

  • connect(to host: String, port: Int32, timeout: UInt = 0) - This API allows you to connect to a server based on the hostname and port you provide. Note: an exception will be thrown by this function if the value of port is not in the range 1-65535. Optionally, you can set timeout to the number of milliseconds to wait for the connect. Note: If the socket is in blocking mode it will be changed to non-blocking mode temporarily if a timeout greater than zero (0) is provided. The returned socket will be set back to its original setting (blocking or non-blocking).

This new API is available in version 0.12.60.

@billabt
Copy link
Collaborator

billabt commented Aug 6, 2017

Going to close this for now. Feel free to re-open if you run into any issues. Thanks.

@billabt billabt closed this as completed Aug 6, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants