Skip to content

Commit

Permalink
add backdial handshake
Browse files Browse the repository at this point in the history
  • Loading branch information
petar committed Mar 25, 2015
1 parent 8ca217a commit c37ede3
Show file tree
Hide file tree
Showing 4 changed files with 115 additions and 2 deletions.
49 changes: 49 additions & 0 deletions sys/backdial/conn.go
@@ -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
}
40 changes: 40 additions & 0 deletions sys/backdial/peer.go
@@ -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)
}
24 changes: 24 additions & 0 deletions sys/net/net.go
@@ -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
}
4 changes: 2 additions & 2 deletions sys/sandbox/conn.go
@@ -1,9 +1,9 @@
// Copyright 2013 Tumblr, Inc.
// 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:
// 2013 Petar Maymounkov <p@gocircuit.org>
// 2015 Petar Maymounkov <p@gocircuit.org>

package sandbox

Expand Down

0 comments on commit c37ede3

Please sign in to comment.