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

Add server and socket constructors receiving a Socket::Address #5871

Conversation

straight-shoota
Copy link
Member

@straight-shoota straight-shoota commented Mar 26, 2018

Currently, to create a TCPSocket connecting to a Socket::IPAddress, you have to access host and port properties and pass them in as separate arguments:

address = Socket::IPAddress.new("localhost", 12345)
TCPSocket.new(address.address, address.port)

with this PR, the socket address can be used as a constructor argument directly:

address = Socket::IPAddress.new("localhost", 12345)
TCPSocket.new(address)

Overloads have been added for:

  • TCPServer.new
  • TCPServer.open
  • TCPSocket.new
  • TCPSocket.open

And with Socket::UNIXAddress:

  • UNIXServer.new
  • UNIXServer.open
  • UNIXSocket.new
  • UNIXSocket.open

UDPSocket#bind and UDPSocket#connect already have overloads accepting a Socket::IPAddress.

@RX14 RX14 requested a review from ysbaddaden March 26, 2018 22:38
Copy link
Contributor

@ysbaddaden ysbaddaden left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What are the use cases? For what benefits? What I see is duplication, here.

@straight-shoota
Copy link
Member Author

From my perspective it helps avoid duplicating address.address, address.port in constructors for TCP sockets. If nothing else, this is just a missing piece that completes the API: Socket classes already return Socket::Address from #local_address and #remote_address, so it makes sense to also use this type to create a socket. It is the natural data type to describe a socket address, the string (+ int) overloads are essentially just short cuts to avoid having to create a IPAddress/UNIXAddress manually when creating a socket.
If it would be preferable to always use host+port/path individually, why does Socket::Address exists anyway? The socket methods could also return a tuple and we'd just need a converter method from LibC address pointer.
I don't think that would make any sense and I like having the address type, although it is probably not used that much. But if this type exists, you should be able to use it to create a socket.

@straight-shoota
Copy link
Member Author

Closing stale PR.

@straight-shoota straight-shoota deleted the jm/feature/initialize-socket-server-with-address branch November 18, 2021 17:44
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants