提交 f743c7ab 作者: Brian Tiger Chow

refactor(identify) pass context to handshake

上级 6f6d79d6
......@@ -20,6 +20,11 @@
"Rev": "00a7d3b31bbab5795b4a51933c04fc2768242970"
},
{
"ImportPath": "code.google.com/p/go.net/context",
"Comment": "null-144",
"Rev": "ad01a6fcc8a19d3a4478c836895ffe883bd2ceab"
},
{
"ImportPath": "code.google.com/p/gogoprotobuf/proto",
"Rev": "6c980277330804e94257ac7ef70a3adbe1641059"
},
......
// Copyright 2014 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package context_test
import (
"fmt"
"time"
"github.com/jbenet/go-ipfs/Godeps/_workspace/src/code.google.com/p/go.net/context"
)
func ExampleWithTimeout() {
// Pass a context with a timeout to tell a blocking function that it
// should abandon its work after the timeout elapses.
ctx, _ := context.WithTimeout(context.Background(), 100*time.Millisecond)
select {
case <-time.After(200 * time.Millisecond):
fmt.Println("overslept")
case <-ctx.Done():
fmt.Println(ctx.Err()) // prints "context deadline exceeded"
}
// Output:
// context deadline exceeded
}
......@@ -16,6 +16,8 @@ import (
"crypto/sha512"
"hash"
context "github.com/jbenet/go-ipfs/Godeps/_workspace/src/code.google.com/p/go.net/context"
proto "github.com/jbenet/go-ipfs/Godeps/_workspace/src/code.google.com/p/goprotobuf/proto"
ci "github.com/jbenet/go-ipfs/crypto"
peer "github.com/jbenet/go-ipfs/peer"
......@@ -33,7 +35,7 @@ var ErrUnsupportedKeyType = errors.New("unsupported key type")
// Performs initial communication with this peer to share node ID's and
// initiate communication. (secureIn, secureOut, error)
func Handshake(self, remote *peer.Peer, in <-chan []byte, out chan<- []byte) (<-chan []byte, chan<- []byte, error) {
func Handshake(ctx context.Context, self, remote *peer.Peer, in <-chan []byte, out chan<- []byte) (<-chan []byte, chan<- []byte, error) {
encodedHello, err := encodedProtoHello(self)
if err != nil {
......@@ -84,7 +86,7 @@ func Handshake(self, remote *peer.Peer, in <-chan []byte, out chan<- []byte) (<-
}
var handshake bytes.Buffer // Gather corpus to sign.
handshake.Write(encoded)
handshake.Write(encodedHello)
handshake.Write(resp)
handshake.Write(epubkey)
......@@ -118,7 +120,7 @@ func Handshake(self, remote *peer.Peer, in <-chan []byte, out chan<- []byte) (<-
if err != nil {
return nil, nil, err
}
_, err = theirHandshake.Write(encoded)
_, err = theirHandshake.Write(encodedHello)
if err != nil {
return nil, nil, err
}
......
......@@ -3,6 +3,8 @@ package identify
import (
"testing"
context "github.com/jbenet/go-ipfs/Godeps/_workspace/src/code.google.com/p/go.net/context"
ci "github.com/jbenet/go-ipfs/crypto"
"github.com/jbenet/go-ipfs/peer"
)
......@@ -40,14 +42,15 @@ func TestHandshake(t *testing.T) {
PrivKey: skb,
}
ctx := context.Background()
go func() {
_, _, err := Handshake(pa, pb, cha, chb)
_, _, err := Handshake(ctx, pa, pb, cha, chb)
if err != nil {
t.Fatal(err)
}
}()
_, _, err = Handshake(pb, pa, chb, cha)
_, _, err = Handshake(ctx, pb, pa, chb, cha)
if err != nil {
t.Fatal(err)
}
......
......@@ -6,6 +6,8 @@ import (
"net"
"sync"
context "github.com/jbenet/go-ipfs/Godeps/_workspace/src/code.google.com/p/go.net/context"
proto "github.com/jbenet/go-ipfs/Godeps/_workspace/src/code.google.com/p/goprotobuf/proto"
ma "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-multiaddr"
ident "github.com/jbenet/go-ipfs/identify"
......@@ -162,6 +164,7 @@ func (s *Swarm) connListen(maddr *ma.Multiaddr) error {
}
// Handle getting ID from this peer and adding it into the map
// TODO(brian): impl a cancellable context
func (s *Swarm) handleNewConn(nconn net.Conn) {
p := new(peer.Peer)
......@@ -172,7 +175,7 @@ func (s *Swarm) handleNewConn(nconn net.Conn) {
}
newConnChans(conn)
sin, sout, err := ident.Handshake(s.local, p, conn.Incoming.MsgChan, conn.Outgoing.MsgChan)
sin, sout, err := ident.Handshake(context.Background(), s.local, p, conn.Incoming.MsgChan, conn.Outgoing.MsgChan)
if err != nil {
u.PErr("%v\n", err.Error())
conn.Close()
......@@ -426,8 +429,10 @@ func (s *Swarm) GetConnection(id peer.ID, addr *ma.Multiaddr) (*peer.Peer, error
}
// Handle performing a handshake on a new connection and ensuring proper forward communication
// TODO(brian): impl a cancellable context
func (s *Swarm) handleDialedCon(conn *Conn) error {
sin, sout, err := ident.Handshake(s.local, conn.Peer, conn.Incoming.MsgChan, conn.Outgoing.MsgChan)
sin, sout, err := ident.Handshake(context.Background(), s.local, conn.Peer, conn.Incoming.MsgChan, conn.Outgoing.MsgChan)
if err != nil {
return err
}
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论