{"action":"created","comment":{"url":"https://api.github.com/repos/crystal-lang/crystal/pulls/comments/93613949","pull_request_review_id":14145066,"id":93613949,"diff_hunk":"@@ -48,113 +51,35 @@ require \"./ip_socket\"\n # end\n # ```\n class UDPSocket < IPSocket\n-  def initialize(@family : Family = Family::INET)\n-    super create_socket(family.value, Type::DGRAM, Protocol::UDP)\n+  def initialize(family : Family = Family::INET)\n+    super(family, Type::DGRAM, Protocol::UDP)\n   end\n \n-  # Binds the UDP socket to a local address.\n+  # Receives a text message from the previously bound address.\n   #\n   # ```\n   # server = UDPSocket.new\n-  # server.bind \"localhost\", 1234\n-  # ```\n-  def bind(host, port, dns_timeout = nil)\n-    getaddrinfo(host, port, @family, Type::DGRAM, Protocol::UDP, timeout: dns_timeout) do |addrinfo|\n-      self.reuse_address = true\n-\n-      ret =\n-        {% if flag?(:freebsd) || flag?(:openbsd) %}\n-          LibC.bind(fd, addrinfo.ai_addr.as(LibC::Sockaddr*), addrinfo.ai_addrlen)\n-        {% else %}\n-          LibC.bind(fd, addrinfo.ai_addr, addrinfo.ai_addrlen)\n-        {% end %}\n-      unless ret == 0\n-        next false if addrinfo.ai_next\n-        raise Errno.new(\"Error binding UDP socket at #{host}:#{port}\")\n-      end\n-\n-      true\n-    end\n-  end\n-\n-  # Connects the UDP socket to a remote address to send messages to.\n+  # server.bind(\"localhost\", 1234)\n   #\n+  # message, client_addr = server.receive\n   # ```\n-  # client = UDPSocket.new\n-  # client.connect(\"localhost\", 1234)\n-  # client.send(\"a text message\")\n-  # ```\n-  def connect(host, port, dns_timeout = nil, connect_timeout = nil)\n-    getaddrinfo(host, port, @family, Type::DGRAM, Protocol::UDP, timeout: dns_timeout) do |addrinfo|\n-      if err = nonblocking_connect host, port, addrinfo, timeout: connect_timeout\n-        next false if addrinfo.ai_next\n-        raise err\n-      end\n-\n-      true\n-    end\n+  def receive(max_message_size = 512) : {String, IPAddress}\n+    bytes = Bytes.new(max_message_size)\n+    bytes_read, sockaddr, addrlen = recvfrom(bytes)\n+    {String.new(bytes.to_unsafe, bytes_read), IPAddress.from(sockaddr, addrlen)}","path":"src/socket/udp_socket.cr","position":79,"original_position":79,"commit_id":"acd148f9f77b6b3cbad258e92f6da69db8089279","original_commit_id":"acd148f9f77b6b3cbad258e92f6da69db8089279","user":{"login":"asterite","id":209371,"avatar_url":"https://avatars.githubusercontent.com/u/209371?v=3","gravatar_id":"","url":"https://api.github.com/users/asterite","html_url":"https://github.com/asterite","followers_url":"https://api.github.com/users/asterite/followers","following_url":"https://api.github.com/users/asterite/following{/other_user}","gists_url":"https://api.github.com/users/asterite/gists{/gist_id}","starred_url":"https://api.github.com/users/asterite/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/asterite/subscriptions","organizations_url":"https://api.github.com/users/asterite/orgs","repos_url":"https://api.github.com/users/asterite/repos","events_url":"https://api.github.com/users/asterite/events{/privacy}","received_events_url":"https://api.github.com/users/asterite/received_events","type":"User","site_admin":false},"body":"Here there's a double allocation: `Bytes.new` and `String.new` (it copies the bytes). You can do something like:\r\n\r\n```cr\r\nstr = String.new(max_message_size) do |buffer|\r\n  bytes = Slice.new(buffer, max_message_size)\r\n  bytes_read, sockaddr, addrlen = recvfrom(bytes)\r\n  # etc.\r\n  {bytes_read, 0}\r\nend\r\n```\r\n\r\nThe only \"problem\" with this is that this will maybe allocate more bytes than needed, so maybe occupy more memory until the string is GC'ed. However, I'll soon change `String#new` to `realloc` the pointer if the resulting bytesize is less than the requested capacity. I even did a few benchmarks and this makes a program go much faster as later less memory needs to be reclaimed.","created_at":"2016-12-22T12:21:58Z","updated_at":"2016-12-22T12:21:58Z","html_url":"https://github.com/crystal-lang/crystal/pull/3750#discussion_r93613949","pull_request_url":"https://api.github.com/repos/crystal-lang/crystal/pulls/3750","_links":{"self":{"href":"https://api.github.com/repos/crystal-lang/crystal/pulls/comments/93613949"},"html":{"href":"https://github.com/crystal-lang/crystal/pull/3750#discussion_r93613949"},"pull_request":{"href":"https://api.github.com/repos/crystal-lang/crystal/pulls/3750"}}},"pull_request":{"url":"https://api.github.com/repos/crystal-lang/crystal/pulls/3750","id":99026024,"html_url":"https://github.com/crystal-lang/crystal/pull/3750","diff_url":"https://github.com/crystal-lang/crystal/pull/3750.diff","patch_url":"https://github.com/crystal-lang/crystal/pull/3750.patch","issue_url":"https://api.github.com/repos/crystal-lang/crystal/issues/3750","number":3750,"state":"open","locked":false,"title":"Sockets refactor: allow any family/type/protocol association","user":{"login":"ysbaddaden","id":47380,"avatar_url":"https://avatars.githubusercontent.com/u/47380?v=3","gravatar_id":"","url":"https://api.github.com/users/ysbaddaden","html_url":"https://github.com/ysbaddaden","followers_url":"https://api.github.com/users/ysbaddaden/followers","following_url":"https://api.github.com/users/ysbaddaden/following{/other_user}","gists_url":"https://api.github.com/users/ysbaddaden/gists{/gist_id}","starred_url":"https://api.github.com/users/ysbaddaden/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/ysbaddaden/subscriptions","organizations_url":"https://api.github.com/users/ysbaddaden/orgs","repos_url":"https://api.github.com/users/ysbaddaden/repos","events_url":"https://api.github.com/users/ysbaddaden/events{/privacy}","received_events_url":"https://api.github.com/users/ysbaddaden/received_events","type":"User","site_admin":false},"body":"The whole idea behing this refactor is to give more possibilities over sockets, for some less common scenarios (eg: DGRAM UNIX sockets) as well as cleanup the sockets implementation, by avoiding some repetition.\r\n\r\nRefactor:\r\n\r\n- Socket is now enough to create, configure and use any kind of socket association of family, type and protocol is also possible, as long as it's supported by the underlying OS implementation;\r\n  - as a consequence the `connect`, `bind`, `listen`, `accept`, `send` and `receive` methods were moved from subclasses into `Socket` itself;\r\n- The `TCPSocket`, `TCPServer`, `UDPSocket`, `UNIXSocket` and `UNIXServer` classes are merely sugar to avoid having to deal with socket details;\r\n- `UNIXSocket` and `UNIXServer` can now be used in DGRAM type, in addition to the default STREAM type.\r\n\r\nFeatures:\r\n\r\n- `Addrinfo` DNS resolver, that wraps results from `getaddrinfo`;\r\n- `Socket#receive : {String, IPAddress}` to receive a String (like we can `send(String)`;\r\n- `Socket#bind(Int)` to bind to all available interfaces;\r\n- `Socket::Server` module, included by both `TCPServer` and `UNIXServer`.\r\n\r\nBreaking Change:\r\n\r\n- `IPAddress` now automatically detects the address family, so the argument was removed (limited impact).\r\n\r\nExamples:\r\n\r\n```crystal\r\nsock = UNIXSocket.new(Socket::Family::DGRAM)\r\nsock.connect(\"/tmp/service.sock\")\r\nsock.send(message)\r\n```\r\n\r\n```crystal\r\nsock = Socket.tcp(Socket::Family::INET6)\r\n# set some socket options (e.g. SO_REUSEPORT)\r\nsock.bind(80)\r\nsock.listen\r\n```\r\n\r\ncloses #3214","created_at":"2016-12-21T23:53:42Z","updated_at":"2016-12-22T12:21:58Z","closed_at":null,"merged_at":null,"merge_commit_sha":"f2b02e6849486c8b160a426a7498f3184e3c6260","assignee":null,"assignees":[],"milestone":null,"commits_url":"https://api.github.com/repos/crystal-lang/crystal/pulls/3750/commits","review_comments_url":"https://api.github.com/repos/crystal-lang/crystal/pulls/3750/comments","review_comment_url":"https://api.github.com/repos/crystal-lang/crystal/pulls/comments{/number}","comments_url":"https://api.github.com/repos/crystal-lang/crystal/issues/3750/comments","statuses_url":"https://api.github.com/repos/crystal-lang/crystal/statuses/acd148f9f77b6b3cbad258e92f6da69db8089279","head":{"label":"ysbaddaden:std-sockets-refactor","ref":"std-sockets-refactor","sha":"acd148f9f77b6b3cbad258e92f6da69db8089279","user":{"login":"ysbaddaden","id":47380,"avatar_url":"https://avatars.githubusercontent.com/u/47380?v=3","gravatar_id":"","url":"https://api.github.com/users/ysbaddaden","html_url":"https://github.com/ysbaddaden","followers_url":"https://api.github.com/users/ysbaddaden/followers","following_url":"https://api.github.com/users/ysbaddaden/following{/other_user}","gists_url":"https://api.github.com/users/ysbaddaden/gists{/gist_id}","starred_url":"https://api.github.com/users/ysbaddaden/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/ysbaddaden/subscriptions","organizations_url":"https://api.github.com/users/ysbaddaden/orgs","repos_url":"https://api.github.com/users/ysbaddaden/repos","events_url":"https://api.github.com/users/ysbaddaden/events{/privacy}","received_events_url":"https://api.github.com/users/ysbaddaden/received_events","type":"User","site_admin":false},"repo":{"id":28955723,"name":"crystal","full_name":"ysbaddaden/crystal","owner":{"login":"ysbaddaden","id":47380,"avatar_url":"https://avatars.githubusercontent.com/u/47380?v=3","gravatar_id":"","url":"https://api.github.com/users/ysbaddaden","html_url":"https://github.com/ysbaddaden","followers_url":"https://api.github.com/users/ysbaddaden/followers","following_url":"https://api.github.com/users/ysbaddaden/following{/other_user}","gists_url":"https://api.github.com/users/ysbaddaden/gists{/gist_id}","starred_url":"https://api.github.com/users/ysbaddaden/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/ysbaddaden/subscriptions","organizations_url":"https://api.github.com/users/ysbaddaden/orgs","repos_url":"https://api.github.com/users/ysbaddaden/repos","events_url":"https://api.github.com/users/ysbaddaden/events{/privacy}","received_events_url":"https://api.github.com/users/ysbaddaden/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/ysbaddaden/crystal","description":"The Crystal Programming Language","fork":true,"url":"https://api.github.com/repos/ysbaddaden/crystal","forks_url":"https://api.github.com/repos/ysbaddaden/crystal/forks","keys_url":"https://api.github.com/repos/ysbaddaden/crystal/keys{/key_id}","collaborators_url":"https://api.github.com/repos/ysbaddaden/crystal/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/ysbaddaden/crystal/teams","hooks_url":"https://api.github.com/repos/ysbaddaden/crystal/hooks","issue_events_url":"https://api.github.com/repos/ysbaddaden/crystal/issues/events{/number}","events_url":"https://api.github.com/repos/ysbaddaden/crystal/events","assignees_url":"https://api.github.com/repos/ysbaddaden/crystal/assignees{/user}","branches_url":"https://api.github.com/repos/ysbaddaden/crystal/branches{/branch}","tags_url":"https://api.github.com/repos/ysbaddaden/crystal/tags","blobs_url":"https://api.github.com/repos/ysbaddaden/crystal/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/ysbaddaden/crystal/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/ysbaddaden/crystal/git/refs{/sha}","trees_url":"https://api.github.com/repos/ysbaddaden/crystal/git/trees{/sha}","statuses_url":"https://api.github.com/repos/ysbaddaden/crystal/statuses/{sha}","languages_url":"https://api.github.com/repos/ysbaddaden/crystal/languages","stargazers_url":"https://api.github.com/repos/ysbaddaden/crystal/stargazers","contributors_url":"https://api.github.com/repos/ysbaddaden/crystal/contributors","subscribers_url":"https://api.github.com/repos/ysbaddaden/crystal/subscribers","subscription_url":"https://api.github.com/repos/ysbaddaden/crystal/subscription","commits_url":"https://api.github.com/repos/ysbaddaden/crystal/commits{/sha}","git_commits_url":"https://api.github.com/repos/ysbaddaden/crystal/git/commits{/sha}","comments_url":"https://api.github.com/repos/ysbaddaden/crystal/comments{/number}","issue_comment_url":"https://api.github.com/repos/ysbaddaden/crystal/issues/comments{/number}","contents_url":"https://api.github.com/repos/ysbaddaden/crystal/contents/{+path}","compare_url":"https://api.github.com/repos/ysbaddaden/crystal/compare/{base}...{head}","merges_url":"https://api.github.com/repos/ysbaddaden/crystal/merges","archive_url":"https://api.github.com/repos/ysbaddaden/crystal/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/ysbaddaden/crystal/downloads","issues_url":"https://api.github.com/repos/ysbaddaden/crystal/issues{/number}","pulls_url":"https://api.github.com/repos/ysbaddaden/crystal/pulls{/number}","milestones_url":"https://api.github.com/repos/ysbaddaden/crystal/milestones{/number}","notifications_url":"https://api.github.com/repos/ysbaddaden/crystal/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/ysbaddaden/crystal/labels{/name}","releases_url":"https://api.github.com/repos/ysbaddaden/crystal/releases{/id}","deployments_url":"https://api.github.com/repos/ysbaddaden/crystal/deployments","created_at":"2015-01-08T08:08:48Z","updated_at":"2016-01-21T10:58:49Z","pushed_at":"2016-12-22T00:35:32Z","git_url":"git://github.com/ysbaddaden/crystal.git","ssh_url":"git@github.com:ysbaddaden/crystal.git","clone_url":"https://github.com/ysbaddaden/crystal.git","svn_url":"https://github.com/ysbaddaden/crystal","homepage":"http://crystal-lang.org","size":36478,"stargazers_count":0,"watchers_count":0,"language":"Crystal","has_issues":false,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":0,"mirror_url":null,"open_issues_count":0,"forks":0,"open_issues":0,"watchers":0,"default_branch":"master"}},"base":{"label":"crystal-lang:master","ref":"master","sha":"aafdafba0fdeed73aca6bec795e74999bfb42a63","user":{"login":"crystal-lang","id":6539796,"avatar_url":"https://avatars.githubusercontent.com/u/6539796?v=3","gravatar_id":"","url":"https://api.github.com/users/crystal-lang","html_url":"https://github.com/crystal-lang","followers_url":"https://api.github.com/users/crystal-lang/followers","following_url":"https://api.github.com/users/crystal-lang/following{/other_user}","gists_url":"https://api.github.com/users/crystal-lang/gists{/gist_id}","starred_url":"https://api.github.com/users/crystal-lang/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/crystal-lang/subscriptions","organizations_url":"https://api.github.com/users/crystal-lang/orgs","repos_url":"https://api.github.com/users/crystal-lang/repos","events_url":"https://api.github.com/users/crystal-lang/events{/privacy}","received_events_url":"https://api.github.com/users/crystal-lang/received_events","type":"Organization","site_admin":false},"repo":{"id":6887813,"name":"crystal","full_name":"crystal-lang/crystal","owner":{"login":"crystal-lang","id":6539796,"avatar_url":"https://avatars.githubusercontent.com/u/6539796?v=3","gravatar_id":"","url":"https://api.github.com/users/crystal-lang","html_url":"https://github.com/crystal-lang","followers_url":"https://api.github.com/users/crystal-lang/followers","following_url":"https://api.github.com/users/crystal-lang/following{/other_user}","gists_url":"https://api.github.com/users/crystal-lang/gists{/gist_id}","starred_url":"https://api.github.com/users/crystal-lang/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/crystal-lang/subscriptions","organizations_url":"https://api.github.com/users/crystal-lang/orgs","repos_url":"https://api.github.com/users/crystal-lang/repos","events_url":"https://api.github.com/users/crystal-lang/events{/privacy}","received_events_url":"https://api.github.com/users/crystal-lang/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/crystal-lang/crystal","description":"The Crystal Programming Language","fork":false,"url":"https://api.github.com/repos/crystal-lang/crystal","forks_url":"https://api.github.com/repos/crystal-lang/crystal/forks","keys_url":"https://api.github.com/repos/crystal-lang/crystal/keys{/key_id}","collaborators_url":"https://api.github.com/repos/crystal-lang/crystal/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/crystal-lang/crystal/teams","hooks_url":"https://api.github.com/repos/crystal-lang/crystal/hooks","issue_events_url":"https://api.github.com/repos/crystal-lang/crystal/issues/events{/number}","events_url":"https://api.github.com/repos/crystal-lang/crystal/events","assignees_url":"https://api.github.com/repos/crystal-lang/crystal/assignees{/user}","branches_url":"https://api.github.com/repos/crystal-lang/crystal/branches{/branch}","tags_url":"https://api.github.com/repos/crystal-lang/crystal/tags","blobs_url":"https://api.github.com/repos/crystal-lang/crystal/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/crystal-lang/crystal/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/crystal-lang/crystal/git/refs{/sha}","trees_url":"https://api.github.com/repos/crystal-lang/crystal/git/trees{/sha}","statuses_url":"https://api.github.com/repos/crystal-lang/crystal/statuses/{sha}","languages_url":"https://api.github.com/repos/crystal-lang/crystal/languages","stargazers_url":"https://api.github.com/repos/crystal-lang/crystal/stargazers","contributors_url":"https://api.github.com/repos/crystal-lang/crystal/contributors","subscribers_url":"https://api.github.com/repos/crystal-lang/crystal/subscribers","subscription_url":"https://api.github.com/repos/crystal-lang/crystal/subscription","commits_url":"https://api.github.com/repos/crystal-lang/crystal/commits{/sha}","git_commits_url":"https://api.github.com/repos/crystal-lang/crystal/git/commits{/sha}","comments_url":"https://api.github.com/repos/crystal-lang/crystal/comments{/number}","issue_comment_url":"https://api.github.com/repos/crystal-lang/crystal/issues/comments{/number}","contents_url":"https://api.github.com/repos/crystal-lang/crystal/contents/{+path}","compare_url":"https://api.github.com/repos/crystal-lang/crystal/compare/{base}...{head}","merges_url":"https://api.github.com/repos/crystal-lang/crystal/merges","archive_url":"https://api.github.com/repos/crystal-lang/crystal/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/crystal-lang/crystal/downloads","issues_url":"https://api.github.com/repos/crystal-lang/crystal/issues{/number}","pulls_url":"https://api.github.com/repos/crystal-lang/crystal/pulls{/number}","milestones_url":"https://api.github.com/repos/crystal-lang/crystal/milestones{/number}","notifications_url":"https://api.github.com/repos/crystal-lang/crystal/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/crystal-lang/crystal/labels{/name}","releases_url":"https://api.github.com/repos/crystal-lang/crystal/releases{/id}","deployments_url":"https://api.github.com/repos/crystal-lang/crystal/deployments","created_at":"2012-11-27T17:32:32Z","updated_at":"2016-12-22T08:36:43Z","pushed_at":"2016-12-22T11:13:56Z","git_url":"git://github.com/crystal-lang/crystal.git","ssh_url":"git@github.com:crystal-lang/crystal.git","clone_url":"https://github.com/crystal-lang/crystal.git","svn_url":"https://github.com/crystal-lang/crystal","homepage":"http://crystal-lang.org","size":32000,"stargazers_count":7069,"watchers_count":7069,"language":"Crystal","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":568,"mirror_url":null,"open_issues_count":349,"forks":568,"open_issues":349,"watchers":7069,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/crystal-lang/crystal/pulls/3750"},"html":{"href":"https://github.com/crystal-lang/crystal/pull/3750"},"issue":{"href":"https://api.github.com/repos/crystal-lang/crystal/issues/3750"},"comments":{"href":"https://api.github.com/repos/crystal-lang/crystal/issues/3750/comments"},"review_comments":{"href":"https://api.github.com/repos/crystal-lang/crystal/pulls/3750/comments"},"review_comment":{"href":"https://api.github.com/repos/crystal-lang/crystal/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/crystal-lang/crystal/pulls/3750/commits"},"statuses":{"href":"https://api.github.com/repos/crystal-lang/crystal/statuses/acd148f9f77b6b3cbad258e92f6da69db8089279"}}}}
{"actor":{"display_login":"asterite"}}