https://github.com/halturin/erlang-llsn
Erlang support for LLSN (binary format)
https://github.com/halturin/erlang-llsn
binaryformat erlang library llsn
Last synced: 10 months ago
JSON representation
Erlang support for LLSN (binary format)
- Host: GitHub
- URL: https://github.com/halturin/erlang-llsn
- Owner: halturin
- License: mit
- Created: 2015-07-28T12:58:52.000Z (almost 11 years ago)
- Default Branch: master
- Last Pushed: 2017-06-07T10:01:30.000Z (almost 9 years ago)
- Last Synced: 2025-03-26T11:51:10.523Z (about 1 year ago)
- Topics: binaryformat, erlang, library, llsn
- Language: Erlang
- Homepage:
- Size: 432 KB
- Stars: 11
- Watchers: 3
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
erlang-LLSN
======
[](https://github.com/allyst/erlang-llsn/releases)
This is Erlang implementation of LLSN binary format.
Here is specifiaction: http://allyst.org/opensource/llsn/
Basics
------
...
```Erlang
Struct = {?LLSN_TYPE_NUMBER, ?LLSN_TYPE_STRING},
Packet = {12345, "Hello World!"},
Bin = llsn:encode(Packet, Struct).
```
...
```Erlang
Struct = {
?LLSN_TYPE_NUMBER, ?LLSN_TYPE_STRING,
{?LLSN_TYPE_ARRAY, ?LLSN_TYPE_NUMBER}
},
Packet = {12345, "Hello World!", [1,2,3,4,5]},
Bin = llsn:encode(Packet, Struct).
```
...
```Erlang
Struct = {
?LLSN_TYPE_NUMBER, ?LLSN_TYPE_STRING,
{?LLSN_TYPE_ARRAYN, ?LLSN_TYPE_NUMBER}
},
Packet = {12345, "Hello World!", [1,2,null,null,5]},
Bin = llsn:encode(Packet, Struct).
```
...
```Erlang
Struct = {
?LLSN_TYPE_NUMBER, ?LLSN_TYPE_STRING,
{?LLSN_TYPE_ARRAYN, ?LLSN_TYPE_NUMBER},
{?LLSN_TYPE_STRUCT,[?LLSN_TYPE_BOOL, ?LLSN_TYPE_DATE]}
},
Packet = {
12345, "Hello World!",
[1,2,null,null,5],
{false, {{2015, 4, 15},{16, 56, 39, 678},{0,0}}}
},
Bin = llsn:encode(Packet, Struct).
```
Extended features of data encoding
-----------------------
#### Tree-like data
...
C-example of tree like data structure
```C
typedef TreeStruct struct {
int value,
TreeStruct *child
}
```
In LLSN you have to use LLSN_TYPE_POINTER.
```Erlang
Struct = {
?LLSN_TYPE_NUMBER, ?LLSN_TYPE_STRING,
{?LLSN_TYPE_ARRAYN, ?LLSN_TYPE_NUMBER},
{?LLSN_TYPE_STRUCT,[?LLSN_TYPE_BOOL, ?LLSN_TYPE_DATE]},
{?LLSN_TYPE_STRUCT,[?LLSN_TYPE_NUMBER, {?LLSN_TYPE_POINTER,[3]}]}
},
Packet = {
12345, "Hello World!",
[1,2,null,null,5],
{false, {{2015, 4, 15},{16, 56, 39, 678},{0,0}},
{1,{2,{3,null}}}}
},
Bin = llsn:encode(Packet, Struct).
```
#### Data framing
...
```Erlang
Bin = llsn:encode(Packet, Struct, self()).
```
Default frame size has value 49152 bytes (as LLSN_DEFAULT_FRAME_LIMIT definition in .erl file). You
can specify custom value
```Erlang
Bin = llsn:encode(Packet, Struct, self(), 128000).
```
... 3th argument is Pid of receiver process.
```Erlang
receive_frame(Bin) ->
receive
{frame, _N, _Size, Frame, UserData } ->
?assert(UserData =:= [userdata]),
receive_frame(<>);
{done, _N, _Size, Frame, UserData} ->
?assert(UserData =:= [userdata]),
<>
after 1000 -> ?assert("timeout")
end.
...
demoframe() ->
...
ok = llsn:encode(Packet, Struct, self(), 50, [userdata]),
Bin = receive_frame(<<>>),
```
#### Decode partitioned data
It's able to decode part of encoded data starting from the first frame.
```Erlang
case decode(Bin) of
{parted, State1} ->
% part of the packet
...
Packet ->
% complete packet
...
end
...
```
To continue decoding you have to use State1 and the next part of binary data
```Erlang
Value = decode(continue, State1, NextFrame)
```
It's useful in case of transfer big files. For example: array of Images.
License
--------
The MIT License (MIT)