1 // Copyright 2015 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/completion_callback.h"
6 #include "ppapi/cpp/instance.h"
7 #include "ppapi/cpp/module.h"
8 #include "ppapi/cpp/tcp_socket.h"
9 #include "ppapi/cpp/udp_socket.h"
10 #include "ppapi/utility/completion_callback_factory.h"
14 class MyInstance
: public pp::Instance
{
16 explicit MyInstance(PP_Instance instance
)
17 : pp::Instance(instance
),
20 udp_multicast_socket_(this),
22 virtual ~MyInstance() { }
24 void DidBindTCPSocket(int32_t result
) {
25 // We should be able to bind the TCP socket with permission.
26 if (result
!= PP_OK
) {
30 PP_NetAddress_IPv4 ipv4_address
= {80, {127, 0, 0, 1}};
31 pp::NetAddress
address(this, ipv4_address
);
32 udp_socket_
.Bind(address
,
33 factory_
.NewCallback(&MyInstance::DidBindUDPSocket
));
36 void DidBindUDPSocket(int32_t result
) {
37 // We should be able to bind the UDP socket with permission.
38 if (result
!= PP_OK
) {
42 // Close the socket by releasing it.
43 udp_socket_
= pp::UDPSocket();
44 udp_multicast_socket_
.SetOption(
45 PP_UDPSOCKET_OPTION_MULTICAST_LOOP
, pp::Var(true),
46 factory_
.NewCallback(&MyInstance::DidSetOptionMulticastUDPSocket
));
49 void DidSetOptionMulticastUDPSocket(int32_t result
) {
50 // We should be able to set the multicast option, even without permission.
51 if (result
!= PP_OK
) {
55 PP_NetAddress_IPv4 ipv4_address
= {80, {127, 0, 0, 1}};
56 pp::NetAddress
address(this, ipv4_address
);
57 udp_multicast_socket_
.Bind(
58 address
, factory_
.NewCallback(&MyInstance::DidBindMulticastUDPSocket
));
61 void DidBindMulticastUDPSocket(int32_t result
) {
62 // We should NOT be able to do UDP multicast without permission.
69 virtual bool Init(uint32_t argc
, const char* argn
[], const char* argv
[]) {
70 PP_NetAddress_IPv4 ipv4_address
= {80, {127, 0, 0, 1} };
71 pp::NetAddress
address(this, ipv4_address
);
72 tcp_socket_
.Bind(address
,
73 factory_
.NewCallback(&MyInstance::DidBindTCPSocket
));
78 pp::TCPSocket tcp_socket_
;
79 pp::UDPSocket udp_socket_
;
80 pp::UDPSocket udp_multicast_socket_
;
81 pp::CompletionCallbackFactory
<MyInstance
> factory_
;
84 class MyModule
: public pp::Module
{
86 MyModule() : pp::Module() { }
87 virtual ~MyModule() { }
89 virtual pp::Instance
* CreateInstance(PP_Instance instance
) {
90 return new MyInstance(instance
);
98 Module
* CreateModule() {
99 return new MyModule();