vibe.stream.stdio 0/73(0%) line coverage

      
10
20
30
40
50
60
70
80
90
100
110
120
130
140
150
160
170
180
190
200
210
220
230
240
250
260
270
280
290
300
310
320
330
340
350
360
370
380
390
400
410
420
430
440
450
460
470
480
490
500
510
520
530
540
550
560
570
580
590
600
610
620
630
640
650
660
670
680
690
700
710
720
730
740
750
760
770
780
790
800
810
820
830
840
850
860
870
880
890
900
910
920
930
940
950
960
970
980
990
1000
1010
1020
1030
1040
1050
1060
1070
1080
1090
1100
1110
1120
1130
1140
1150
1160
1170
1180
1190
1200
1210
1220
1230
1240
1250
1260
1270
1280
1290
1300
1310
1320
1330
1340
1350
1360
1370
1380
1390
1400
1410
1420
1430
1440
1450
1460
1470
1480
1490
1500
1510
1520
1530
1540
1550
1560
1570
1580
1590
1600
1610
1620
1630
1640
1650
1660
1670
1680
1690
1700
1710
1720
1730
1740
1750
1760
1770
1780
1790
1800
1810
1820
1830
1840
1850
1860
1870
1880
1890
/** Standard I/O streams Copyright: © 2014 RejectedSoftware e.K. License: Subject to the terms of the MIT license, as written in the included LICENSE.txt file. Authors: Sönke Ludwig, Eric Cornelius */ module vibe.stream.stdio; import vibe.core.core; import vibe.core.stream; import vibe.stream.taskpipe; import std.stdio; import core.thread; import std.exception; class StdFileStream : ConnectionStream { private { std.stdio.File m_file; TaskPipe m_readPipe; TaskPipe m_writePipe; Thread m_readThread; Thread m_writeThread; } this(bool read, bool write) { if (read) m_readPipe = new TaskPipe; if (write) m_writePipe = new TaskPipe; } void setup(std.stdio.File file) { m_file = file; if (m_readPipe) { m_readThread = new Thread(&readThreadFunc); m_readThread.name = "StdFileStream reader"; m_readThread.start(); } if (m_writePipe) { m_writeThread = new Thread(&writeThreadFunc); m_writeThread.name = "StdFileStream writer"; m_writeThread.start(); } } override @property bool empty() { enforceReadable(); return m_readPipe.empty; } override @property ulong leastSize() { enforceReadable(); return m_readPipe.leastSize; } override @property bool dataAvailableForRead() { enforceReadable(); return m_readPipe.dataAvailableForRead; } override @property bool connected() const { return m_readPipe.connected; } override void close() { m_writePipe.close(); } override bool waitForData(Duration timeout) { return m_readPipe.waitForData(timeout); } override const(ubyte)[] peek() { enforceReadable(); return m_readPipe.peek(); } override size_t read(scope ubyte[] dst, IOMode mode) { enforceReadable(); return m_readPipe.read(dst, mode); } alias read = ConnectionStream.read; override size_t write(in ubyte[] bytes_, IOMode mode) { enforceWritable(); return m_writePipe.write(bytes_, mode); } alias write = ConnectionStream.write; override void flush() { enforceWritable(); m_writePipe.flush(); } override void finalize() { enforceWritable(); if (!m_writePipe.connected) return; flush(); m_writePipe.finalize(); } void enforceReadable() @safe { enforce(m_readPipe, "Stream is not readable!"); } void enforceWritable() @safe { enforce(m_writePipe, "Stream is not writable!"); } private void readThreadFunc() { bool loop_flag = false; runTask({ ubyte[1] buf; scope(exit) { if (m_file.isOpen) m_file.close(); m_readPipe.finalize(); if (loop_flag) exitEventLoop(); else loop_flag = true; } while (!m_file.eof) { auto data = m_file.rawRead(buf); if (!data.length) break; m_readPipe.write(data, IOMode.all); vibe.core.core.yield(); } }); if (!loop_flag) { loop_flag = true; runEventLoop(); } } private void writeThreadFunc() { import std.algorithm : min; bool loop_flag = false; runTask({ ubyte[1024] buf; scope(exit) { if (m_file.isOpen) m_file.close(); if (loop_flag) exitEventLoop(); else loop_flag = true; } while (m_file.isOpen && !m_writePipe.empty) { auto len = min(buf.length, m_writePipe.leastSize); if (!len) break; m_writePipe.read(buf[0 .. len], IOMode.all); m_file.rawWrite(buf[0 .. len]); vibe.core.core.yield(); } }); if (!loop_flag) { loop_flag = true; runEventLoop(); } } } /** OutputStream that writes to stdout */ final class StdoutStream : StdFileStream { this() { super(false, true); setup(stdout); } } /** OutputStream that writes to stderr */ final class StderrStream : StdFileStream { this() { super(false, true); setup(stderr); } } /** InputStream that reads from stdin */ final class StdinStream : StdFileStream { this() { super(true, false); setup(stdin); } }