- add sources.
[platform/framework/web/crosswalk.git] / src / native_client_sdk / src / libraries / nacl_io / event_emitter_tcp.cc
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 #include "nacl_io/event_emitter_tcp.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
16 EventEmitterTCP::EventEmitterTCP(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
24 uint32_t EventEmitterTCP::ReadIn_Locked(char* data, uint32_t len) {
25   uint32_t count = in_fifo_.Read(data, len);
26   UpdateStatus_Locked();
27   return count;
28 }
29
30 void EventEmitterTCP::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   EventEmitterStream::UpdateStatus_Locked();
43 }
44
45 void EventEmitterTCP::SetListening_Locked() {
46   listening_ = true;
47   UpdateStatus_Locked();
48 }
49
50 uint32_t EventEmitterTCP::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
57 uint32_t EventEmitterTCP::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
64 uint32_t EventEmitterTCP::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
71 void EventEmitterTCP::ConnectDone_Locked() {
72   RaiseEvents_Locked(POLLOUT);
73   UpdateStatus_Locked();
74 }
75
76 bool EventEmitterTCP::GetError_Locked() {
77   return error_;
78 }
79
80 void EventEmitterTCP::SetError_Locked() {
81   error_ = true;
82   UpdateStatus_Locked();
83 }
84
85 void EventEmitterTCP::SetAcceptedSocket_Locked(PP_Resource socket) {
86   accepted_socket_ = socket;
87   UpdateStatus_Locked();
88 }
89
90 PP_Resource EventEmitterTCP::GetAcceptedSocket_Locked() {
91   int rtn = accepted_socket_;
92   accepted_socket_ = 0;
93   UpdateStatus_Locked();
94   return rtn;
95 }
96
97 }  // namespace nacl_io