netchan

package module
v0.0.0-...-4029021 Latest Latest
Warning

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

Go to latest
Published: Sep 15, 2014 License: BSD-3-Clause Imports: 12 Imported by: 1

README

netchan GoDoc Build Status

--

netchan is a proof-of-concept channels over network, works exactly like you would expect.

Create a new channel, connect to remote hosts and/or listen on one or more ports and use it like you would use a normal channel.

Install

go get github.com/OneOfOne/netchan

Usage

check examples/*

Project Status

This is alpha software, ymmv.

All contributions and pull requests are also very welcome.

License

Apache v2.0

Copyright 2014 OneOfOne

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.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrInvalidResponse is returned when the client or server sent something
	// unexpected during the handshake.
	ErrInvalidResponse = errors.New("invalid response")
)

Functions

This section is empty.

Types

type Channel

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

func Connect

func Connect(network, addr string) (ct *Channel, err error)

Connect is an alias for New(1).Connect(network, addr)

func ListenAndServe

func ListenAndServe(initialCap int, network, addr string) (ct *Channel, err error)

ListenAndServe is an alias for New(initialCap).ListenAndServe(network, addr)

func New

func New(initialCap int) *Channel

New returns a new Channel with the initial cap size for the receiver and connection pool

func (*Channel) Close

func (ct *Channel) Close() error

Close closes all the listeners and connected channels

func (*Channel) Connect

func (ct *Channel) Connect(network, addr string) error

Connect connects to a remote channel it can be called multiple times with different addresses (or really the same one if you're that kind of a person)

func (*Channel) ConnectTo

func (ct *Channel) ConnectTo(conn net.Conn) error

ConnectTo conencts to a specific net.Conn

func (*Channel) IsClosed

func (ct *Channel) IsClosed() (st bool)

IsClosed returns if the channel is closed or not

func (*Channel) ListenAndServe

func (ct *Channel) ListenAndServe(network, addr string) error

ListenAndServe listens on the specific network and address waiting for remote channels it can be called multiple times with different addresses

func (*Channel) Recv

func (ct *Channel) Recv(block bool) (id []byte, val interface{}, st ChannelStatus)

Recv returns who sent the message, the message and the status of the channel optionally blocks until there's a message to receive.

func (*Channel) Send

func (ct *Channel) Send(block bool, val interface{}) (st ChannelStatus)

Send sends a message over the network, optionally blocks until it finds a receiver.

func (*Channel) SendAll

func (ct *Channel) SendAll(block bool, val interface{}) (st []ChannelStatus)

func (*Channel) SendTo

func (ct *Channel) SendTo(id []byte, val interface{}) (st ChannelStatus)

SendTo like send but tries to send to a specific client, returns StatusNotFound if the client doesn't exist anymore. note that SendTo always blocks until the message is sent or it errors out

func (*Channel) Serve

func (ct *Channel) Serve(l net.Listener) error

Serve well, it serves a listener, ok golint?

func (*Channel) SetLogger

func (ct *Channel) SetLogger(l *log.Logger)

Logger returns the *log.Logger instance the channel is using.

func (*Channel) Stats

func (ct *Channel) Stats() (activeConnections, activeListeners int)

Stats returns information about the current connections

func (*Channel) String

func (ct *Channel) String() string

type ChannelStatus

type ChannelStatus byte

ChannelStatus defines the return values from Send/Recv

const (
	// StatusNotFound returned when a valid receiver (or sender) coudln't be found
	StatusNotFound ChannelStatus = iota

	// StatusNotReady means all channel buffers were full, try again later
	StatusNotReady

	// StatusClosed means the channel is closed and should no longer be used
	StatusClosed

	// StatusOK means the message was received (or sent) successfully
	StatusOK
)

func (ChannelStatus) String

func (i ChannelStatus) String() string

type Error

type Error struct {
	Id   []byte
	Addr net.Addr
	Err  error
}

func (Error) Error

func (err Error) Error() string

type Interface

type Interface interface {
	Sender
	Receiver
	io.Closer

	// ConnectTo conencts to a remote channel using the specific net.Conn
	ConnectTo(conn net.Conn) error

	// Serve well, serves a listener.
	// it blocks until the server is terminated.
	Serve(l net.Listener) error

	// IsClosed returns true if the channel is not usable anymore
	IsClosed() bool
}

Interface is the common interface for all network channels

type LocalReceiver

type LocalReceiver <-chan interface{}

LocalReceiver wraps a local channel as a receiver interface, the id will always be nil

func (LocalReceiver) Recv

func (lr LocalReceiver) Recv(block bool) (id []byte, val interface{}, st ChannelStatus)

type LocalSender

type LocalSender chan<- interface{}

LocalSender wraps a local channel as a Sender interface, keep in mind that it will use defer to prevent panics. SendTo will always return StatusNotFound

func (LocalSender) Send

func (ls LocalSender) Send(block bool, val interface{}) (st ChannelStatus)

func (LocalSender) SendTo

func (ls LocalSender) SendTo(id []byte, val interface{}) (st ChannelStatus)

type Receiver

type Receiver interface {
	// Recv returns who sent the message, the message and the status of the channel
	// optionally blocks until there's a message to receive.
	Recv(block bool) (id []byte, val interface{}, st ChannelStatus)
}

Receiver defines a read-only channel

func SelectRecv

func SelectRecv(recvs []Receiver) (r Receiver, val interface{})

SelectRecv loops on multiple receivers until it gets a value, it blocks until a value is returned or all channels are closed. this function doesn't panic.

type Sender

type Sender interface {
	// Send sends a message over the network, optionally blocks until it finds a receiver.
	Send(block bool, val interface{}) (st ChannelStatus)

	// SendTo like send but tries to send to a specific client,
	// returns StatusNotFound if the client doesn't exist anymore.
	// note that SendTo always blocks until the message is sent or it errors out
	SendTo(id []byte, val interface{}) (st ChannelStatus)
}

Sender defines a write-only channel

func SelectSend

func SelectSend(targets []Sender, val interface{}) (sch Sender)

SelectSend loops on multiple senders and returns the first channel that was able to send. it returns the target that sent the value or returns nil if all channels were closed this function doesn't panic.

Directories

Path Synopsis
examples
pingpong
this is a simple ping/pong example run one instance as a server with: go run pingpong.go -s [-addr 10101] then run one (or more) clients with: go run pingpong [-addr 10101]
this is a simple ping/pong example run one instance as a server with: go run pingpong.go -s [-addr 10101] then run one (or more) clients with: go run pingpong [-addr 10101]

Jump to

Keyboard shortcuts

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