-
Notifications
You must be signed in to change notification settings - Fork 79
/
gtp.go
54 lines (46 loc) · 1.24 KB
/
gtp.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
// Copyright 2019-2024 go-gtp authors. All rights reserved.
// Use of this source code is governed by a MIT-style license that can be
// found in the LICENSE file.
package gtp
import (
v0msg "github.com/wmnsk/go-gtp/gtpv0/message"
v1msg "github.com/wmnsk/go-gtp/gtpv1/message"
v2msg "github.com/wmnsk/go-gtp/gtpv2/message"
)
// Message is an interface that defines all versions of GTP message.
type Message interface {
MarshalTo([]byte) error
UnmarshalBinary(b []byte) error
MarshalLen() int
Version() int
MessageType() uint8
MessageTypeName() string
// deprecated
SerializeTo([]byte) error
DecodeFromBytes(b []byte) error
}
// Marshal returns the byte sequence generated from a Message instance.
// Better to use (*MessageName).Marshal instead if you know the name of message to be serialized.
func Marshal(m Message) ([]byte, error) {
b := make([]byte, m.MarshalLen())
if err := m.MarshalTo(b); err != nil {
return nil, err
}
return b, nil
}
// Parse decodes given bytes as Message.
func Parse(b []byte) (Message, error) {
if len(b) < 8 {
return nil, ErrTooShortToParse
}
switch b[0] >> 5 {
case 0:
return v0msg.Parse(b)
case 1:
return v1msg.Parse(b)
case 2:
return v2msg.Parse(b)
default:
return nil, ErrInvalidVersion
}
}