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"
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"
12
13 namespace pp {
14
15 namespace {
16
interface_name()17 template <> const char* interface_name<PPB_UDPSocket_1_0>() {
18 return PPB_UDPSOCKET_INTERFACE_1_0;
19 }
20
21 } // namespace
22
UDPSocket()23 UDPSocket::UDPSocket() {
24 }
25
UDPSocket(const InstanceHandle & instance)26 UDPSocket::UDPSocket(const InstanceHandle& instance) {
27 if (has_interface<PPB_UDPSocket_1_0>()) {
28 PassRefFromConstructor(get_interface<PPB_UDPSocket_1_0>()->Create(
29 instance.pp_instance()));
30 }
31 }
32
UDPSocket(PassRef,PP_Resource resource)33 UDPSocket::UDPSocket(PassRef, PP_Resource resource)
34 : Resource(PASS_REF, resource) {
35 }
36
UDPSocket(const UDPSocket & other)37 UDPSocket::UDPSocket(const UDPSocket& other) : Resource(other) {
38 }
39
~UDPSocket()40 UDPSocket::~UDPSocket() {
41 }
42
operator =(const UDPSocket & other)43 UDPSocket& UDPSocket::operator=(const UDPSocket& other) {
44 Resource::operator=(other);
45 return *this;
46 }
47
48 // static
IsAvailable()49 bool UDPSocket::IsAvailable() {
50 return has_interface<PPB_UDPSocket_1_0>();
51 }
52
Bind(const NetAddress & addr,const CompletionCallback & callback)53 int32_t UDPSocket::Bind(const NetAddress& addr,
54 const CompletionCallback& callback) {
55 if (has_interface<PPB_UDPSocket_1_0>()) {
56 return get_interface<PPB_UDPSocket_1_0>()->Bind(
57 pp_resource(), addr.pp_resource(), callback.pp_completion_callback());
58 }
59 return callback.MayForce(PP_ERROR_NOINTERFACE);
60 }
61
GetBoundAddress()62 NetAddress UDPSocket::GetBoundAddress() {
63 if (has_interface<PPB_UDPSocket_1_0>()) {
64 return NetAddress(
65 PASS_REF,
66 get_interface<PPB_UDPSocket_1_0>()->GetBoundAddress(pp_resource()));
67 }
68 return NetAddress();
69 }
70
RecvFrom(char * buffer,int32_t num_bytes,const CompletionCallbackWithOutput<NetAddress> & callback)71 int32_t UDPSocket::RecvFrom(
72 char* buffer,
73 int32_t num_bytes,
74 const CompletionCallbackWithOutput<NetAddress>& callback) {
75 if (has_interface<PPB_UDPSocket_1_0>()) {
76 return get_interface<PPB_UDPSocket_1_0>()->RecvFrom(
77 pp_resource(), buffer, num_bytes, callback.output(),
78 callback.pp_completion_callback());
79 }
80 return callback.MayForce(PP_ERROR_NOINTERFACE);
81 }
82
SendTo(const char * buffer,int32_t num_bytes,const NetAddress & addr,const CompletionCallback & callback)83 int32_t UDPSocket::SendTo(const char* buffer,
84 int32_t num_bytes,
85 const NetAddress& addr,
86 const CompletionCallback& callback) {
87 if (has_interface<PPB_UDPSocket_1_0>()) {
88 return get_interface<PPB_UDPSocket_1_0>()->SendTo(
89 pp_resource(), buffer, num_bytes, addr.pp_resource(),
90 callback.pp_completion_callback());
91 }
92 return callback.MayForce(PP_ERROR_NOINTERFACE);
93 }
94
Close()95 void UDPSocket::Close() {
96 if (has_interface<PPB_UDPSocket_1_0>())
97 return get_interface<PPB_UDPSocket_1_0>()->Close(pp_resource());
98 }
99
SetOption(PP_UDPSocket_Option name,const Var & value,const CompletionCallback & callback)100 int32_t UDPSocket::SetOption(PP_UDPSocket_Option name,
101 const Var& value,
102 const CompletionCallback& callback) {
103 if (has_interface<PPB_UDPSocket_1_0>()) {
104 return get_interface<PPB_UDPSocket_1_0>()->SetOption(
105 pp_resource(), name, value.pp_var(), callback.pp_completion_callback());
106 }
107 return callback.MayForce(PP_ERROR_NOINTERFACE);
108 }
109
110 } // namespace pp
111