Mercurial > projects > ldc
diff tango/tango/io/protocol/NativeProtocol.d @ 132:1700239cab2e trunk
[svn r136] MAJOR UNSTABLE UPDATE!!!
Initial commit after moving to Tango instead of Phobos.
Lots of bugfixes...
This build is not suitable for most things.
author | lindquist |
---|---|
date | Fri, 11 Jan 2008 17:57:40 +0100 |
parents | |
children |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/tango/tango/io/protocol/NativeProtocol.d Fri Jan 11 17:57:40 2008 +0100 @@ -0,0 +1,121 @@ +/******************************************************************************* + + copyright: Copyright (c) 2007 Kris Bell. All rights reserved + + license: BSD style: $(LICENSE) + + version: Jan 2007 : initial release + + author: Kris + +*******************************************************************************/ + +module tango.io.protocol.NativeProtocol; + +private import tango.io.Buffer; + +private import tango.io.protocol.model.IProtocol; + +/******************************************************************************* + +*******************************************************************************/ + +class NativeProtocol : IProtocol +{ + protected bool prefix_; + protected IBuffer buffer_; + + /*********************************************************************** + + ***********************************************************************/ + + this (IConduit conduit, bool prefix=true) + { + this.prefix_ = prefix; + + auto b = cast(Buffered) conduit; + buffer_ = b ? b.buffer : new Buffer(conduit); + } + + /*********************************************************************** + + ***********************************************************************/ + + IBuffer buffer () + { + return buffer_; + } + + /*********************************************************************** + + ***********************************************************************/ + + void[] read (void* dst, uint bytes, Type type) + { + return buffer_.readExact (dst, bytes); + } + + /*********************************************************************** + + ***********************************************************************/ + + void write (void* src, uint bytes, Type type) + { + buffer_.append (src, bytes); + } + + /*********************************************************************** + + ***********************************************************************/ + + void[] readArray (void* dst, uint bytes, Type type, Allocator alloc) + { + if (prefix_) + { + read (&bytes, bytes.sizeof, Type.UInt); + return alloc (&read, bytes, type); + } + + return read (dst, bytes, type); + } + + /*********************************************************************** + + ***********************************************************************/ + + void writeArray (void* src, uint bytes, Type type) + { + if (prefix_) + write (&bytes, bytes.sizeof, Type.UInt); + + write (src, bytes, type); + } +} + + + +/******************************************************************************* + +*******************************************************************************/ + +debug (UnitTest) +{ + import tango.io.Buffer; + import tango.io.protocol.Writer; + import tango.io.protocol.Reader; + import tango.io.protocol.NativeProtocol; + + unittest + { + auto protocol = new NativeProtocol (new Buffer(32)); + auto input = new Reader (protocol); + auto output = new Writer (protocol); + + char[] foo; + output ("testing testing 123"c); + input (foo); + assert (foo == "testing testing 123"); + } +} + + \ No newline at end of file