blob: bb4b1c895b0582a0c28f93052b415c1993700a9e [file] [log] [blame]
Ben Murdocheb525c52013-07-10 11:40:50 +01001// 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
13namespace pp {
14
15namespace {
16
17template <> const char* interface_name<PPB_UDPSocket_1_0>() {
18 return PPB_UDPSOCKET_INTERFACE_1_0;
19}
20
21} // namespace
22
23UDPSocket::UDPSocket() {
24}
25
26UDPSocket::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
33UDPSocket::UDPSocket(PassRef, PP_Resource resource)
34 : Resource(PASS_REF, resource) {
35}
36
37UDPSocket::UDPSocket(const UDPSocket& other) : Resource(other) {
38}
39
40UDPSocket::~UDPSocket() {
41}
42
43UDPSocket& UDPSocket::operator=(const UDPSocket& other) {
44 Resource::operator=(other);
45 return *this;
46}
47
48// static
49bool UDPSocket::IsAvailable() {
50 return has_interface<PPB_UDPSocket_1_0>();
51}
52
53int32_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
62NetAddress 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
71int32_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
83int32_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
95void UDPSocket::Close() {
96 if (has_interface<PPB_UDPSocket_1_0>())
97 return get_interface<PPB_UDPSocket_1_0>()->Close(pp_resource());
98}
99
100int32_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