-
Notifications
You must be signed in to change notification settings - Fork 33
/
tcp_socket.cc
322 lines (280 loc) · 8.09 KB
/
tcp_socket.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
// Copyright 2012 The ChromiumOS Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "tcp_socket.h"
#include <algorithm>
#include <assert.h>
#include <string.h>
#include "ppapi/c/pp_errors.h"
#include "ppapi/cpp/module.h"
#include "file_system.h"
TCPSocket::TCPSocket(int fd, int oflag)
: ref_(1), fd_(fd), oflag_(oflag), factory_(this), socket_(NULL),
read_buf_(kBufSize), read_sent_(false), write_sent_(false) {
}
TCPSocket::~TCPSocket() {
assert(!socket_);
assert(!ref_);
}
void TCPSocket::addref() {
++ref_;
}
void TCPSocket::release() {
if (!--ref_)
delete this;
}
FileStream* TCPSocket::dup(int fd) {
return NULL;
}
bool TCPSocket::connect(const char* host, uint16_t port) {
int32_t result = PP_OK_COMPLETIONPENDING;
pp::Module::Get()->core()->CallOnMainThread(0,
factory_.NewCallback(&TCPSocket::Connect, host, port, &result));
FileSystem* sys = FileSystem::GetFileSystem();
while (result == PP_OK_COMPLETIONPENDING)
sys->cond().wait(sys->mutex());
return result == PP_OK;
}
bool TCPSocket::accept(PP_Resource resource) {
int32_t result = PP_OK_COMPLETIONPENDING;
pp::Module::Get()->core()->CallOnMainThread(0,
factory_.NewCallback(&TCPSocket::Accept, resource, &result));
FileSystem* sys = FileSystem::GetFileSystem();
while (result == PP_OK_COMPLETIONPENDING)
sys->cond().wait(sys->mutex());
return result == PP_OK;
}
void TCPSocket::close() {
if (socket_) {
int32_t result = PP_OK_COMPLETIONPENDING;
pp::Module::Get()->core()->CallOnMainThread(0,
factory_.NewCallback(&TCPSocket::Close, &result));
FileSystem* sys = FileSystem::GetFileSystem();
while (result == PP_OK_COMPLETIONPENDING)
sys->cond().wait(sys->mutex());
}
}
int TCPSocket::read(char* buf, size_t count, size_t* nread) {
if (is_block()) {
FileSystem* sys = FileSystem::GetFileSystem();
while (in_buf_.empty() && is_open())
sys->cond().wait(sys->mutex());
}
*nread = std::min(count, in_buf_.size());
if (*nread) {
std::copy(in_buf_.begin(), in_buf_.begin() + *nread, buf);
in_buf_.erase(in_buf_.begin(), in_buf_.begin() + *nread);
}
if (*nread == 0) {
if (!is_open()) {
return 0;
} else {
*nread = -1;
return EAGAIN;
}
}
PostReadTask();
return 0;
}
int TCPSocket::write(const char* buf, size_t count, size_t* nwrote) {
if (!is_open())
return EIO;
out_buf_.insert(out_buf_.end(), buf, buf + count);
if (is_block()) {
int32_t result = PP_OK_COMPLETIONPENDING;
PostWriteTask(&result, true);
FileSystem* sys = FileSystem::GetFileSystem();
while (result == PP_OK_COMPLETIONPENDING)
sys->cond().wait(sys->mutex());
if ((size_t)result != count) {
*nwrote = -1;
return EIO;
} else {
*nwrote = count;
return 0;
}
} else {
PostWriteTask(NULL, true);
*nwrote = count;
return 0;
}
}
int TCPSocket::fcntl(int cmd, va_list ap) {
if (cmd == F_GETFL) {
return oflag_;
} else if (cmd == F_SETFL) {
oflag_ = va_arg(ap, long);
return 0;
} else {
return -1;
}
}
bool TCPSocket::is_read_ready() {
return !is_open() || !in_buf_.empty();
}
bool TCPSocket::is_write_ready() {
return !is_open() || out_buf_.size() < kBufSize;
}
bool TCPSocket::is_exception() {
return !is_open();
}
void TCPSocket::PostReadTask() {
if (is_open() && !read_sent_ && in_buf_.size() < kBufSize / 2) {
read_sent_ = true;
if (!pp::Module::Get()->core()->IsMainThread()) {
pp::Module::Get()->core()->CallOnMainThread(
0, factory_.NewCallback(&TCPSocket::Read));
} else {
// If on main Pepper thread and delay is not required call it directly.
Read(PP_OK);
}
}
}
void TCPSocket::PostWriteTask(int32_t* pres, bool always_post) {
if (is_open() && !write_sent_ && !out_buf_.empty()) {
write_sent_ = true;
if (always_post || !pp::Module::Get()->core()->IsMainThread()) {
pp::Module::Get()->core()->CallOnMainThread(0,
factory_.NewCallback(&TCPSocket::Write, pres));
} else {
// If on main Pepper thread and delay is not required call it directly.
Write(PP_OK, pres);
}
}
}
void TCPSocket::Connect(int32_t result, const char* host, uint16_t port,
int32_t* pres) {
FileSystem* sys = FileSystem::GetFileSystem();
Mutex::Lock lock(sys->mutex());
assert(!socket_);
socket_ = new pp::TCPSocketPrivate(sys->instance());
*pres = socket_->Connect(host, port,
factory_.NewCallback(&TCPSocket::OnConnect, pres));
if (*pres != PP_OK_COMPLETIONPENDING)
sys->cond().broadcast();
}
void TCPSocket::OnConnect(int32_t result, int32_t* pres) {
FileSystem* sys = FileSystem::GetFileSystem();
Mutex::Lock lock(sys->mutex());
if (result == PP_OK) {
PostReadTask();
} else {
delete socket_;
socket_ = NULL;
}
*pres = result;
sys->cond().broadcast();
}
void TCPSocket::Read(int32_t result) {
FileSystem* sys = FileSystem::GetFileSystem();
Mutex::Lock lock(sys->mutex());
if (!is_open()) {
read_sent_ = false;
sys->cond().broadcast();
return;
}
result = socket_->Read(&read_buf_[0], read_buf_.size(),
factory_.NewCallback(&TCPSocket::OnRead));
if (result != PP_OK_COMPLETIONPENDING) {
delete socket_;
socket_ = NULL;
read_sent_ = false;
sys->cond().broadcast();
}
}
void TCPSocket::OnRead(int32_t result) {
FileSystem* sys = FileSystem::GetFileSystem();
Mutex::Lock lock(sys->mutex());
read_sent_ = false;
if (!is_open()) {
sys->cond().broadcast();
return;
}
if (result > 0) {
in_buf_.insert(in_buf_.end(),
read_buf_.begin(), read_buf_.begin() + result);
PostReadTask();
} else {
delete socket_;
socket_ = NULL;
}
sys->cond().broadcast();
}
void TCPSocket::Write(int32_t result, int32_t* pres) {
FileSystem* sys = FileSystem::GetFileSystem();
Mutex::Lock lock(sys->mutex());
if (!is_open()) {
if (pres)
*pres = PP_ERROR_FAILED;
write_sent_ = false;
sys->cond().broadcast();
return;
}
if (write_buf_.size()) {
// Previous write operation is in progress.
PostWriteTask(pres, true);
return;
}
assert(out_buf_.size());
write_buf_.swap(out_buf_);
result = socket_->Write(&write_buf_[0], write_buf_.size(),
factory_.NewCallback(&TCPSocket::OnWrite, pres));
if (result != PP_OK_COMPLETIONPENDING) {
LOG("TCPSocket::Write: failed %d %d %d\n", fd_, result, write_buf_.size());
delete socket_;
socket_ = NULL;
if (pres)
*pres = result;
write_sent_ = false;
sys->cond().broadcast();
}
}
void TCPSocket::OnWrite(int32_t result, int32_t* pres) {
FileSystem* sys = FileSystem::GetFileSystem();
Mutex::Lock lock(sys->mutex());
write_sent_ = false;
if (!is_open()) {
if (pres)
*pres = PP_ERROR_FAILED;
sys->cond().broadcast();
return;
}
if (result < 0 || (size_t)result > write_buf_.size()) {
// Write error.
LOG("TCPSocket::OnWrite: close socket %d\n", fd_);
delete socket_;
socket_ = NULL;
} else if ((size_t)result < write_buf_.size()) {
// Partial write. Insert remaining bytes at the beginning of out_buf_.
out_buf_.insert(out_buf_.begin(), &write_buf_[result], &*write_buf_.end());
}
if (pres)
*pres = result;
write_buf_.clear();
sys->cond().broadcast();
if (!is_block()) {
// For async sockets some more data could be written while Pepper sends
// previous portion so check do we have some data to write. For sync case,
// we always wait write operation completion.
PostWriteTask(NULL, false);
}
}
void TCPSocket::Close(int32_t result, int32_t* pres) {
FileSystem* sys = FileSystem::GetFileSystem();
Mutex::Lock lock(sys->mutex());
delete socket_;
socket_ = NULL;
if (pres)
*pres = PP_OK;
sys->cond().broadcast();
}
bool TCPSocket::Accept(int32_t result, PP_Resource resource, int32_t* pres) {
FileSystem* sys = FileSystem::GetFileSystem();
Mutex::Lock lock(sys->mutex());
assert(!socket_);
socket_ = new pp::TCPSocketPrivate(pp::PassRef(), resource);
PostReadTask();
*pres = PP_OK;
sys->cond().broadcast();
return true;
}