Merge pull request #2 from syncthing/integers

Integer type policy
This commit is contained in:
Audrius Butkevicius 2015-01-19 20:40:17 +00:00
commit 442e93d3fc
4 changed files with 52 additions and 53 deletions

View File

@ -10,25 +10,25 @@ import (
type countingReader struct { type countingReader struct {
io.Reader io.Reader
tot uint64 // bytes tot int64 // bytes
last int64 // unix nanos last int64 // unix nanos
} }
var ( var (
totalIncoming uint64 totalIncoming int64
totalOutgoing uint64 totalOutgoing int64
) )
func (c *countingReader) Read(bs []byte) (int, error) { func (c *countingReader) Read(bs []byte) (int, error) {
n, err := c.Reader.Read(bs) n, err := c.Reader.Read(bs)
atomic.AddUint64(&c.tot, uint64(n)) atomic.AddInt64(&c.tot, int64(n))
atomic.AddUint64(&totalIncoming, uint64(n)) atomic.AddInt64(&totalIncoming, int64(n))
atomic.StoreInt64(&c.last, time.Now().UnixNano()) atomic.StoreInt64(&c.last, time.Now().UnixNano())
return n, err return n, err
} }
func (c *countingReader) Tot() uint64 { func (c *countingReader) Tot() int64 {
return atomic.LoadUint64(&c.tot) return atomic.LoadInt64(&c.tot)
} }
func (c *countingReader) Last() time.Time { func (c *countingReader) Last() time.Time {
@ -37,26 +37,26 @@ func (c *countingReader) Last() time.Time {
type countingWriter struct { type countingWriter struct {
io.Writer io.Writer
tot uint64 // bytes tot int64 // bytes
last int64 // unix nanos last int64 // unix nanos
} }
func (c *countingWriter) Write(bs []byte) (int, error) { func (c *countingWriter) Write(bs []byte) (int, error) {
n, err := c.Writer.Write(bs) n, err := c.Writer.Write(bs)
atomic.AddUint64(&c.tot, uint64(n)) atomic.AddInt64(&c.tot, int64(n))
atomic.AddUint64(&totalOutgoing, uint64(n)) atomic.AddInt64(&totalOutgoing, int64(n))
atomic.StoreInt64(&c.last, time.Now().UnixNano()) atomic.StoreInt64(&c.last, time.Now().UnixNano())
return n, err return n, err
} }
func (c *countingWriter) Tot() uint64 { func (c *countingWriter) Tot() int64 {
return atomic.LoadUint64(&c.tot) return atomic.LoadInt64(&c.tot)
} }
func (c *countingWriter) Last() time.Time { func (c *countingWriter) Last() time.Time {
return time.Unix(0, atomic.LoadInt64(&c.last)) return time.Unix(0, atomic.LoadInt64(&c.last))
} }
func TotalInOut() (uint64, uint64) { func TotalInOut() (int64, int64) {
return atomic.LoadUint64(&totalIncoming), atomic.LoadUint64(&totalOutgoing) return atomic.LoadInt64(&totalIncoming), atomic.LoadInt64(&totalOutgoing)
} }

View File

@ -1,6 +1,5 @@
// Copyright (C) 2014 The Protocol Authors. // Copyright (C) 2014 The Protocol Authors.
//go:generate -command genxdr go run ../../Godeps/_workspace/src/github.com/calmh/xdr/cmd/genxdr/main.go
//go:generate genxdr -o message_xdr.go message.go //go:generate genxdr -o message_xdr.go message.go
package protocol package protocol
@ -18,8 +17,8 @@ type FileInfo struct {
Name string // max:8192 Name string // max:8192
Flags uint32 Flags uint32
Modified int64 Modified int64
Version uint64 Version int64
LocalVersion uint64 LocalVersion int64
Blocks []BlockInfo Blocks []BlockInfo
} }
@ -60,7 +59,7 @@ func (f FileInfo) HasPermissionBits() bool {
type BlockInfo struct { type BlockInfo struct {
Offset int64 // noencode (cache only) Offset int64 // noencode (cache only)
Size uint32 Size int32
Hash []byte // max:64 Hash []byte // max:64
} }
@ -71,8 +70,8 @@ func (b BlockInfo) String() string {
type RequestMessage struct { type RequestMessage struct {
Folder string // max:64 Folder string // max:64
Name string // max:8192 Name string // max:8192
Offset uint64 Offset int64
Size uint32 Size int32
Hash []byte // max:64 Hash []byte // max:64
Flags uint32 Flags uint32
Options []Option // max:64 Options []Option // max:64
@ -80,7 +79,7 @@ type RequestMessage struct {
type ResponseMessage struct { type ResponseMessage struct {
Data []byte Data []byte
Error uint32 Error int32
} }
type ClusterConfigMessage struct { type ClusterConfigMessage struct {
@ -107,7 +106,7 @@ type Folder struct {
type Device struct { type Device struct {
ID []byte // max:32 ID []byte // max:32
Flags uint32 Flags uint32
MaxLocalVersion uint64 MaxLocalVersion int64
} }
type Option struct { type Option struct {
@ -117,7 +116,7 @@ type Option struct {
type CloseMessage struct { type CloseMessage struct {
Reason string // max:1024 Reason string // max:1024
Code uint32 Code int32
} }
type EmptyMessage struct{} type EmptyMessage struct{}

View File

@ -168,8 +168,8 @@ struct FileInfo {
string Name<8192>; string Name<8192>;
unsigned int Flags; unsigned int Flags;
hyper Modified; hyper Modified;
unsigned hyper Version; hyper Version;
unsigned hyper LocalVersion; hyper LocalVersion;
BlockInfo Blocks<>; BlockInfo Blocks<>;
} }
@ -206,8 +206,8 @@ func (o FileInfo) encodeXDR(xw *xdr.Writer) (int, error) {
xw.WriteString(o.Name) xw.WriteString(o.Name)
xw.WriteUint32(o.Flags) xw.WriteUint32(o.Flags)
xw.WriteUint64(uint64(o.Modified)) xw.WriteUint64(uint64(o.Modified))
xw.WriteUint64(o.Version) xw.WriteUint64(uint64(o.Version))
xw.WriteUint64(o.LocalVersion) xw.WriteUint64(uint64(o.LocalVersion))
xw.WriteUint32(uint32(len(o.Blocks))) xw.WriteUint32(uint32(len(o.Blocks)))
for i := range o.Blocks { for i := range o.Blocks {
_, err := o.Blocks[i].encodeXDR(xw) _, err := o.Blocks[i].encodeXDR(xw)
@ -233,8 +233,8 @@ func (o *FileInfo) decodeXDR(xr *xdr.Reader) error {
o.Name = xr.ReadStringMax(8192) o.Name = xr.ReadStringMax(8192)
o.Flags = xr.ReadUint32() o.Flags = xr.ReadUint32()
o.Modified = int64(xr.ReadUint64()) o.Modified = int64(xr.ReadUint64())
o.Version = xr.ReadUint64() o.Version = int64(xr.ReadUint64())
o.LocalVersion = xr.ReadUint64() o.LocalVersion = int64(xr.ReadUint64())
_BlocksSize := int(xr.ReadUint32()) _BlocksSize := int(xr.ReadUint32())
o.Blocks = make([]BlockInfo, _BlocksSize) o.Blocks = make([]BlockInfo, _BlocksSize)
for i := range o.Blocks { for i := range o.Blocks {
@ -261,7 +261,7 @@ BlockInfo Structure:
struct BlockInfo { struct BlockInfo {
unsigned int Size; int Size;
opaque Hash<64>; opaque Hash<64>;
} }
@ -292,7 +292,7 @@ func (o BlockInfo) AppendXDR(bs []byte) ([]byte, error) {
} }
func (o BlockInfo) encodeXDR(xw *xdr.Writer) (int, error) { func (o BlockInfo) encodeXDR(xw *xdr.Writer) (int, error) {
xw.WriteUint32(o.Size) xw.WriteUint32(uint32(o.Size))
if l := len(o.Hash); l > 64 { if l := len(o.Hash); l > 64 {
return xw.Tot(), xdr.ElementSizeExceeded("Hash", l, 64) return xw.Tot(), xdr.ElementSizeExceeded("Hash", l, 64)
} }
@ -312,7 +312,7 @@ func (o *BlockInfo) UnmarshalXDR(bs []byte) error {
} }
func (o *BlockInfo) decodeXDR(xr *xdr.Reader) error { func (o *BlockInfo) decodeXDR(xr *xdr.Reader) error {
o.Size = xr.ReadUint32() o.Size = int32(xr.ReadUint32())
o.Hash = xr.ReadBytesMax(64) o.Hash = xr.ReadBytesMax(64)
return xr.Error() return xr.Error()
} }
@ -361,8 +361,8 @@ RequestMessage Structure:
struct RequestMessage { struct RequestMessage {
string Folder<64>; string Folder<64>;
string Name<8192>; string Name<8192>;
unsigned hyper Offset; hyper Offset;
unsigned int Size; int Size;
opaque Hash<64>; opaque Hash<64>;
unsigned int Flags; unsigned int Flags;
Option Options<64>; Option Options<64>;
@ -403,8 +403,8 @@ func (o RequestMessage) encodeXDR(xw *xdr.Writer) (int, error) {
return xw.Tot(), xdr.ElementSizeExceeded("Name", l, 8192) return xw.Tot(), xdr.ElementSizeExceeded("Name", l, 8192)
} }
xw.WriteString(o.Name) xw.WriteString(o.Name)
xw.WriteUint64(o.Offset) xw.WriteUint64(uint64(o.Offset))
xw.WriteUint32(o.Size) xw.WriteUint32(uint32(o.Size))
if l := len(o.Hash); l > 64 { if l := len(o.Hash); l > 64 {
return xw.Tot(), xdr.ElementSizeExceeded("Hash", l, 64) return xw.Tot(), xdr.ElementSizeExceeded("Hash", l, 64)
} }
@ -437,8 +437,8 @@ func (o *RequestMessage) UnmarshalXDR(bs []byte) error {
func (o *RequestMessage) decodeXDR(xr *xdr.Reader) error { func (o *RequestMessage) decodeXDR(xr *xdr.Reader) error {
o.Folder = xr.ReadStringMax(64) o.Folder = xr.ReadStringMax(64)
o.Name = xr.ReadStringMax(8192) o.Name = xr.ReadStringMax(8192)
o.Offset = xr.ReadUint64() o.Offset = int64(xr.ReadUint64())
o.Size = xr.ReadUint32() o.Size = int32(xr.ReadUint32())
o.Hash = xr.ReadBytesMax(64) o.Hash = xr.ReadBytesMax(64)
o.Flags = xr.ReadUint32() o.Flags = xr.ReadUint32()
_OptionsSize := int(xr.ReadUint32()) _OptionsSize := int(xr.ReadUint32())
@ -471,7 +471,7 @@ ResponseMessage Structure:
struct ResponseMessage { struct ResponseMessage {
opaque Data<>; opaque Data<>;
unsigned int Error; int Error;
} }
*/ */
@ -502,7 +502,7 @@ func (o ResponseMessage) AppendXDR(bs []byte) ([]byte, error) {
func (o ResponseMessage) encodeXDR(xw *xdr.Writer) (int, error) { func (o ResponseMessage) encodeXDR(xw *xdr.Writer) (int, error) {
xw.WriteBytes(o.Data) xw.WriteBytes(o.Data)
xw.WriteUint32(o.Error) xw.WriteUint32(uint32(o.Error))
return xw.Tot(), xw.Error() return xw.Tot(), xw.Error()
} }
@ -519,7 +519,7 @@ func (o *ResponseMessage) UnmarshalXDR(bs []byte) error {
func (o *ResponseMessage) decodeXDR(xr *xdr.Reader) error { func (o *ResponseMessage) decodeXDR(xr *xdr.Reader) error {
o.Data = xr.ReadBytes() o.Data = xr.ReadBytes()
o.Error = xr.ReadUint32() o.Error = int32(xr.ReadUint32())
return xr.Error() return xr.Error()
} }
@ -766,7 +766,7 @@ Device Structure:
struct Device { struct Device {
opaque ID<32>; opaque ID<32>;
unsigned int Flags; unsigned int Flags;
unsigned hyper MaxLocalVersion; hyper MaxLocalVersion;
} }
*/ */
@ -801,7 +801,7 @@ func (o Device) encodeXDR(xw *xdr.Writer) (int, error) {
} }
xw.WriteBytes(o.ID) xw.WriteBytes(o.ID)
xw.WriteUint32(o.Flags) xw.WriteUint32(o.Flags)
xw.WriteUint64(o.MaxLocalVersion) xw.WriteUint64(uint64(o.MaxLocalVersion))
return xw.Tot(), xw.Error() return xw.Tot(), xw.Error()
} }
@ -819,7 +819,7 @@ func (o *Device) UnmarshalXDR(bs []byte) error {
func (o *Device) decodeXDR(xr *xdr.Reader) error { func (o *Device) decodeXDR(xr *xdr.Reader) error {
o.ID = xr.ReadBytesMax(32) o.ID = xr.ReadBytesMax(32)
o.Flags = xr.ReadUint32() o.Flags = xr.ReadUint32()
o.MaxLocalVersion = xr.ReadUint64() o.MaxLocalVersion = int64(xr.ReadUint64())
return xr.Error() return xr.Error()
} }
@ -923,7 +923,7 @@ CloseMessage Structure:
struct CloseMessage { struct CloseMessage {
string Reason<1024>; string Reason<1024>;
unsigned int Code; int Code;
} }
*/ */
@ -957,7 +957,7 @@ func (o CloseMessage) encodeXDR(xw *xdr.Writer) (int, error) {
return xw.Tot(), xdr.ElementSizeExceeded("Reason", l, 1024) return xw.Tot(), xdr.ElementSizeExceeded("Reason", l, 1024)
} }
xw.WriteString(o.Reason) xw.WriteString(o.Reason)
xw.WriteUint32(o.Code) xw.WriteUint32(uint32(o.Code))
return xw.Tot(), xw.Error() return xw.Tot(), xw.Error()
} }
@ -974,7 +974,7 @@ func (o *CloseMessage) UnmarshalXDR(bs []byte) error {
func (o *CloseMessage) decodeXDR(xr *xdr.Reader) error { func (o *CloseMessage) decodeXDR(xr *xdr.Reader) error {
o.Reason = xr.ReadStringMax(1024) o.Reason = xr.ReadStringMax(1024)
o.Code = xr.ReadUint32() o.Code = int32(xr.ReadUint32())
return xr.Error() return xr.Error()
} }

View File

@ -222,8 +222,8 @@ func (c *rawConnection) Request(folder string, name string, offset int64, size i
ok := c.send(id, messageTypeRequest, RequestMessage{ ok := c.send(id, messageTypeRequest, RequestMessage{
Folder: folder, Folder: folder,
Name: name, Name: name,
Offset: uint64(offset), Offset: offset,
Size: uint32(size), Size: int32(size),
}) })
if !ok { if !ok {
return nil, ErrClosed return nil, ErrClosed
@ -706,8 +706,8 @@ func (c *rawConnection) pingerLoop() {
type Statistics struct { type Statistics struct {
At time.Time At time.Time
InBytesTotal uint64 InBytesTotal int64
OutBytesTotal uint64 OutBytesTotal int64
} }
func (c *rawConnection) Statistics() Statistics { func (c *rawConnection) Statistics() Statistics {