Pepper_47_C++_interfaces
udp_socket.cc
Go to the documentation of this file.
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.
4 
5 #include "ppapi/cpp/udp_socket.h"
6 
7 #include "ppapi/c/pp_errors.h"
10 #include "ppapi/cpp/module_impl.h"
11 #include "ppapi/cpp/var.h"
12 
13 namespace pp {
14 
15 namespace {
16 
17 template <> const char* interface_name<PPB_UDPSocket_1_0>() {
18  return PPB_UDPSOCKET_INTERFACE_1_0;
19 }
20 
21 template <> const char* interface_name<PPB_UDPSocket_1_1>() {
22  return PPB_UDPSOCKET_INTERFACE_1_1;
23 }
24 
25 template <> const char* interface_name<PPB_UDPSocket_1_2>() {
26  return PPB_UDPSOCKET_INTERFACE_1_2;
27 }
28 
29 } // namespace
30 
32 }
33 
35  if (has_interface<PPB_UDPSocket_1_2>()) {
36  PassRefFromConstructor(get_interface<PPB_UDPSocket_1_2>()->Create(
37  instance.pp_instance()));
38  } else if (has_interface<PPB_UDPSocket_1_1>()) {
39  PassRefFromConstructor(get_interface<PPB_UDPSocket_1_1>()->Create(
40  instance.pp_instance()));
41  } else if (has_interface<PPB_UDPSocket_1_0>()) {
42  PassRefFromConstructor(get_interface<PPB_UDPSocket_1_0>()->Create(
43  instance.pp_instance()));
44  }
45 }
46 
48  : Resource(PASS_REF, resource) {
49 }
50 
51 UDPSocket::UDPSocket(const UDPSocket& other) : Resource(other) {
52 }
53 
55 }
56 
58  Resource::operator=(other);
59  return *this;
60 }
61 
62 // static
64  return has_interface<PPB_UDPSocket_1_2>() ||
65  has_interface<PPB_UDPSocket_1_1>() ||
66  has_interface<PPB_UDPSocket_1_0>();
67 }
68 
70  const CompletionCallback& callback) {
71  if (has_interface<PPB_UDPSocket_1_2>()) {
72  return get_interface<PPB_UDPSocket_1_2>()->Bind(
73  pp_resource(), addr.pp_resource(), callback.pp_completion_callback());
74  }
75  if (has_interface<PPB_UDPSocket_1_1>()) {
76  return get_interface<PPB_UDPSocket_1_1>()->Bind(
77  pp_resource(), addr.pp_resource(), callback.pp_completion_callback());
78  }
79  if (has_interface<PPB_UDPSocket_1_0>()) {
80  return get_interface<PPB_UDPSocket_1_0>()->Bind(
81  pp_resource(), addr.pp_resource(), callback.pp_completion_callback());
82  }
83  return callback.MayForce(PP_ERROR_NOINTERFACE);
84 }
85 
87  if (has_interface<PPB_UDPSocket_1_2>()) {
88  return NetAddress(
89  PASS_REF,
90  get_interface<PPB_UDPSocket_1_2>()->GetBoundAddress(pp_resource()));
91  }
92  if (has_interface<PPB_UDPSocket_1_1>()) {
93  return NetAddress(
94  PASS_REF,
95  get_interface<PPB_UDPSocket_1_1>()->GetBoundAddress(pp_resource()));
96  }
97  if (has_interface<PPB_UDPSocket_1_0>()) {
98  return NetAddress(
99  PASS_REF,
100  get_interface<PPB_UDPSocket_1_0>()->GetBoundAddress(pp_resource()));
101  }
102  return NetAddress();
103 }
104 
106  char* buffer,
107  int32_t num_bytes,
109  if (has_interface<PPB_UDPSocket_1_2>()) {
110  return get_interface<PPB_UDPSocket_1_2>()->RecvFrom(
111  pp_resource(), buffer, num_bytes, callback.output(),
112  callback.pp_completion_callback());
113  }
114  if (has_interface<PPB_UDPSocket_1_1>()) {
115  return get_interface<PPB_UDPSocket_1_1>()->RecvFrom(
116  pp_resource(), buffer, num_bytes, callback.output(),
117  callback.pp_completion_callback());
118  }
119  if (has_interface<PPB_UDPSocket_1_0>()) {
120  return get_interface<PPB_UDPSocket_1_0>()->RecvFrom(
121  pp_resource(), buffer, num_bytes, callback.output(),
122  callback.pp_completion_callback());
123  }
124  return callback.MayForce(PP_ERROR_NOINTERFACE);
125 }
126 
127 int32_t UDPSocket::SendTo(const char* buffer,
128  int32_t num_bytes,
129  const NetAddress& addr,
130  const CompletionCallback& callback) {
131  if (has_interface<PPB_UDPSocket_1_2>()) {
132  return get_interface<PPB_UDPSocket_1_2>()->SendTo(
133  pp_resource(), buffer, num_bytes, addr.pp_resource(),
134  callback.pp_completion_callback());
135  }
136  if (has_interface<PPB_UDPSocket_1_1>()) {
137  return get_interface<PPB_UDPSocket_1_1>()->SendTo(
138  pp_resource(), buffer, num_bytes, addr.pp_resource(),
139  callback.pp_completion_callback());
140  }
141  if (has_interface<PPB_UDPSocket_1_0>()) {
142  return get_interface<PPB_UDPSocket_1_0>()->SendTo(
143  pp_resource(), buffer, num_bytes, addr.pp_resource(),
144  callback.pp_completion_callback());
145  }
146  return callback.MayForce(PP_ERROR_NOINTERFACE);
147 }
148 
150  if (has_interface<PPB_UDPSocket_1_2>())
151  return get_interface<PPB_UDPSocket_1_2>()->Close(pp_resource());
152  if (has_interface<PPB_UDPSocket_1_1>())
153  return get_interface<PPB_UDPSocket_1_1>()->Close(pp_resource());
154  if (has_interface<PPB_UDPSocket_1_0>())
155  return get_interface<PPB_UDPSocket_1_0>()->Close(pp_resource());
156 }
157 
158 int32_t UDPSocket::SetOption(PP_UDPSocket_Option name,
159  const Var& value,
160  const CompletionCallback& callback) {
161  if (has_interface<PPB_UDPSocket_1_2>()) {
162  return get_interface<PPB_UDPSocket_1_2>()->SetOption(
163  pp_resource(), name, value.pp_var(), callback.pp_completion_callback());
164  }
165  if (has_interface<PPB_UDPSocket_1_1>()) {
166  return get_interface<PPB_UDPSocket_1_1>()->SetOption(
167  pp_resource(), name, value.pp_var(), callback.pp_completion_callback());
168  }
169  if (has_interface<PPB_UDPSocket_1_0>()) {
170  return get_interface<PPB_UDPSocket_1_0>()->SetOption(
171  pp_resource(), name, value.pp_var(), callback.pp_completion_callback());
172  }
173  return callback.MayForce(PP_ERROR_NOINTERFACE);
174 }
175 
177  const CompletionCallback callback) {
178  if (has_interface<PPB_UDPSocket_1_2>()) {
179  return get_interface<PPB_UDPSocket_1_2>()->JoinGroup(
180  pp_resource(), group.pp_resource(), callback.pp_completion_callback());
181  }
182  return callback.MayForce(PP_ERROR_NOINTERFACE);
183 }
184 
186  const CompletionCallback callback) {
187  if (has_interface<PPB_UDPSocket_1_2>()) {
188  return get_interface<PPB_UDPSocket_1_2>()->LeaveGroup(
189  pp_resource(), group.pp_resource(), callback.pp_completion_callback());
190  }
191  return callback.MayForce(PP_ERROR_NOINTERFACE);
192 }
193 
194 } // namespace pp
int32_t SetOption(PP_UDPSocket_Option name, const Var &value, const CompletionCallback &callback)
Definition: udp_socket.cc:158
int32_t LeaveGroup(const NetAddress &group, const CompletionCallback callback)
Definition: udp_socket.cc:185
void PassRefFromConstructor(PP_Resource resource)
Definition: resource.cc:50
int32_t JoinGroup(const NetAddress &group, const CompletionCallback callback)
Definition: udp_socket.cc:176
int32_t MayForce(int32_t result) const
const PP_CompletionCallback & pp_completion_callback() const
NetAddress GetBoundAddress()
Definition: udp_socket.cc:86
The NetAddress class represents a network address.
Definition: net_address.h:18
const PP_Var & pp_var() const
Definition: var.h:226
int32_t SendTo(const char *buffer, int32_t num_bytes, const NetAddress &addr, const CompletionCallback &callback)
Definition: udp_socket.cc:127
static bool IsAvailable()
Definition: udp_socket.cc:63
PassRef
Definition: pass_ref.h:17
int32_t RecvFrom(char *buffer, int32_t num_bytes, const CompletionCallbackWithOutput< NetAddress > &callback)
Definition: udp_socket.cc:105
UDPSocket & operator=(const UDPSocket &other)
Definition: udp_socket.cc:57
PP_Resource pp_resource() const
Definition: resource.h:47
A generic type used for passing data types between the module and the page.
Definition: var.h:21
virtual ~UDPSocket()
The destructor.
Definition: udp_socket.cc:54
PP_Instance pp_instance() const
int32_t Bind(const NetAddress &addr, const CompletionCallback &callback)
Definition: udp_socket.cc:69
A reference counted module resource.
Definition: resource.h:20
Resource & operator=(const Resource &other)
Definition: resource.cc:27