132
|
1 /*******************************************************************************
|
|
2
|
|
3 copyright: Copyright (c) 2007 Kris Bell. All rights reserved
|
|
4
|
|
5 license: BSD style: $(LICENSE)
|
|
6
|
|
7 version: Jan 2007 : initial release
|
|
8
|
|
9 author: Kris
|
|
10
|
|
11 *******************************************************************************/
|
|
12
|
|
13 module tango.io.protocol.NativeProtocol;
|
|
14
|
|
15 private import tango.io.Buffer;
|
|
16
|
|
17 private import tango.io.protocol.model.IProtocol;
|
|
18
|
|
19 /*******************************************************************************
|
|
20
|
|
21 *******************************************************************************/
|
|
22
|
|
23 class NativeProtocol : IProtocol
|
|
24 {
|
|
25 protected bool prefix_;
|
|
26 protected IBuffer buffer_;
|
|
27
|
|
28 /***********************************************************************
|
|
29
|
|
30 ***********************************************************************/
|
|
31
|
|
32 this (IConduit conduit, bool prefix=true)
|
|
33 {
|
|
34 this.prefix_ = prefix;
|
|
35
|
|
36 auto b = cast(Buffered) conduit;
|
|
37 buffer_ = b ? b.buffer : new Buffer(conduit);
|
|
38 }
|
|
39
|
|
40 /***********************************************************************
|
|
41
|
|
42 ***********************************************************************/
|
|
43
|
|
44 IBuffer buffer ()
|
|
45 {
|
|
46 return buffer_;
|
|
47 }
|
|
48
|
|
49 /***********************************************************************
|
|
50
|
|
51 ***********************************************************************/
|
|
52
|
|
53 void[] read (void* dst, uint bytes, Type type)
|
|
54 {
|
|
55 return buffer_.readExact (dst, bytes);
|
|
56 }
|
|
57
|
|
58 /***********************************************************************
|
|
59
|
|
60 ***********************************************************************/
|
|
61
|
|
62 void write (void* src, uint bytes, Type type)
|
|
63 {
|
|
64 buffer_.append (src, bytes);
|
|
65 }
|
|
66
|
|
67 /***********************************************************************
|
|
68
|
|
69 ***********************************************************************/
|
|
70
|
|
71 void[] readArray (void* dst, uint bytes, Type type, Allocator alloc)
|
|
72 {
|
|
73 if (prefix_)
|
|
74 {
|
|
75 read (&bytes, bytes.sizeof, Type.UInt);
|
|
76 return alloc (&read, bytes, type);
|
|
77 }
|
|
78
|
|
79 return read (dst, bytes, type);
|
|
80 }
|
|
81
|
|
82 /***********************************************************************
|
|
83
|
|
84 ***********************************************************************/
|
|
85
|
|
86 void writeArray (void* src, uint bytes, Type type)
|
|
87 {
|
|
88 if (prefix_)
|
|
89 write (&bytes, bytes.sizeof, Type.UInt);
|
|
90
|
|
91 write (src, bytes, type);
|
|
92 }
|
|
93 }
|
|
94
|
|
95
|
|
96
|
|
97 /*******************************************************************************
|
|
98
|
|
99 *******************************************************************************/
|
|
100
|
|
101 debug (UnitTest)
|
|
102 {
|
|
103 import tango.io.Buffer;
|
|
104 import tango.io.protocol.Writer;
|
|
105 import tango.io.protocol.Reader;
|
|
106 import tango.io.protocol.NativeProtocol;
|
|
107
|
|
108 unittest
|
|
109 {
|
|
110 auto protocol = new NativeProtocol (new Buffer(32));
|
|
111 auto input = new Reader (protocol);
|
|
112 auto output = new Writer (protocol);
|
|
113
|
|
114 char[] foo;
|
|
115 output ("testing testing 123"c);
|
|
116 input (foo);
|
|
117 assert (foo == "testing testing 123");
|
|
118 }
|
|
119 }
|
|
120
|
|
121 |