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/dev/tcp_socket_dev.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_Dev_0_1
>() {
17 return PPB_TCPSOCKET_DEV_INTERFACE_0_1
;
22 TCPSocket_Dev::TCPSocket_Dev() {
25 TCPSocket_Dev::TCPSocket_Dev(const InstanceHandle
& instance
) {
26 if (has_interface
<PPB_TCPSocket_Dev_0_1
>()) {
27 PassRefFromConstructor(get_interface
<PPB_TCPSocket_Dev_0_1
>()->Create(
28 instance
.pp_instance()));
32 TCPSocket_Dev::TCPSocket_Dev(PassRef
, PP_Resource resource
)
33 : Resource(PASS_REF
, resource
) {
36 TCPSocket_Dev::TCPSocket_Dev(const TCPSocket_Dev
& other
) : Resource(other
) {
39 TCPSocket_Dev::~TCPSocket_Dev() {
42 TCPSocket_Dev
& TCPSocket_Dev::operator=(const TCPSocket_Dev
& other
) {
43 Resource::operator=(other
);
48 bool TCPSocket_Dev::IsAvailable() {
49 return has_interface
<PPB_TCPSocket_Dev_0_1
>();
52 int32_t TCPSocket_Dev::Connect(const NetAddress_Dev
& addr
,
53 const CompletionCallback
& callback
) {
54 if (has_interface
<PPB_TCPSocket_Dev_0_1
>()) {
55 return get_interface
<PPB_TCPSocket_Dev_0_1
>()->Connect(
56 pp_resource(), addr
.pp_resource(), callback
.pp_completion_callback());
58 return callback
.MayForce(PP_ERROR_NOINTERFACE
);
61 NetAddress_Dev
TCPSocket_Dev::GetLocalAddress() const {
62 if (has_interface
<PPB_TCPSocket_Dev_0_1
>()) {
63 return NetAddress_Dev(
65 get_interface
<PPB_TCPSocket_Dev_0_1
>()->GetLocalAddress(pp_resource()));
67 return NetAddress_Dev();
70 NetAddress_Dev
TCPSocket_Dev::GetRemoteAddress() const {
71 if (has_interface
<PPB_TCPSocket_Dev_0_1
>()) {
72 return NetAddress_Dev(
74 get_interface
<PPB_TCPSocket_Dev_0_1
>()->GetRemoteAddress(
77 return NetAddress_Dev();
80 int32_t TCPSocket_Dev::Read(char* buffer
,
81 int32_t bytes_to_read
,
82 const CompletionCallback
& callback
) {
83 if (has_interface
<PPB_TCPSocket_Dev_0_1
>()) {
84 return get_interface
<PPB_TCPSocket_Dev_0_1
>()->Read(
85 pp_resource(), buffer
, bytes_to_read
,
86 callback
.pp_completion_callback());
88 return callback
.MayForce(PP_ERROR_NOINTERFACE
);
91 int32_t TCPSocket_Dev::Write(const char* buffer
,
92 int32_t bytes_to_write
,
93 const CompletionCallback
& callback
) {
94 if (has_interface
<PPB_TCPSocket_Dev_0_1
>()) {
95 return get_interface
<PPB_TCPSocket_Dev_0_1
>()->Write(
96 pp_resource(), buffer
, bytes_to_write
,
97 callback
.pp_completion_callback());
99 return callback
.MayForce(PP_ERROR_NOINTERFACE
);
102 void TCPSocket_Dev::Close() {
103 if (has_interface
<PPB_TCPSocket_Dev_0_1
>())
104 get_interface
<PPB_TCPSocket_Dev_0_1
>()->Close(pp_resource());
107 int32_t TCPSocket_Dev::SetOption(PP_TCPSocket_Option_Dev name
,
109 const CompletionCallback
& callback
) {
110 if (has_interface
<PPB_TCPSocket_Dev_0_1
>()) {
111 return get_interface
<PPB_TCPSocket_Dev_0_1
>()->SetOption(
112 pp_resource(), name
, value
.pp_var(), callback
.pp_completion_callback());
114 return callback
.MayForce(PP_ERROR_NOINTERFACE
);