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

Side by Side Diff: libraries/nacl-mounts/net/TcpServerSocket.cc

Issue 10392070: Socket subsystem implementation (Closed) Base URL: http://naclports.googlecode.com/svn/trunk/src/
Patch Set: Created 8 years, 6 months 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 | Annotate | Revision Log
OLDNEW
(Empty)
1 // Copyright (c) 2012 The Chromium OS 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 "net/TcpServerSocket.h"
6
7 #include <assert.h>
8 #include <string.h>
9
10 #include "include/ppapi/cpp/private/net_address_private.h"
11 #include "net/SocketSubSystem.h"
12 #include "ppapi/c/pp_errors.h"
13 #include "ppapi/cpp/module.h"
14 #include "util/DebugPrint.h"
15
16 TCPServerSocket::TCPServerSocket(SocketSubSystem* sys, int oflag,
17 const char* host, uint16_t port)
18 : ref_(1), oflag_(oflag), factory_(this), socket_(NULL),
19 host_(host), port_(port), resource_(0), sys(sys) {
20 }
21
22 TCPServerSocket::~TCPServerSocket() {
23 assert(!socket_);
24 assert(!ref_);
25 }
26
27 void TCPServerSocket::addref() {
28 ++ref_;
29 }
30
31 void TCPServerSocket::release() {
32 if (!--ref_)
33 delete this;
34 }
35
36 int TCPServerSocket::read(char* buf, size_t count, size_t* nread) {
37 return -1;
38 }
39
40 int TCPServerSocket::write(const char* buf, size_t count, size_t* nwrote) {
41 return -1;
42 }
43
44 void TCPServerSocket::close() {
45 if (socket_) {
46 int32_t result = PP_OK_COMPLETIONPENDING;
47 pp::Module::Get()->core()->CallOnMainThread(0,
48 factory_.NewCallback(&TCPServerSocket::Close, &result));
49 while (result == PP_OK_COMPLETIONPENDING)
50 sys->cond().wait(sys->mutex());
51 }
52 }
53
54 int TCPServerSocket::fcntl(int cmd, va_list ap) {
55 if (cmd == F_GETFL) {
56 return oflag_;
57 } else if (cmd == F_SETFL) {
58 oflag_ = va_arg(ap, long);
59 return 0;
60 } else {
61 return -1;
62 }
63 }
64
65 bool TCPServerSocket::is_read_ready() {
66 return !is_open() || resource_;
67 }
68
69 bool TCPServerSocket::is_write_ready() {
70 return !is_open();
71 }
72
73 bool TCPServerSocket::is_exception() {
74 return !is_open();
75 }
76
77 bool TCPServerSocket::listen(int backlog) {
78 int32_t result = PP_OK_COMPLETIONPENDING;
79 pp::Module::Get()->core()->CallOnMainThread(0,
80 factory_.NewCallback(&TCPServerSocket::Listen, backlog, &result));
81 while (result == PP_OK_COMPLETIONPENDING)
82 sys->cond().wait(sys->mutex());
83 return result == PP_OK;
84 }
85
86 PP_Resource TCPServerSocket::accept() {
87 if (!resource_)
88 return 0;
89
90 PP_Resource ret = resource_;
91 resource_ = 0;
92 pp::Module::Get()->core()->CallOnMainThread(0,
93 factory_.NewCallback(&TCPServerSocket::Accept,
94 static_cast<int32_t*>(NULL)));
95
96 return ret;
97 }
98
99 void TCPServerSocket::Listen(int32_t result, int backlog, int32_t* pres) {
100 Mutex::Lock lock(sys->mutex());
101 assert(!socket_);
102 socket_ = new pp::TCPServerSocketPrivate(sys->instance());
103
104 // Ignore bind address and always bind to localhost.
Dmitry Polukhin 2012/05/29 15:00:51 Not sure that in general lib we need bind for loca
vissi 2012/05/30 08:10:51 What else?
105 uint8_t localhost_ip[4] = { 127, 0, 0, 1 };
106 PP_NetAddress_Private localhost = {};
107 if (pp::NetAddressPrivate::CreateFromIPv4Address(
108 localhost_ip, port_, &localhost)) {
109 LOG("TCPServerSocket::Listen: %s\n",
110 pp::NetAddressPrivate::Describe(localhost, true).c_str());
111 *pres = socket_->Listen(&localhost, backlog,
112 factory_.NewCallback(&TCPServerSocket::Accept, pres));
113 } else {
114 *pres = PP_ERROR_FAILED;
115 }
116 if (*pres != PP_OK_COMPLETIONPENDING) {
117 sys->cond().broadcast();
118 }
119 }
120
121 void TCPServerSocket::Accept(int32_t result, int32_t* pres) {
122 Mutex::Lock lock(sys->mutex());
123 assert(socket_);
124 if (result == PP_OK) {
125 result = socket_->Accept(&resource_,
126 factory_.NewCallback(&TCPServerSocket::OnAccept));
127 if (result == PP_OK_COMPLETIONPENDING)
128 result = PP_OK;
129 }
130 if (pres)
131 *pres = result;
132 sys->cond().broadcast();
133 }
134
135 void TCPServerSocket::OnAccept(int32_t result) {
136 Mutex::Lock lock(sys->mutex());
137 assert(socket_);
138 sys->cond().broadcast();
139 }
140
141 void TCPServerSocket::Close(int32_t result, int32_t* pres) {
142 Mutex::Lock lock(sys->mutex());
143 delete socket_;
144 socket_ = NULL;
145 *pres = PP_OK;
146 sys->cond().broadcast();
147 }
148
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698