Skip to content

Commit

Permalink
Add ListenConfig option and ListenContext function
Browse files Browse the repository at this point in the history
  • Loading branch information
fhs committed Feb 13, 2021
1 parent 976c5fb commit 481d642
Showing 1 changed file with 13 additions and 3 deletions.
16 changes: 13 additions & 3 deletions mux9p.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ package mux9p
// write msg.rx to client

import (
"context"
"fmt"
"io"
"log"
Expand All @@ -51,6 +52,9 @@ type Config struct {
// It can be overridden with environment variable verbose9pserve.
LogLevel int

// Options for the listener.
ListenConfig net.ListenConfig

msize uint32 // 9P message size
versioned bool // Do not initialize the connection with a Tversion

Expand Down Expand Up @@ -90,17 +94,23 @@ type client struct {
// Listen creates a listener at the given network and address,
// accepts 9P clients from it and mutiplexes them into 9P server srv.
func Listen(network, address string, srv io.ReadWriter, cfg *Config) error {
ln, err := net.Listen(network, address)
return ListenContext(context.Background(), network, address, srv, cfg)
}

// ListenContext is equivalent to Listen but with a context.
func ListenContext(ctx context.Context, network, address string, srv io.ReadWriter, cfg *Config) error {
ln, err := cfg.ListenConfig.Listen(ctx, network, address)
if err != nil && network == "unix" && isAddrInUse(err) {
if _, err1 := net.Dial(network, address); !isConnRefused(err1) {
var d net.Dialer
if _, err1 := d.DialContext(ctx, network, address); !isConnRefused(err1) {
return err // Listen error
}
// Dead socket, so remove it.
err = os.Remove(address)
if err != nil {
return err
}
ln, err = net.Listen(network, address)
ln, err = cfg.ListenConfig.Listen(ctx, network, address)
}
if err != nil {
return err
Expand Down

0 comments on commit 481d642

Please sign in to comment.