OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 "content/browser/renderer_host/p2p/socket_host_udp.h" | 5 #include "content/browser/renderer_host/p2p/socket_host_udp.h" |
6 | 6 |
7 #include <deque> | 7 #include <deque> |
8 #include <vector> | 8 #include <vector> |
9 | 9 |
10 #include "content/browser/renderer_host/p2p/socket_host_test_utils.h" | 10 #include "content/browser/renderer_host/p2p/socket_host_test_utils.h" |
(...skipping 12 matching lines...) Expand all Loading... |
23 | 23 |
24 namespace { | 24 namespace { |
25 | 25 |
26 class FakeDatagramServerSocket : public net::DatagramServerSocket { | 26 class FakeDatagramServerSocket : public net::DatagramServerSocket { |
27 public: | 27 public: |
28 typedef std::pair<net::IPEndPoint, std::vector<char> > UDPPacket; | 28 typedef std::pair<net::IPEndPoint, std::vector<char> > UDPPacket; |
29 | 29 |
30 // P2PSocketHostUdp destroyes a socket on errors so sent packets | 30 // P2PSocketHostUdp destroyes a socket on errors so sent packets |
31 // need to be stored outside of this object. | 31 // need to be stored outside of this object. |
32 explicit FakeDatagramServerSocket(std::deque<UDPPacket>* sent_packets) | 32 explicit FakeDatagramServerSocket(std::deque<UDPPacket>* sent_packets) |
33 : sent_packets_(sent_packets), recv_callback_(NULL) { | 33 : sent_packets_(sent_packets) { |
34 } | 34 } |
35 | 35 |
36 virtual void Close() OVERRIDE { | 36 virtual void Close() OVERRIDE { |
37 } | 37 } |
38 | 38 |
39 virtual int GetPeerAddress(net::IPEndPoint* address) const OVERRIDE { | 39 virtual int GetPeerAddress(net::IPEndPoint* address) const OVERRIDE { |
40 NOTREACHED(); | 40 NOTREACHED(); |
41 return net::ERR_SOCKET_NOT_CONNECTED; | 41 return net::ERR_SOCKET_NOT_CONNECTED; |
42 } | 42 } |
43 | 43 |
44 virtual int GetLocalAddress(net::IPEndPoint* address) const OVERRIDE { | 44 virtual int GetLocalAddress(net::IPEndPoint* address) const OVERRIDE { |
45 *address = address_; | 45 *address = address_; |
46 return 0; | 46 return 0; |
47 } | 47 } |
48 | 48 |
49 virtual int Listen(const net::IPEndPoint& address) OVERRIDE { | 49 virtual int Listen(const net::IPEndPoint& address) OVERRIDE { |
50 address_ = address; | 50 address_ = address; |
51 return 0; | 51 return 0; |
52 } | 52 } |
53 | 53 |
54 virtual int RecvFrom(net::IOBuffer* buf, int buf_len, | 54 virtual int RecvFrom(net::IOBuffer* buf, int buf_len, |
55 net::IPEndPoint* address, | 55 net::IPEndPoint* address, |
56 net::OldCompletionCallback* callback) OVERRIDE { | 56 const net::CompletionCallback& callback) OVERRIDE { |
57 CHECK(!recv_callback_); | 57 CHECK(recv_callback_.is_null()); |
58 if (incoming_packets_.size() > 0) { | 58 if (incoming_packets_.size() > 0) { |
59 scoped_refptr<net::IOBuffer> buffer(buf); | 59 scoped_refptr<net::IOBuffer> buffer(buf); |
60 int size = std::min( | 60 int size = std::min( |
61 static_cast<int>(incoming_packets_.front().second.size()), buf_len); | 61 static_cast<int>(incoming_packets_.front().second.size()), buf_len); |
62 memcpy(buffer->data(), &*incoming_packets_.front().second.begin(), size); | 62 memcpy(buffer->data(), &*incoming_packets_.front().second.begin(), size); |
63 *address = incoming_packets_.front().first; | 63 *address = incoming_packets_.front().first; |
64 incoming_packets_.pop_front(); | 64 incoming_packets_.pop_front(); |
65 return size; | 65 return size; |
66 } else { | 66 } else { |
67 recv_callback_ = callback; | 67 recv_callback_ = callback; |
68 recv_buffer_ = buf; | 68 recv_buffer_ = buf; |
69 recv_size_ = buf_len; | 69 recv_size_ = buf_len; |
70 recv_address_ = address; | 70 recv_address_ = address; |
71 return net::ERR_IO_PENDING; | 71 return net::ERR_IO_PENDING; |
72 } | 72 } |
73 } | 73 } |
74 | 74 |
75 virtual int SendTo(net::IOBuffer* buf, int buf_len, | 75 virtual int SendTo(net::IOBuffer* buf, int buf_len, |
76 const net::IPEndPoint& address, | 76 const net::IPEndPoint& address, |
77 net::OldCompletionCallback* callback) OVERRIDE { | 77 const net::CompletionCallback& callback) OVERRIDE { |
78 scoped_refptr<net::IOBuffer> buffer(buf); | 78 scoped_refptr<net::IOBuffer> buffer(buf); |
79 std::vector<char> data_vector(buffer->data(), buffer->data() + buf_len); | 79 std::vector<char> data_vector(buffer->data(), buffer->data() + buf_len); |
80 sent_packets_->push_back(UDPPacket(address, data_vector)); | 80 sent_packets_->push_back(UDPPacket(address, data_vector)); |
81 return buf_len; | 81 return buf_len; |
82 } | 82 } |
83 | 83 |
84 virtual bool SetReceiveBufferSize(int32 size) OVERRIDE { | 84 virtual bool SetReceiveBufferSize(int32 size) OVERRIDE { |
85 return true; | 85 return true; |
86 } | 86 } |
87 | 87 |
88 virtual bool SetSendBufferSize(int32 size) OVERRIDE { | 88 virtual bool SetSendBufferSize(int32 size) OVERRIDE { |
89 return true; | 89 return true; |
90 } | 90 } |
91 | 91 |
92 void ReceivePacket(const net::IPEndPoint& address, std::vector<char> data) { | 92 void ReceivePacket(const net::IPEndPoint& address, std::vector<char> data) { |
93 if (recv_callback_) { | 93 if (!recv_callback_.is_null()) { |
94 int size = std::min(recv_size_, static_cast<int>(data.size())); | 94 int size = std::min(recv_size_, static_cast<int>(data.size())); |
95 memcpy(recv_buffer_->data(), &*data.begin(), size); | 95 memcpy(recv_buffer_->data(), &*data.begin(), size); |
96 *recv_address_ = address; | 96 *recv_address_ = address; |
97 net::OldCompletionCallback* cb = recv_callback_; | 97 net::CompletionCallback cb = recv_callback_; |
98 recv_callback_ = NULL; | 98 recv_callback_.Reset(); |
99 recv_buffer_ = NULL; | 99 recv_buffer_ = NULL; |
100 cb->Run(size); | 100 cb.Run(size); |
101 } else { | 101 } else { |
102 incoming_packets_.push_back(UDPPacket(address, data)); | 102 incoming_packets_.push_back(UDPPacket(address, data)); |
103 } | 103 } |
104 } | 104 } |
105 | 105 |
106 virtual const net::BoundNetLog& NetLog() const { | 106 virtual const net::BoundNetLog& NetLog() const { |
107 return net_log_; | 107 return net_log_; |
108 } | 108 } |
109 | 109 |
110 private: | 110 private: |
111 net::IPEndPoint address_; | 111 net::IPEndPoint address_; |
112 std::deque<UDPPacket>* sent_packets_; | 112 std::deque<UDPPacket>* sent_packets_; |
113 std::deque<UDPPacket> incoming_packets_; | 113 std::deque<UDPPacket> incoming_packets_; |
114 net::BoundNetLog net_log_; | 114 net::BoundNetLog net_log_; |
115 | 115 |
116 scoped_refptr<net::IOBuffer> recv_buffer_; | 116 scoped_refptr<net::IOBuffer> recv_buffer_; |
117 net::IPEndPoint* recv_address_; | 117 net::IPEndPoint* recv_address_; |
118 int recv_size_; | 118 int recv_size_; |
119 net::OldCompletionCallback* recv_callback_; | 119 net::CompletionCallback recv_callback_; |
120 }; | 120 }; |
121 | 121 |
122 } // namespace | 122 } // namespace |
123 | 123 |
124 namespace content { | 124 namespace content { |
125 | 125 |
126 class P2PSocketHostUdpTest : public testing::Test { | 126 class P2PSocketHostUdpTest : public testing::Test { |
127 protected: | 127 protected: |
128 virtual void SetUp() OVERRIDE { | 128 virtual void SetUp() OVERRIDE { |
129 EXPECT_CALL(sender_, Send( | 129 EXPECT_CALL(sender_, Send( |
(...skipping 111 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
241 // Should fail when trying to send the same packet to |dest2_|. | 241 // Should fail when trying to send the same packet to |dest2_|. |
242 std::vector<char> packet; | 242 std::vector<char> packet; |
243 CreateRandomPacket(&packet); | 243 CreateRandomPacket(&packet); |
244 EXPECT_CALL(sender_, Send( | 244 EXPECT_CALL(sender_, Send( |
245 MatchMessage(static_cast<uint32>(P2PMsg_OnError::ID)))) | 245 MatchMessage(static_cast<uint32>(P2PMsg_OnError::ID)))) |
246 .WillOnce(DoAll(DeleteArg<0>(), Return(true))); | 246 .WillOnce(DoAll(DeleteArg<0>(), Return(true))); |
247 socket_host_->Send(dest2_, packet); | 247 socket_host_->Send(dest2_, packet); |
248 } | 248 } |
249 | 249 |
250 } // namespace content | 250 } // namespace content |
OLD | NEW |