Skip to content

Commit

Permalink
ugly optimization
Browse files Browse the repository at this point in the history
  • Loading branch information
bigeagle committed Jul 30, 2013
1 parent b2b77e4 commit 0a31d10
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 18 deletions.
2 changes: 1 addition & 1 deletion hop/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ func ip4_uint32(ip net.IP) (i uint32) {
return i
}

func randAddr(a []net.Addr) net.Addr {
func randAddr(a []*hUDPAddr) *hUDPAddr {
i := rand.Intn(len(a))
return a[i]
}
50 changes: 35 additions & 15 deletions hop/protocol.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,39 +55,59 @@ func unpackHopPacket(b []byte) (*HopPacket, error) {
return p, nil
}


func udpAddrHash(a *net.UDPAddr) [6]byte{
var b [6]byte
copy(b[:4], []byte(a.IP)[:4])
p := uint16(a.Port)
b[4] = byte((p >> 8) & 0xFF)
b[5] = byte(p & 0xFF)
return b
}

type hUDPAddr struct {
u *net.UDPAddr
hash [6]byte
}

func newhUDPAddr(a *net.UDPAddr) *hUDPAddr {
return &hUDPAddr{a, udpAddrHash(a)}
}

// gohop Peer is a record of a peer's available UDP addrs
type HopPeer struct {
id uint32
addrs map[string]int
_addrs_lst []net.Addr // i know it's ugly!
addrs map[[6]byte]int
_addrs_lst []*hUDPAddr // i know it's ugly!
inited bool // whether a connection is initialized
}

func newHopPeer(id uint32, addr net.Addr, idx int) *HopPeer {
func newHopPeer(id uint32, addr *net.UDPAddr, idx int) *HopPeer {
hp := new(HopPeer)
hp.id = id
hp._addrs_lst = make([]net.Addr, 0)
hp.addrs = make(map[string]int)
hp._addrs_lst = make([]*hUDPAddr, 0)
hp.addrs = make(map[[6]byte]int)
hp.inited = false

hp._addrs_lst = append(hp._addrs_lst, addr)
hp.addrs[addr.String()] = idx
a := newhUDPAddr(addr)
hp._addrs_lst = append(hp._addrs_lst, a)
hp.addrs[a.hash] = idx

return hp
}

func (h *HopPeer) addr() (net.Addr, int, bool) {
func (h *HopPeer) addr() (*net.UDPAddr, int, bool) {
addr := randAddr(h._addrs_lst)
idx, ok := h.addrs[addr.String()]
idx, ok := h.addrs[addr.hash]

return addr, idx, ok
return addr.u, idx, ok
}

func (h *HopPeer) insertAddr(addr net.Addr, idx int) {
k := addr.String()
if _, found := h.addrs[k]; !found {
h.addrs[k] = idx
h._addrs_lst = append(h._addrs_lst, addr)
func (h *HopPeer) insertAddr(addr *net.UDPAddr, idx int) {
a := newhUDPAddr(addr)
if _, found := h.addrs[a.hash]; !found {
h.addrs[a.hash] = idx
h._addrs_lst = append(h._addrs_lst, a)
//logger.Info("%v %d", addr, len(h._addrs_lst))
}
}
4 changes: 2 additions & 2 deletions hop/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ type hopServerConfig struct {
// a udpPacket
type udpPacket struct {
// client's addr
addr net.Addr
addr *net.UDPAddr
// data
data []byte
// channel index
Expand Down Expand Up @@ -211,7 +211,7 @@ func (srv *HopServer) listenAndServe(port string, idx int) {
packet := new(udpPacket)
packet.channel = idx
buf := make([]byte, TAPBUFSIZE)
plen, packet.addr, err = udpConn.ReadFrom(buf)
plen, packet.addr, err = udpConn.ReadFromUDP(buf)

packet.data = buf[:plen]
if err != nil {
Expand Down

0 comments on commit 0a31d10

Please sign in to comment.