| 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 "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 14 matching lines...) Expand all Loading... |
| 25 namespace { | 25 namespace { |
| 26 | 26 |
| 27 class UDPSocketTest : public PlatformTest { | 27 class UDPSocketTest : public PlatformTest { |
| 28 public: | 28 public: |
| 29 UDPSocketTest() | 29 UDPSocketTest() |
| 30 : buffer_(new IOBufferWithSize(kMaxRead)) { | 30 : buffer_(new IOBufferWithSize(kMaxRead)) { |
| 31 } | 31 } |
| 32 | 32 |
| 33 // Blocks until data is read from the socket. | 33 // Blocks until data is read from the socket. |
| 34 std::string RecvFromSocket(UDPServerSocket* socket) { | 34 std::string RecvFromSocket(UDPServerSocket* socket) { |
| 35 TestOldCompletionCallback callback; | 35 TestCompletionCallback callback; |
| 36 | 36 |
| 37 int rv = socket->RecvFrom(buffer_, kMaxRead, &recv_from_address_, | 37 int rv = socket->RecvFrom(buffer_, kMaxRead, &recv_from_address_, |
| 38 &callback); | 38 callback.callback()); |
| 39 if (rv == ERR_IO_PENDING) | 39 if (rv == ERR_IO_PENDING) |
| 40 rv = callback.WaitForResult(); | 40 rv = callback.WaitForResult(); |
| 41 if (rv < 0) | 41 if (rv < 0) |
| 42 return ""; // error! | 42 return ""; // error! |
| 43 return std::string(buffer_->data(), rv); | 43 return std::string(buffer_->data(), rv); |
| 44 } | 44 } |
| 45 | 45 |
| 46 // Loop until |msg| has been written to the socket or until an | 46 // Loop until |msg| has been written to the socket or until an |
| 47 // error occurs. | 47 // error occurs. |
| 48 // If |address| is specified, then it is used for the destination | 48 // If |address| is specified, then it is used for the destination |
| 49 // to send to. Otherwise, will send to the last socket this server | 49 // to send to. Otherwise, will send to the last socket this server |
| 50 // received from. | 50 // received from. |
| 51 int SendToSocket(UDPServerSocket* socket, std::string msg) { | 51 int SendToSocket(UDPServerSocket* socket, std::string msg) { |
| 52 return SendToSocket(socket, msg, recv_from_address_); | 52 return SendToSocket(socket, msg, recv_from_address_); |
| 53 } | 53 } |
| 54 | 54 |
| 55 int SendToSocket(UDPServerSocket* socket, | 55 int SendToSocket(UDPServerSocket* socket, |
| 56 std::string msg, | 56 std::string msg, |
| 57 const IPEndPoint& address) { | 57 const IPEndPoint& address) { |
| 58 TestOldCompletionCallback callback; | 58 TestCompletionCallback callback; |
| 59 | 59 |
| 60 int length = msg.length(); | 60 int length = msg.length(); |
| 61 scoped_refptr<StringIOBuffer> io_buffer(new StringIOBuffer(msg)); | 61 scoped_refptr<StringIOBuffer> io_buffer(new StringIOBuffer(msg)); |
| 62 scoped_refptr<DrainableIOBuffer> buffer( | 62 scoped_refptr<DrainableIOBuffer> buffer( |
| 63 new DrainableIOBuffer(io_buffer, length)); | 63 new DrainableIOBuffer(io_buffer, length)); |
| 64 | 64 |
| 65 int bytes_sent = 0; | 65 int bytes_sent = 0; |
| 66 while (buffer->BytesRemaining()) { | 66 while (buffer->BytesRemaining()) { |
| 67 int rv = socket->SendTo(buffer, buffer->BytesRemaining(), | 67 int rv = socket->SendTo(buffer, buffer->BytesRemaining(), |
| 68 address, &callback); | 68 address, callback.callback()); |
| 69 if (rv == ERR_IO_PENDING) | 69 if (rv == ERR_IO_PENDING) |
| 70 rv = callback.WaitForResult(); | 70 rv = callback.WaitForResult(); |
| 71 if (rv <= 0) | 71 if (rv <= 0) |
| 72 return bytes_sent > 0 ? bytes_sent : rv; | 72 return bytes_sent > 0 ? bytes_sent : rv; |
| 73 bytes_sent += rv; | 73 bytes_sent += rv; |
| 74 buffer->DidConsume(rv); | 74 buffer->DidConsume(rv); |
| 75 } | 75 } |
| 76 return bytes_sent; | 76 return bytes_sent; |
| 77 } | 77 } |
| 78 | 78 |
| 79 std::string ReadSocket(UDPClientSocket* socket) { | 79 std::string ReadSocket(UDPClientSocket* socket) { |
| 80 TestOldCompletionCallback callback; | 80 TestCompletionCallback callback; |
| 81 | 81 |
| 82 int rv = socket->Read(buffer_, kMaxRead, &callback); | 82 int rv = socket->Read(buffer_, kMaxRead, callback.callback()); |
| 83 if (rv == ERR_IO_PENDING) | 83 if (rv == ERR_IO_PENDING) |
| 84 rv = callback.WaitForResult(); | 84 rv = callback.WaitForResult(); |
| 85 if (rv < 0) | 85 if (rv < 0) |
| 86 return ""; // error! | 86 return ""; // error! |
| 87 return std::string(buffer_->data(), rv); | 87 return std::string(buffer_->data(), rv); |
| 88 } | 88 } |
| 89 | 89 |
| 90 // Loop until |msg| has been written to the socket or until an | 90 // Loop until |msg| has been written to the socket or until an |
| 91 // error occurs. | 91 // error occurs. |
| 92 int WriteSocket(UDPClientSocket* socket, std::string msg) { | 92 int WriteSocket(UDPClientSocket* socket, std::string msg) { |
| 93 TestOldCompletionCallback callback; | 93 TestCompletionCallback callback; |
| 94 | 94 |
| 95 int length = msg.length(); | 95 int length = msg.length(); |
| 96 scoped_refptr<StringIOBuffer> io_buffer(new StringIOBuffer(msg)); | 96 scoped_refptr<StringIOBuffer> io_buffer(new StringIOBuffer(msg)); |
| 97 scoped_refptr<DrainableIOBuffer> buffer( | 97 scoped_refptr<DrainableIOBuffer> buffer( |
| 98 new DrainableIOBuffer(io_buffer, length)); | 98 new DrainableIOBuffer(io_buffer, length)); |
| 99 | 99 |
| 100 int bytes_sent = 0; | 100 int bytes_sent = 0; |
| 101 while (buffer->BytesRemaining()) { | 101 while (buffer->BytesRemaining()) { |
| 102 int rv = socket->Write(buffer, buffer->BytesRemaining(), &callback); | 102 int rv = socket->Write(buffer, buffer->BytesRemaining(), |
| 103 callback.callback()); |
| 103 if (rv == ERR_IO_PENDING) | 104 if (rv == ERR_IO_PENDING) |
| 104 rv = callback.WaitForResult(); | 105 rv = callback.WaitForResult(); |
| 105 if (rv <= 0) | 106 if (rv <= 0) |
| 106 return bytes_sent > 0 ? bytes_sent : rv; | 107 return bytes_sent > 0 ? bytes_sent : rv; |
| 107 bytes_sent += rv; | 108 bytes_sent += rv; |
| 108 buffer->DidConsume(rv); | 109 buffer->DidConsume(rv); |
| 109 } | 110 } |
| 110 return bytes_sent; | 111 return bytes_sent; |
| 111 } | 112 } |
| 112 | 113 |
| (...skipping 313 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 426 } | 427 } |
| 427 | 428 |
| 428 // Close the socket while read is pending. | 429 // Close the socket while read is pending. |
| 429 TEST_F(UDPSocketTest, CloseWithPendingRead) { | 430 TEST_F(UDPSocketTest, CloseWithPendingRead) { |
| 430 IPEndPoint bind_address; | 431 IPEndPoint bind_address; |
| 431 CreateUDPAddress("127.0.0.1", 0, &bind_address); | 432 CreateUDPAddress("127.0.0.1", 0, &bind_address); |
| 432 UDPServerSocket server(NULL, NetLog::Source()); | 433 UDPServerSocket server(NULL, NetLog::Source()); |
| 433 int rv = server.Listen(bind_address); | 434 int rv = server.Listen(bind_address); |
| 434 EXPECT_EQ(OK, rv); | 435 EXPECT_EQ(OK, rv); |
| 435 | 436 |
| 436 TestOldCompletionCallback callback; | 437 TestCompletionCallback callback; |
| 437 IPEndPoint from; | 438 IPEndPoint from; |
| 438 rv = server.RecvFrom(buffer_, kMaxRead, &from, &callback); | 439 rv = server.RecvFrom(buffer_, kMaxRead, &from, callback.callback()); |
| 439 EXPECT_EQ(rv, ERR_IO_PENDING); | 440 EXPECT_EQ(rv, ERR_IO_PENDING); |
| 440 | 441 |
| 441 server.Close(); | 442 server.Close(); |
| 442 | 443 |
| 443 EXPECT_FALSE(callback.have_result()); | 444 EXPECT_FALSE(callback.have_result()); |
| 444 } | 445 } |
| 445 | 446 |
| 446 } // namespace | 447 } // namespace |
| 447 | 448 |
| 448 } // namespace net | 449 } // namespace net |
| OLD | NEW |