• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.
4 
5 #include "nacl_io/socket/tcp_event_emitter.h"
6 
7 #include <poll.h>
8 #include <stdint.h>
9 #include <stdlib.h>
10 
11 #include "nacl_io/fifo_char.h"
12 #include "sdk_util/auto_lock.h"
13 
14 namespace nacl_io {
15 
TcpEventEmitter(size_t rsize,size_t wsize)16 TcpEventEmitter::TcpEventEmitter(size_t rsize, size_t wsize)
17     : in_fifo_(rsize),
18       out_fifo_(wsize),
19       error_(false),
20       listening_(false),
21       accepted_socket_(0) {
22 }
23 
ReadIn_Locked(char * data,uint32_t len)24 uint32_t TcpEventEmitter::ReadIn_Locked(char* data, uint32_t len) {
25   uint32_t count = in_fifo_.Read(data, len);
26   UpdateStatus_Locked();
27   return count;
28 }
29 
UpdateStatus_Locked()30 void TcpEventEmitter::UpdateStatus_Locked() {
31   if (error_) {
32     RaiseEvents_Locked(POLLIN | POLLOUT);
33     return;
34   }
35 
36   if (listening_) {
37     if (accepted_socket_)
38       RaiseEvents_Locked(POLLIN);
39     return;
40   }
41 
42   StreamEventEmitter::UpdateStatus_Locked();
43 }
44 
SetListening_Locked()45 void TcpEventEmitter::SetListening_Locked() {
46   listening_ = true;
47   UpdateStatus_Locked();
48 }
49 
WriteIn_Locked(const char * data,uint32_t len)50 uint32_t TcpEventEmitter::WriteIn_Locked(const char* data, uint32_t len) {
51   uint32_t count = in_fifo_.Write(data, len);
52 
53   UpdateStatus_Locked();
54   return count;
55 }
56 
ReadOut_Locked(char * data,uint32_t len)57 uint32_t TcpEventEmitter::ReadOut_Locked(char* data, uint32_t len) {
58   uint32_t count = out_fifo_.Read(data, len);
59 
60   UpdateStatus_Locked();
61   return count;
62 }
63 
WriteOut_Locked(const char * data,uint32_t len)64 uint32_t TcpEventEmitter::WriteOut_Locked(const char* data, uint32_t len) {
65   uint32_t count = out_fifo_.Write(data, len);
66 
67   UpdateStatus_Locked();
68   return count;
69 }
70 
ConnectDone_Locked()71 void TcpEventEmitter::ConnectDone_Locked() {
72   RaiseEvents_Locked(POLLOUT);
73   UpdateStatus_Locked();
74 }
75 
GetError_Locked()76 bool TcpEventEmitter::GetError_Locked() {
77   return error_;
78 }
79 
SetError_Locked()80 void TcpEventEmitter::SetError_Locked() {
81   error_ = true;
82   UpdateStatus_Locked();
83 }
84 
SetAcceptedSocket_Locked(PP_Resource socket)85 void TcpEventEmitter::SetAcceptedSocket_Locked(PP_Resource socket) {
86   accepted_socket_ = socket;
87   UpdateStatus_Locked();
88 }
89 
GetAcceptedSocket_Locked()90 PP_Resource TcpEventEmitter::GetAcceptedSocket_Locked() {
91   int rtn = accepted_socket_;
92   accepted_socket_ = 0;
93   UpdateStatus_Locked();
94   return rtn;
95 }
96 
BytesInOutputFIFO()97 uint32_t TcpEventEmitter::BytesInOutputFIFO() {
98   return out_fifo()->ReadAvailable();
99 }
100 
SpaceInInputFIFO()101 uint32_t TcpEventEmitter::SpaceInInputFIFO() {
102   return in_fifo()->WriteAvailable();
103 }
104 
105 }  // namespace nacl_io
106