Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / native_client_sdk / src / libraries / nacl_io / socket / tcp_event_emitter.cc
blobcd9e8694541d3840276e730164107096d528b952
1 // Copyright 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.
5 #include "nacl_io/socket/tcp_event_emitter.h"
7 #include <poll.h>
8 #include <stdint.h>
9 #include <stdlib.h>
11 #include "nacl_io/fifo_char.h"
12 #include "sdk_util/auto_lock.h"
14 namespace nacl_io {
16 TcpEventEmitter::TcpEventEmitter(size_t rsize, size_t wsize)
17 : in_fifo_(rsize),
18 out_fifo_(wsize),
19 error_(false),
20 listening_(false),
21 recv_endofstream_(false),
22 accepted_socket_(0) {
25 uint32_t TcpEventEmitter::ReadIn_Locked(char* data, uint32_t len) {
26 uint32_t count = in_fifo_.Read(data, len);
27 UpdateStatus_Locked();
28 return count;
31 void TcpEventEmitter::UpdateStatus_Locked() {
32 if (error_) {
33 RaiseEvents_Locked(POLLIN | POLLOUT);
34 return;
37 if (recv_endofstream_) {
38 RaiseEvents_Locked(POLLIN);
39 return;
42 if (listening_) {
43 if (accepted_socket_)
44 RaiseEvents_Locked(POLLIN);
45 return;
48 StreamEventEmitter::UpdateStatus_Locked();
51 void TcpEventEmitter::SetListening_Locked() {
52 listening_ = true;
53 UpdateStatus_Locked();
56 uint32_t TcpEventEmitter::WriteIn_Locked(const char* data, uint32_t len) {
57 uint32_t count = in_fifo_.Write(data, len);
59 UpdateStatus_Locked();
60 return count;
63 uint32_t TcpEventEmitter::ReadOut_Locked(char* data, uint32_t len) {
64 uint32_t count = out_fifo_.Read(data, len);
66 UpdateStatus_Locked();
67 return count;
70 uint32_t TcpEventEmitter::WriteOut_Locked(const char* data, uint32_t len) {
71 uint32_t count = out_fifo_.Write(data, len);
73 UpdateStatus_Locked();
74 return count;
77 void TcpEventEmitter::ConnectDone_Locked() {
78 RaiseEvents_Locked(POLLOUT);
79 UpdateStatus_Locked();
82 bool TcpEventEmitter::GetError_Locked() {
83 return error_;
86 void TcpEventEmitter::SetError_Locked() {
87 error_ = true;
88 UpdateStatus_Locked();
91 void TcpEventEmitter::SetAcceptedSocket_Locked(PP_Resource socket) {
92 accepted_socket_ = socket;
93 UpdateStatus_Locked();
96 PP_Resource TcpEventEmitter::GetAcceptedSocket_Locked() {
97 int rtn = accepted_socket_;
98 accepted_socket_ = 0;
99 UpdateStatus_Locked();
100 return rtn;
103 void TcpEventEmitter::SetRecvEndOfStream_Locked() {
104 recv_endofstream_ = true;
105 UpdateStatus_Locked();
108 uint32_t TcpEventEmitter::BytesInOutputFIFO() {
109 return out_fifo()->ReadAvailable();
112 uint32_t TcpEventEmitter::SpaceInInputFIFO() {
113 return in_fifo()->WriteAvailable();
116 } // namespace nacl_io