1 // Copyright 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "ppapi/cpp/tcp_socket.h"
7 #include "ppapi/c/pp_errors.h"
8 #include "ppapi/cpp/completion_callback.h"
9 #include "ppapi/cpp/instance_handle.h"
10 #include "ppapi/cpp/module_impl.h"
16 template <> const char* interface_name
<PPB_TCPSocket_1_0
>() {
17 return PPB_TCPSOCKET_INTERFACE_1_0
;
20 template <> const char* interface_name
<PPB_TCPSocket_1_1
>() {
21 return PPB_TCPSOCKET_INTERFACE_1_1
;
24 template <> const char* interface_name
<PPB_TCPSocket_1_2
>() {
25 return PPB_TCPSOCKET_INTERFACE_1_2
;
30 TCPSocket::TCPSocket() {
33 TCPSocket::TCPSocket(const InstanceHandle
& instance
) {
34 if (has_interface
<PPB_TCPSocket_1_2
>()) {
35 PassRefFromConstructor(get_interface
<PPB_TCPSocket_1_2
>()->Create(
36 instance
.pp_instance()));
37 } else if (has_interface
<PPB_TCPSocket_1_1
>()) {
38 PassRefFromConstructor(get_interface
<PPB_TCPSocket_1_1
>()->Create(
39 instance
.pp_instance()));
40 } else if (has_interface
<PPB_TCPSocket_1_0
>()) {
41 PassRefFromConstructor(get_interface
<PPB_TCPSocket_1_0
>()->Create(
42 instance
.pp_instance()));
46 TCPSocket::TCPSocket(PassRef
, PP_Resource resource
)
47 : Resource(PASS_REF
, resource
) {
50 TCPSocket::TCPSocket(const TCPSocket
& other
) : Resource(other
) {
53 TCPSocket::~TCPSocket() {
56 TCPSocket
& TCPSocket::operator=(const TCPSocket
& other
) {
57 Resource::operator=(other
);
62 bool TCPSocket::IsAvailable() {
63 return has_interface
<PPB_TCPSocket_1_2
>() ||
64 has_interface
<PPB_TCPSocket_1_1
>() ||
65 has_interface
<PPB_TCPSocket_1_0
>();
68 int32_t TCPSocket::Bind(const NetAddress
& addr
,
69 const CompletionCallback
& callback
) {
70 if (has_interface
<PPB_TCPSocket_1_2
>()) {
71 return get_interface
<PPB_TCPSocket_1_2
>()->Bind(
72 pp_resource(), addr
.pp_resource(), callback
.pp_completion_callback());
74 if (has_interface
<PPB_TCPSocket_1_1
>()) {
75 return get_interface
<PPB_TCPSocket_1_1
>()->Bind(
76 pp_resource(), addr
.pp_resource(), callback
.pp_completion_callback());
78 return callback
.MayForce(PP_ERROR_NOINTERFACE
);
81 int32_t TCPSocket::Connect(const NetAddress
& addr
,
82 const CompletionCallback
& callback
) {
83 if (has_interface
<PPB_TCPSocket_1_2
>()) {
84 return get_interface
<PPB_TCPSocket_1_2
>()->Connect(
85 pp_resource(), addr
.pp_resource(), callback
.pp_completion_callback());
87 if (has_interface
<PPB_TCPSocket_1_1
>()) {
88 return get_interface
<PPB_TCPSocket_1_1
>()->Connect(
89 pp_resource(), addr
.pp_resource(), callback
.pp_completion_callback());
91 if (has_interface
<PPB_TCPSocket_1_0
>()) {
92 return get_interface
<PPB_TCPSocket_1_0
>()->Connect(
93 pp_resource(), addr
.pp_resource(), callback
.pp_completion_callback());
95 return callback
.MayForce(PP_ERROR_NOINTERFACE
);
98 NetAddress
TCPSocket::GetLocalAddress() const {
99 if (has_interface
<PPB_TCPSocket_1_2
>()) {
102 get_interface
<PPB_TCPSocket_1_2
>()->GetLocalAddress(pp_resource()));
104 if (has_interface
<PPB_TCPSocket_1_1
>()) {
107 get_interface
<PPB_TCPSocket_1_1
>()->GetLocalAddress(pp_resource()));
109 if (has_interface
<PPB_TCPSocket_1_0
>()) {
112 get_interface
<PPB_TCPSocket_1_0
>()->GetLocalAddress(pp_resource()));
117 NetAddress
TCPSocket::GetRemoteAddress() const {
118 if (has_interface
<PPB_TCPSocket_1_2
>()) {
121 get_interface
<PPB_TCPSocket_1_2
>()->GetRemoteAddress(pp_resource()));
123 if (has_interface
<PPB_TCPSocket_1_1
>()) {
126 get_interface
<PPB_TCPSocket_1_1
>()->GetRemoteAddress(pp_resource()));
128 if (has_interface
<PPB_TCPSocket_1_0
>()) {
131 get_interface
<PPB_TCPSocket_1_0
>()->GetRemoteAddress(pp_resource()));
136 int32_t TCPSocket::Read(char* buffer
,
137 int32_t bytes_to_read
,
138 const CompletionCallback
& callback
) {
139 if (has_interface
<PPB_TCPSocket_1_2
>()) {
140 return get_interface
<PPB_TCPSocket_1_2
>()->Read(
141 pp_resource(), buffer
, bytes_to_read
,
142 callback
.pp_completion_callback());
144 if (has_interface
<PPB_TCPSocket_1_1
>()) {
145 return get_interface
<PPB_TCPSocket_1_1
>()->Read(
146 pp_resource(), buffer
, bytes_to_read
,
147 callback
.pp_completion_callback());
149 if (has_interface
<PPB_TCPSocket_1_0
>()) {
150 return get_interface
<PPB_TCPSocket_1_0
>()->Read(
151 pp_resource(), buffer
, bytes_to_read
,
152 callback
.pp_completion_callback());
154 return callback
.MayForce(PP_ERROR_NOINTERFACE
);
157 int32_t TCPSocket::Write(const char* buffer
,
158 int32_t bytes_to_write
,
159 const CompletionCallback
& callback
) {
160 if (has_interface
<PPB_TCPSocket_1_2
>()) {
161 return get_interface
<PPB_TCPSocket_1_2
>()->Write(
162 pp_resource(), buffer
, bytes_to_write
,
163 callback
.pp_completion_callback());
165 if (has_interface
<PPB_TCPSocket_1_1
>()) {
166 return get_interface
<PPB_TCPSocket_1_1
>()->Write(
167 pp_resource(), buffer
, bytes_to_write
,
168 callback
.pp_completion_callback());
170 if (has_interface
<PPB_TCPSocket_1_0
>()) {
171 return get_interface
<PPB_TCPSocket_1_0
>()->Write(
172 pp_resource(), buffer
, bytes_to_write
,
173 callback
.pp_completion_callback());
175 return callback
.MayForce(PP_ERROR_NOINTERFACE
);
178 int32_t TCPSocket::Listen(int32_t backlog
,
179 const CompletionCallback
& callback
) {
180 if (has_interface
<PPB_TCPSocket_1_2
>()) {
181 return get_interface
<PPB_TCPSocket_1_2
>()->Listen(
182 pp_resource(), backlog
, callback
.pp_completion_callback());
184 if (has_interface
<PPB_TCPSocket_1_1
>()) {
185 return get_interface
<PPB_TCPSocket_1_1
>()->Listen(
186 pp_resource(), backlog
, callback
.pp_completion_callback());
188 return callback
.MayForce(PP_ERROR_NOINTERFACE
);
191 int32_t TCPSocket::Accept(
192 const CompletionCallbackWithOutput
<TCPSocket
>& callback
) {
193 if (has_interface
<PPB_TCPSocket_1_2
>()) {
194 return get_interface
<PPB_TCPSocket_1_2
>()->Accept(
195 pp_resource(), callback
.output(), callback
.pp_completion_callback());
197 if (has_interface
<PPB_TCPSocket_1_1
>()) {
198 return get_interface
<PPB_TCPSocket_1_1
>()->Accept(
199 pp_resource(), callback
.output(), callback
.pp_completion_callback());
201 return callback
.MayForce(PP_ERROR_NOINTERFACE
);
204 void TCPSocket::Close() {
205 if (has_interface
<PPB_TCPSocket_1_2
>()) {
206 get_interface
<PPB_TCPSocket_1_2
>()->Close(pp_resource());
207 } else if (has_interface
<PPB_TCPSocket_1_1
>()) {
208 get_interface
<PPB_TCPSocket_1_1
>()->Close(pp_resource());
209 } else if (has_interface
<PPB_TCPSocket_1_0
>()) {
210 get_interface
<PPB_TCPSocket_1_0
>()->Close(pp_resource());
214 int32_t TCPSocket::SetOption(PP_TCPSocket_Option name
,
216 const CompletionCallback
& callback
) {
217 if (has_interface
<PPB_TCPSocket_1_2
>()) {
218 return get_interface
<PPB_TCPSocket_1_2
>()->SetOption(
219 pp_resource(), name
, value
.pp_var(), callback
.pp_completion_callback());
221 if (has_interface
<PPB_TCPSocket_1_1
>()) {
222 return get_interface
<PPB_TCPSocket_1_1
>()->SetOption(
223 pp_resource(), name
, value
.pp_var(), callback
.pp_completion_callback());
225 if (has_interface
<PPB_TCPSocket_1_0
>()) {
226 return get_interface
<PPB_TCPSocket_1_0
>()->SetOption(
227 pp_resource(), name
, value
.pp_var(), callback
.pp_completion_callback());
229 return callback
.MayForce(PP_ERROR_NOINTERFACE
);