diff --git a/message.go b/message.go index 371eba8..1211897 100644 --- a/message.go +++ b/message.go @@ -65,6 +65,18 @@ func Decode(data []byte, m *Message) error { // // Message, its fields, results of m.Get or any attribute a.GetFrom // are valid only until Message.Raw is not modified. +// +// Usage: +// var m Message +// if err := m.Build(TransactionID, BindingRequest, Fingerprint); err != nil { +// panic(err) +// } +// You can call Build on zero value, and m.Raw will contain on-the-wire +// representation of Message. To decode message from byte slice, use Decode: +// var m Message +// if err := Decode(buf, &m); err != nil { +// panic(err) +// } type Message struct { Type MessageType Length uint32 // len(Raw) not including header @@ -92,7 +104,7 @@ func (m *Message) NewTransactionID() error { return err } -func (m *Message) String() string { +func (m Message) String() string { tID := base64.StdEncoding.EncodeToString(m.TransactionID[:]) return fmt.Sprintf("%s l=%d attrs=%d id=%s", m.Type, m.Length, len(m.Attributes), tID) } diff --git a/message_test.go b/message_test.go index 71415c5..32bf0b3 100644 --- a/message_test.go +++ b/message_test.go @@ -671,6 +671,20 @@ func TestMessage_Contains(t *testing.T) { } } +func TestMessage_Build(t *testing.T) { + var m Message + if err := m.Build(TransactionID, BindingRequest, Fingerprint); err != nil { + panic(err) + } + var p Message + if err := Decode(m.Raw, &p); err != nil { + panic(err) + } + if !m.Equal(&p) { + t.Fatal("Not equal") + } +} + func ExampleMessage() { buf := new(bytes.Buffer) m := new(Message) diff --git a/stun.go b/stun.go index b9be735..6bace49 100644 --- a/stun.go +++ b/stun.go @@ -47,5 +47,5 @@ func (transactionIDSetter) AddTo(m *Message) error { return m.NewTransactionID() } -// TransactionID is Setter for m.TransactionID. +// TransactionID sets message transaction id to random value. var TransactionID Setter = transactionIDSetter{}