channel_listener

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Jan 21, 2024 License: MIT Imports: 2 Imported by: 1

README

channel-listener

Go Reference

channel-listener provides a simple way to create a manually controlled net.Listener.

Installation

To use channel-listener in your Go project, install it using go get:

go get github.com/point-c/channel-listener

Usage

c := make(chan net.Conn)
// The provided address will be passed through to `ln.Addr()`
ln := channel_listener.New(c, &net.TCPAddr{})
defer ln.Close() // Closing `c` will also close the listener
for {
    // Any `net.Conn` send through `c` will be accepted here
	ln, err := ln.Accept()
	if err != nil {
		log.Fatal(err)
    }
	// Use ln...
}
Closing With An Error
ln.CloseWithErr(errors.New("error"))
// Will only return the above error
_, err := ln.Accept()
// Will only return the above error
err := ln.Close()
// This error will be ignored and the first error will be returned
err := ln.CloseWithErr(errors.New("another error"))

Testing

The package includes tests that demonstrate its functionality. Use Go's testing tools to run the tests:

go test

Godocs

To regenerate godocs:

go generate -tags docs ./...

Documentation

Overview

Package channel_listener contains a listener that is able to pass arbitrary connections through a channel.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Listener

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

Listener is an implementation of a network listener that accepts connections from a channel. This allows for a flexible way to handle incoming connections.

func New

func New(in <-chan net.Conn, addr net.Addr) *Listener

New creates a new instance of Listener. net.Conn from the channel in are passed to Listener.Accept calls. addr is passed through to Listener.Addr.

func (*Listener) Accept

func (d *Listener) Accept() (net.Conn, error)

Accept waits for and returns the next connection from the channel. If the listener is closed, it returns the error used to close.

func (*Listener) Addr

func (d *Listener) Addr() net.Addr

Addr returns the address of the listener

func (*Listener) Close

func (d *Listener) Close() error

Close closes the listener and signals any goroutines in Listener.Accept to stop waiting. It calls Listener.CloseWithErr with nil.

func (*Listener) CloseWithErr

func (d *Listener) CloseWithErr(err error) error

CloseWithErr allows closing the listener with a specific error. This error will be returned by Listener.Accept when the listener is closed. Only the error from the first time this is called will be returned. If the passed err is nil, the error used to close is net.ErrClosed.

func (*Listener) Done

func (d *Listener) Done() <-chan struct{}

Done returns a channel that's closed when the listener is closed.

Jump to

Keyboard shortcuts

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