| 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_socket.h" | 5 #include "net/udp/udp_socket.h" |
| 6 | 6 |
| 7 #include "net/udp/udp_client_socket.h" | 7 #include "net/udp/udp_client_socket.h" |
| 8 #include "net/udp/udp_server_socket.h" | 8 #include "net/udp/udp_server_socket.h" |
| 9 | 9 |
| 10 #include "base/basictypes.h" | 10 #include "base/basictypes.h" |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 46 if (rv < 0) | 46 if (rv < 0) |
| 47 return std::string(); // error! | 47 return std::string(); // error! |
| 48 return std::string(buffer_->data(), rv); | 48 return std::string(buffer_->data(), rv); |
| 49 } | 49 } |
| 50 | 50 |
| 51 // Loop until |msg| has been written to the socket or until an | 51 // Loop until |msg| has been written to the socket or until an |
| 52 // error occurs. | 52 // error occurs. |
| 53 // If |address| is specified, then it is used for the destination | 53 // If |address| is specified, then it is used for the destination |
| 54 // to send to. Otherwise, will send to the last socket this server | 54 // to send to. Otherwise, will send to the last socket this server |
| 55 // received from. | 55 // received from. |
| 56 int SendToSocket(UDPServerSocket* socket, std::string msg) { | 56 int SendToSocket(UDPServerSocket* socket, const std::string& msg) { |
| 57 return SendToSocket(socket, msg, recv_from_address_); | 57 return SendToSocket(socket, msg, recv_from_address_); |
| 58 } | 58 } |
| 59 | 59 |
| 60 int SendToSocket(UDPServerSocket* socket, | 60 int SendToSocket(UDPServerSocket* socket, |
| 61 std::string msg, | 61 std::string msg, |
| 62 const IPEndPoint& address) { | 62 const IPEndPoint& address) { |
| 63 TestCompletionCallback callback; | 63 TestCompletionCallback callback; |
| 64 | 64 |
| 65 int length = msg.length(); | 65 int length = msg.length(); |
| 66 scoped_refptr<StringIOBuffer> io_buffer(new StringIOBuffer(msg)); | 66 scoped_refptr<StringIOBuffer> io_buffer(new StringIOBuffer(msg)); |
| (...skipping 20 matching lines...) Expand all Loading... |
| 87 int rv = socket->Read(buffer_.get(), kMaxRead, callback.callback()); | 87 int rv = socket->Read(buffer_.get(), kMaxRead, callback.callback()); |
| 88 if (rv == ERR_IO_PENDING) | 88 if (rv == ERR_IO_PENDING) |
| 89 rv = callback.WaitForResult(); | 89 rv = callback.WaitForResult(); |
| 90 if (rv < 0) | 90 if (rv < 0) |
| 91 return std::string(); // error! | 91 return std::string(); // error! |
| 92 return std::string(buffer_->data(), rv); | 92 return std::string(buffer_->data(), rv); |
| 93 } | 93 } |
| 94 | 94 |
| 95 // Loop until |msg| has been written to the socket or until an | 95 // Loop until |msg| has been written to the socket or until an |
| 96 // error occurs. | 96 // error occurs. |
| 97 int WriteSocket(UDPClientSocket* socket, std::string msg) { | 97 int WriteSocket(UDPClientSocket* socket, const std::string& msg) { |
| 98 TestCompletionCallback callback; | 98 TestCompletionCallback callback; |
| 99 | 99 |
| 100 int length = msg.length(); | 100 int length = msg.length(); |
| 101 scoped_refptr<StringIOBuffer> io_buffer(new StringIOBuffer(msg)); | 101 scoped_refptr<StringIOBuffer> io_buffer(new StringIOBuffer(msg)); |
| 102 scoped_refptr<DrainableIOBuffer> buffer( | 102 scoped_refptr<DrainableIOBuffer> buffer( |
| 103 new DrainableIOBuffer(io_buffer.get(), length)); | 103 new DrainableIOBuffer(io_buffer.get(), length)); |
| 104 | 104 |
| 105 int bytes_sent = 0; | 105 int bytes_sent = 0; |
| 106 while (buffer->BytesRemaining()) { | 106 while (buffer->BytesRemaining()) { |
| 107 int rv = socket->Write( | 107 int rv = socket->Write( |
| 108 buffer.get(), buffer->BytesRemaining(), callback.callback()); | 108 buffer.get(), buffer->BytesRemaining(), callback.callback()); |
| 109 if (rv == ERR_IO_PENDING) | 109 if (rv == ERR_IO_PENDING) |
| 110 rv = callback.WaitForResult(); | 110 rv = callback.WaitForResult(); |
| 111 if (rv <= 0) | 111 if (rv <= 0) |
| 112 return bytes_sent > 0 ? bytes_sent : rv; | 112 return bytes_sent > 0 ? bytes_sent : rv; |
| 113 bytes_sent += rv; | 113 bytes_sent += rv; |
| 114 buffer->DidConsume(rv); | 114 buffer->DidConsume(rv); |
| 115 } | 115 } |
| 116 return bytes_sent; | 116 return bytes_sent; |
| 117 } | 117 } |
| 118 | 118 |
| 119 void WriteSocketIgnoreResult(UDPClientSocket* socket, std::string msg) { | 119 void WriteSocketIgnoreResult(UDPClientSocket* socket, |
| 120 const std::string& msg) { |
| 120 WriteSocket(socket, msg); | 121 WriteSocket(socket, msg); |
| 121 } | 122 } |
| 122 | 123 |
| 123 // Creates an address from ip address and port and writes it to |*address|. | 124 // Creates an address from ip address and port and writes it to |*address|. |
| 124 void CreateUDPAddress(std::string ip_str, uint16 port, IPEndPoint* address) { | 125 void CreateUDPAddress(const std::string& ip_str, |
| 126 uint16 port, |
| 127 IPEndPoint* address) { |
| 125 IPAddressNumber ip_number; | 128 IPAddressNumber ip_number; |
| 126 bool rv = ParseIPLiteralToNumber(ip_str, &ip_number); | 129 bool rv = ParseIPLiteralToNumber(ip_str, &ip_number); |
| 127 if (!rv) | 130 if (!rv) |
| 128 return; | 131 return; |
| 129 *address = IPEndPoint(ip_number, port); | 132 *address = IPEndPoint(ip_number, port); |
| 130 } | 133 } |
| 131 | 134 |
| 132 // Run unit test for a connection test. | 135 // Run unit test for a connection test. |
| 133 // |use_nonblocking_io| is used to switch between overlapped and non-blocking | 136 // |use_nonblocking_io| is used to switch between overlapped and non-blocking |
| 134 // IO on Windows. It has no effect in other ports. | 137 // IO on Windows. It has no effect in other ports. |
| (...skipping 660 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 795 g_expected_traffic_type = QOSTrafficTypeExcellentEffort; | 798 g_expected_traffic_type = QOSTrafficTypeExcellentEffort; |
| 796 EXPECT_EQ(OK, client.SetDiffServCodePoint(DSCP_NO_CHANGE)); | 799 EXPECT_EQ(OK, client.SetDiffServCodePoint(DSCP_NO_CHANGE)); |
| 797 g_expected_dscp = DSCP_DEFAULT; | 800 g_expected_dscp = DSCP_DEFAULT; |
| 798 g_expected_traffic_type = QOSTrafficTypeBestEffort; | 801 g_expected_traffic_type = QOSTrafficTypeBestEffort; |
| 799 EXPECT_EQ(OK, client.SetDiffServCodePoint(DSCP_DEFAULT)); | 802 EXPECT_EQ(OK, client.SetDiffServCodePoint(DSCP_DEFAULT)); |
| 800 client.Close(); | 803 client.Close(); |
| 801 } | 804 } |
| 802 #endif | 805 #endif |
| 803 | 806 |
| 804 } // namespace net | 807 } // namespace net |
| OLD | NEW |