cnet

package
v0.0.0-...-f5c3557 Latest Latest
Warning

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

Go to latest
Published: May 2, 2018 License: MIT Imports: 11 Imported by: 0

Documentation

Overview

Package cnet provides more network utils, including sockets made from channels.

Addresses for this package are standardized as follows:

tcp:// - operate in TCP space
ucp:// - operate in UDP space
 ch:// - operate with named, process-local, channel-based sockets
Example (NetUsage)
package main

import (
	"fmt"
	"net"
	"time"

	"github.com/iheartradio/cog/cnet"
)

func echoServer(c net.Conn) {
	buff := make([]byte, 1024)
	for {
		got, err := c.Read(buff)
		if err == nil {
			_, err = c.Write(buff[0:got])
		}

		if err != nil {
			return
		}
	}
}

func main() {
	net := cnet.New("NetUsageExample")

	// Create a new listener in process-local, channel-based socket space
	addr := "ch://some-channel-name"

	l, err := net.Listen(addr)
	if err != nil {
		panic(err)
	}

	defer l.Close()

	go func() {
		for {
			c, err := l.Accept()
			if err != nil {
				return
			}

			go echoServer(c)
		}
	}()

	c, err := net.Dial(addr, time.Second)
	if err != nil {
		panic(err)
	}

	defer c.Close()

	_, err = c.Write([]byte("test message"))
	if err != nil {
		panic(err)
	}

	buff := make([]byte, 128)
	n, err := c.Read(buff)
	if err != nil {
		panic(err)
	}

	fmt.Println(string(buff[0:n]))

}
Output:

test message
Example (XNetUsage)
package main

import (
	"errors"
	"time"

	"github.com/iheartradio/cog/cnet"
)

func main() {
	xnet := cnet.NewX("XNetUsageExample")

	// Don't let anything flow.
	xnet.SetOffline(true)

	l, err := xnet.Listen("127.0.0.1:0")
	if err != nil {
		panic(err)
	}

	defer l.Close()

	go func() {
		for {
			_, err := l.Accept()
			if err != nil {
				return
			}
		}
	}()

	_, err = xnet.Dial(l.Addr().String(), time.Second)
	if err == nil {
		panic(errors.New("xnet is offline, Dial should fail"))
	}

	// Allow stuff to start flowing again
	xnet.SetOffline(false)

	// Dial should succeed now: since everything is back online, the connection
	// will be allowed through.
	_, err = xnet.Dial(l.Addr().String(), time.Second)
	if err != nil {
		panic(err)
	}

}
Output:

Index

Examples

Constants

This section is empty.

Variables

View Source
var ErrOffline = errors.New("net is currently offline")

ErrOffline is returned from some operations when SetOffline(true) is called.

Functions

func RemoveDefaultPort

func RemoveDefaultPort(addr string, defaultPort string) string

RemoveDefaultPort strips the port from an address, assuming the port matches defaultPort. This is most useful for turning an address like "example.com:80" into "example.com", where the default port for HTTP (80) is uncessary.

Types

type ChError

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

ChError is the error returned from any operations that happen on channel- based sockets.

func (ChError) Error

func (e ChError) Error() string

Error returns the error message

func (ChError) Temporary

func (e ChError) Temporary() bool

Temporary returns if this error is temporary, so that you can retry the operation

func (ChError) Timeout

func (e ChError) Timeout() bool

Timeout returns if this error is because of a timeout

type Net

type Net interface {
	// Analogue of net.Dial. Addresses should be of the form "tcp://address",
	// "udp://address", or "ch://channel-name", depending on the type of socket
	// you want to create.
	Dial(addr string, timeout time.Duration) (net.Conn, error)

	// Check if a hostname exists on the network
	HostExists(addr string) bool

	// Resolve the given address. If no protocol is given in the address (in the
	// form "tcp://address"), then protocol is used to determine which protocol
	// to resolve for.
	Resolve(protocol, addr string) (net.Addr, error)

	// Analogue of net.Listen. Address rules are the same as for Dial().
	Listen(addr string) (net.Listener, error)

	// Analogue of net.ListenPacket. Address rules are the same as for Dial().
	ListenPacket(addr string) (net.PacketConn, error)
}

Net wraps golang's standard net package, providing process-local channel connections.

func New

func New(debugName string) Net

New creates a new networking interface. The debugName is used as the RemoteAddr() for channel connections.

type XNet

type XNet interface {
	Net

	// SetOffline sets this net interface as offline. Any incoming connections
	// are silently closed. Any existing connections are closed. Any attempts to
	// dial out are met with an error. Any data sent/received over a PacketConn
	// is silently dropped.
	SetOffline(bool)
}

XNet provides extensions on top of basic Net. This may be used in place of a Net for testing.

func NewX

func NewX(debugName string) XNet

NewX creates a new instance of XNet.

Jump to

Keyboard shortcuts

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