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/udp/udp_client_socket.h" | 5 #include "net/udp/udp_client_socket.h" |
6 #include "net/udp/udp_server_socket.h" | 6 #include "net/udp/udp_server_socket.h" |
7 | 7 |
8 #include "base/basictypes.h" | 8 #include "base/basictypes.h" |
9 #include "base/bind.h" | 9 #include "base/bind.h" |
10 #include "base/metrics/histogram.h" | 10 #include "base/metrics/histogram.h" |
(...skipping 20 matching lines...) Expand all Loading... |
31 | 31 |
32 // Blocks until data is read from the socket. | 32 // Blocks until data is read from the socket. |
33 std::string RecvFromSocket(UDPServerSocket* socket) { | 33 std::string RecvFromSocket(UDPServerSocket* socket) { |
34 TestCompletionCallback callback; | 34 TestCompletionCallback callback; |
35 | 35 |
36 int rv = socket->RecvFrom(buffer_, kMaxRead, &recv_from_address_, | 36 int rv = socket->RecvFrom(buffer_, kMaxRead, &recv_from_address_, |
37 callback.callback()); | 37 callback.callback()); |
38 if (rv == ERR_IO_PENDING) | 38 if (rv == ERR_IO_PENDING) |
39 rv = callback.WaitForResult(); | 39 rv = callback.WaitForResult(); |
40 if (rv < 0) | 40 if (rv < 0) |
41 return ""; // error! | 41 return std::string(); // error! |
42 return std::string(buffer_->data(), rv); | 42 return std::string(buffer_->data(), rv); |
43 } | 43 } |
44 | 44 |
45 // Loop until |msg| has been written to the socket or until an | 45 // Loop until |msg| has been written to the socket or until an |
46 // error occurs. | 46 // error occurs. |
47 // If |address| is specified, then it is used for the destination | 47 // If |address| is specified, then it is used for the destination |
48 // to send to. Otherwise, will send to the last socket this server | 48 // to send to. Otherwise, will send to the last socket this server |
49 // received from. | 49 // received from. |
50 int SendToSocket(UDPServerSocket* socket, std::string msg) { | 50 int SendToSocket(UDPServerSocket* socket, std::string msg) { |
51 return SendToSocket(socket, msg, recv_from_address_); | 51 return SendToSocket(socket, msg, recv_from_address_); |
(...skipping 23 matching lines...) Expand all Loading... |
75 return bytes_sent; | 75 return bytes_sent; |
76 } | 76 } |
77 | 77 |
78 std::string ReadSocket(UDPClientSocket* socket) { | 78 std::string ReadSocket(UDPClientSocket* socket) { |
79 TestCompletionCallback callback; | 79 TestCompletionCallback callback; |
80 | 80 |
81 int rv = socket->Read(buffer_, kMaxRead, callback.callback()); | 81 int rv = socket->Read(buffer_, kMaxRead, callback.callback()); |
82 if (rv == ERR_IO_PENDING) | 82 if (rv == ERR_IO_PENDING) |
83 rv = callback.WaitForResult(); | 83 rv = callback.WaitForResult(); |
84 if (rv < 0) | 84 if (rv < 0) |
85 return ""; // error! | 85 return std::string(); // error! |
86 return std::string(buffer_->data(), rv); | 86 return std::string(buffer_->data(), rv); |
87 } | 87 } |
88 | 88 |
89 // Loop until |msg| has been written to the socket or until an | 89 // Loop until |msg| has been written to the socket or until an |
90 // error occurs. | 90 // error occurs. |
91 int WriteSocket(UDPClientSocket* socket, std::string msg) { | 91 int WriteSocket(UDPClientSocket* socket, std::string msg) { |
92 TestCompletionCallback callback; | 92 TestCompletionCallback callback; |
93 | 93 |
94 int length = msg.length(); | 94 int length = msg.length(); |
95 scoped_refptr<StringIOBuffer> io_buffer(new StringIOBuffer(msg)); | 95 scoped_refptr<StringIOBuffer> io_buffer(new StringIOBuffer(msg)); |
(...skipping 426 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
522 EXPECT_EQ(rv, ERR_IO_PENDING); | 522 EXPECT_EQ(rv, ERR_IO_PENDING); |
523 | 523 |
524 server.Close(); | 524 server.Close(); |
525 | 525 |
526 EXPECT_FALSE(callback.have_result()); | 526 EXPECT_FALSE(callback.have_result()); |
527 } | 527 } |
528 | 528 |
529 } // namespace | 529 } // namespace |
530 | 530 |
531 } // namespace net | 531 } // namespace net |
OLD | NEW |