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 "chrome/browser/extensions/api/socket/tcp_socket.h" | 5 #include "chrome/browser/extensions/api/socket/tcp_socket.h" |
6 | 6 |
7 #include "chrome/browser/extensions/api/api_resource.h" | 7 #include "chrome/browser/extensions/api/api_resource.h" |
8 #include "chrome/browser/extensions/api/api_resource_event_notifier.h" | 8 #include "chrome/browser/extensions/api/api_resource_event_notifier.h" |
9 #include "net/base/address_list.h" | 9 #include "net/base/address_list.h" |
10 #include "net/base/ip_endpoint.h" | 10 #include "net/base/ip_endpoint.h" |
(...skipping 20 matching lines...) Expand all Loading... | |
31 APIResourceEventNotifier* event_notifier) { | 31 APIResourceEventNotifier* event_notifier) { |
32 return new TCPSocket(tcp_client_socket, event_notifier); | 32 return new TCPSocket(tcp_client_socket, event_notifier); |
33 } | 33 } |
34 | 34 |
35 TCPSocket::~TCPSocket() { | 35 TCPSocket::~TCPSocket() { |
36 if (is_connected_) { | 36 if (is_connected_) { |
37 Disconnect(); | 37 Disconnect(); |
38 } | 38 } |
39 } | 39 } |
40 | 40 |
41 int TCPSocket::Connect(const std::string& address, int port) { | 41 void TCPSocket::Connect(const std::string& address, |
42 if (is_connected_) | 42 int port, |
43 return net::ERR_CONNECTION_FAILED; | 43 const CompletionCallback& callback) { |
44 DCHECK(!callback.is_null()); | |
44 | 45 |
45 net::AddressList address_list; | 46 if (!connect_callback_.is_null()) { |
46 if (!StringAndPortToAddressList(address, port, &address_list)) | 47 callback.Run(net::ERR_IO_PENDING); |
miket_OOO
2012/05/03 17:08:13
Is there something like an "another IO operation a
Peng
2012/05/03 19:21:50
Linux returns -EALREADY for sock is connecting and
| |
47 return net::ERR_INVALID_ARGUMENT; | 48 return; |
49 } | |
50 connect_callback_ = callback; | |
48 | 51 |
49 socket_.reset(new net::TCPClientSocket(address_list, NULL, | 52 int result = net::ERR_CONNECTION_FAILED; |
50 net::NetLog::Source())); | 53 do { |
54 if (is_connected_) { | |
55 result = net::ERR_CONNECTION_FAILED; | |
miket_OOO
2012/05/03 17:08:13
Might as well take advantage of the initialization
Peng
2012/05/03 19:21:50
Done.
| |
56 break; | |
57 } | |
51 | 58 |
52 int result = socket_->Connect(base::Bind( | 59 net::AddressList address_list; |
53 &TCPSocket::OnConnect, base::Unretained(this))); | 60 if (!StringAndPortToAddressList(address, port, &address_list)) { |
54 if (result == net::OK) { | 61 result = net::ERR_INVALID_ARGUMENT; |
55 is_connected_ = true; | 62 break; |
56 } | 63 } |
57 return result; | 64 |
65 socket_.reset(new net::TCPClientSocket(address_list, NULL, | |
66 net::NetLog::Source())); | |
67 connect_callback_ = callback; | |
68 result = socket_->Connect(base::Bind( | |
69 &TCPSocket::OnConnectComplete, base::Unretained(this))); | |
70 } while (0); | |
71 | |
72 if (result != net::ERR_IO_PENDING) | |
73 OnConnectComplete(result); | |
58 } | 74 } |
59 | 75 |
60 void TCPSocket::Disconnect() { | 76 void TCPSocket::Disconnect() { |
61 is_connected_ = false; | 77 is_connected_ = false; |
62 socket_->Disconnect(); | 78 socket_->Disconnect(); |
63 } | 79 } |
64 | 80 |
65 int TCPSocket::Bind(const std::string& address, int port) { | 81 int TCPSocket::Bind(const std::string& address, int port) { |
66 // TODO(penghuang): Supports bind for tcp? | 82 // TODO(penghuang): Supports bind for tcp? |
67 return net::ERR_FAILED; | 83 return net::ERR_FAILED; |
68 } | 84 } |
69 | 85 |
70 int TCPSocket::Read(scoped_refptr<net::IOBuffer> io_buffer, int io_buffer_len) { | 86 void TCPSocket::Read(int count, |
71 if (!socket_.get() || !socket_->IsConnected()) | 87 const ReadCompletionCallback& callback) { |
72 return net::ERR_SOCKET_NOT_CONNECTED; | 88 DCHECK(!callback.is_null()); |
73 | 89 |
74 return socket_->Read( | 90 if (!read_callback_.is_null()) { |
75 io_buffer.get(), | 91 callback.Run(net::ERR_IO_PENDING, NULL); |
miket_OOO
2012/05/03 17:08:13
Same comment as earlier.
Peng
2012/05/03 19:21:50
I can not find any better errors than IO_PENDING i
jeremya
2012/05/04 00:38:00
I don't think this system can ever return ERR_IO_P
| |
76 io_buffer_len, | 92 return; |
77 base::Bind(&Socket::OnDataRead, base::Unretained(this), io_buffer, | 93 } else { |
78 static_cast<net::IPEndPoint*>(NULL))); | 94 read_callback_ = callback; |
95 } | |
96 | |
97 int result = net::ERR_FAILED; | |
98 scoped_refptr<net::IOBuffer> io_buffer; | |
99 do { | |
100 if (count < 0) { | |
101 result = net::ERR_INVALID_ARGUMENT; | |
102 break; | |
103 } | |
104 | |
105 if (!socket_.get() || !socket_->IsConnected()) { | |
106 result = net::ERR_SOCKET_NOT_CONNECTED; | |
107 break; | |
108 } | |
109 | |
110 io_buffer = new net::IOBuffer(count); | |
111 result = socket_->Read(io_buffer.get(), count, | |
112 base::Bind(&TCPSocket::OnReadComplete, base::Unretained(this), | |
113 io_buffer)); | |
114 } while (0); | |
115 | |
116 if (result != net::ERR_IO_PENDING) | |
117 OnReadComplete(io_buffer, result); | |
79 } | 118 } |
80 | 119 |
81 int TCPSocket::Write(scoped_refptr<net::IOBuffer> io_buffer, int byte_count) { | 120 void TCPSocket::Write(scoped_refptr<net::IOBuffer> io_buffer, |
82 if (!socket_.get() || !socket_->IsConnected()) | 121 int byte_count, |
83 return net::ERR_SOCKET_NOT_CONNECTED; | 122 const CompletionCallback& callback) { |
123 DCHECK(!callback.is_null()); | |
84 | 124 |
85 return socket_->Write( | 125 if (!write_callback_.is_null()) { |
86 io_buffer.get(), byte_count, | 126 // TODO(penghuang): Put requests in a pending queue to support multiple |
87 base::Bind(&Socket::OnWriteComplete, base::Unretained(this))); | 127 // write calls. |
128 callback.Run(net::ERR_IO_PENDING); | |
129 return; | |
130 } else { | |
131 write_callback_ = callback; | |
132 } | |
133 | |
134 int result = net::ERR_FAILED; | |
135 do { | |
136 if (!socket_.get() || !socket_->IsConnected()) { | |
137 result = net::ERR_SOCKET_NOT_CONNECTED; | |
138 break; | |
139 } | |
140 | |
141 result = socket_->Write( | |
142 io_buffer.get(), byte_count, | |
143 base::Bind(&TCPSocket::OnWriteComplete, base::Unretained(this))); | |
144 } while (0); | |
145 | |
146 if (result != net::ERR_IO_PENDING) | |
147 OnWriteComplete(result); | |
88 } | 148 } |
89 | 149 |
90 int TCPSocket::RecvFrom(scoped_refptr<net::IOBuffer> io_buffer, | 150 void TCPSocket::RecvFrom(int count, |
91 int io_buffer_len, | 151 const RecvFromCompletionCallback& callback) { |
92 net::IPEndPoint* address) { | 152 callback.Run(net::ERR_FAILED, NULL, NULL, 0); |
93 return net::ERR_FAILED; | |
94 } | 153 } |
95 | 154 |
96 int TCPSocket::SendTo(scoped_refptr<net::IOBuffer> io_buffer, | 155 void TCPSocket::SendTo(scoped_refptr<net::IOBuffer> io_buffer, |
97 int byte_count, | 156 int byte_count, |
98 const std::string& address, | 157 const std::string& address, |
99 int port) { | 158 int port, |
100 return net::ERR_FAILED; | 159 const CompletionCallback& callback) { |
160 callback.Run(net::ERR_FAILED); | |
101 } | 161 } |
102 | 162 |
103 void TCPSocket::OnConnect(int result) { | 163 void TCPSocket::OnConnectComplete(int result) { |
164 DCHECK(!connect_callback_.is_null()); | |
165 DCHECK(!is_connected_); | |
104 is_connected_ = result == net::OK; | 166 is_connected_ = result == net::OK; |
105 event_notifier()->OnConnectComplete(result); | 167 connect_callback_.Run(result); |
168 connect_callback_.Reset(); | |
169 } | |
170 | |
171 void TCPSocket::OnReadComplete(scoped_refptr<net::IOBuffer> io_buffer, | |
172 int result) { | |
173 DCHECK(!read_callback_.is_null()); | |
174 read_callback_.Run(result, io_buffer); | |
175 read_callback_.Reset(); | |
176 } | |
177 | |
178 void TCPSocket::OnWriteComplete(int result) { | |
179 DCHECK(!write_callback_.is_null()); | |
180 write_callback_.Run(result); | |
181 write_callback_.Reset(); | |
106 } | 182 } |
107 | 183 |
108 } // namespace extensions | 184 } // namespace extensions |
OLD | NEW |