-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
Allow TCPSocket to bind to specific local address #3495
Conversation
sock.close | ||
end | ||
end | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You might want to add # ditto
special comment so the other self.open
you took the documentation from obtains the same one 😄
https://crystal-lang.org/docs/conventions/documenting_code.html
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We could actually make this just def self.open(*args, **nargs)
and then forward the arguments to new
, so we only need one version of this method.
fc5e86d
to
963d8ea
Compare
e2a8239
to
0edbc62
Compare
0edbc62
to
53c85a3
Compare
I'm not sure this is so common that it's really worth adding more arguments to TCPSocket (and complicate it). I've been considering to refactor Socket to look more like Ruby, where Socket has all the low level C-like methods (bind, listen, ...), so we have access to everything, and where TCPSocket and UDPSocket are limited, but simple, abstractions on top of it. This way one could: sock = Socket.new(Socket::Protocol::TCP)
sock.bind(local_host, local_port)
sock.connect(host, port) Or by making the connection lazy: sock = TCPSocket.new(host, port)
sock.bind(local_host, local_port) |
I agree, Ruby/C style is much better but I wanted to avoid breaking anything with this patch. In order to have an explicit bind call an explicit connect call is also needed, which TCPSocket does not currently have (but UDPSocket does 😕). |
@ysbaddaden , @maxpowa client: |
Allows a TCPSocket to use a specific address for outgoing connections, for use in systems with more than a single interface. Adds two TCPSocket overloads to the existing instantiation methods,
TCPSocket.open
andTCPSocket.new
, with additional parameters to allow specifying an IP and port combination to bind. Not sure how great the specs are, UDPSocket has much better testing for this but unfortunately TCPSocket cannot be instantiated in the same way as UDPSocket.cc @RX14