Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(184)

Side by Side Diff: ppapi/shared_impl/udp_socket_impl.cc

Issue 8688002: PPB_TCPSocket_Private/PPB_UDPSocket_Private are exposed to Browser (Closed) Base URL: http://git.chromium.org/chromium/src.git@master
Patch Set: Added PPAPI_IN_PROCESS TCP and UDP tests. Created 9 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright (c) 2011 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/shared_impl/udp_socket_impl.h"
6
7 #include <string.h>
8
9 #include <algorithm>
10
11 #include "base/basictypes.h"
12 #include "base/logging.h"
13 #include "ppapi/c/pp_errors.h"
14
15 namespace ppapi {
16
17 const int32_t UDPSocketImpl::kMaxReadSize = 1024 * 1024;
18 const int32_t UDPSocketImpl::kMaxWriteSize = 1024 * 1024;
19
20 UDPSocketImpl::UDPSocketImpl(const HostResource& resource, uint32 socket_id)
21 : Resource(resource) {
22 Init(socket_id);
23 }
24
25 UDPSocketImpl::UDPSocketImpl(PP_Instance instance, uint32 socket_id)
26 : Resource(instance) {
27 Init(socket_id);
28 }
29
30 UDPSocketImpl::~UDPSocketImpl() {
31 }
32
33 thunk::PPB_UDPSocket_Private_API*
34 UDPSocketImpl::AsPPB_UDPSocket_Private_API() {
35 return this;
36 }
37
38 int32_t UDPSocketImpl::Bind(const PP_NetAddress_Private* addr,
39 PP_CompletionCallback callback) {
40 if (!addr || !callback.func)
41 return PP_ERROR_BADARGUMENT;
42 if (bound_ || closed_)
43 return PP_ERROR_FAILED;
44 if (bind_callback_.func)
45 return PP_ERROR_INPROGRESS;
46 // TODO(dmichael): use some other strategy for determining if an
47 // operation is in progress
48
49 bind_callback_ = callback;
50
51 // Send the request, the browser will call us back via BindACK.
52 SendBind(*addr);
53 return PP_OK_COMPLETIONPENDING;
54 }
55
56 int32_t UDPSocketImpl::RecvFrom(char* buffer,
57 int32_t num_bytes,
58 PP_CompletionCallback callback) {
59 if (!buffer || num_bytes <= 0 || !callback.func)
60 return PP_ERROR_BADARGUMENT;
61 if (!bound_)
62 return PP_ERROR_FAILED;
63 if (recvfrom_callback_.func)
64 return PP_ERROR_INPROGRESS;
65
66 read_buffer_ = buffer;
67 bytes_to_read_ = std::min(num_bytes, kMaxReadSize);
68 recvfrom_callback_ = callback;
69
70 // Send the request, the browser will call us back via RecvFromACK.
71 SendRecvFrom(bytes_to_read_);
72 return PP_OK_COMPLETIONPENDING;
73 }
74
75 PP_Bool UDPSocketImpl::GetRecvFromAddress(PP_NetAddress_Private* addr) {
76 if (!addr)
77 return PP_FALSE;
78
79 *addr = recvfrom_addr_;
80 return PP_TRUE;
81 }
82
83 int32_t UDPSocketImpl::SendTo(const char* buffer,
84 int32_t num_bytes,
85 const PP_NetAddress_Private* addr,
86 PP_CompletionCallback callback) {
87 if (!buffer || num_bytes <= 0 || !addr || !callback.func)
88 return PP_ERROR_BADARGUMENT;
89 if (!bound_)
90 return PP_ERROR_FAILED;
91 if (sendto_callback_.func)
92 return PP_ERROR_INPROGRESS;
93
94 if (num_bytes > kMaxWriteSize)
95 num_bytes = kMaxWriteSize;
96
97 sendto_callback_ = callback;
98
99 // Send the request, the browser will call us back via SendToACK.
100 SendSendTo(std::string(buffer, num_bytes), *addr);
101 return PP_OK_COMPLETIONPENDING;
102 }
103
104 void UDPSocketImpl::Close() {
105 if(closed_)
106 return;
107
108 bound_ = false;
109 closed_ = true;
110
111 SendClose();
112
113 socket_id_ = 0;
114
115 PostAbortAndClearIfNecessary(&bind_callback_);
116 PostAbortAndClearIfNecessary(&recvfrom_callback_);
117 PostAbortAndClearIfNecessary(&sendto_callback_);
118 }
119
120 void UDPSocketImpl::OnBindCompleted(bool succeeded) {
121 if (!bind_callback_.func) {
122 NOTREACHED();
123 return;
124 }
125
126 if (succeeded)
127 bound_ = true;
128
129 PP_RunAndClearCompletionCallback(&bind_callback_,
130 succeeded ? PP_OK : PP_ERROR_FAILED);
131 }
132
133 void UDPSocketImpl::OnRecvFromCompleted(bool succeeded,
134 const std::string& data,
135 const PP_NetAddress_Private& addr) {
136 if (!recvfrom_callback_.func || !read_buffer_) {
137 NOTREACHED();
138 return;
139 }
140
141 if (succeeded) {
142 CHECK_LE(static_cast<int32_t>(data.size()), bytes_to_read_);
143 if (!data.empty())
144 memcpy(read_buffer_, data.c_str(), data.size());
145 }
146 read_buffer_ = NULL;
147 bytes_to_read_ = -1;
148 recvfrom_addr_ = addr;
149
150 PP_RunAndClearCompletionCallback(
151 &recvfrom_callback_,
152 succeeded ? static_cast<int32_t>(data.size()) :
153 static_cast<int32_t>(PP_ERROR_FAILED));
154 }
155
156 void UDPSocketImpl::OnSendToCompleted(bool succeeded, int32_t bytes_written) {
157 if (!sendto_callback_.func) {
158 NOTREACHED();
159 return;
160 }
161
162 PP_RunAndClearCompletionCallback(
163 &sendto_callback_,
164 succeeded ? bytes_written : static_cast<int32_t>(PP_ERROR_FAILED));
165 }
166
167 void UDPSocketImpl::Init(uint32 socket_id) {
168 DCHECK(socket_id != 0);
169 socket_id_ = socket_id;
170 bound_ = false;
171 closed_ = false;
172 bind_callback_ = PP_BlockUntilComplete();
173 recvfrom_callback_ = PP_BlockUntilComplete();
174 sendto_callback_ = PP_BlockUntilComplete();
175 read_buffer_ = NULL;
176 bytes_to_read_ = -1;
177
178 recvfrom_addr_.size = 0;
179 memset(recvfrom_addr_.data, 0,
180 arraysize(recvfrom_addr_.data) * sizeof(*recvfrom_addr_.data));
181 }
182
183 void UDPSocketImpl::PostAbortAndClearIfNecessary(
184 PP_CompletionCallback* callback) {
185 DCHECK(callback);
186
187 if (callback->func) {
188 PostAbort(*callback);
189 *callback = PP_BlockUntilComplete();
190 }
191 }
192
193 } // namespace ppapi
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698