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
;
22 TCPSocket::TCPSocket() {
25 TCPSocket::TCPSocket(const InstanceHandle
& instance
) {
26 if (has_interface
<PPB_TCPSocket_1_0
>()) {
27 PassRefFromConstructor(get_interface
<PPB_TCPSocket_1_0
>()->Create(
28 instance
.pp_instance()));
32 TCPSocket::TCPSocket(PassRef
, PP_Resource resource
)
33 : Resource(PASS_REF
, resource
) {
36 TCPSocket::TCPSocket(const TCPSocket
& other
) : Resource(other
) {
39 TCPSocket::~TCPSocket() {
42 TCPSocket
& TCPSocket::operator=(const TCPSocket
& other
) {
43 Resource::operator=(other
);
48 bool TCPSocket::IsAvailable() {
49 return has_interface
<PPB_TCPSocket_1_0
>();
52 int32_t TCPSocket::Connect(const NetAddress
& addr
,
53 const CompletionCallback
& callback
) {
54 if (has_interface
<PPB_TCPSocket_1_0
>()) {
55 return get_interface
<PPB_TCPSocket_1_0
>()->Connect(
56 pp_resource(), addr
.pp_resource(), callback
.pp_completion_callback());
58 return callback
.MayForce(PP_ERROR_NOINTERFACE
);
61 NetAddress
TCPSocket::GetLocalAddress() const {
62 if (has_interface
<PPB_TCPSocket_1_0
>()) {
65 get_interface
<PPB_TCPSocket_1_0
>()->GetLocalAddress(pp_resource()));
70 NetAddress
TCPSocket::GetRemoteAddress() const {
71 if (has_interface
<PPB_TCPSocket_1_0
>()) {
74 get_interface
<PPB_TCPSocket_1_0
>()->GetRemoteAddress(pp_resource()));
79 int32_t TCPSocket::Read(char* buffer
,
80 int32_t bytes_to_read
,
81 const CompletionCallback
& callback
) {
82 if (has_interface
<PPB_TCPSocket_1_0
>()) {
83 return get_interface
<PPB_TCPSocket_1_0
>()->Read(
84 pp_resource(), buffer
, bytes_to_read
,
85 callback
.pp_completion_callback());
87 return callback
.MayForce(PP_ERROR_NOINTERFACE
);
90 int32_t TCPSocket::Write(const char* buffer
,
91 int32_t bytes_to_write
,
92 const CompletionCallback
& callback
) {
93 if (has_interface
<PPB_TCPSocket_1_0
>()) {
94 return get_interface
<PPB_TCPSocket_1_0
>()->Write(
95 pp_resource(), buffer
, bytes_to_write
,
96 callback
.pp_completion_callback());
98 return callback
.MayForce(PP_ERROR_NOINTERFACE
);
101 void TCPSocket::Close() {
102 if (has_interface
<PPB_TCPSocket_1_0
>())
103 get_interface
<PPB_TCPSocket_1_0
>()->Close(pp_resource());
106 int32_t TCPSocket::SetOption(PP_TCPSocket_Option name
,
108 const CompletionCallback
& callback
) {
109 if (has_interface
<PPB_TCPSocket_1_0
>()) {
110 return get_interface
<PPB_TCPSocket_1_0
>()->SetOption(
111 pp_resource(), name
, value
.pp_var(), callback
.pp_completion_callback());
113 return callback
.MayForce(PP_ERROR_NOINTERFACE
);