本文整理汇总了Golang中net.FileListener函数的典型用法代码示例。如果您正苦于以下问题:Golang FileListener函数的具体用法?Golang FileListener怎么用?Golang FileListener使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了FileListener函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: listenTo
func listenTo(bind string) (net.Listener, error) {
if strings.Contains(bind, ":") {
return net.Listen("tcp", bind)
} else if strings.HasPrefix(bind, ".") || strings.HasPrefix(bind, "/") {
return net.Listen("unix", bind)
} else if strings.HasPrefix(bind, "[email protected]") {
fd, err := strconv.Atoi(bind[3:])
if err != nil {
return nil, fmt.Errorf("error while parsing fd %v: %v",
bind, err)
}
f := os.NewFile(uintptr(fd), bind)
defer f.Close()
return net.FileListener(f)
} else if strings.HasPrefix(bind, "[email protected]") {
fd, err := strconv.Atoi(bind[8:])
if err != nil {
return nil, fmt.Errorf(
"error while parsing einhorn %v: %v", bind, err)
}
return einhornBind(fd)
}
return nil, fmt.Errorf("error while parsing bind arg %v", bind)
}
示例2: listenStream
func listenStream(netw, addr string) (l net.Listener, err error) {
var (
file *os.File
)
fd, err := listen(netw, addr)
if err != nil {
return nil, err
}
// Set backlog size to the maximum
if err = syscall.Listen(fd, syscall.SOMAXCONN); err != nil {
syscall.Close(fd)
return nil, err
}
file = os.NewFile(uintptr(fd), filePrefix+strconv.Itoa(os.Getpid()))
if l, err = net.FileListener(file); err != nil {
syscall.Close(fd)
return nil, err
}
if err = file.Close(); err != nil {
syscall.Close(fd)
l.Close()
return nil, err
}
return l, err
}
示例3: Start
func (g *graphitePickleServiceManager) Start(file *os.File) error {
var (
gl net.Listener
err error
)
if g.listenSpec != "" {
if file != nil {
gl, err = net.FileListener(file)
} else {
gl, err = net.Listen("tcp", processListenSpec(g.listenSpec))
}
} else {
log.Printf("Not starting Graphite Pickle Protocol because graphite-pickle-listen-spec is blank.")
return nil
}
if err != nil {
return fmt.Errorf("Error starting Graphite Pickle Protocol serviceManager: %v", err)
}
g.listener = graceful.NewListener(gl)
fmt.Printf("Graphite Pickle protocol Listening on %s\n", processListenSpec(g.listenSpec))
go g.graphitePickleServer()
return nil
}
示例4: getStoppableListener
// Get a new stoppableListener, either by creating a new TCPListener or if a
// valid fd is specified opening a TCP listener on that file descriptor
func getStoppableListener(fd uintptr, addr string) (sl *stoppableListener, err error) {
var listener net.Listener
var f *os.File
// listen on already open file
if fd != 0 {
simplelogf("Opening file listener on fd: %d\n", fd)
f = os.NewFile(uintptr(fd), "listen socket")
listener, err = net.FileListener(f)
if err != nil {
simplelogf("Couldn't open File listener on fd: %d, falling back "+
"to new tcp listener\n", fd)
}
}
// if we failed or no file descriptor was given, create new tcp connection
if fd == 0 || err != nil { // create new tcp listener
simplelog("Opening a new tcp listener")
listener, err = net.Listen("tcp", addr)
if err != nil {
simplelog("Couldn't get new tcp listener!")
return
}
}
sl = upgradeListener(listener)
// if we started from an open file, attach it to the listener
sl.underlyingFile = f
return
}
示例5: openAPISockets
// Open API sockets based on command line parameters and
// the magic environment variable from systemd
//
// see sd_listen_fds(3)
func openAPISockets() ([]net.Listener, error) {
listeners := []net.Listener{}
fds := systemdFDs(true) // Try to get the socket fds from systemd
if len(fds) > 0 {
if flagAPIServiceListenAddr != "" {
return nil, fmt.Errorf("started under systemd.socket(5), but --listen passed! Quitting.")
}
stderr.Printf("Listening on %d systemd-provided socket(s)\n", len(fds))
for _, fd := range fds {
l, err := net.FileListener(fd)
if err != nil {
return nil, errwrap.Wrap(fmt.Errorf("could not open listener"), err)
}
listeners = append(listeners, l)
}
} else {
if flagAPIServiceListenAddr == "" {
flagAPIServiceListenAddr = common.APIServiceListenAddr
}
stderr.Printf("Listening on %s\n", flagAPIServiceListenAddr)
l, err := net.Listen("tcp", flagAPIServiceListenAddr)
if err != nil {
return nil, errwrap.Wrap(fmt.Errorf("could not open tcp socket"), err)
}
listeners = append(listeners, l)
}
return listeners, nil
}
示例6: TcpListen
// TcpListen is listening for incoming IP packets which are being intercepted.
// In conflict to regular Listen mehtod the socket destination and source addresses
// are of the intercepted connection.
// Else then that it works exactly like net package net.Listen.
func TcpListen(listenAddr string) (listener net.Listener, err error) {
s, err := unix.Socket(unix.AF_INET6, unix.SOCK_STREAM, 0)
if err != nil {
return nil, err
}
defer unix.Close(s)
err = unix.SetsockoptInt(s, unix.SOL_IP, unix.IP_TRANSPARENT, 1)
if err != nil {
return nil, err
}
sa, err := IPv6TcpAddrToUnixSocksAddr(listenAddr)
if err != nil {
return nil, err
}
err = unix.Bind(s, sa)
if err != nil {
return nil, err
}
err = unix.Listen(s, unix.SOMAXCONN)
if err != nil {
return nil, err
}
f := os.NewFile(uintptr(s), "TProxy")
defer f.Close()
return net.FileListener(f)
}
示例7: Recover
// Recover from a seamless binary upgrade and use an already
// existing listener to take over the connections
func Recover() (l net.Listener, ppid int, err error) {
var fd uintptr
_, err = fmt.Sscan(os.Getenv("OLD_FD"), &fd)
if err != nil {
return
}
var i net.Listener
i, err = net.FileListener(os.NewFile(fd, os.Getenv("OLD_NAME")))
if err != nil {
return
}
switch i.(type) {
case *net.TCPListener:
l = i.(*net.TCPListener)
case *net.UnixListener:
l = i.(*net.UnixListener)
default:
err = errors.New(fmt.Sprintf(
"file descriptor is %T not *net.TCPListener or *net.UnixListener", i))
return
}
if err = syscall.Close(int(fd)); err != nil {
return
}
_, err = fmt.Sscan(os.Getenv("OLD_PPID"), &ppid)
if err != nil {
return
}
return
}
示例8: FileListener
func FileListener(f *os.File) (net.Listener, error) {
l, err := npipex.FileListener(f)
if err != nil {
return net.FileListener(f)
}
return l, nil
}
示例9: getListener
/*
getListener either opens a new socket to listen on, or takes the acceptor socket
it got passed when restarted.
*/
func (srv *endlessServer) getListener(laddr string) (l net.Listener, err error) {
if srv.isChild {
var ptrOffset uint = 0
runningServerReg.RLock()
defer runningServerReg.RUnlock()
if len(socketPtrOffsetMap) > 0 {
ptrOffset = socketPtrOffsetMap[laddr]
// log.Println("laddr", laddr, "ptr offset", socketPtrOffsetMap[laddr])
}
f := os.NewFile(uintptr(3+ptrOffset), "")
l, err = net.FileListener(f)
if err != nil {
err = fmt.Errorf("net.FileListener error: %v", err)
return
}
} else {
l, err = net.Listen("tcp", laddr)
if err != nil {
err = fmt.Errorf("net.Listen error: %v", err)
return
}
}
return
}
示例10: TfoListen
//network is useless ,it will always use tcp4
func TfoListen(network string, listenAddr string) (listener net.Listener, err error) {
s, err := unix.Socket(unix.AF_INET, unix.SOCK_STREAM|unix.SOCK_NONBLOCK|unix.SOCK_CLOEXEC, 0)
if err != nil {
return nil, err
}
defer unix.Close(s)
err = unix.SetsockoptInt(s, unix.SOL_TCP, 23, 10)
if err != nil {
return nil, err
}
err = unix.SetsockoptInt(s, unix.SOL_SOCKET, unix.SO_REUSEADDR, 1)
if err != nil {
return nil, err
}
sa, err := kmgUnix.IPv4TcpAddrToUnixSocksAddr(listenAddr)
if err != nil {
return nil, err
}
err = unix.Bind(s, sa)
if err != nil {
return nil, err
}
err = unix.Listen(s, 10)
if err != nil {
return nil, err
}
f := os.NewFile(uintptr(s), "TFOListen")
defer f.Close()
return net.FileListener(f)
}
示例11: RetrieveListeners
// RetrieveListeners if invoked in a process created by PrepareCmd(...).Start()
// returns the list of 'inherited' listeners.
//
// flag.Parse() needs to be invoked first.
func RetrieveListeners() (ls []net.Listener, err error) {
var a, b uintptr
if a, b, err = readFDsFlag(); err != nil {
return nil, err
}
if b < a {
return nil, errors.New("Invalid values for netlimitFDs flag.")
}
ls = make([]net.Listener, b-a)
for fd, i := a, 0; fd < b; {
file := os.NewFile(fd, "listener")
ls[i], err = net.FileListener(file)
file.Close()
if err != nil {
// close created listeners
for j := 0; j < i; j++ {
ls[j].Close()
}
// return an error
return nil, err
}
fd++
i++
}
return ls, err
}
示例12: Serve
func Serve(laddr string, handler func(net.Conn)) {
var l net.Listener
var err error
graceful := os.Getenv(Graceful)
if graceful != "" {
// entry 0 becomes file descriptor 3.
log.Printf("main: Listening to existing file descriptor %v.", FD)
f := os.NewFile(uintptr(FD), "")
// file listener dup fd
l, err = net.FileListener(f)
// close file descriptor 3
f.Close()
} else {
log.Print("main: Listening on a new file descriptor.")
l, err = net.Listen("tcp", laddr)
}
if err != nil {
log.Fatalf("start fail: %v", err)
}
go func() {
serve(l, handler)
}()
WaitSignal(l)
}
示例13: ListenAndServe
func (self *TcpServer) ListenAndServe() error {
if self.inherit {
file := os.NewFile(uintptr(3), "sock")
tmp, err := net.FileListener(file)
file.Close()
if err != nil {
log.Error("Error: %s", err)
return nil
}
listener := tmp.(*net.TCPListener)
self.listener = &MyListener{Listener: listener}
} else {
addr, err := net.ResolveTCPAddr("tcp4", self.ListenAddress)
listener, err := net.ListenTCP("tcp", addr)
if err != nil {
panic(fmt.Sprintf("Error: %s", err))
return err
}
self.listener = &MyListener{Listener: listener}
}
log.Info("momonga_tcp: started tcp server: %s", self.listener.Addr().String())
for i := 0; i < self.config.GetAcceptorCount(); i++ {
go self.Serve(self.listener)
}
return nil
}
示例14: Listen
// Utility to listen on tcp[46]? sockets or a file descriptor
func Listen(rawurl string) (listener net.Listener, err error) {
u, err := url.Parse(rawurl)
if err != nil {
return net.Listen("tcp", rawurl)
}
switch u.Scheme {
case "fd":
var fd uint64
fd, err = strconv.ParseUint(u.Host, 10, 8)
if err != nil {
return
}
// NOTE: The name argument doesn't really matter apparently
sockfile := os.NewFile(uintptr(fd), fmt.Sprintf("fd://%d", fd))
listener, err = net.FileListener(sockfile)
if err != nil {
return
}
default:
var laddr *net.TCPAddr
laddr, err = net.ResolveTCPAddr(u.Scheme, u.Host)
if err != nil {
return
}
listener, err = net.ListenTCP("tcp", laddr)
if err != nil {
return
}
}
return
}
示例15: previousListener
// Helper function which checks environment variables
// for socket descriptors and starts listening on it if any.
func previousListener() (l net.Listener, hasPrev bool, e error) {
const errLoc = "main.previousListener()"
var fd uintptr
if _, e = fmt.Sscan(os.Getenv(envVarName), &fd); e != nil {
e = fmt.Errorf(
"%s: could not read file descriptor from environment, reason -> %s",
errLoc, e.Error(),
)
return
}
hasPrev = true
if l, e = net.FileListener(os.NewFile(fd, "parent socket")); e != nil {
e = fmt.Errorf(
"%s: could not listen on old file descriptor, reason -> %s",
errLoc, e.Error(),
)
return
}
switch l.(type) {
case *net.TCPListener:
default:
e = fmt.Errorf(
"%s: file descriptor is %T not *net.TCPListener", errLoc, l,
)
return
}
if e = syscall.Close(int(fd)); e != nil {
e = fmt.Errorf(
"%s: failed to close old file descriptor, reason -> %s",
errLoc, e.Error(),
)
}
return
}