2015-02-23 01:30:47 -07: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-01 23:07:05 -07:00
|
|
|
CompressionNever: "never",
|
|
|
|
CompressionMetadata: "metadata",
|
|
|
|
CompressionAlways: "always",
|
2015-02-23 01:30:47 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
var compressionUnmarshal = map[string]Compression{
|
|
|
|
// Legacy
|
2020-10-01 23:07:05 -07:00
|
|
|
"false": CompressionNever,
|
|
|
|
"true": CompressionMetadata,
|
2015-02-23 01:30:47 -07:00
|
|
|
|
|
|
|
// Current
|
2020-10-01 23:07:05 -07:00
|
|
|
"never": CompressionNever,
|
|
|
|
"metadata": CompressionMetadata,
|
|
|
|
"always": CompressionAlways,
|
2015-02-23 01:30:47 -07: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
|
|
|
|
}
|