| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "net/dns/address_sorter.h" | 5 #include "net/dns/address_sorter.h" |
| 6 | 6 |
| 7 #include <winsock2.h> | 7 #include <winsock2.h> |
| 8 | 8 |
| 9 #include <algorithm> | 9 #include <algorithm> |
| 10 | 10 |
| 11 #include "base/bind.h" | 11 #include "base/bind.h" |
| 12 #include "base/location.h" | 12 #include "base/location.h" |
| 13 #include "base/logging.h" | 13 #include "base/logging.h" |
| 14 #include "base/macros.h" | 14 #include "base/macros.h" |
| 15 #include "base/memory/free_deleter.h" | 15 #include "base/memory/free_deleter.h" |
| 16 #include "base/threading/worker_pool.h" | 16 #include "base/task_scheduler/post_task.h" |
| 17 #include "net/base/address_list.h" | 17 #include "net/base/address_list.h" |
| 18 #include "net/base/ip_address.h" | 18 #include "net/base/ip_address.h" |
| 19 #include "net/base/ip_endpoint.h" | 19 #include "net/base/ip_endpoint.h" |
| 20 #include "net/base/winsock_init.h" | 20 #include "net/base/winsock_init.h" |
| 21 | 21 |
| 22 namespace net { | 22 namespace net { |
| 23 | 23 |
| 24 namespace { | 24 namespace { |
| 25 | 25 |
| 26 class AddressSorterWin : public AddressSorter { | 26 class AddressSorterWin : public AddressSorter { |
| 27 public: | 27 public: |
| 28 AddressSorterWin() { | 28 AddressSorterWin() { |
| 29 EnsureWinsockInit(); | 29 EnsureWinsockInit(); |
| 30 } | 30 } |
| 31 | 31 |
| 32 ~AddressSorterWin() override {} | 32 ~AddressSorterWin() override {} |
| 33 | 33 |
| 34 // AddressSorter: | 34 // AddressSorter: |
| 35 void Sort(const AddressList& list, | 35 void Sort(const AddressList& list, |
| 36 const CallbackType& callback) const override { | 36 const CallbackType& callback) const override { |
| 37 DCHECK(!list.empty()); | 37 DCHECK(!list.empty()); |
| 38 scoped_refptr<Job> job = new Job(list, callback); | 38 scoped_refptr<Job> job = new Job(list, callback); |
| 39 } | 39 } |
| 40 | 40 |
| 41 private: | 41 private: |
| 42 // Executes the SIO_ADDRESS_LIST_SORT ioctl on the WorkerPool, and | 42 // Executes the SIO_ADDRESS_LIST_SORT ioctl asynchronously, and |
| 43 // performs the necessary conversions to/from AddressList. | 43 // performs the necessary conversions to/from AddressList. |
| 44 class Job : public base::RefCountedThreadSafe<Job> { | 44 class Job : public base::RefCountedThreadSafe<Job> { |
| 45 public: | 45 public: |
| 46 Job(const AddressList& list, const CallbackType& callback) | 46 Job(const AddressList& list, const CallbackType& callback) |
| 47 : callback_(callback), | 47 : callback_(callback), |
| 48 buffer_size_(sizeof(SOCKET_ADDRESS_LIST) + | 48 buffer_size_(sizeof(SOCKET_ADDRESS_LIST) + |
| 49 list.size() * (sizeof(SOCKET_ADDRESS) + | 49 list.size() * (sizeof(SOCKET_ADDRESS) + |
| 50 sizeof(SOCKADDR_STORAGE))), | 50 sizeof(SOCKADDR_STORAGE))), |
| 51 input_buffer_(reinterpret_cast<SOCKET_ADDRESS_LIST*>( | 51 input_buffer_(reinterpret_cast<SOCKET_ADDRESS_LIST*>( |
| 52 malloc(buffer_size_))), | 52 malloc(buffer_size_))), |
| (...skipping 13 matching lines...) Expand all Loading... |
| 66 } | 66 } |
| 67 | 67 |
| 68 struct sockaddr* addr = reinterpret_cast<struct sockaddr*>(storage + i); | 68 struct sockaddr* addr = reinterpret_cast<struct sockaddr*>(storage + i); |
| 69 socklen_t addr_len = sizeof(SOCKADDR_STORAGE); | 69 socklen_t addr_len = sizeof(SOCKADDR_STORAGE); |
| 70 bool result = ipe.ToSockAddr(addr, &addr_len); | 70 bool result = ipe.ToSockAddr(addr, &addr_len); |
| 71 DCHECK(result); | 71 DCHECK(result); |
| 72 input_buffer_->Address[i].lpSockaddr = addr; | 72 input_buffer_->Address[i].lpSockaddr = addr; |
| 73 input_buffer_->Address[i].iSockaddrLength = addr_len; | 73 input_buffer_->Address[i].iSockaddrLength = addr_len; |
| 74 } | 74 } |
| 75 | 75 |
| 76 if (!base::WorkerPool::PostTaskAndReply( | 76 base::PostTaskWithTraitsAndReply( |
| 77 FROM_HERE, | 77 FROM_HERE, base::TaskTraits() |
| 78 base::Bind(&Job::Run, this), | 78 .WithShutdownBehavior( |
| 79 base::Bind(&Job::OnComplete, this), | 79 base::TaskShutdownBehavior::CONTINUE_ON_SHUTDOWN) |
| 80 false /* task is slow */)) { | 80 .MayBlock(), |
| 81 LOG(ERROR) << "WorkerPool::PostTaskAndReply failed"; | 81 base::Bind(&Job::Run, this), base::Bind(&Job::OnComplete, this)); |
| 82 OnComplete(); | |
| 83 } | |
| 84 } | 82 } |
| 85 | 83 |
| 86 private: | 84 private: |
| 87 friend class base::RefCountedThreadSafe<Job>; | 85 friend class base::RefCountedThreadSafe<Job>; |
| 88 ~Job() {} | 86 ~Job() {} |
| 89 | 87 |
| 90 // Executed on the WorkerPool. | 88 // Executed asynchronously in TaskScheduler. |
| 91 void Run() { | 89 void Run() { |
| 92 SOCKET sock = socket(AF_INET6, SOCK_DGRAM, IPPROTO_UDP); | 90 SOCKET sock = socket(AF_INET6, SOCK_DGRAM, IPPROTO_UDP); |
| 93 if (sock == INVALID_SOCKET) | 91 if (sock == INVALID_SOCKET) |
| 94 return; | 92 return; |
| 95 DWORD result_size = 0; | 93 DWORD result_size = 0; |
| 96 int result = WSAIoctl(sock, SIO_ADDRESS_LIST_SORT, input_buffer_.get(), | 94 int result = WSAIoctl(sock, SIO_ADDRESS_LIST_SORT, input_buffer_.get(), |
| 97 buffer_size_, output_buffer_.get(), buffer_size_, | 95 buffer_size_, output_buffer_.get(), buffer_size_, |
| 98 &result_size, NULL, NULL); | 96 &result_size, NULL, NULL); |
| 99 if (result == SOCKET_ERROR) { | 97 if (result == SOCKET_ERROR) { |
| 100 LOG(ERROR) << "SIO_ADDRESS_LIST_SORT failed " << WSAGetLastError(); | 98 LOG(ERROR) << "SIO_ADDRESS_LIST_SORT failed " << WSAGetLastError(); |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 140 }; | 138 }; |
| 141 | 139 |
| 142 } // namespace | 140 } // namespace |
| 143 | 141 |
| 144 // static | 142 // static |
| 145 std::unique_ptr<AddressSorter> AddressSorter::CreateAddressSorter() { | 143 std::unique_ptr<AddressSorter> AddressSorter::CreateAddressSorter() { |
| 146 return std::unique_ptr<AddressSorter>(new AddressSorterWin()); | 144 return std::unique_ptr<AddressSorter>(new AddressSorterWin()); |
| 147 } | 145 } |
| 148 | 146 |
| 149 } // namespace net | 147 } // namespace net |
| OLD | NEW |