Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
115 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
// Copyright 2015 The Go Circuit Project | ||
// Use of this source code is governed by the license for | ||
// The Go Circuit Project, found in the LICENSE file. | ||
// | ||
// Authors: | ||
// 2015 Petar Maymounkov <p@gocircuit.org> | ||
|
||
package backdial | ||
|
||
import ( | ||
"encoding/gob" | ||
"errors" | ||
"github.com/gocircuit/core/sys" | ||
) | ||
|
||
type Welcome struct { | ||
Back sys.Addr | ||
} | ||
|
||
func init() { | ||
gob.Register(&Welcome{}) | ||
} | ||
|
||
type conn struct { | ||
back sys.Addr | ||
sys.Conn | ||
} | ||
|
||
func (p *peer) handshake(u sys.Conn) (*conn, error) { | ||
if err := u.Send(&Welcome{p.Addr()}); err != nil { | ||
u.Close() | ||
return nil, err | ||
} | ||
w, err := u.Receive() | ||
if err != nil { | ||
u.Close() | ||
return nil, err | ||
} | ||
welcome, ok := w.(*Welcome) | ||
if !ok { | ||
u.Close() | ||
return nil, errors.New("protocol") | ||
} | ||
return &conn{welcome.Back, u}, nil | ||
} | ||
|
||
func (c *conn) Addr() sys.Addr { | ||
return c.back | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
// Copyright 2015 The Go Circuit Project | ||
// Use of this source code is governed by the license for | ||
// The Go Circuit Project, found in the LICENSE file. | ||
// | ||
// Authors: | ||
// 2015 Petar Maymounkov <p@gocircuit.org> | ||
|
||
package backdial | ||
|
||
import ( | ||
"github.com/gocircuit/core/sys" | ||
) | ||
|
||
type peer struct { | ||
u sys.Peer | ||
} | ||
|
||
func New(under sys.Peer) sys.Peer { | ||
return &peer{under} | ||
} | ||
|
||
func (p *peer) Accept() (sys.Conn, error) { | ||
c, err := p.u.Accept() | ||
if err != nil { | ||
return nil, err | ||
} | ||
return p.handshake(c) | ||
} | ||
|
||
func (p *peer) Addr() sys.Addr { | ||
return p.u.Addr() | ||
} | ||
|
||
func (p *peer) Dial(addr sys.Addr) (conn sys.Conn, err error) { | ||
c, err := p.u.Dial(addr) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return p.handshake(c) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
// Copyright 2015 The Go Circuit Project | ||
// Use of this source code is governed by the license for | ||
// The Go Circuit Project, found in the LICENSE file. | ||
// | ||
// Authors: | ||
// 2015 Petar Maymounkov <p@gocircuit.org> | ||
|
||
package net | ||
|
||
import ( | ||
"github.com/gocircuit/core/sys" | ||
"io" | ||
) | ||
|
||
type Peer interface { | ||
Dial(addr sys.Addr) (Conn, error) | ||
Accept() (Conn, error) | ||
Addr() sys.Addr | ||
} | ||
|
||
type Conn interface { | ||
io.ReadWriteCloser | ||
Addr() sys.Addr | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters