Skip to content
This repository has been archived by the owner on Jun 5, 2021. It is now read-only.

Commit

Permalink
message: improve docs
Browse files Browse the repository at this point in the history
  • Loading branch information
ernado committed Jan 2, 2020
1 parent 495aa7d commit 84e5334
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 2 deletions.
14 changes: 13 additions & 1 deletion message.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
}
Expand Down
14 changes: 14 additions & 0 deletions message_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion stun.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{}

0 comments on commit 84e5334

Please sign in to comment.