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

Side by Side Diff: native_client_sdk/src/libraries/nacl_io/mount_node_tcp.cc

Issue 22587003: [NaCl SDK] Add UDP and TCP Sockets (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Running tests on package Created 7 years, 4 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) 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
6 #include "nacl_io/ossocket.h"
7 #ifdef PROVIDES_SOCKET_API
8
9 #include <errno.h>
10 #include <string.h>
11
12 #include "nacl_io/mount.h"
13 #include "nacl_io/mount_node_socket.h"
14 #include "nacl_io/mount_node_tcp.h"
15 #include "nacl_io/pepper_interface.h"
16
17 namespace nacl_io {
18
19 MountNodeTCP::MountNodeTCP(Mount* mount) : MountNodeSocket(mount) {}
20
21
22 TCPSocketInterface* MountNodeTCP::TCPSocket() {
23 if (mount_->ppapi() == NULL)
24 return NULL;
25
26 return mount_->ppapi()->GetTCPSocketInterface();
27 }
28
29 Error MountNodeTCP::Init(int flags) {
30 if (TCPSocket() == NULL)
31 return EACCES;
32
33 socket_resource_ = TCPSocket()->Create(mount_->ppapi()->GetInstance());
34 if (0 == socket_resource_)
35 return EACCES;
36
37 return 0;
38 }
39
40 Error MountNodeTCP::Bind(const struct sockaddr* addr, socklen_t len) {
41 AUTO_LOCK(node_lock_);
42
43 if (0 == socket_resource_)
44 return EBADF;
45
46 /* Only bind once. */
47 if (local_addr_ != 0)
48 return EINVAL;
49
50 /* Lie, we won't known until we connect. */
binji 2013/08/09 19:28:06 s/known/know/
noelallen1 2013/08/09 22:53:22 Done.
51 return 0;
52 }
53
54 Error MountNodeTCP::Connect(const struct sockaddr* addr, socklen_t len) {
55 AUTO_LOCK(node_lock_);
56
57 if (0 == socket_resource_)
58 return EBADF;
59
60 if (remote_addr_ != 0)
61 return EISCONN;
62
63 remote_addr_ = SockAddrToResource(addr, len);
64 if (0 == remote_addr_)
65 return EINVAL;
66
67 int err = TCPSocket()->Connect(socket_resource_,
68 remote_addr_,
69 PP_BlockUntilComplete());
70
71 if (err == PP_OK) {
72 local_addr_ = TCPSocket()->GetLocalAddress(socket_resource_);
73 mount_->ppapi()->AddRefResource(local_addr_);
binji 2013/08/09 19:28:06 did you mean to return early here? Otherwise this
noelallen1 2013/08/09 22:53:22 Done.
74 }
75
76 mount_->ppapi()->ReleaseResource(remote_addr_);
77 remote_addr_ = 0;
78 return PPErrorToErrno(err);
79 }
80
81 Error MountNodeTCP::Recv(void* buf, size_t len, int flags, int* out_len) {
82 AUTO_LOCK(node_lock_);
83 if (0 == socket_resource_)
84 return EBADF;
85
86 int err = TCPSocket()->Read(socket_resource_,
binji 2013/08/09 19:28:06 nit: extra space after =
87 static_cast<char*>(buf),
88 len & 0xFFFF, /* Max 64K at a time. */
binji 2013/08/09 19:28:06 this seems arbitrary, why?
noelallen1 2013/08/09 22:53:22 Max IP "packet" size is 64K. Until we support str
binji 2013/08/09 23:08:27 OK, but you may want to use std::min then, this wi
89 PP_BlockUntilComplete());
90 if (err < 0)
91 return PPErrorToErrno(err);
92
93 *out_len = err;
94 return 0;
95 }
96
97 Error MountNodeTCP::RecvFrom(void* buf,
98 size_t len,
99 int flags,
100 struct sockaddr* src_addr,
101 socklen_t* addrlen,
102 int* out_len) {
103 Error err = Recv(buf, len, flags, out_len);
104 if (err == 0)
105 GetPeerName(src_addr, addrlen);
106 return err;
107 }
108
109
110 Error MountNodeTCP::Send(const void* buf, size_t len, int flags, int* out_len) {
111 AUTO_LOCK(node_lock_);
112
113 if (0 == socket_resource_)
114 return EBADF;
115
116 if (0 == remote_addr_)
117 return ENOTCONN;
118
119 int err = TCPSocket()->Write(socket_resource_,
120 static_cast<const char*>(buf),
121 len & 0xFFFF, /* Max 64K at a time. */
122 PP_BlockUntilComplete());
123 if (err >= 0) {
binji 2013/08/09 19:28:06 Invert this to match similar check in Recv() above
noelallen1 2013/08/09 22:53:22 Done.
124 *out_len = err;
125 return 0;
126 }
127
128 return PPErrorToErrno(err);
129 }
130
131 Error MountNodeTCP::SendTo(const void* buf,
132 size_t len,
133 int flags,
134 const struct sockaddr* dest_addr,
135 socklen_t addrlen,
136 int* out_len) {
137 return Send(buf, len, flags, out_len);
138 }
139
140 } // namespace nacl_io
141
142 #endif // PROVIDES_SOCKET_API
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698