1 module osc.packet;
2 
3 import osc.message;
4 import osc.bundle;
5 
6 //TODO support for bundle
7 /++
8 +/
9 struct Packet {
10     public{
11         ///
12         this(in Message message){
13             _hasMessage = true;
14             _message = message;
15         }
16 
17         ///
18         this(in Bundle bundle){
19             _hasBundle = true;
20             _bundle = bundle;
21         }
22         
23         ///
24         this(in ubyte[] packet){
25             _hasBundle = packet[0] == 0x23;
26             _hasMessage= packet[0] != 0x23;
27             
28             if(_hasBundle){
29                 _bundle = Bundle(packet);
30             }
31             if(_hasMessage){
32                 _message = Message(packet);
33             }
34         }
35 
36         ///
37         string toString()const{
38             return _opCast!string;
39         }
40 
41         ///
42         T opCast(T:ubyte[])()const{
43             return _opCast!(ubyte[])();
44         }
45 
46         ///
47         const(Bundle) bundle()const{
48             return _bundle;
49         }
50         
51         ///
52         const(Message) message()const{
53             return _message;
54         }
55         
56         bool hasMessage()const{return _hasMessage;}
57         bool hasBundle()const{return _hasBundle;}
58     }//public
59 
60     private{
61         bool _hasMessage;
62         bool _hasBundle;
63 
64         const Bundle _bundle;
65         const Message _message;
66 
67         T _opCast(T)()const{
68             import std.conv;
69             T r;
70             if(_hasMessage){
71                 r = _message.to!T;
72             }else if(_hasBundle){
73                 r = _bundle.to!T;
74             }
75             return r;
76         }
77     }//private
78 }//struct Packet
79 unittest{
80     //TODO
81     Message message = Message();
82     message.addressPattern = "/foo";
83     message.addValue(1000);
84     message.addValue(-1);
85     message.addValue("hello");
86     message.addValue(1.234f);
87     message.addValue(5.678f);
88     Packet packet = Packet(message);
89     import std.conv;
90     ubyte[] b = packet.to!(ubyte[]);
91     assert(b.length != 0);
92 }