OLD | NEW |
(Empty) | |
| 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" |
| 8 #include "ppapi/cpp/completion_callback.h" |
| 9 #include "ppapi/cpp/instance_handle.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 } // namespace |
| 21 |
| 22 TCPSocket::TCPSocket() { |
| 23 } |
| 24 |
| 25 TCPSocket::TCPSocket(const InstanceHandle& instance) { |
| 26 if (has_interface<PPB_TCPSocket_1_0>()) { |
| 27 PassRefFromConstructor(get_interface<PPB_TCPSocket_1_0>()->Create( |
| 28 instance.pp_instance())); |
| 29 } |
| 30 } |
| 31 |
| 32 TCPSocket::TCPSocket(PassRef, PP_Resource resource) |
| 33 : Resource(PASS_REF, resource) { |
| 34 } |
| 35 |
| 36 TCPSocket::TCPSocket(const TCPSocket& other) : Resource(other) { |
| 37 } |
| 38 |
| 39 TCPSocket::~TCPSocket() { |
| 40 } |
| 41 |
| 42 TCPSocket& TCPSocket::operator=(const TCPSocket& other) { |
| 43 Resource::operator=(other); |
| 44 return *this; |
| 45 } |
| 46 |
| 47 // static |
| 48 bool TCPSocket::IsAvailable() { |
| 49 return has_interface<PPB_TCPSocket_1_0>(); |
| 50 } |
| 51 |
| 52 int32_t TCPSocket::Connect(const NetAddress& addr, |
| 53 const CompletionCallback& callback) { |
| 54 if (has_interface<PPB_TCPSocket_1_0>()) { |
| 55 return get_interface<PPB_TCPSocket_1_0>()->Connect( |
| 56 pp_resource(), addr.pp_resource(), callback.pp_completion_callback()); |
| 57 } |
| 58 return callback.MayForce(PP_ERROR_NOINTERFACE); |
| 59 } |
| 60 |
| 61 NetAddress TCPSocket::GetLocalAddress() const { |
| 62 if (has_interface<PPB_TCPSocket_1_0>()) { |
| 63 return NetAddress( |
| 64 PASS_REF, |
| 65 get_interface<PPB_TCPSocket_1_0>()->GetLocalAddress(pp_resource())); |
| 66 } |
| 67 return NetAddress(); |
| 68 } |
| 69 |
| 70 NetAddress TCPSocket::GetRemoteAddress() const { |
| 71 if (has_interface<PPB_TCPSocket_1_0>()) { |
| 72 return NetAddress( |
| 73 PASS_REF, |
| 74 get_interface<PPB_TCPSocket_1_0>()->GetRemoteAddress(pp_resource())); |
| 75 } |
| 76 return NetAddress(); |
| 77 } |
| 78 |
| 79 int32_t TCPSocket::Read(char* buffer, |
| 80 int32_t bytes_to_read, |
| 81 const CompletionCallback& callback) { |
| 82 if (has_interface<PPB_TCPSocket_1_0>()) { |
| 83 return get_interface<PPB_TCPSocket_1_0>()->Read( |
| 84 pp_resource(), buffer, bytes_to_read, |
| 85 callback.pp_completion_callback()); |
| 86 } |
| 87 return callback.MayForce(PP_ERROR_NOINTERFACE); |
| 88 } |
| 89 |
| 90 int32_t TCPSocket::Write(const char* buffer, |
| 91 int32_t bytes_to_write, |
| 92 const CompletionCallback& callback) { |
| 93 if (has_interface<PPB_TCPSocket_1_0>()) { |
| 94 return get_interface<PPB_TCPSocket_1_0>()->Write( |
| 95 pp_resource(), buffer, bytes_to_write, |
| 96 callback.pp_completion_callback()); |
| 97 } |
| 98 return callback.MayForce(PP_ERROR_NOINTERFACE); |
| 99 } |
| 100 |
| 101 void TCPSocket::Close() { |
| 102 if (has_interface<PPB_TCPSocket_1_0>()) |
| 103 get_interface<PPB_TCPSocket_1_0>()->Close(pp_resource()); |
| 104 } |
| 105 |
| 106 int32_t TCPSocket::SetOption(PP_TCPSocket_Option name, |
| 107 const Var& value, |
| 108 const CompletionCallback& callback) { |
| 109 if (has_interface<PPB_TCPSocket_1_0>()) { |
| 110 return get_interface<PPB_TCPSocket_1_0>()->SetOption( |
| 111 pp_resource(), name, value.pp_var(), callback.pp_completion_callback()); |
| 112 } |
| 113 return callback.MayForce(PP_ERROR_NOINTERFACE); |
| 114 } |
| 115 |
| 116 } // namespace pp |
OLD | NEW |