-
Notifications
You must be signed in to change notification settings - Fork 33
/
tcp_server_socket.cc
157 lines (131 loc) · 4.01 KB
/
tcp_server_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
// 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_server_socket.h"
#include <assert.h>
#include <string.h>
#include "ppapi/c/pp_errors.h"
#include "ppapi/cpp/module.h"
#include "ppapi/cpp/private/net_address_private.h"
#include "file_system.h"
TCPServerSocket::TCPServerSocket(int fd, int oflag,
const sockaddr* saddr, socklen_t addrlen)
: ref_(1), fd_(fd), oflag_(oflag), factory_(this), socket_(NULL),
sin6_(), resource_(0) {
assert(sizeof(sin6_) >= addrlen);
memcpy(&sin6_, saddr, std::min(sizeof(sin6_), addrlen));
}
TCPServerSocket::~TCPServerSocket() {
assert(!socket_);
assert(!ref_);
}
void TCPServerSocket::addref() {
++ref_;
}
void TCPServerSocket::release() {
if (!--ref_)
delete this;
}
FileStream* TCPServerSocket::dup(int fd) {
return NULL;
}
int TCPServerSocket::read(char* buf, size_t count, size_t* nread) {
return -1;
}
int TCPServerSocket::write(const char* buf, size_t count, size_t* nwrote) {
return -1;
}
void TCPServerSocket::close() {
if (socket_) {
int32_t result = PP_OK_COMPLETIONPENDING;
pp::Module::Get()->core()->CallOnMainThread(0,
factory_.NewCallback(&TCPServerSocket::Close, &result));
FileSystem* sys = FileSystem::GetFileSystem();
while (result == PP_OK_COMPLETIONPENDING)
sys->cond().wait(sys->mutex());
}
}
int TCPServerSocket::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 TCPServerSocket::is_read_ready() {
return !is_open() || resource_;
}
bool TCPServerSocket::is_write_ready() {
return !is_open();
}
bool TCPServerSocket::is_exception() {
return !is_open();
}
bool TCPServerSocket::listen(int backlog) {
int32_t result = PP_OK_COMPLETIONPENDING;
pp::Module::Get()->core()->CallOnMainThread(0,
factory_.NewCallback(&TCPServerSocket::Listen, backlog, &result));
FileSystem* sys = FileSystem::GetFileSystem();
while (result == PP_OK_COMPLETIONPENDING)
sys->cond().wait(sys->mutex());
return result == PP_OK;
}
PP_Resource TCPServerSocket::accept() {
if (!resource_)
return 0;
PP_Resource ret = resource_;
resource_ = 0;
pp::Module::Get()->core()->CallOnMainThread(0,
factory_.NewCallback(&TCPServerSocket::Accept,
static_cast<int32_t*>(NULL)));
return ret;
}
void TCPServerSocket::Listen(int32_t result, int backlog, int32_t* pres) {
FileSystem* sys = FileSystem::GetFileSystem();
Mutex::Lock lock(sys->mutex());
assert(!socket_);
socket_ = new pp::TCPServerSocketPrivate(sys->instance());
PP_NetAddress_Private addr = {};
if (FileSystem::CreateNetAddress(reinterpret_cast<const sockaddr*>(&sin6_),
sizeof(sin6_), &addr)) {
LOG("TCPServerSocket::Listen: %s\n",
pp::NetAddressPrivate::Describe(addr, true).c_str());
*pres = socket_->Listen(&addr, backlog,
factory_.NewCallback(&TCPServerSocket::Accept, pres));
} else {
*pres = PP_ERROR_FAILED;
}
if (*pres != PP_OK_COMPLETIONPENDING)
sys->cond().broadcast();
}
void TCPServerSocket::Accept(int32_t result, int32_t* pres) {
FileSystem* sys = FileSystem::GetFileSystem();
Mutex::Lock lock(sys->mutex());
assert(socket_);
if (result == PP_OK) {
result = socket_->Accept(&resource_,
factory_.NewCallback(&TCPServerSocket::OnAccept));
if (result == PP_OK_COMPLETIONPENDING)
result = PP_OK;
}
if (pres)
*pres = result;
sys->cond().broadcast();
}
void TCPServerSocket::OnAccept(int32_t result) {
FileSystem* sys = FileSystem::GetFileSystem();
Mutex::Lock lock(sys->mutex());
assert(socket_);
sys->cond().broadcast();
}
void TCPServerSocket::Close(int32_t result, int32_t* pres) {
FileSystem* sys = FileSystem::GetFileSystem();
Mutex::Lock lock(sys->mutex());
delete socket_;
socket_ = NULL;
*pres = PP_OK;
sys->cond().broadcast();
}