Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(63)

Side by Side Diff: chrome/browser/extensions/api/socket/tcp_socket.cc

Issue 10134008: Add bind(), recvFrom(), sendTo() for UDP socket. (Closed) Base URL: http://git.chromium.org/chromium/src.git@master
Patch Set: wip Created 8 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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"
11 #include "net/base/net_errors.h" 11 #include "net/base/net_errors.h"
12 #include "net/base/rand_callback.h" 12 #include "net/base/rand_callback.h"
13 #include "net/socket/tcp_client_socket.h" 13 #include "net/socket/tcp_client_socket.h"
14 14
15 namespace extensions { 15 namespace extensions {
16 16
17 TCPSocket::TCPSocket(const std::string& address, int port, 17 TCPSocket::TCPSocket(APIResourceEventNotifier* event_notifier)
18 APIResourceEventNotifier* event_notifier) 18 : Socket(event_notifier) {
19 : Socket(address, port, event_notifier) {
20 net::IPAddressNumber ip_number;
21 if (net::ParseIPLiteralToNumber(address, &ip_number)) {
22 net::AddressList address_list =
23 net::AddressList::CreateFromIPAddress(ip_number, port);
24 socket_.reset(new net::TCPClientSocket(address_list, NULL,
25 net::NetLog::Source()));
26 }
27 } 19 }
28 20
29 // For testing. 21 // For testing.
30 TCPSocket::TCPSocket(net::TCPClientSocket* tcp_client_socket, 22 TCPSocket::TCPSocket(net::TCPClientSocket* tcp_client_socket,
31 const std::string& address, int port,
32 APIResourceEventNotifier* event_notifier) 23 APIResourceEventNotifier* event_notifier)
33 : Socket(address, port, event_notifier), 24 : Socket(event_notifier),
34 socket_(tcp_client_socket) { 25 socket_(tcp_client_socket) {
35 } 26 }
36 27
37 // static 28 // static
38 TCPSocket* TCPSocket::CreateSocketForTesting( 29 TCPSocket* TCPSocket::CreateSocketForTesting(
39 net::TCPClientSocket* tcp_client_socket, 30 net::TCPClientSocket* tcp_client_socket,
40 const std::string& address, int port,
41 APIResourceEventNotifier* event_notifier) { 31 APIResourceEventNotifier* event_notifier) {
42 return new TCPSocket(tcp_client_socket, address, port, event_notifier); 32 return new TCPSocket(tcp_client_socket, event_notifier);
43 } 33 }
44 34
45 TCPSocket::~TCPSocket() { 35 TCPSocket::~TCPSocket() {
46 if (is_connected_) { 36 if (is_connected_) {
47 Disconnect(); 37 Disconnect();
48 } 38 }
49 } 39 }
50 40
51 bool TCPSocket::IsValid() { 41 bool TCPSocket::IsValid() {
52 return socket_ != NULL; 42 return socket_ != NULL;
53 } 43 }
54 44
55 net::Socket* TCPSocket::socket() { 45 int TCPSocket::Connect(const std::string& address, int port) {
56 return socket_.get(); 46 if (is_connected_)
57 } 47 return net::ERR_CONNECTION_FAILED;
58 48
59 int TCPSocket::Connect() { 49 net::AddressList address_list;
50 if (!StringAndPortToAddressList(address, port, &address_list))
51 return net::ERR_INVALID_ARGUMENT;
52
53 socket_.reset(new net::TCPClientSocket(address_list, NULL,
54 net::NetLog::Source()));
55
60 int result = socket_->Connect(base::Bind( 56 int result = socket_->Connect(base::Bind(
61 &TCPSocket::OnConnect, base::Unretained(this))); 57 &TCPSocket::OnConnect, base::Unretained(this)));
62 is_connected_ = result == net::OK; 58 if (result == net::OK) {
59 is_connected_ = true;
60 } else {
61 socket_.reset(NULL);
62 }
63 return result; 63 return result;
64 } 64 }
65 65
66 void TCPSocket::Disconnect() { 66 void TCPSocket::Disconnect() {
67 is_connected_ = false; 67 is_connected_ = false;
68 socket_->Disconnect(); 68 socket_->Disconnect();
69 socket_.reset(NULL);
70 }
71
72 int TCPSocket::Bind(const std::string& address, int port) {
73 // TODO(penghuang): Supports bind for tcp?
74 return net::ERR_FAILED;
75 }
76
77 int TCPSocket::Read(scoped_refptr<net::IOBuffer> io_buffer, int io_buffer_len) {
78 return socket_->Read(
79 io_buffer.get(),
80 io_buffer_len,
81 base::Bind(&Socket::OnDataRead, base::Unretained(this), io_buffer,
82 (net::IPEndPoint *)NULL));
83 }
84
85 int TCPSocket::Write(scoped_refptr<net::IOBuffer> io_buffer, int byte_count) {
86 return socket_->Write(
87 io_buffer.get(), byte_count,
88 base::Bind(&Socket::OnWriteComplete, base::Unretained(this)));
89 }
90
91 int TCPSocket::RecvFrom(scoped_refptr<net::IOBuffer> io_buffer,
92 int io_buffer_len,
93 net::IPEndPoint *address) {
94 return net::ERR_FAILED;
95 }
96
97 int TCPSocket::SendTo(scoped_refptr<net::IOBuffer> io_buffer,
98 int byte_count,
99 const std::string& address,
100 int port) {
101 return net::ERR_FAILED;
69 } 102 }
70 103
71 void TCPSocket::OnConnect(int result) { 104 void TCPSocket::OnConnect(int result) {
72 is_connected_ = result == net::OK; 105 is_connected_ = result == net::OK;
73 event_notifier()->OnConnectComplete(result); 106 event_notifier()->OnConnectComplete(result);
74 } 107 }
75 108
76 } // namespace extensions 109 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698