v2ray-core/transport/internet/header.go

33 lines
669 B
Go
Raw Normal View History

2016-12-08 15:27:41 +00:00
package internet
import "v2ray.com/core/common"
type PacketHeader interface {
Size() int
2016-12-09 11:08:25 +00:00
Write([]byte) (int, error)
2016-12-08 15:27:41 +00:00
}
type PacketHeaderFactory interface {
Create(interface{}) PacketHeader
}
var (
headerCache = make(map[string]PacketHeaderFactory)
)
func RegisterPacketHeader(name string, factory PacketHeaderFactory) error {
if _, found := headerCache[name]; found {
return common.ErrDuplicatedName
}
headerCache[name] = factory
return nil
}
func CreatePacketHeader(name string, config interface{}) (PacketHeader, error) {
factory, found := headerCache[name]
if !found {
return nil, common.ErrObjectNotFound
}
return factory.Create(config), nil
}