Pepper_47_C++_interfaces
tcp_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/tcp_socket.h"
6 
7 #include "ppapi/c/pp_errors.h"
10 #include "ppapi/cpp/module_impl.h"
11 
12 namespace pp {
13 
14 namespace {
15 
16 template <> const char* interface_name<PPB_TCPSocket_1_0>() {
17  return PPB_TCPSOCKET_INTERFACE_1_0;
18 }
19 
20 template <> const char* interface_name<PPB_TCPSocket_1_1>() {
21  return PPB_TCPSOCKET_INTERFACE_1_1;
22 }
23 
24 template <> const char* interface_name<PPB_TCPSocket_1_2>() {
25  return PPB_TCPSOCKET_INTERFACE_1_2;
26 }
27 
28 } // namespace
29 
31 }
32 
34  if (has_interface<PPB_TCPSocket_1_2>()) {
35  PassRefFromConstructor(get_interface<PPB_TCPSocket_1_2>()->Create(
36  instance.pp_instance()));
37  } else if (has_interface<PPB_TCPSocket_1_1>()) {
38  PassRefFromConstructor(get_interface<PPB_TCPSocket_1_1>()->Create(
39  instance.pp_instance()));
40  } else if (has_interface<PPB_TCPSocket_1_0>()) {
41  PassRefFromConstructor(get_interface<PPB_TCPSocket_1_0>()->Create(
42  instance.pp_instance()));
43  }
44 }
45 
47  : Resource(PASS_REF, resource) {
48 }
49 
50 TCPSocket::TCPSocket(const TCPSocket& other) : Resource(other) {
51 }
52 
54 }
55 
57  Resource::operator=(other);
58  return *this;
59 }
60 
61 // static
63  return has_interface<PPB_TCPSocket_1_2>() ||
64  has_interface<PPB_TCPSocket_1_1>() ||
65  has_interface<PPB_TCPSocket_1_0>();
66 }
67 
69  const CompletionCallback& callback) {
70  if (has_interface<PPB_TCPSocket_1_2>()) {
71  return get_interface<PPB_TCPSocket_1_2>()->Bind(
72  pp_resource(), addr.pp_resource(), callback.pp_completion_callback());
73  }
74  if (has_interface<PPB_TCPSocket_1_1>()) {
75  return get_interface<PPB_TCPSocket_1_1>()->Bind(
76  pp_resource(), addr.pp_resource(), callback.pp_completion_callback());
77  }
78  return callback.MayForce(PP_ERROR_NOINTERFACE);
79 }
80 
82  const CompletionCallback& callback) {
83  if (has_interface<PPB_TCPSocket_1_2>()) {
84  return get_interface<PPB_TCPSocket_1_2>()->Connect(
85  pp_resource(), addr.pp_resource(), callback.pp_completion_callback());
86  }
87  if (has_interface<PPB_TCPSocket_1_1>()) {
88  return get_interface<PPB_TCPSocket_1_1>()->Connect(
89  pp_resource(), addr.pp_resource(), callback.pp_completion_callback());
90  }
91  if (has_interface<PPB_TCPSocket_1_0>()) {
92  return get_interface<PPB_TCPSocket_1_0>()->Connect(
93  pp_resource(), addr.pp_resource(), callback.pp_completion_callback());
94  }
95  return callback.MayForce(PP_ERROR_NOINTERFACE);
96 }
97 
99  if (has_interface<PPB_TCPSocket_1_2>()) {
100  return NetAddress(
101  PASS_REF,
102  get_interface<PPB_TCPSocket_1_2>()->GetLocalAddress(pp_resource()));
103  }
104  if (has_interface<PPB_TCPSocket_1_1>()) {
105  return NetAddress(
106  PASS_REF,
107  get_interface<PPB_TCPSocket_1_1>()->GetLocalAddress(pp_resource()));
108  }
109  if (has_interface<PPB_TCPSocket_1_0>()) {
110  return NetAddress(
111  PASS_REF,
112  get_interface<PPB_TCPSocket_1_0>()->GetLocalAddress(pp_resource()));
113  }
114  return NetAddress();
115 }
116 
118  if (has_interface<PPB_TCPSocket_1_2>()) {
119  return NetAddress(
120  PASS_REF,
121  get_interface<PPB_TCPSocket_1_2>()->GetRemoteAddress(pp_resource()));
122  }
123  if (has_interface<PPB_TCPSocket_1_1>()) {
124  return NetAddress(
125  PASS_REF,
126  get_interface<PPB_TCPSocket_1_1>()->GetRemoteAddress(pp_resource()));
127  }
128  if (has_interface<PPB_TCPSocket_1_0>()) {
129  return NetAddress(
130  PASS_REF,
131  get_interface<PPB_TCPSocket_1_0>()->GetRemoteAddress(pp_resource()));
132  }
133  return NetAddress();
134 }
135 
137  int32_t bytes_to_read,
138  const CompletionCallback& callback) {
139  if (has_interface<PPB_TCPSocket_1_2>()) {
140  return get_interface<PPB_TCPSocket_1_2>()->Read(
141  pp_resource(), buffer, bytes_to_read,
142  callback.pp_completion_callback());
143  }
144  if (has_interface<PPB_TCPSocket_1_1>()) {
145  return get_interface<PPB_TCPSocket_1_1>()->Read(
146  pp_resource(), buffer, bytes_to_read,
147  callback.pp_completion_callback());
148  }
149  if (has_interface<PPB_TCPSocket_1_0>()) {
150  return get_interface<PPB_TCPSocket_1_0>()->Read(
151  pp_resource(), buffer, bytes_to_read,
152  callback.pp_completion_callback());
153  }
154  return callback.MayForce(PP_ERROR_NOINTERFACE);
155 }
156 
157 int32_t TCPSocket::Write(const char* buffer,
158  int32_t bytes_to_write,
159  const CompletionCallback& callback) {
160  if (has_interface<PPB_TCPSocket_1_2>()) {
161  return get_interface<PPB_TCPSocket_1_2>()->Write(
162  pp_resource(), buffer, bytes_to_write,
163  callback.pp_completion_callback());
164  }
165  if (has_interface<PPB_TCPSocket_1_1>()) {
166  return get_interface<PPB_TCPSocket_1_1>()->Write(
167  pp_resource(), buffer, bytes_to_write,
168  callback.pp_completion_callback());
169  }
170  if (has_interface<PPB_TCPSocket_1_0>()) {
171  return get_interface<PPB_TCPSocket_1_0>()->Write(
172  pp_resource(), buffer, bytes_to_write,
173  callback.pp_completion_callback());
174  }
175  return callback.MayForce(PP_ERROR_NOINTERFACE);
176 }
177 
179  const CompletionCallback& callback) {
180  if (has_interface<PPB_TCPSocket_1_2>()) {
181  return get_interface<PPB_TCPSocket_1_2>()->Listen(
182  pp_resource(), backlog, callback.pp_completion_callback());
183  }
184  if (has_interface<PPB_TCPSocket_1_1>()) {
185  return get_interface<PPB_TCPSocket_1_1>()->Listen(
186  pp_resource(), backlog, callback.pp_completion_callback());
187  }
188  return callback.MayForce(PP_ERROR_NOINTERFACE);
189 }
190 
192  const CompletionCallbackWithOutput<TCPSocket>& callback) {
193  if (has_interface<PPB_TCPSocket_1_2>()) {
194  return get_interface<PPB_TCPSocket_1_2>()->Accept(
195  pp_resource(), callback.output(), callback.pp_completion_callback());
196  }
197  if (has_interface<PPB_TCPSocket_1_1>()) {
198  return get_interface<PPB_TCPSocket_1_1>()->Accept(
199  pp_resource(), callback.output(), callback.pp_completion_callback());
200  }
201  return callback.MayForce(PP_ERROR_NOINTERFACE);
202 }
203 
205  if (has_interface<PPB_TCPSocket_1_2>()) {
206  get_interface<PPB_TCPSocket_1_2>()->Close(pp_resource());
207  } else if (has_interface<PPB_TCPSocket_1_1>()) {
208  get_interface<PPB_TCPSocket_1_1>()->Close(pp_resource());
209  } else if (has_interface<PPB_TCPSocket_1_0>()) {
210  get_interface<PPB_TCPSocket_1_0>()->Close(pp_resource());
211  }
212 }
213 
214 int32_t TCPSocket::SetOption(PP_TCPSocket_Option name,
215  const Var& value,
216  const CompletionCallback& callback) {
217  if (has_interface<PPB_TCPSocket_1_2>()) {
218  return get_interface<PPB_TCPSocket_1_2>()->SetOption(
219  pp_resource(), name, value.pp_var(), callback.pp_completion_callback());
220  }
221  if (has_interface<PPB_TCPSocket_1_1>()) {
222  return get_interface<PPB_TCPSocket_1_1>()->SetOption(
223  pp_resource(), name, value.pp_var(), callback.pp_completion_callback());
224  }
225  if (has_interface<PPB_TCPSocket_1_0>()) {
226  return get_interface<PPB_TCPSocket_1_0>()->SetOption(
227  pp_resource(), name, value.pp_var(), callback.pp_completion_callback());
228  }
229  return callback.MayForce(PP_ERROR_NOINTERFACE);
230 }
231 
232 } // namespace pp
void PassRefFromConstructor(PP_Resource resource)
Definition: resource.cc:50
int32_t Listen(int32_t backlog, const CompletionCallback &callback)
Definition: tcp_socket.cc:178
int32_t SetOption(PP_TCPSocket_Option name, const Var &value, const CompletionCallback &callback)
Definition: tcp_socket.cc:214
NetAddress GetLocalAddress() const
Definition: tcp_socket.cc:98
int32_t Bind(const NetAddress &addr, const CompletionCallback &callback)
Definition: tcp_socket.cc:68
int32_t Accept(const CompletionCallbackWithOutput< TCPSocket > &callback)
Definition: tcp_socket.cc:191
int32_t Read(char *buffer, int32_t bytes_to_read, const CompletionCallback &callback)
Definition: tcp_socket.cc:136
static bool IsAvailable()
Definition: tcp_socket.cc:62
int32_t MayForce(int32_t result) const
const PP_CompletionCallback & pp_completion_callback() const
int32_t Connect(const NetAddress &addr, const CompletionCallback &callback)
Definition: tcp_socket.cc:81
The NetAddress class represents a network address.
Definition: net_address.h:18
const PP_Var & pp_var() const
Definition: var.h:226
PassRef
Definition: pass_ref.h:17
TCPSocket & operator=(const TCPSocket &other)
Definition: tcp_socket.cc:56
PP_Resource pp_resource() const
Definition: resource.h:47
int32_t Write(const char *buffer, int32_t bytes_to_write, const CompletionCallback &callback)
Definition: tcp_socket.cc:157
NetAddress GetRemoteAddress() const
Definition: tcp_socket.cc:117
virtual ~TCPSocket()
The destructor.
Definition: tcp_socket.cc:53
A generic type used for passing data types between the module and the page.
Definition: var.h:21
PP_Instance pp_instance() const
A reference counted module resource.
Definition: resource.h:20
Resource & operator=(const Resource &other)
Definition: resource.cc:27