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/udp_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"
11 #include "ppapi/cpp/var.h"
17 template <> const char* interface_name
<PPB_UDPSocket_1_0
>() {
18 return PPB_UDPSOCKET_INTERFACE_1_0
;
21 template <> const char* interface_name
<PPB_UDPSocket_1_1
>() {
22 return PPB_UDPSOCKET_INTERFACE_1_1
;
27 UDPSocket::UDPSocket() {
30 UDPSocket::UDPSocket(const InstanceHandle
& instance
) {
31 if (has_interface
<PPB_UDPSocket_1_1
>()) {
32 PassRefFromConstructor(get_interface
<PPB_UDPSocket_1_1
>()->Create(
33 instance
.pp_instance()));
34 } else if (has_interface
<PPB_UDPSocket_1_0
>()) {
35 PassRefFromConstructor(get_interface
<PPB_UDPSocket_1_0
>()->Create(
36 instance
.pp_instance()));
40 UDPSocket::UDPSocket(PassRef
, PP_Resource resource
)
41 : Resource(PASS_REF
, resource
) {
44 UDPSocket::UDPSocket(const UDPSocket
& other
) : Resource(other
) {
47 UDPSocket::~UDPSocket() {
50 UDPSocket
& UDPSocket::operator=(const UDPSocket
& other
) {
51 Resource::operator=(other
);
56 bool UDPSocket::IsAvailable() {
57 return has_interface
<PPB_UDPSocket_1_1
>() ||
58 has_interface
<PPB_UDPSocket_1_0
>();
61 int32_t UDPSocket::Bind(const NetAddress
& addr
,
62 const CompletionCallback
& callback
) {
63 if (has_interface
<PPB_UDPSocket_1_1
>()) {
64 return get_interface
<PPB_UDPSocket_1_1
>()->Bind(
65 pp_resource(), addr
.pp_resource(), callback
.pp_completion_callback());
67 if (has_interface
<PPB_UDPSocket_1_0
>()) {
68 return get_interface
<PPB_UDPSocket_1_0
>()->Bind(
69 pp_resource(), addr
.pp_resource(), callback
.pp_completion_callback());
71 return callback
.MayForce(PP_ERROR_NOINTERFACE
);
74 NetAddress
UDPSocket::GetBoundAddress() {
75 if (has_interface
<PPB_UDPSocket_1_1
>()) {
78 get_interface
<PPB_UDPSocket_1_1
>()->GetBoundAddress(pp_resource()));
80 if (has_interface
<PPB_UDPSocket_1_0
>()) {
83 get_interface
<PPB_UDPSocket_1_0
>()->GetBoundAddress(pp_resource()));
88 int32_t UDPSocket::RecvFrom(
91 const CompletionCallbackWithOutput
<NetAddress
>& callback
) {
92 if (has_interface
<PPB_UDPSocket_1_1
>()) {
93 return get_interface
<PPB_UDPSocket_1_1
>()->RecvFrom(
94 pp_resource(), buffer
, num_bytes
, callback
.output(),
95 callback
.pp_completion_callback());
97 if (has_interface
<PPB_UDPSocket_1_0
>()) {
98 return get_interface
<PPB_UDPSocket_1_0
>()->RecvFrom(
99 pp_resource(), buffer
, num_bytes
, callback
.output(),
100 callback
.pp_completion_callback());
102 return callback
.MayForce(PP_ERROR_NOINTERFACE
);
105 int32_t UDPSocket::SendTo(const char* buffer
,
107 const NetAddress
& addr
,
108 const CompletionCallback
& callback
) {
109 if (has_interface
<PPB_UDPSocket_1_1
>()) {
110 return get_interface
<PPB_UDPSocket_1_1
>()->SendTo(
111 pp_resource(), buffer
, num_bytes
, addr
.pp_resource(),
112 callback
.pp_completion_callback());
114 if (has_interface
<PPB_UDPSocket_1_0
>()) {
115 return get_interface
<PPB_UDPSocket_1_0
>()->SendTo(
116 pp_resource(), buffer
, num_bytes
, addr
.pp_resource(),
117 callback
.pp_completion_callback());
119 return callback
.MayForce(PP_ERROR_NOINTERFACE
);
122 void UDPSocket::Close() {
123 if (has_interface
<PPB_UDPSocket_1_1
>())
124 return get_interface
<PPB_UDPSocket_1_1
>()->Close(pp_resource());
125 if (has_interface
<PPB_UDPSocket_1_0
>())
126 return get_interface
<PPB_UDPSocket_1_0
>()->Close(pp_resource());
129 int32_t UDPSocket::SetOption(PP_UDPSocket_Option name
,
131 const CompletionCallback
& callback
) {
132 if (has_interface
<PPB_UDPSocket_1_1
>()) {
133 return get_interface
<PPB_UDPSocket_1_1
>()->SetOption(
134 pp_resource(), name
, value
.pp_var(), callback
.pp_completion_callback());
136 if (has_interface
<PPB_UDPSocket_1_0
>()) {
137 return get_interface
<PPB_UDPSocket_1_0
>()->SetOption(
138 pp_resource(), name
, value
.pp_var(), callback
.pp_completion_callback());
140 return callback
.MayForce(PP_ERROR_NOINTERFACE
);