2015-02-23 08:30:47 +00:00
|
|
|
// Copyright (C) 2015 The Protocol Authors.
|
|
|
|
|
|
|
|
package protocol
|
|
|
|
|
|
|
|
import "fmt"
|
|
|
|
|
|
|
|
const (
|
|
|
|
compressionThreshold = 128 // don't bother compressing messages smaller than this many bytes
|
|
|
|
)
|
|
|
|
|
|
|
|
var compressionMarshal = map[Compression]string{
|
2020-10-02 06:07:05 +00:00
|
|
|
CompressionNever: "never",
|
|
|
|
CompressionMetadata: "metadata",
|
|
|
|
CompressionAlways: "always",
|
2015-02-23 08:30:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var compressionUnmarshal = map[string]Compression{
|
|
|
|
// Legacy
|
2020-10-02 06:07:05 +00:00
|
|
|
"false": CompressionNever,
|
|
|
|
"true": CompressionMetadata,
|
2015-02-23 08:30:47 +00:00
|
|
|
|
|
|
|
// Current
|
2020-10-02 06:07:05 +00:00
|
|
|
"never": CompressionNever,
|
|
|
|
"metadata": CompressionMetadata,
|
|
|
|
"always": CompressionAlways,
|
2015-02-23 08:30:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c Compression) GoString() string {
|
|
|
|
return fmt.Sprintf("%q", c.String())
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c Compression) MarshalText() ([]byte, error) {
|
|
|
|
return []byte(compressionMarshal[c]), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Compression) UnmarshalText(bs []byte) error {
|
|
|
|
*c = compressionUnmarshal[string(bs)]
|
|
|
|
return nil
|
|
|
|
}
|