1 /**
2 Copyright 2018 Mark Fisher
3 
4 Permission is hereby granted, free of charge, to any person obtaining a copy of
5 this software and associated documentation files (the "Software"), to deal in
6 the Software without restriction, including without limitation the rights to
7 use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
8 of the Software, and to permit persons to whom the Software is furnished to do
9 so, subject to the following conditions:
10 
11 The above copyright notice and this permission notice shall be included in all
12 copies or substantial portions of the Software.
13 
14 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20 SOFTWARE.
21 **/
22 module dxx.util.stream;
23 
24 private import dxx.util.notify;
25 
26 
27 
28 interface Closeable {
29   void onClose(void delegate());
30 }
31 
32 interface Channel : Closeable {
33   void onOpen(void delegate());
34 }
35 
36 interface InputChannel : Channel {
37     void onRead(void delegate(byte[]));
38 }
39 
40 interface OutputChannel : Channel {
41     void onWrite(byte[] delegate());
42 }
43 
44 interface IOChannel : InputChannel,OutputChannel {
45 }
46 
47 interface ConnectionChannel : Channel {
48     void onConnect(void delegate(IOChannel));
49 }
50 
51 class ChannelImpl : SyncNotificationSource,Channel {
52   void delegate()* _onClose;
53   void onClose(void delegate() d) {
54     _onClose = &d;
55   }
56   void delegate()* _onOpen;
57   void onOpen(void delegate() d) {
58     _onOpen = &d;
59   }
60   void close() {
61     if(_onClose !is null) {
62       (*_onClose)();
63     }
64   }
65   void open() {
66     if(_onOpen !is null) {
67       (*_onOpen)();
68     }
69   }
70 }
71 
72 class InputChannelImpl : ChannelImpl,InputChannel {
73   void delegate(byte[])* _onRead;
74   void onRead(void delegate(byte[]) d) {
75     _onRead = &d;
76   }
77   void read(byte[] b) {
78     if(_onRead !is null) {
79       (*_onRead)(b);
80     }
81   }
82 }
83 class OutputChannelImpl : ChannelImpl,OutputChannel {
84   byte[] delegate()* _onWrite;
85   void onWrite(byte[] delegate() d) {
86     _onWrite = &d;
87   }
88   byte[] write() {
89     if(_onWrite !is null) {
90       return (*_onWrite)();
91     }
92     return null;
93   }
94 }
95 class IOChannelImpl : ChannelImpl,IOChannel {
96   void delegate(byte[])* _onRead;
97   void onRead(void delegate(byte[]) d) {
98     _onRead = &d;
99   }
100   void read(byte[] data) {
101     if(_onRead !is null) {
102       (*_onRead)(data);
103     }
104   }
105   byte[] delegate()* _onWrite;
106   void onWrite(byte[] delegate() d) {
107     _onWrite = &d;
108   }
109   byte[] write() {
110     if(_onWrite !is null) {
111       return (*_onWrite)();
112     }
113     return null;
114   }
115 }
116 class ConnectionChannelImpl : ChannelImpl,ConnectionChannel {
117   void delegate(IOChannel)* _onConnect;
118   void onConnect(void delegate(IOChannel) d) {
119     _onConnect = &d;
120   }
121   void connect(IOChannel c) {
122     if(_onConnect !is null) {
123       (*_onConnect)(c);
124     }
125   }
126 }