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 ¶
var ErrOffline = errors.New("net is currently offline")
ErrOffline is returned from some operations when SetOffline(true) is called.
Functions ¶
func RemoveDefaultPort ¶
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.
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.
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.