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
;
26 TCPSocket::TCPSocket() {
29 TCPSocket::TCPSocket(const InstanceHandle
& instance
) {
30 if (has_interface
<PPB_TCPSocket_1_1
>()) {
31 PassRefFromConstructor(get_interface
<PPB_TCPSocket_1_1
>()->Create(
32 instance
.pp_instance()));
33 } else if (has_interface
<PPB_TCPSocket_1_0
>()) {
34 PassRefFromConstructor(get_interface
<PPB_TCPSocket_1_0
>()->Create(
35 instance
.pp_instance()));
39 TCPSocket::TCPSocket(PassRef
, PP_Resource resource
)
40 : Resource(PASS_REF
, resource
) {
43 TCPSocket::TCPSocket(const TCPSocket
& other
) : Resource(other
) {
46 TCPSocket::~TCPSocket() {
49 TCPSocket
& TCPSocket::operator=(const TCPSocket
& other
) {
50 Resource::operator=(other
);
55 bool TCPSocket::IsAvailable() {
56 return has_interface
<PPB_TCPSocket_1_1
>() ||
57 has_interface
<PPB_TCPSocket_1_0
>();
60 int32_t TCPSocket::Bind(const NetAddress
& addr
,
61 const CompletionCallback
& callback
) {
62 if (has_interface
<PPB_TCPSocket_1_1
>()) {
63 return get_interface
<PPB_TCPSocket_1_1
>()->Bind(
64 pp_resource(), addr
.pp_resource(), callback
.pp_completion_callback());
66 return callback
.MayForce(PP_ERROR_NOINTERFACE
);
69 int32_t TCPSocket::Connect(const NetAddress
& addr
,
70 const CompletionCallback
& callback
) {
71 if (has_interface
<PPB_TCPSocket_1_1
>()) {
72 return get_interface
<PPB_TCPSocket_1_1
>()->Connect(
73 pp_resource(), addr
.pp_resource(), callback
.pp_completion_callback());
75 if (has_interface
<PPB_TCPSocket_1_0
>()) {
76 return get_interface
<PPB_TCPSocket_1_0
>()->Connect(
77 pp_resource(), addr
.pp_resource(), callback
.pp_completion_callback());
79 return callback
.MayForce(PP_ERROR_NOINTERFACE
);
82 NetAddress
TCPSocket::GetLocalAddress() const {
83 if (has_interface
<PPB_TCPSocket_1_1
>()) {
86 get_interface
<PPB_TCPSocket_1_1
>()->GetLocalAddress(pp_resource()));
88 if (has_interface
<PPB_TCPSocket_1_0
>()) {
91 get_interface
<PPB_TCPSocket_1_0
>()->GetLocalAddress(pp_resource()));
96 NetAddress
TCPSocket::GetRemoteAddress() const {
97 if (has_interface
<PPB_TCPSocket_1_1
>()) {
100 get_interface
<PPB_TCPSocket_1_1
>()->GetRemoteAddress(pp_resource()));
102 if (has_interface
<PPB_TCPSocket_1_0
>()) {
105 get_interface
<PPB_TCPSocket_1_0
>()->GetRemoteAddress(pp_resource()));
110 int32_t TCPSocket::Read(char* buffer
,
111 int32_t bytes_to_read
,
112 const CompletionCallback
& callback
) {
113 if (has_interface
<PPB_TCPSocket_1_1
>()) {
114 return get_interface
<PPB_TCPSocket_1_1
>()->Read(
115 pp_resource(), buffer
, bytes_to_read
,
116 callback
.pp_completion_callback());
118 if (has_interface
<PPB_TCPSocket_1_0
>()) {
119 return get_interface
<PPB_TCPSocket_1_0
>()->Read(
120 pp_resource(), buffer
, bytes_to_read
,
121 callback
.pp_completion_callback());
123 return callback
.MayForce(PP_ERROR_NOINTERFACE
);
126 int32_t TCPSocket::Write(const char* buffer
,
127 int32_t bytes_to_write
,
128 const CompletionCallback
& callback
) {
129 if (has_interface
<PPB_TCPSocket_1_1
>()) {
130 return get_interface
<PPB_TCPSocket_1_1
>()->Write(
131 pp_resource(), buffer
, bytes_to_write
,
132 callback
.pp_completion_callback());
134 if (has_interface
<PPB_TCPSocket_1_0
>()) {
135 return get_interface
<PPB_TCPSocket_1_0
>()->Write(
136 pp_resource(), buffer
, bytes_to_write
,
137 callback
.pp_completion_callback());
139 return callback
.MayForce(PP_ERROR_NOINTERFACE
);
142 int32_t TCPSocket::Listen(int32_t backlog
,
143 const CompletionCallback
& callback
) {
144 if (has_interface
<PPB_TCPSocket_1_1
>()) {
145 return get_interface
<PPB_TCPSocket_1_1
>()->Listen(
146 pp_resource(), backlog
, callback
.pp_completion_callback());
148 return callback
.MayForce(PP_ERROR_NOINTERFACE
);
151 int32_t TCPSocket::Accept(
152 const CompletionCallbackWithOutput
<TCPSocket
>& callback
) {
153 if (has_interface
<PPB_TCPSocket_1_1
>()) {
154 return get_interface
<PPB_TCPSocket_1_1
>()->Accept(
155 pp_resource(), callback
.output(), callback
.pp_completion_callback());
157 return callback
.MayForce(PP_ERROR_NOINTERFACE
);
160 void TCPSocket::Close() {
161 if (has_interface
<PPB_TCPSocket_1_1
>()) {
162 get_interface
<PPB_TCPSocket_1_1
>()->Close(pp_resource());
163 } else if (has_interface
<PPB_TCPSocket_1_0
>()) {
164 get_interface
<PPB_TCPSocket_1_0
>()->Close(pp_resource());
168 int32_t TCPSocket::SetOption(PP_TCPSocket_Option name
,
170 const CompletionCallback
& callback
) {
171 if (has_interface
<PPB_TCPSocket_1_1
>()) {
172 return get_interface
<PPB_TCPSocket_1_1
>()->SetOption(
173 pp_resource(), name
, value
.pp_var(), callback
.pp_completion_callback());
175 if (has_interface
<PPB_TCPSocket_1_0
>()) {
176 return get_interface
<PPB_TCPSocket_1_0
>()->SetOption(
177 pp_resource(), name
, value
.pp_var(), callback
.pp_completion_callback());
179 return callback
.MayForce(PP_ERROR_NOINTERFACE
);