OLD | NEW |
---|---|
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 | 5 |
6 #include "nacl_io/ossocket.h" | 6 #include "nacl_io/ossocket.h" |
7 #ifdef PROVIDES_SOCKET_API | 7 #ifdef PROVIDES_SOCKET_API |
8 | 8 |
9 #include <assert.h> | 9 #include <assert.h> |
10 #include <errno.h> | 10 #include <errno.h> |
(...skipping 254 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
265 | 265 |
266 node->ConnectDone_Locked(); | 266 node->ConnectDone_Locked(); |
267 } | 267 } |
268 | 268 |
269 protected: | 269 protected: |
270 ScopedEventEmitterTCP emitter_; | 270 ScopedEventEmitterTCP emitter_; |
271 }; | 271 }; |
272 | 272 |
273 MountNodeTCP::MountNodeTCP(Mount* mount) | 273 MountNodeTCP::MountNodeTCP(Mount* mount) |
274 : MountNodeSocket(mount), | 274 : MountNodeSocket(mount), |
275 emitter_(new EventEmitterTCP(kDefaultFifoSize, kDefaultFifoSize)) { | 275 emitter_(new EventEmitterTCP(kDefaultFifoSize, kDefaultFifoSize)), |
276 tcp_nodelay_(false) { | |
276 emitter_->AttachStream(this); | 277 emitter_->AttachStream(this); |
277 } | 278 } |
278 | 279 |
279 MountNodeTCP::MountNodeTCP(Mount* mount, PP_Resource socket) | 280 MountNodeTCP::MountNodeTCP(Mount* mount, PP_Resource socket) |
280 : MountNodeSocket(mount, socket), | 281 : MountNodeSocket(mount, socket), |
281 emitter_(new EventEmitterTCP(kDefaultFifoSize, kDefaultFifoSize)) { | 282 emitter_(new EventEmitterTCP(kDefaultFifoSize, kDefaultFifoSize)), |
283 tcp_nodelay_(false) { | |
282 emitter_->AttachStream(this); | 284 emitter_->AttachStream(this); |
283 } | 285 } |
284 | 286 |
285 void MountNodeTCP::Destroy() { | 287 void MountNodeTCP::Destroy() { |
286 emitter_->DetachStream(); | 288 emitter_->DetachStream(); |
287 MountNodeSocket::Destroy(); | 289 MountNodeSocket::Destroy(); |
288 } | 290 } |
289 | 291 |
290 Error MountNodeTCP::Init(int open_flags) { | 292 Error MountNodeTCP::Init(int open_flags) { |
291 Error err = MountNodeSocket::Init(open_flags); | 293 Error err = MountNodeSocket::Init(open_flags); |
(...skipping 21 matching lines...) Expand all Loading... | |
313 | 315 |
314 EventEmitter* MountNodeTCP::GetEventEmitter() { | 316 EventEmitter* MountNodeTCP::GetEventEmitter() { |
315 return emitter_.get(); | 317 return emitter_.get(); |
316 } | 318 } |
317 | 319 |
318 void MountNodeTCP::SetError_Locked(int pp_error_num) { | 320 void MountNodeTCP::SetError_Locked(int pp_error_num) { |
319 MountNodeSocket::SetError_Locked(pp_error_num); | 321 MountNodeSocket::SetError_Locked(pp_error_num); |
320 emitter_->SetError_Locked(); | 322 emitter_->SetError_Locked(); |
321 } | 323 } |
322 | 324 |
325 Error MountNodeTCP::GetSockOpt(int lvl, | |
326 int optname, | |
327 void* optval, | |
328 socklen_t* len) { | |
329 if (lvl == IPPROTO_TCP && optname == TCP_NODELAY) { | |
330 int value = tcp_nodelay_; | |
331 socklen_t value_len = sizeof(value); | |
332 int copy_bytes = std::min(value_len, *len); | |
333 memcpy(optval, &value, copy_bytes); | |
334 *len = value_len; | |
335 return 0; | |
336 } | |
337 | |
338 return MountNodeSocket::GetSockOpt(lvl, optname, optval, len); | |
339 } | |
340 | |
341 | |
342 Error MountNodeTCP::SetNoDelay() { | |
binji
2013/12/02 20:55:32
should this be called SetNoDelay_Locked? Or does i
Sam Clegg
2013/12/02 22:55:44
Done.
| |
343 if (!IsConnected()) | |
344 return 0; | |
345 | |
346 int32_t error = TCPInterface()->SetOption(socket_resource_, | |
347 PP_TCPSOCKET_OPTION_NO_DELAY, | |
348 PP_MakeBool(tcp_nodelay_ ? PP_TRUE : PP_FALSE), | |
349 PP_BlockUntilComplete()); | |
350 return PPErrorToErrno(error); | |
351 } | |
352 | |
353 Error MountNodeTCP::SetSockOpt(int lvl, | |
354 int optname, | |
355 const void* optval, | |
356 socklen_t len) { | |
357 if (lvl == IPPROTO_TCP && optname == TCP_NODELAY) { | |
358 if (len < sizeof(int)) | |
359 return EINVAL; | |
360 tcp_nodelay_ = *static_cast<const int*>(optval) ? true : false; | |
binji
2013/12/02 20:55:32
nit: I'd prefer
*static_cast<const int*>(optval)
Sam Clegg
2013/12/02 22:55:44
Done.
| |
361 return SetNoDelay(); | |
362 } | |
363 | |
364 return MountNodeSocket::SetSockOpt(lvl, optname, optval, len); | |
365 } | |
366 | |
323 void MountNodeTCP::QueueAccept() { | 367 void MountNodeTCP::QueueAccept() { |
324 MountStream::Work* work = new TCPAcceptWork(mount_stream(), emitter_); | 368 MountStream::Work* work = new TCPAcceptWork(mount_stream(), emitter_); |
325 mount_stream()->EnqueueWork(work); | 369 mount_stream()->EnqueueWork(work); |
326 } | 370 } |
327 | 371 |
328 void MountNodeTCP::QueueConnect() { | 372 void MountNodeTCP::QueueConnect() { |
329 MountStream::Work* work = new TCPConnectWork(mount_stream(), emitter_); | 373 MountStream::Work* work = new TCPConnectWork(mount_stream(), emitter_); |
330 mount_stream()->EnqueueWork(work); | 374 mount_stream()->EnqueueWork(work); |
331 } | 375 } |
332 | 376 |
(...skipping 124 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
457 | 501 |
458 void MountNodeTCP::ConnectDone_Locked() { | 502 void MountNodeTCP::ConnectDone_Locked() { |
459 local_addr_ = TCPInterface()->GetLocalAddress(socket_resource_); | 503 local_addr_ = TCPInterface()->GetLocalAddress(socket_resource_); |
460 | 504 |
461 // Now that we are connected, we can start sending and receiving. | 505 // Now that we are connected, we can start sending and receiving. |
462 ClearStreamFlags(SSF_CONNECTING | SSF_CAN_CONNECT); | 506 ClearStreamFlags(SSF_CONNECTING | SSF_CAN_CONNECT); |
463 SetStreamFlags(SSF_CAN_SEND | SSF_CAN_RECV); | 507 SetStreamFlags(SSF_CAN_SEND | SSF_CAN_RECV); |
464 | 508 |
465 emitter_->ConnectDone_Locked(); | 509 emitter_->ConnectDone_Locked(); |
466 | 510 |
511 // The NODELAY option cannot be set in PPAPI before the socket | |
512 // is connected, but setsockopt() might have already set it. | |
513 SetNoDelay(); | |
514 | |
467 // Begin the input pump | 515 // Begin the input pump |
468 QueueInput(); | 516 QueueInput(); |
469 } | 517 } |
470 | 518 |
471 void MountNodeTCP::ConnectFailed_Locked() { | 519 void MountNodeTCP::ConnectFailed_Locked() { |
472 mount_->ppapi()->ReleaseResource(remote_addr_); | 520 mount_->ppapi()->ReleaseResource(remote_addr_); |
473 remote_addr_ = 0; | 521 remote_addr_ = 0; |
474 } | 522 } |
475 | 523 |
476 Error MountNodeTCP::Listen(int backlog) { | 524 Error MountNodeTCP::Listen(int backlog) { |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
513 if (emitter_->GetError_Locked()) | 561 if (emitter_->GetError_Locked()) |
514 return EPIPE; | 562 return EPIPE; |
515 *out_len = emitter_->WriteOut_Locked((char*)buf, len); | 563 *out_len = emitter_->WriteOut_Locked((char*)buf, len); |
516 return 0; | 564 return 0; |
517 } | 565 } |
518 | 566 |
519 | 567 |
520 } // namespace nacl_io | 568 } // namespace nacl_io |
521 | 569 |
522 #endif // PROVIDES_SOCKET_API | 570 #endif // PROVIDES_SOCKET_API |
OLD | NEW |