diff --git a/dub.json b/dub.json index e853eb5..c4f555c 100644 --- a/dub.json +++ b/dub.json @@ -7,12 +7,10 @@ ], "dependencies": { - "kiss":{ - "path" :"../kiss/" - } + "dlang-snappy": "~>0.0.1", + "flatbuffers": "~>0.1.1", + "kiss" : "~>0.0.7", }, - "license": "Apache-2.0", - "lflags":["-L/usr/local/lib"], - "libs":["snappy"] + "license": "Apache-2.0" } diff --git a/source/KissRpc/RpcBinaryPackage.d b/source/KissRpc/RpcBinaryPackage.d index 17d0196..9ff5ef0 100755 --- a/source/KissRpc/RpcBinaryPackage.d +++ b/source/KissRpc/RpcBinaryPackage.d @@ -4,7 +4,7 @@ import KissRpc.Endian; import KissRpc.Unit; import KissRpc.Logs; -import util.snappy; +import snappy.snappy; import std.stdio; enum RPC_PACKAGE_STATUS_CODE diff --git a/source/KissRpc/RpcClientSocket.d b/source/KissRpc/RpcClientSocket.d index f8e9fc5..7c510f1 100755 --- a/source/KissRpc/RpcClientSocket.d +++ b/source/KissRpc/RpcClientSocket.d @@ -36,20 +36,21 @@ public: return true; } override void onConnectCompleted(void* attachment) { - _socketEventDelegate.socketEvent(this, SOCKET_STATUS.SE_CONNECTD, "connect to server is ok!"); doRead(); + _socketEventDelegate.socketEvent(this, SOCKET_STATUS.SE_CONNECTD, "connect to server is ok!"); } override void onConnectFailed(void* attachment) { writeln("onConnectFailed"); } override void onWriteCompleted(void* attachment, size_t count , ByteBuffer buffer) { - // writeln("write success index ",index++); + // writeln("write success index ",index); } override void onWriteFailed(void* attachment) { - // writeln("onWriteFailed"); + writeln("onWriteFailed"); _socketEventDelegate.socketEvent(this, SOCKET_STATUS.SE_WRITE_FAILED, "write data to server is failed"); } override void onReadCompleted(void* attachment, size_t count , ByteBuffer buffer) { + _packageManage.add(cast(ubyte[])(buffer.getCurBuffer())); _readBuffer.clear(); } diff --git a/source/flatbuffers/bytebuffer.d b/source/flatbuffers/bytebuffer.d deleted file mode 100644 index 4bf5dba..0000000 --- a/source/flatbuffers/bytebuffer.d +++ /dev/null @@ -1,165 +0,0 @@ -/* - * Copyright 2016 Google Inc. All rights reserved. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -module flatbuffers.bytebuffer; - -import std.exception; -import std.bitmanip; -import std.exception; -import core.exception; - -/** - Class warp a ubyte[] to provide byte handle. -*/ - -final class ByteBuffer -{ -public: - this(){} - - /// init ByteBuffer obj with buffer data - this(ubyte[] buffer) - { - restData(buffer,0); - } - - /// Returns buffer length - @property size_t length() - { - return _buffer.length; - } - - /// Returns buffer data - @property ubyte[] data() - { - return _buffer; - } - - /// Returns buffer position - @property size_t position() - { - return _pos; - } - /// put boolen value into buffer - void put(T)(size_t offset, T value) if (is(T == bool)) - { - put!ubyte(offset, (value ? 0x01 : 0x00)); - } - - /// put byte value into buffer - void put(T)(size_t offset, T value) if (isByte!T) - { - mixin(verifyOffset!1); - _buffer[offset] = value; - _pos = offset; - } - - /// put numbirc value into buffer - void put(T)(size_t offset, T value) if (isNum!T) - { - mixin(verifyOffset!(T.sizeof)); - version (FLATBUFFER_BIGENDIAN) - { - auto array = nativeToBigEndian!T(value); - _buffer[offset .. (offset + T.sizeof)] = array[]; - } - else - { - auto array = nativeToLittleEndian!T(value); - _buffer[offset .. (offset + T.sizeof)] = array[]; - } - _pos = offset; - } - - ///get Byte value in buffer from index - T get(T)(size_t index) if (isByte!T) - { - return cast(T) _buffer[index]; - } - - T get(T)(size_t index) if(is(T == bool)) - { - ubyte value = get!ubyte(index); - return (value ==0x01 ? true : false); - } - - T get(T)(size_t index) if (isNum!T) - { - ubyte[T.sizeof] buf = _buffer[index .. (index + T.sizeof)]; - version (FLATBUFFER_BIGENDIAN) - return bigEndianToNative!(T, T.sizeof)(buf); - else - return littleEndianToNative!(T, T.sizeof)(buf); - } - - void restData(ubyte[] buffer,size_t pos){ - _buffer = buffer; - _pos = pos; - } - -private: /// Variables. - ubyte[] _buffer; - size_t _pos; /// Must track start of the buffer. -} - -unittest -{ - ByteBuffer buf = new ByteBuffer(new ubyte[50]); - int a = 10; - buf.put(5, a); - short t = 4; - buf.put(9, t); - - assert(buf.get!int(5) == 10); - assert(buf.get!short(9) == 4); - -} -/*********************************** - * test for boolen value - */ -unittest -{ - ByteBuffer buf = new ByteBuffer(new ubyte[50]); - bool a = true; - buf.put(5, a); - bool b = false; - buf.put(9, b); - assert(buf.get!bool(5) == true); - assert(buf.get!bool(9) == false); - -} -private: -template verifyOffset(size_t length) -{ - enum verifyOffset = "if(offset < 0 || offset >= _buffer.length || (offset + " ~ length.stringof ~ ") > _buffer.length) throw new RangeError();"; -} - -template isNum(T) -{ - static if (is(T == short) || is(T == ushort) || is(T == int) || is(T == uint) - || is(T == long) || is(T == ulong) || is(T == float) || is(T == double)) - enum isNum = true; - else - enum isNum = false; -} - -template isByte(T) -{ - static if (is(T == byte) || is(T == ubyte)) - enum isByte = true; - else - enum isByte = false; -} diff --git a/source/flatbuffers/exception.d b/source/flatbuffers/exception.d deleted file mode 100644 index 1c1f06a..0000000 --- a/source/flatbuffers/exception.d +++ /dev/null @@ -1,51 +0,0 @@ -/* - * Copyright 2016 Google Inc. All rights reserved. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -module flatbuffers.exception; - -import std.exception; - -///The number of bytes in a file identifier. -enum fileIdentifierLength = 4; - -///ArgumentException -class ArgumentException : Exception -{ - this(string msg, string argument) pure nothrow @safe - { - super(msg); - } -} - -///ArgumentOutOfRangeException -class ArgumentOutOfRangeException : Exception -{ - this(string argument, long value, string msg) pure nothrow @safe - { - import std.conv : to; - - super(argument ~ ' ' ~ msg ~ " (instead: " ~ value.to!string ~ ")"); - } -} - -///InvalidOperationException -class InvalidOperationException : Exception -{ - this(string msg) pure nothrow @safe - { - super(msg); - } -} diff --git a/source/flatbuffers/flatbufferbuilder.d b/source/flatbuffers/flatbufferbuilder.d deleted file mode 100644 index bf2c204..0000000 --- a/source/flatbuffers/flatbufferbuilder.d +++ /dev/null @@ -1,392 +0,0 @@ -/* - * Copyright 2016 Google Inc. All rights reserved. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -module flatbuffers.flatbufferbuilder; - -import flatbuffers.exception; -import flatbuffers.bytebuffer; - -import std.exception; -import std.traits : isNumeric; - -/** - Responsible for building up and accessing a FlatBuffer formatted byte -*/ -final class FlatBufferBuilder -{ - /** - Create a FlatBufferBuilder with a given initial size. - Params: - initsize = The initial size to use for the internal buffer. - */ - this(size_t initsize = 32) - in{ - assert(initsize > 0); - }body{ - this(new ByteBuffer(new ubyte[initsize])); - } - - this(ByteBuffer buffer){ - _space = buffer.length; - _buffer = buffer; - } - - uint offset() - { - return cast(uint)(_buffer.length - _space); - } - - void pad(size_t size) - { - for (int i = 0; i < size; i++) - { - --_space; - _buffer.put!ubyte(_space, 0x00); - } - } - - /** Doubles the size of the ByteBuffer, and copies the old data towards - the end of the new buffer (since we build the buffer backwards). - */ - void growBuffer() - { - auto oldBuf = _buffer.data; - auto oldBufSize = oldBuf.length; - if ((oldBufSize & 0xC0000000) != 0) - throw new Exception("FlatBuffers: cannot grow buffer beyond 2 gigabytes."); - - auto newBufSize = oldBufSize >= 32 ? oldBufSize * 2 : 64; - auto newBuf = new ubyte[](newBufSize); - newBuf[(newBufSize - oldBufSize) .. $] = oldBuf[]; - _buffer.restData(newBuf,0); - } - - /** - Prepare to write an element of `size` after `additional_bytes` - have been written, e.g. if you write a string, you need to align - such the int length field is aligned to SIZEOF_INT, and the string - data follows it directly. - If all you need to do is align, `additional_bytes` will be 0. - */ - void prep(size_t size, size_t additionalBytes) - { - // Track the biggest thing we've ever aligned to. - if (size > _minAlign) - _minAlign = size; - - // Find the amount of alignment needed such that `size` is properly - // aligned after `additional_bytes`. - auto alignSize = ((~( _buffer.length - _space + additionalBytes)) + 1) & (size - 1); - - // Reallocate the buffer if needed. - while (_space < alignSize + size + additionalBytes) - { - auto oldBufSize = cast(int) _buffer.length; - growBuffer(); - _space += cast(int) _buffer.length - oldBufSize; - } - if (alignSize > 0) - pad(alignSize); - } - - /** - put a value into the buffer. - */ - void put(T)(T x) if (is(T == bool) || isNumeric!T) - { - static if (is(T == bool)) - { - _space -= 1; - } - else - { - _space -= T.sizeof; - } - _buffer.put!T(_space, x); - } - - /// Adds a scalar to the buffer, properly aligned, and the buffer grown if needed. - void add(T)(T x)if (is(T == bool) || isNumeric!T) - { - static if (is(T == bool)) - prep(1, 0); - else - prep(T.sizeof, 0); - put!T(x); - } - /// Adds on offset, relative to where it will be written. - void addOffset(uint off) - { - prep(uint.sizeof, 0); // Ensure alignment is already done. - if (off > offset()) - throw new ArgumentException("FlatBuffers: must be less than offset.", "off"); - - off = offset() - off + cast(uint)uint.sizeof; - put!uint(off); - } - - void startVector(int elemSize, int count, int alignment) - { - notNested(); - _vectorNumElems = count; - prep(int.sizeof, elemSize * count); - prep(alignment, elemSize * count); // Just in case alignment > int. - } - - uint endVector() - { - put!int(cast(int)_vectorNumElems); - return offset(); - } - - void nested(int obj) - { - // Structs are always stored inline, so need to be created right - // where they are used. You'll get this assert if you created it - // elsewhere. - if (obj != offset()) - throw new Exception("FlatBuffers: struct must be serialized inline."); - } - - void notNested() - { - // You should not be creating any other objects or strings/vectors - // while an object is being constructed. - if (_vtable) - throw new Exception("FlatBuffers: object serialization must not be nested."); - } - - void startObject(int numfields) - { - if (numfields < 0) - throw new ArgumentOutOfRangeException("numfields", numfields, - "must be greater than zero"); - - notNested(); - _vtable = new size_t[](numfields); - _objectStart = offset(); - } - - /// Set the current vtable at `voffset` to the current location in the buffer. - void slot(size_t voffset) - { - _vtable[voffset] = offset(); - } - - /// Add a scalar to a table at `o` into its vtable, with value `x` and default `d`. - void add(T : bool)(size_t o, T x, T d) - { - if (x != d) - { - add!T(x); - slot(o); - } - } - /// ditto - void add(T)(size_t o, T x, T d) if(isNumeric!T) - { - if (x != d) - { - add!T(x); - slot(o); - } - } - /// ditto - void addOffset(int o, int x, int d) - { - if (x != d) - { - addOffset(x); - slot(o); - } - } - - /** Structs are stored inline, so nothing additional is being added. - `d` is always 0. - */ - void addStruct(int voffset, int x, int d) - { - if (x != d) - { - nested(x); - slot(voffset); - } - } - - /** - Encode the string `s` in the buffer using UTF-8. - Params: - s = The string to encode. - Returns: - The offset in the buffer where the encoded string starts. - */ - uint createString(string s) - { - notNested(); - auto utf8 = cast(ubyte[]) s; - add!ubyte(cast(ubyte) 0); - startVector(1, cast(int) utf8.length, 1); - _space -= utf8.length; - _buffer.data[_space .. _space + utf8.length] = utf8[]; - return endVector(); - } - - uint endObject() - { - if (!_vtable) - throw new InvalidOperationException( - "Flatbuffers: calling endObject without a startObject"); - - add!int(cast(int) 0); - auto vtableloc = offset(); - - // Write out the current vtable. - for (int i = cast(int) _vtable.length - 1; i >= 0; i--) - { - // Offset relative to the start of the table. - short off = cast(short)(_vtable[i] != 0 ? vtableloc - _vtable[i] : 0); - add!short(off); - } - - const int standardFields = 2; // The fields below: - add!short(cast(short)(vtableloc - _objectStart)); - add!short(cast(short)((_vtable.length + standardFields) * short.sizeof)); - - /// Search for an existing vtable that matches the current one. - size_t existingVtable = 0; - - ubyte[] data = _buffer.data(); - - for (int i = 0; i < _numVtables; i++) - { - auto vt1 = _buffer.length - _vtables[i]; - auto vt2 = _space; - short vt1len = _buffer.get!short(vt1); - short vt2len = _buffer.get!short(vt2); - - if (vt1len != vt2len || data[vt1 .. (vt1 + vt1len)] != data[vt2 .. (vt2 + vt2len)]) - continue; - existingVtable = _vtables[i]; - } - - if (existingVtable != 0) - { - // Found a match: - // Remove the current vtable. - _space = _buffer.length - vtableloc; - // Point table to existing vtable. - _buffer.put!int(_space, cast(int)(existingVtable - vtableloc)); - } - else - { - // No match: - // Add the location of the current vtable to the list of vtables. - if (_numVtables == _vtables.length) - _vtables.length *= 2; - _vtables[_numVtables++] = offset(); - // Point table to current vtable. - _buffer.put!int(_buffer.length - vtableloc, offset() - vtableloc); - } - - destroy(_vtable); - _vtable = null; - return vtableloc; - } - - /** This checks a required field has been set in a given table that has - just been constructed. - */ - void required(int table, int field) - { - import std.string; - - auto table_start = _buffer.length - table; - auto vtable_start = table_start - _buffer.get!int(table_start); - bool ok = _buffer.get!short(vtable_start + field) != 0; - // If this fails, the caller will show what field needs to be set. - if (!ok) - throw new InvalidOperationException(format("FlatBuffers: field %s must be set.", - field)); - } - - /** - Finalize a buffer, pointing to the given `root_table`. - Params: - rootTable = An offset to be added to the buffer. - */ - void finish(int rootTable) - { - prep(_minAlign, int.sizeof); - addOffset(rootTable); - } - - /** - Get the ByteBuffer representing the FlatBuffer. - Notes: his is typically only called after you call `Finish()`. - Returns: - Returns the ByteBuffer for this FlatBuffer. - */ - ByteBuffer dataBuffer() - { - return _buffer; - } - - /** - A utility function to copy and return the ByteBuffer data as a `ubyte[]` - Retuens: - the byte used in FlatBuffer data, it is not copy. - */ - ubyte[] sizedByteArray() - { - return _buffer.data[_buffer.position .. $]; - } - - /** - Finalize a buffer, pointing to the given `rootTable`. - Params: - rootTable = An offset to be added to the buffer. - fileIdentifier = A FlatBuffer file identifier to be added to the buffer before `root_table`. - */ - void finish(int rootTable, string fileIdentifier) - { - import std.string; - - prep(_minAlign, int.sizeof + fileIdentifierLength); - if (fileIdentifier.length != fileIdentifierLength) - throw new ArgumentException( - format("FlatBuffers: file identifier must be length %s.", fileIdentifierLength), - "fileIdentifier"); - for (int i = fileIdentifierLength - 1; i >= 0; i--) - add!ubyte(cast(ubyte) fileIdentifier[i]); - addOffset(rootTable); - } - -private: - size_t _space; - ByteBuffer _buffer; - size_t _minAlign = 1; - - /// The vtable for the current table, null otherwise. - size_t[] _vtable; - /// Starting offset of the current struct/table. - size_t _objectStart; - /// List of offsets of all vtables. - size_t[] _vtables = new int[](16); - /// Number of entries in `vtables` in use. - size_t _numVtables = 0; - /// For the current vector being built. - size_t _vectorNumElems = 0; -} diff --git a/source/flatbuffers/package.d b/source/flatbuffers/package.d deleted file mode 100644 index cda1442..0000000 --- a/source/flatbuffers/package.d +++ /dev/null @@ -1,22 +0,0 @@ -/* - * Copyright 2016 Google Inc. All rights reserved. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -module flatbuffers; - -public import flatbuffers.exception; -public import flatbuffers.bytebuffer; -public import flatbuffers.table; -public import flatbuffers.flatbufferbuilder; \ No newline at end of file diff --git a/source/flatbuffers/table.d b/source/flatbuffers/table.d deleted file mode 100644 index 36b9a87..0000000 --- a/source/flatbuffers/table.d +++ /dev/null @@ -1,210 +0,0 @@ -/* - * Copyright 2016 Google Inc. All rights reserved. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -module flatbuffers.table; - -import flatbuffers.exception; -import flatbuffers.bytebuffer; -public import std.typecons; - -/// Mixin this template to all structs in the generated code derive , and add their own accessors. -mixin template Struct(ParentType) -{ - /** - Create this Struct. - */ - static ParentType init_(size_t pos, ByteBuffer buffer) - { - return ParentType(buffer, pos); - } - -private: // Variables. - /** - disable the constor. - */ - @disable this(); - this(ByteBuffer buffer, size_t pos) - { - this._buffer = buffer; - this._pos = pos; - } - - ByteBuffer _buffer; - size_t _pos; -} - -/// Mixin this template to all tables in the generated code derive , and add their own accessors. -mixin template Table(ParentType) -{ - /** - Create this Struct as a Table. - */ - static ParentType init_(size_t pos, ByteBuffer buffer) - { - return ParentType(buffer, pos); - } - -private: - ByteBuffer _buffer; - size_t _pos; - -private: // Methods. - @disable this(); - this(ByteBuffer buffer, size_t pos) - { - this._buffer = buffer; - this._pos = pos; - } - - /// Look up a field in the vtable, return an offset into the object, or 0 if the field is not present. - uint __offset(size_t vtableOffset) - { - auto vtable = _pos - _buffer.get!int(_pos); - return vtableOffset < _buffer.get!short(vtable) ? cast( - uint) _buffer.get!short(vtable + vtableOffset) : 0; - } - - /// Retrieve the relative offset stored at "offset". - uint __indirect(size_t offset) - { - return cast(uint)(offset + _buffer.get!int(offset)); - } - - /// Create a D string from UTF-8 data stored inside the flatbuffer. - string __string(size_t offset) - { - offset += _buffer.get!int(offset); - auto len = _buffer.get!uint(offset); - auto startPos = offset + uint.sizeof; - return cast(string) _buffer.data[startPos .. startPos + len]; - } - - /// Get the length of a vector whose offset is stored at "offset" in this object. - uint __vector_len(size_t offset) - { - offset += _pos; - offset += _buffer.get!int(offset); - return _buffer.get!uint(offset); - } - - /// Get the start of data of a vector whose offset is stored at "offset" in this object. - uint __dvector(size_t offset) - { - offset += _pos; - return cast(uint)(offset + _buffer.get!int(offset) + int.sizeof); // Data starts after the length. - } - - /// Initialize any Table-derived type to point to the union at the given offset. - T __union(T)(size_t offset) - { - offset += _pos; - return T.init_((offset + _buffer.get!int(offset)), _buffer); - } - - static bool __has_identifier(ByteBuffer bb, string ident) - { - import std.string; - - if (ident.length != fileIdentifierLength) - throw new ArgumentException( - format("FlatBuffers: file identifier must be length %s.", fileIdentifierLength), - "ident"); - - for (auto i = 0; i < fileIdentifierLength; i++) - { - if (ident[i] != cast(char) bb.get!byte(bb.position() + cast(uint)uint.sizeof + i)) - return false; - } - - return true; - } -} - -import std.traits; - -/** - Iterator for the vector. -*/ -struct Iterator(ParentType, ReturnType, string accessor) -{ - static if (isScalarType!(ReturnType) || isSomeString!(ReturnType)) - alias ApplyType = ReturnType; - else - alias ApplyType = Nullable!ReturnType; -private: - ParentType parent; - int index; -public: - this(ParentType parent) - { - this.parent = parent; - } - - @property int length() - { - mixin("return parent." ~ accessor ~ "Length;"); - } - - @property bool empty() - { - return index == length; - } - - @property ReturnType popFront() - { - mixin("return parent." ~ accessor ~ "(++index);"); - } - - @property ReturnType front() - { - mixin("return parent." ~ accessor ~ "(index);"); - } - - ReturnType opIndex(int index) - { - mixin("return parent." ~ accessor ~ "(index);"); - } - - int opApply(int delegate(ApplyType) operations) - { - int result = 0; - for (int number = 0; number < length(); ++number) - { - static if (isScalarType!(ReturnType) || isSomeString!(ReturnType)) - result = operations(opIndex(number)); - else - mixin("result = operations(parent." ~ accessor ~ "(number));"); - if (result) - break; - } - return result; - } - - int opApply(int delegate(int, ApplyType) operations) - { - int result = 0; - for (int number = 0; number < length(); ++number) - { - static if (isScalarType!(ReturnType) || isSomeString!(ReturnType)) - result = operations(number, opIndex(number)); - else - mixin("result = operations(number, parent." ~ accessor ~ "(number));"); - if (result) - break; - } - return result; - } -} diff --git a/source/util/snappy.d b/source/util/snappy.d deleted file mode 100644 index b5643bc..0000000 --- a/source/util/snappy.d +++ /dev/null @@ -1,76 +0,0 @@ -module util.snappy; - -import std.conv; - -extern (C) { - enum snappy_status { - SNAPPY_OK = 0, - SNAPPY_INVALID_INPUT = 1, - SNAPPY_BUFFER_TOO_SMALL = 2, - }; - - snappy_status snappy_uncompressed_length(const byte* compressed, - size_t compressed_length, - size_t* result); - - snappy_status snappy_uncompress(const byte* compressed, - size_t compressed_length, - byte* uncompressed, - size_t* uncompressed_length); - snappy_status snappy_compress(const byte* input, - size_t input_length, - byte* compressed, - size_t* compressed_length); - size_t snappy_max_compressed_length(size_t source_length); -} - -class Snappy { - -static byte[] uncompress(byte[] compressed) { - size_t uncompressedPrediction; - snappy_status ok = snappy_uncompressed_length(compressed.ptr, compressed.length, &uncompressedPrediction); - if (ok != snappy_status.SNAPPY_OK) { - throw new Exception(to!(string)(ok)); - } - auto res = new byte[uncompressedPrediction]; - size_t uncompressed = uncompressedPrediction; - ok = snappy_uncompress(compressed.ptr, compressed.length, res.ptr, &uncompressed); - if (ok != snappy_status.SNAPPY_OK) { - throw new Exception(to!(string)(ok)); - } - if (uncompressed != uncompressedPrediction) { - throw new Exception("uncompressedPrediction " ~ to!(string)(uncompressedPrediction) ~ " != " ~ "uncompressed " ~ to!(string)(uncompressed)); - } - return res; - } - - - - static byte[] compress(byte[] uncompressed) { - size_t maxCompressedSize = snappy_max_compressed_length(uncompressed.length); - byte[] res = new byte[maxCompressedSize]; - size_t compressedSize = maxCompressedSize; - snappy_status ok = snappy_compress(uncompressed.ptr, uncompressed.length, res.ptr, &compressedSize); - if (ok != snappy_status.SNAPPY_OK) { - throw new Exception(to!(string)(ok)); - } - return res[0..compressedSize]; - } - -} - - -unittest{ - import std.stdio; - import util.snappy; - - byte[] data = cast(byte[])"ffdsffffffffffffffffaaaaaaaaaaaaaaaaaaccccccccccccccccccccccccdddddddddddddddddddeeeeeeeeeeeeeeeeeeeeeee"; - writeln("-------------------------------------------------"); - writefln("start test compress data, length:%s", data.length); - - byte[] cprData = Snappy.compress(data); - writefln("compress data, length:%s, data:%s", cprData.length, cprData); - - byte[] unData = Snappy.uncompress(cprData); - writefln("uncompress data, length:%s, data:%s", unData.length, unData); -} \ No newline at end of file diff --git a/source/util/ssl.tar.gz b/source/util/ssl.tar.gz deleted file mode 100644 index 29cb164..0000000 Binary files a/source/util/ssl.tar.gz and /dev/null differ