Skip to content

Commit

Permalink
Added constants where necessary.
Browse files Browse the repository at this point in the history
  • Loading branch information
Evan Lin committed Aug 14, 2019
1 parent 8b70a95 commit 2fd452a
Show file tree
Hide file tree
Showing 7 changed files with 32 additions and 13 deletions.
4 changes: 3 additions & 1 deletion pkg/router/route_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,9 @@ func (rm *routeManager) GetRule(routeID routing.RouteID) (routing.Rule, error) {
return nil, errors.New("unknown RouteID")
}

if len(rule) < 13 {
// TODO(evanlinjin): This is a workaround for ensuring the read-in rule is of the correct size.
// Sometimes it is not, causing a segfault later down the line.
if len(rule) < routing.RuleHeaderSize {
return nil, errors.New("corrupted rule")
}

Expand Down
9 changes: 7 additions & 2 deletions pkg/routing/packet.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@ import (
"math"
)

// PacketHeaderSize represents the base size of a packet.
// All rules should have at-least this size.
// TODO(evanlinjin): Document the format of packets in comments.
const PacketHeaderSize = 6

// RouteID represents ID of a Route in a Packet.
type RouteID uint32

Expand All @@ -18,7 +23,7 @@ func MakePacket(id RouteID, payload []byte) Packet {
panic("packet size exceeded")
}

packet := make([]byte, 6)
packet := make([]byte, PacketHeaderSize)
binary.BigEndian.PutUint16(packet, uint16(len(payload)))
binary.BigEndian.PutUint32(packet[2:], uint32(id))
return Packet(append(packet, payload...))
Expand All @@ -36,5 +41,5 @@ func (p Packet) RouteID() RouteID {

// Payload returns payload from a Packet.
func (p Packet) Payload() []byte {
return p[6:]
return p[PacketHeaderSize:]
}
9 changes: 7 additions & 2 deletions pkg/routing/rule.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@ import (
"github.com/skycoin/dmsg/cipher"
)

// RuleHeaderSize represents the base size of a rule.
// All rules should be at-least this size.
// TODO(evanlinjin): Document the format of rules in comments.
const RuleHeaderSize = 13

// RuleType defines type of a routing rule
type RuleType byte

Expand Down Expand Up @@ -164,7 +169,7 @@ func (r Rule) Summary() *RuleSummary {

// AppRule constructs a new consume RoutingRule.
func AppRule(expireAt time.Time, respRoute RouteID, remotePK cipher.PubKey, remotePort, localPort Port) Rule {
rule := make([]byte, 13)
rule := make([]byte, RuleHeaderSize)
if expireAt.Unix() <= time.Now().Unix() {
binary.BigEndian.PutUint64(rule[0:], 0)
} else {
Expand All @@ -182,7 +187,7 @@ func AppRule(expireAt time.Time, respRoute RouteID, remotePK cipher.PubKey, remo

// ForwardRule constructs a new forward RoutingRule.
func ForwardRule(expireAt time.Time, nextRoute RouteID, nextTrID uuid.UUID) Rule {
rule := make([]byte, 13)
rule := make([]byte, RuleHeaderSize)
if expireAt.Unix() <= time.Now().Unix() {
binary.BigEndian.PutUint64(rule[0:], 0)
} else {
Expand Down
8 changes: 8 additions & 0 deletions pkg/setup/config.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,17 @@
package setup

import (
"time"

"github.com/skycoin/dmsg/cipher"
)

// Various timeouts for setup node.
const (
ServeTransportTimeout = time.Second * 30
ReadTimeout = time.Second * 10
)

// Config defines configuration parameters for setup Node.
type Config struct {
PubKey cipher.PubKey `json:"public_key"`
Expand Down
2 changes: 1 addition & 1 deletion pkg/setup/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ func (sn *Node) Serve(ctx context.Context) error {
}

func (sn *Node) serveTransport(ctx context.Context, tr transport.Transport) error {
ctx, cancel := context.WithTimeout(ctx, time.Second*30)
ctx, cancel := context.WithTimeout(ctx, ServeTransportTimeout)
defer cancel()

proto := NewSetupProtocol(tr)
Expand Down
3 changes: 1 addition & 2 deletions pkg/setup/protocol.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"errors"
"fmt"
"io"
"time"

"github.com/skycoin/skywire/pkg/routing"
)
Expand Down Expand Up @@ -164,7 +163,7 @@ func LoopClosed(ctx context.Context, p *Protocol, ld routing.LoopData) error {
}

func readAndDecodePacketWithTimeout(ctx context.Context, p *Protocol, v interface{}) error {
ctx, cancel := context.WithTimeout(ctx, time.Second*10)
ctx, cancel := context.WithTimeout(ctx, ReadTimeout)
defer cancel()

done := make(chan struct{})
Expand Down
10 changes: 5 additions & 5 deletions pkg/transport/managed_transport.go
Original file line number Diff line number Diff line change
Expand Up @@ -296,8 +296,8 @@ func (mt *ManagedTransport) WritePacket(ctx context.Context, rtID routing.RouteI
mt.clearConn(ctx)
return err
}
if n > 6 {
mt.logSent(uint64(n - 6))
if n > routing.PacketHeaderSize {
mt.logSent(uint64(n - routing.PacketHeaderSize))
}
return nil
}
Expand All @@ -316,7 +316,7 @@ func (mt *ManagedTransport) readPacket() (packet routing.Packet, err error) {
}
}

h := make(routing.Packet, 6)
h := make(routing.Packet, routing.PacketHeaderSize)
if _, err = io.ReadFull(conn, h); err != nil {
return nil, err
}
Expand All @@ -325,8 +325,8 @@ func (mt *ManagedTransport) readPacket() (packet routing.Packet, err error) {
return nil, err
}
packet = append(h, p...)
if n := len(packet); n > 6 {
mt.logRecv(uint64(n - 6))
if n := len(packet); n > routing.PacketHeaderSize {
mt.logRecv(uint64(n - routing.PacketHeaderSize))
}
mt.log.Infof("recv packet: rtID(%d) size(%d)", packet.RouteID(), packet.Size())
return packet, nil
Expand Down

0 comments on commit 2fd452a

Please sign in to comment.