summaryrefslogtreecommitdiffstats
path: root/mojo/public/dart/src/handle.dart
blob: bf2f74e0efccb9b65cca8e18e3007bdd9d155222 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

part of core;

class _MojoHandleNatives {
  static int register(MojoHandle handle) native "MojoHandle_Register";
  static int close(int handle) native "MojoHandle_Close";
  static int wait(int handle, int signals, int deadline)
      native "MojoHandle_Wait";
  static int waitMany(
      List<int> handles, List<int> signals, int num_handles, int deadline)
      native "MojoHandle_WaitMany";
}


class RawMojoHandle {
  static const int INVALID = 0;
  static const int DEADLINE_INDEFINITE = -1;

  int h;

  RawMojoHandle(this.h);

  MojoResult close() {
    int result = _MojoHandleNatives.close(h);
    h = INVALID;
    return new MojoResult(result);
  }

  MojoResult wait(int signals, int deadline) {
    int result = _MojoHandleNatives.wait(h, signals, deadline);
    return new MojoResult(result);
  }

  bool _ready(int signal) {
    MojoResult res = wait(signal, 0);
    switch (res) {
      case MojoResult.OK:
        return true;
      case MojoResult.DEADLINE_EXCEEDED:
      case MojoResult.CANCELLED:
      case MojoResult.INVALID_ARGUMENT:
      case MojoResult.FAILED_PRECONDITION:
        return false;
      default:
        // Should be unreachable.
        throw new Exception("Unreachable");
    }
  }

  bool readyRead() => _ready(MojoHandleSignals.READABLE);
  bool readyWrite() => _ready(MojoHandleSignals.WRITABLE);

  static int waitMany(List<int> handles,
                      List<int> signals,
                      int deadline) {
    if (handles.length != signals.length) {
      return MojoResult.kInvalidArgument;
    }
    return _MojoHandleNatives.waitMany(
        handles, signals, handles.length, deadline);
  }

  static MojoResult register(MojoHandle handle) {
    return new MojoResult(_MojoHandleNatives.register(handle));
  }

  bool get isValid => (h != INVALID);

  String toString() => "$h";

  bool operator ==(RawMojoHandle other) {
    return h == other.h;
  }
}


class MojoHandle extends Stream<int> {
  // The underlying Mojo handle.
  RawMojoHandle _handle;

  // Providing our own stream controller allows us to take custom actions when
  // listeners pause/resume/etc. their StreamSubscription.
  StreamController _controller;

  // The send port that we give to the handle watcher to notify us of handle
  // events.
  SendPort _sendPort;

  // The receive port on which we listen and receive events from the handle 
  // watcher.
  ReceivePort _receivePort;

  // The signals on this handle that we're interested in.
  int _signals;

  // Whether the handle has been added to the handle watcher.
  bool _eventHandlerAdded;

  MojoHandle(this._handle) : 
      _signals = MojoHandleSignals.READABLE,
      _eventHandlerAdded = false {
    MojoResult result = RawMojoHandle.register(this);
    if (!result.isOk) {
      throw new Exception("Failed to register the MojoHandle");
    }
  }

  void close() {
    if (_eventHandlerAdded) {
      MojoHandleWatcher.close(_handle);
      _eventHandlerAdded = false;
    } else {
      // If we're not in the handle watcher, then close the handle manually.
      _handle.close();
    }
    if (_receivePort != null) {
      _receivePort.close();
    }
  }

  // We wrap the callback provided by clients in listen() with some code to
  // handle adding and removing the handle to/from the handle watcher. Because
  // the handle watcher removes this handle whenever it receives an event,
  // we have to re-add it when the callback is finished.
  Function _onDataClosure(origOnData) {
    return ((int event) {
      // The handle watcher removes this handle from its set on an event.
      _eventHandlerAdded = false;
      origOnData(event);

      // The callback could have closed the handle. If so, don't add it back to
      // the MojoHandleWatcher.
      if (_handle.isValid) {
        assert(!_eventHandlerAdded);
        var res = MojoHandleWatcher.add(_handle, _sendPort, _signals);
        if (!res.isOk) {
          throw new Exception("Failed to re-add handle: $res");
        }
        _eventHandlerAdded = true;
      }
    });
  }

  StreamSubscription<int> listen(
      void onData(int event),
      {Function onError, void onDone(), bool cancelOnError}) {
    _receivePort = new ReceivePort();
    _sendPort = _receivePort.sendPort;
    _controller = new StreamController(sync: true,
        onListen: _onSubscriptionStateChange,
        onCancel: _onSubscriptionStateChange,
        onPause: _onPauseStateChange,
        onResume: _onPauseStateChange);
    _controller.addStream(_receivePort);

    assert(!_eventHandlerAdded);
    var res = MojoHandleWatcher.add(_handle, _sendPort, _signals);
    if (!res.isOk) {
      throw new Exception("MojoHandleWatcher add failed: $res");
    }
    _eventHandlerAdded = true;

    return _controller.stream.listen(
        _onDataClosure(onData),
        onError: onError,
        onDone: onDone,
        cancelOnError: cancelOnError);
  }

  bool writeEnabled() => MojoHandleSignals.isWritable(_signals);

  void toggleWriteEvents() {
    _signals = MojoHandleSignals.toggleWrite(_signals);
    if (_eventHandlerAdded) {
      var res = MojoHandleWatcher.toggleWrite(_handle);
      if (!res.isOk) {
        throw new Exception("MojoHandleWatcher failed to toggle write: $res");
      }
    }
  }

  void enableWriteEvents() {
    assert(!writeEnabled());
    toggleWriteEvents();
  }

  void disableWriteEvents() {
    assert(writeEnabled());
    toggleWriteEvents();
  }

  void _onSubscriptionStateChange() {
    if (!_controller.hasListener) {
      close();
    }
  }

  void _onPauseStateChange() {
    if (_controller.isPaused) {
      if (_eventHandlerAdded) {
        var res = MojoHandleWatcher.remove(_handle);
        if (!res.isOk) {
          throw new Exception("MojoHandleWatcher add failed: $res");
        }
        _eventHandlerAdded = false;
      }
    } else {
      if (!_eventHandlerAdded) {
        var res = MojoHandleWatcher.add(_handle, _sendPort, _signals);
        if (!res.isOk) {
          throw new Exception("MojoHandleWatcher add failed: $res");
        }
        _eventHandlerAdded = true;
      }
    }
  }

  String toString() => "$_handle";
}