conn

package
v0.1.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 11, 2019 License: Apache-2.0 Imports: 21 Imported by: 6

Documentation

Overview

Copyright (c) 2016-2019 Uber Technologies, Inc.

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

Copyright (c) 2016-2019 Uber Technologies, Inc.

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

Copyright (c) 2016-2019 Uber Technologies, Inc.

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

Copyright (c) 2016-2019 Uber Technologies, Inc.

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

Copyright (c) 2016-2019 Uber Technologies, Inc.

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func PipeFixture

func PipeFixture(
	config Config, info *storage.TorrentInfo) (local *Conn, remote *Conn, cleanupFunc func())

PipeFixture returns Conns for both sides of a live connection for testing.

Types

type Config

type Config struct {

	// HandshakeTimeout is the timeout for dialing, writing, and reading connections
	// during handshake.
	HandshakeTimeout time.Duration `yaml:"handshake_timeout"`

	// SenderBufferSize is the size of the sender channel for a connection.
	// Prevents writers to the connection from being blocked if there are many
	// writers trying to send messages at the same time.
	SenderBufferSize int `yaml:"sender_buffer_size"`

	// ReceiverBufferSize is the size of the receiver channel for a connection.
	// Prevents the connection reader from being blocked if a receiver consumer
	// is taking a long time to process a message.
	ReceiverBufferSize int `yaml:"receiver_buffer_size"`

	Bandwidth bandwidth.Config `yaml:"bandwidth"`
}

Config is the configuration for individual live connections.

func ConfigFixture

func ConfigFixture() Config

ConfigFixture returns a Config for testing.

type Conn

type Conn struct {
	// contains filtered or unexported fields
}

Conn manages peer communication over a connection for multiple torrents. Inbound messages are multiplexed based on the torrent they pertain to.

func Fixture

func Fixture() (*Conn, func())

Fixture returns a single local Conn for testing.

func (*Conn) Close

func (c *Conn) Close()

Close starts the shutdown sequence for the Conn.

func (*Conn) CreatedAt

func (c *Conn) CreatedAt() time.Time

CreatedAt returns the time at which the Conn was created.

func (*Conn) InfoHash

func (c *Conn) InfoHash() core.InfoHash

InfoHash returns the info hash for the torrent being transmitted over this connection.

func (*Conn) IsClosed

func (c *Conn) IsClosed() bool

IsClosed returns true if the c is closed.

func (*Conn) PeerID

func (c *Conn) PeerID() core.PeerID

PeerID returns the remote peer id.

func (*Conn) Receiver

func (c *Conn) Receiver() <-chan *Message

Receiver returns a read-only channel for reading incoming messages off the connection.

func (*Conn) Send

func (c *Conn) Send(msg *Message) error

Send writes the given message to the underlying connection.

func (*Conn) String

func (c *Conn) String() string

type Events

type Events interface {
	ConnClosed(*Conn)
}

Events defines Conn events.

type HandshakeResult

type HandshakeResult struct {
	Conn            *Conn
	Bitfield        *bitset.BitSet
	RemoteBitfields RemoteBitfields
}

HandshakeResult wraps data returned from a successful handshake.

type Handshaker

type Handshaker struct {
	// contains filtered or unexported fields
}

Handshaker defines the handshake protocol for establishing connections to other peers.

func HandshakerFixture

func HandshakerFixture(config Config) *Handshaker

HandshakerFixture returns a Handshaker for testing.

func NewHandshaker

func NewHandshaker(
	config Config,
	stats tally.Scope,
	clk clock.Clock,
	networkEvents networkevent.Producer,
	peerID core.PeerID,
	events Events,
	logger *zap.SugaredLogger) (*Handshaker, error)

NewHandshaker creates a new Handshaker.

func (*Handshaker) Accept

func (h *Handshaker) Accept(nc net.Conn) (*PendingConn, error)

Accept upgrades a raw network connection opened by a remote peer into a PendingConn.

func (*Handshaker) Establish

func (h *Handshaker) Establish(
	pc *PendingConn,
	info *storage.TorrentInfo,
	remoteBitfields RemoteBitfields) (*Conn, error)

Establish upgrades a PendingConn returned via Accept into a fully established Conn.

func (*Handshaker) Initialize

func (h *Handshaker) Initialize(
	peerID core.PeerID,
	addr string,
	info *storage.TorrentInfo,
	remoteBitfields RemoteBitfields,
	namespace string) (*HandshakeResult, error)

Initialize returns a fully established Conn for the given torrent to the given peer / address. Also returns the bitfield of the remote peer and its connections for the torrent.

type Message

type Message struct {
	Message *p2p.Message
	Payload storage.PieceReader
}

Message joins a protobuf message with an optional payload. The only p2p.Message type which should include a payload is PiecePayloadMessage.

func NewAnnouncePieceMessage

func NewAnnouncePieceMessage(index int) *Message

NewAnnouncePieceMessage returns a Message for announcing a piece.

func NewCompleteMessage

func NewCompleteMessage() *Message

NewCompleteMessage returns a Message for a completed torrent.

func NewErrorMessage

func NewErrorMessage(index int, code p2p.ErrorMessage_ErrorCode, err error) *Message

NewErrorMessage returns a Message for indicating an error.

func NewPiecePayloadMessage

func NewPiecePayloadMessage(index int, pr storage.PieceReader) *Message

NewPiecePayloadMessage returns a Message for sending a piece payload.

func NewPieceRequestMessage

func NewPieceRequestMessage(index int, length int64) *Message

NewPieceRequestMessage returns a Message for requesting a piece.

type PendingConn

type PendingConn struct {
	// contains filtered or unexported fields
}

PendingConn represents half-opened, pending connection initialized by a remote peer.

func (*PendingConn) Bitfield

func (pc *PendingConn) Bitfield() *bitset.BitSet

Bitfield returns the bitfield of the remote peer's torrent.

func (*PendingConn) Close

func (pc *PendingConn) Close()

Close closes the connection.

func (*PendingConn) Digest

func (pc *PendingConn) Digest() core.Digest

Digest returns the digest of the blob the remote peer wants to open.

func (*PendingConn) InfoHash

func (pc *PendingConn) InfoHash() core.InfoHash

InfoHash returns the info hash of the torrent the remote peer wants to open.

func (*PendingConn) Namespace

func (pc *PendingConn) Namespace() string

Namespace returns the namespace of the remote peer's torrent.

func (*PendingConn) PeerID

func (pc *PendingConn) PeerID() core.PeerID

PeerID returns the remote peer id.

func (*PendingConn) RemoteBitfields

func (pc *PendingConn) RemoteBitfields() RemoteBitfields

RemoteBitfields returns the bitfield of the remote peer's torrent.

type RemoteBitfields

type RemoteBitfields map[core.PeerID]*bitset.BitSet

RemoteBitfields represents the bitfields of an agent's peers for a given torrent.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL