Allow TCPSocket to bind to specific local address#3495
Allow TCPSocket to bind to specific local address#3495maxpowa wants to merge 2 commits intocrystal-lang:masterfrom
Conversation
src/socket/tcp_socket.cr
Outdated
| sock.close | ||
| end | ||
| end | ||
|
|
There was a problem hiding this comment.
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.
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.openandTCPSocket.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